OpenSIPS provisioning, RT10993
[freeside.git] / FS / FS / part_export / phone_sqlopensips.pm
1 package FS::part_export::phone_sqlopensips;
2
3 use vars qw(@ISA @EXPORT_OK %info %options);
4 use Exporter;
5 use Tie::IxHash;
6 use FS::Record qw( dbh qsearch qsearchs );
7 use FS::part_export;
8 use FS::svc_phone;
9 use FS::export_svc;
10 use LWP::UserAgent;
11
12 @ISA = qw(FS::part_export);
13
14 tie %options, 'Tie::IxHash',
15   'datasrc'  => { label=>'DBI data source ' },
16   'username' => { label=>'Database username' },
17   'password' => { label=>'Database password' },
18   'xmlrpc_url' => { label=>'XMLRPC URL' },
19 ;
20
21 %info = (
22   'svc'      => 'svc_phone',
23   'desc'     => 'Export DIDs to OpenSIPs dr_rules table',
24   'options'  => \%options,
25   'notes'    => 'Export DIDs to OpenSIPs dr_rules table',
26 );
27
28 sub rebless { shift; }
29
30 sub _export_insert {
31   my($self, $svc_x) = (shift, shift);
32   my $dbh = $self->opensips_connect;
33   my $sth = $dbh->prepare("insert into dr_rules ".
34             "( groupid, prefix, timerec, routeid, gwlist, description ) ".
35             " values ( ?, ?, ?, ?, ?, ? )") or die $dbh->errstr;
36   $sth->execute('0',$svc_x->phonenum,'',$svc_x->route,$svc_x->gwlist,
37                 $svc_x->phone_name) or die $sth->errstr;
38   $dbh->disconnect;
39   $self->dr_reload;
40 }
41
42 sub opensips_connect {
43     my $self = shift;
44     DBI->connect($self->option('datasrc'),$self->option('username'),
45                         $self->option('password')) or die $DBI::errstr;
46 }
47
48 sub _export_replace {
49   my( $self, $new, $old ) = (shift, shift, shift);
50     my @update = ();
51     my @paramvalues = ();
52
53     if($old->route ne $new->route){
54         push @update, 'routeid = ?';
55         push @paramvalues, $new->route;
56     }
57
58     if($old->phone_name ne $new->phone_name) {
59         push @update, 'description = ?';
60         push @paramvalues, $new->phone_name;
61     }
62
63     if($old->gwlist ne $new->gwlist) {
64         push @update, 'gwlist = ?';
65         push @paramvalues, $new->gwlist;
66     }
67
68     if(scalar(@update)) {
69       my $update_str = join(' and ',@update);
70       my $dbh = $self->opensips_connect;
71       my $sth = $dbh->prepare("update dr_rules set $update_str " . 
72             " where prefix = ? ") or die $dbh->errstr;
73       push @paramvalues, $old->phonenum;
74       $sth->execute(@paramvalues) or die $sth->errstr;
75       $dbh->disconnect;
76       return $self->dr_reload;
77     }
78   '';
79 }
80
81 sub _export_suspend {
82   my( $self, $svc_phone ) = (shift, shift);
83   '';
84 }
85
86 sub _export_unsuspend {
87   my( $self, $svc_phone ) = (shift, shift);
88   '';
89 }
90
91 sub _export_delete {
92   my( $self, $svc_x ) = (shift, shift);
93   my $dbh = $self->opensips_connect;
94   my $sth = $dbh->prepare("delete from dr_rules where prefix = ?")
95     or die $dbh->errstr;
96   $sth->execute($svc_x->phonenum) or die $sth->errstr;
97   $dbh->disconnect;
98   $self->dr_reload;
99 }
100
101 sub dr_reload {
102     my $self = shift;
103     my $reqxml = "<?xml version=\"1.0\"?>
104 <methodCall>
105   <methodName>dr_reload</methodName>
106 </methodCall>";
107     my $ua = LWP::UserAgent->new;
108     my $resp = $ua->post(   $self->option('xmlrpc_url'),
109                             Content_Type => 'text/xml', 
110                             Content => $reqxml );
111     return "invalid HTTP response from OpenSIPS: " . $resp->status_line
112         unless $resp->is_success;
113     '';
114 }
115