I needed to quickly generate a full directory for a receptionist console. Since Trixbox doesn’t do this, I wrote some Perl to do so. It’s pretty simple, you will need to install Polycom::Contact::Directory from CPAN. It will connect to the localhost MySQL server and pull all extensions out, make an xml and save it to the appropriate path. You will need to supply the MAC address, I guess I could modify a bit to pull the MAC out of the Endpoint Manager table, but I like being able to just supply the MAC.

Thanks Zachary Blair for the easy module!

#!/usr/bin/perl -w
# Quick script to hack out a directory for a mac address. I use it for the
# receptionist's BLF on her IP650 with sidecars.
use strict;
use Polycom::Contact::Directory;
use DBI;

# Grab the MAC address from ARGV and make a file
my $mac = $ARGV[0] or die "No MAC Specified\n";
my $contactFile = "/tftpboot/polycom/contacts/$mac-directory.xml";

# Create a new Empty Directory
my $dir = Polycom::Contact::Directory->new();
# Connect to the trixbox MySQL DB
my $dbh = DBI->connect('dbi:mysql:asterisk:localhost:3306','root','passw0rd',{ RaiseError => 1});

# Pull an array ref for the extensions
my $userAry = $dbh->selectall_arrayref("SELECT extension,name FROM users ORDER BY extension");

$dbh->disconnect();

# Set counter for speed dial index
my $x = 1;

# Loop through extensions
for my $a (@$userAry) {
# Split the trixbox name into first and last.
my ($fn,$ln) = split(/\s+/,$a->[1],2);

# My contacts are generally dirty, I'll make them look better. Some people may want
# to comment this out if you have people with unique capitalization.
$fn = ucfirst(lc($fn));
$ln = ucfirst(lc($ln));

# Insert the record into the object.
# I like the labels to be: extension firstname lastname "3721 Awesome Dude"
#  -- buddy_watching lets the polycom monitor BLF status. For this to work,
#     you must have feature.1.name="presence" feature.1.enabled="1" in
#     /tftpboot/sip.cfg
#  -- Check Polycom::Contact Documentation for Options
$dir->insert(
{   first_name => $fn,                  # <fn> in xml
last_name  => $ln,                  # <ln> in xml
contact    => "$a->[0]",            # <ct> in xml
label      => "$a->[0] $fn $ln",    # <lb> in xml
buddy_watching => 1,                # <bw> in xml
speed_index => $x,                  # <sd> in xml
buddy_block => 0,                   # <bb> in xml
auto_divert => 0,                   # <ad> in xml
auto_reject => 0,                   # <ar> in xml

},
);
$x++;
}

# Save the contact file.
$dir->save($contactFile);

1;