#!/usr/bin/perl use strict; use warnings; use Digest::SHA qw(hmac_sha1_hex); use Encode; use Text::Unidecode; mkdir "tftp/Phones"; my $fh; my %config; open $fh, '<', 'config.txt' or die "Cannot read file config: $!\n"; while (<$fh>) { chomp; my ($key, $value) = split /=/, $_, 2; $config{$key} = $value; } close $fh; my $server_ip = $config{server_ip}; my $ip_template = $server_ip; $ip_template =~ s/\.[^.]*$//; my $fake_second_half = $config{fake_year}; my $sipdefault_reflash = <<"EOD"; image_version: P0S3-8-12-00 phone_password: $config{telnet_pass} encrypt_key: 00000000000000000000000000000000 telnet_level: 2 tftp_cfg_dir: "" EOD chomp $sipdefault_reflash; open my $phones_fh, '<', 'phones.txt' or die "Cannot read file phones.txt: $!\n"; open my $ast_fh, '>', 'etc_asterisk/pjsip_wizard_phones.conf' or die "Cannot create file etc_asterisk/pjsip_wizard_phones.conf: $!\n"; open my $dnsmasq_fh, '>', 'etc_dnsmasq.d/phones.conf' or die "Cannot create file etc_dnsmasq.d/phones.conf: $!\n"; open my $directory_fh, '>', 'www/directory.txt' or die "Cannot create file www/directory.txt: $!\n"; open my $voicemail_fh, '>', 'etc_asterisk/voicemail_gen.conf' or die "Cannot create file etc_asterisk/voicemail_gen.conf: $!\n"; print $ast_fh "; This is autogenerated file, do not edit it!\n"; print $dnsmasq_fh "# This is autogenerated file, do not edit it!\n"; print $directory_fh "# This is autogenerated file, do not edit it!\n"; print $voicemail_fh "; This is autogenerated file, do not edit it!\n"; while (<$phones_fh>) { chomp $_; next if $_ =~ /^(?:#|$)/; $_ =~ s/#.*//; $_ =~ s/\s*$//; my ($line, $mac, $alias, $name) = split /\s+/, $_, 4; die "Invalid line $.: $_\n" unless defined $name; $mac = uc $mac; my $name_latin1 = $name; Encode::from_to($name_latin1, 'UTF-8', 'ISO-8859-1', sub { unidecode(chr($_[0])) }); $line =~ /^[2-9][0-9]$/ or die "Invalid LINE $line on line $.: $_\n"; $alias =~ /^[A-Za-z0-9_]{1,20}$/ or die "Invalid ALIAS $alias on line $.: $_\n"; length $name_latin1 <= 30 or die "Too long NAME $name on line $.: $_\n"; my $parent = ($line =~ /^2[0-9]$/) ? 'org_phone' : 'participant_phone'; my $pass = substr(hmac_sha1_hex($line, $config{hmac_key}), 0, 12); my $cisco_fh; my $ciscofile; if ($mac ne '-') { $mac =~ /^(?:[0-9A-F]{2}:){5}[0-9A-F]{2}$/ or die "Invalid MAC $mac on line $.: $_\n"; $ciscofile = $mac; $ciscofile =~ s/://g; open $cisco_fh, '>', "tftp/Phones/SIP$ciscofile.cnf" or die "Cannot create file tftp/Phones/SIP$ciscofile.cnf: $!\n"; print $cisco_fh <<"EOD"; # This is autogenerated file, do not edit it! phone_label: "$fake_second_half Klapka $line " phone_prompt: "Telnet phone $line" proxy_register: 1 proxy1_address: $server_ip proxy1_port: 5060 line1_name: $line line1_authname: $line line1_password: $pass line1_displayname: "$alias" line1_shortname: "$name_latin1" sntp_server: $server_ip sntp_mode: unicast services_url: http://$server_ip/services.xml directory_url: http://$server_ip/directory.xml logo_url: http://$server_ip/logo.bmp messages_uri: 131 telnet_level: 2 EOD close $fh; } print $ast_fh <<"EOD"; [$line]($parent) inbound_auth/username = $line inbound_auth/password = $pass endpoint/mailboxes = $line EOD if ($parent eq 'participant_phone') { print $ast_fh <<"EOD"; [emerg$line](participant_emerg) inbound_auth/username = emerg$line inbound_auth/password = $pass EOD if ($mac ne '-') { print $cisco_fh <<"EOD" proxy2_address: $server_ip proxy2_port: 5060 line2_name: emerg$line line2_authname: emerg$line line2_password: $pass line2_displayname: "Emergency $line" line2_shortname: " " EOD } } else { if ($mac ne '-') { print $cisco_fh <<"EOD" dnd_control: 0 EOD } } if ($mac ne '-') { close $cisco_fh; open $fh, '>', "tftp_reflash/SIP$ciscofile.cnf" or die "Cannot create file tftp_reflash/SIP$ciscofile.cnf: $!\n"; print $fh <<"EOD"; # This is autogenerated file, do not edit it! $sipdefault_reflash proxy_register: 0 line1_name: $line EOD } if ($mac ne '-') { print $dnsmasq_fh <<"EOD"; dhcp-host=$mac,$ip_template.$line,$alias EOD } print $directory_fh <<"EOD"; $name=$line EOD print $voicemail_fh <<"EOD" $line => 0000,$name EOD } close $phones_fh; close $ast_fh; close $dnsmasq_fh; close $directory_fh; close $voicemail_fh; open $fh, '>', 'tftp_reflash/SIPDefault.cnf' or die "Cannot create file tftp_reflash/SIPDefault.cnf: $!\n"; print $fh <<"EOD"; # This is autogenerated file, do not edit it! $sipdefault_reflash; EOD close $fh; open $fh, '>', 'tftp/RINGLIST.DAT' or die "Cannot create file tftp/RINGLIST.DAT: $!\n"; foreach () { $_ =~ m{^tftp/(RingTones/(.*)\.pcm)$}; my ($file, $name) = ($1, $2); $name =~ s/_/ /g; print $fh "$name\t$file\n"; } close $fh;