]>
git.defcon.no Git - hermes/blob - guc-clients/addsipuser
4 #TODO: Add support for assigning phone number
5 #TODO: Add support for overriding default domain ...
9 use Net
::LDAP
::Control
::Paged
;
10 use Net
::LDAP
::Constant
qw( LDAP_CONTROL_PAGED );
16 my $utf2iso = Text
::Iconv
->new("utf-8","latin1");
21 my $configfile = undef;
23 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
24 my ($user, $displayname, $phone, $mail, $sipuser, $linetext);
26 my $config = AppConfig
->new({ CREATE
=> 1 });
28 $config->define("api_url=s");
29 $config->define("api_keyfile=s");
31 #$config->define("numbers_local_prefix=s");
32 #$config->define("numbers_local_series=s");
33 #$config->define("numbers_countrycode=s");
35 $config->define("default_domain=s");
37 #TODO: Move this to config file.
38 $config->default_domain("hig.no");
41 "/usr/local/etc/hermes/hermes_config",
42 "/usr/local/etc/hermes/config",
44 $ENV{"HOME"} . "/.hermes/config",
45 $ENV{"HOME"} . "/.hermes_config",
46 ) { $configfile = $_ if ( -f
$_ ); }
50 "username=s" => \
$username,
51 "displayname=s" => \
$displayname,
52 "configfile=s" => \
$configfile,
59 (( $configfile ) && ( not -f
$configfile ))
65 $config->file( $configfile );
67 if ( ( not $config->api_url ) ||
68 ( not $config->api_keyfile ) ||
69 ( not $config->default_domain ) ||
70 ( $config->api_keyfile && not -f
$config->api_keyfile )
78 Verify that the following options are set:
84 Verify the contents of the configuration file.
85 Verify that the key-file exists.
89 open KEY, "<" . $config->api_keyfile;
90 chomp( $api_key = <KEY> );
93 if ( not $username =~ m/\w+/ )
94 { print "Illegal username\n"; exit; }
96 $sipuser = $username . "@" . $config->default_domain;
97 $linetext = $username;
99 print "Data so far:" . "\n";
100 print " Username: " . $username . "\n";
101 print " Displayname: " . $displayname . "\n";
102 print " Line-text: " . $linetext . "\n";
103 print " SIP address: " . $sipuser . "\n";
105 # Convert the displayname to Latin1/ISO-8859-1
106 #$displayname = $utf2iso->convert($displayname);
108 $g_ua = LWP::UserAgent->new;
109 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
113 $data = exec_apinode("user/available", { "user" => $sipuser });
114 if ( not $data->{'response'} eq 'ok' )
116 printf("Unable to add user, Hermes response to available query is: %s\n", $data->{'cause'});
122 print("Dryrun specified. All OK so far, stopping before add.\n");
127 $data = exec_apinode("user/add_local", {
129 "displayname" => $displayname,
130 "email" => 'null@null.nul', #TODO: API MUST BE UPDATED
132 if ( not $data->{'response'} eq 'ok' )
134 printf("Unable to add user, Hermes response to add_local query is: %s\n", $data->{'cause'});
139 printf("Added user, login information:\n");
140 printf("username: %s@%s\nauthid: %s\npassword: %s\n" .
141 "registrar: %s:%d\nproxy: %s:%d\ndisplayname: %s\n" .
142 "email: %s\npermission: %s\n",
143 $data->{'user'}->{'username'},
144 $data->{'user'}->{'domain'},
145 $data->{'user'}->{'authid'},
146 $data->{'user'}->{'password'},
147 $data->{'user'}->{'registrar'},
148 $data->{'user'}->{'r_port'},
149 $data->{'user'}->{'proxy'},
150 $data->{'user'}->{'p_port'},
151 $data->{'user'}->{'displayname'},
152 $data->{'user'}->{'email'},
153 $data->{'user'}->{'permittedcalls'},
155 $domain = $data->{'user'}->{'domain'},
156 $phone .= "\@" . $domain;
160 ##TODO## $data = exec_apinode("alias/add", {
161 ##TODO## "destination" => $sipuser,
162 ##TODO## "alias" => $phone,
164 ##TODO## if ( not $data->{'response'} eq 'ok' )
166 ##TODO## printf("Unable to add E164 number, Hermes response to add_local query is: %s\n", $data->{'cause'});
167 ##TODO## printf("Attempting to roll back user %s: ", $sipuser);
168 ##TODO## exec_apinode("alias/remove", { "alias" => $mail });
169 ##TODO## exec_apinode("user/remove", { "user" => $sipuser });
173 ##TODO## { printf("e164: %s\n", $phone); }
174 ##TODO## undef $data;
176 ##TODO## $data = exec_apinode("user/update", {
177 ##TODO## "user" => $sipuser,
178 ##TODO## "linetext" => $linetext,
180 ##TODO## if ( not $data->{'response'} eq 'ok' )
182 ##TODO## printf("Did not update 'linetext' element.");
187 #exec_apinode("alias/remove", { "alias" => $mail });
188 #exec_apinode("alias/remove", { "alias" => $phone });
189 #exec_apinode("user/remove", { "user" => $sipuser });
192 ################################################################################################
198 my ( $response, $data );
200 $session = "" if not defined $session;
201 $auth_key = "" if not defined $auth_key;
202 my $url = $config->api_url . "/" . $node;
204 $param->{'session'} = $session;
205 $param->{'auth_key'} = $auth_key;
207 $response = $g_ua->post( $url, $param );
208 if ( $response->is_success )
210 if ( $response->content =~ m/\s*{/ )
212 $data = decode_json( $response->content);
216 $data = $response->content;
225 my $response = $g_ua->post( $config->api_url . "/auth/login",
226 [ "api_key" => $api_key ] );
228 my $data = decode_json( $response->content) if $response->is_success;
229 die("HTTP error") unless $response->is_success;
231 if ( $data->{'response'} eq "ok" )
233 $session = $data->{'session'};
234 $auth_key = $data->{'auth_key'};
238 print "Unable to log in to Hermes API\n";
241 undef $data; undef $response;
246 my $response = $g_ua->post( $config->api_url . "/auth/logout",
247 [ "session" => $session ] );
248 die("HTTP error") unless $response->is_success;
249 undef $session; undef $auth_key;