]>
git.defcon.no Git - hermes/blob - guc-clients/addsipuser
cacbad2fd8df8339b2a03ad60fb91a0f80751e32
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");
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");
42 "username=s" => \
$username,
43 "displayname=s" => \
$displayname,
44 "configfile=s" => \
$configfile,
51 (( $configfile ) && ( not -f
$configfile ))
57 $config->file( $configfile );
59 if ( ( not $config->api_url ) ||
60 ( not $config->api_keyfile ) ||
61 ( not $config->default_domain ) ||
62 ( $config->api_keyfile && not -f
$config->api_keyfile )
70 Verify that the following options are set:
76 Verify the contents of the configuration file.
77 Verify that the key-file exists.
81 open KEY, "<" . $config->api_keyfile;
82 chomp( $api_key = <KEY> );
85 if ( not $username =~ m/\w+/ )
86 { print "Illegal username\n"; exit; }
88 $sipuser = $username . "@" . $config->default_domain;
89 $linetext = $username;
91 print "Data so far:" . "\n";
92 print " Username: " . $username . "\n";
93 print " Displayname: " . $displayname . "\n";
94 print " Line-text: " . $linetext . "\n";
95 print " SIP address: " . $sipuser . "\n";
97 # Convert the displayname to Latin1/ISO-8859-1
98 #$displayname = $utf2iso->convert($displayname);
100 $g_ua = LWP::UserAgent->new;
101 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
105 $data = exec_apinode("user/available", { "user" => $sipuser });
106 if ( not $data->{'response'} eq 'ok' )
108 printf("Unable to add user, Hermes response to available query is: %s\n", $data->{'cause'});
114 print("Dryrun specified. All OK so far, stopping before add.\n");
119 $data = exec_apinode("user/add_local", {
121 "displayname" => $displayname,
122 "email" => 'null@null.nul', #TODO: API MUST BE UPDATED
124 if ( not $data->{'response'} eq 'ok' )
126 printf("Unable to add user, Hermes response to add_local query is: %s\n", $data->{'cause'});
131 printf("Added user, login information:\n");
132 printf("username: %s@%s\nauthid: %s\npassword: %s\n" .
133 "registrar: %s:%d\nproxy: %s:%d\ndisplayname: %s\n" .
134 "email: %s\npermission: %s\n",
135 $data->{'user'}->{'username'},
136 $data->{'user'}->{'domain'},
137 $data->{'user'}->{'authid'},
138 $data->{'user'}->{'password'},
139 $data->{'user'}->{'registrar'},
140 $data->{'user'}->{'r_port'},
141 $data->{'user'}->{'proxy'},
142 $data->{'user'}->{'p_port'},
143 $data->{'user'}->{'displayname'},
144 $data->{'user'}->{'email'},
145 $data->{'user'}->{'permittedcalls'},
147 $domain = $data->{'user'}->{'domain'},
148 $phone .= "\@" . $domain;
152 ##TODO## $data = exec_apinode("alias/add", {
153 ##TODO## "destination" => $sipuser,
154 ##TODO## "alias" => $phone,
156 ##TODO## if ( not $data->{'response'} eq 'ok' )
158 ##TODO## printf("Unable to add E164 number, Hermes response to add_local query is: %s\n", $data->{'cause'});
159 ##TODO## printf("Attempting to roll back user %s: ", $sipuser);
160 ##TODO## exec_apinode("alias/remove", { "alias" => $mail });
161 ##TODO## exec_apinode("user/remove", { "user" => $sipuser });
165 ##TODO## { printf("e164: %s\n", $phone); }
166 ##TODO## undef $data;
168 ##TODO## $data = exec_apinode("user/update", {
169 ##TODO## "user" => $sipuser,
170 ##TODO## "linetext" => $linetext,
172 ##TODO## if ( not $data->{'response'} eq 'ok' )
174 ##TODO## printf("Did not update 'linetext' element.");
179 #exec_apinode("alias/remove", { "alias" => $mail });
180 #exec_apinode("alias/remove", { "alias" => $phone });
181 #exec_apinode("user/remove", { "user" => $sipuser });
184 ################################################################################################
190 my ( $response, $data );
192 $session = "" if not defined $session;
193 $auth_key = "" if not defined $auth_key;
194 my $url = $config->api_url . "/" . $node;
196 $param->{'session'} = $session;
197 $param->{'auth_key'} = $auth_key;
199 $response = $g_ua->post( $url, $param );
200 if ( $response->is_success )
202 if ( $response->content =~ m/\s*{/ )
204 $data = decode_json( $response->content);
208 $data = $response->content;
217 my $response = $g_ua->post( $config->api_url . "/auth/login",
218 [ "api_key" => $api_key ] );
220 my $data = decode_json( $response->content) if $response->is_success;
221 die("HTTP error") unless $response->is_success;
223 if ( $data->{'response'} eq "ok" )
225 $session = $data->{'session'};
226 $auth_key = $data->{'auth_key'};
230 print "Unable to log in to Hermes API\n";
233 undef $data; undef $response;
238 my $response = $g_ua->post( $config->api_url . "/auth/logout",
239 [ "session" => $session ] );
240 die("HTTP error") unless $response->is_success;
241 undef $session; undef $auth_key;