This commit was generated by cvs2svn to compensate for changes in r11022,
[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   # XXX: in future, add non-agent-virtualized config, i.e. per-export setting of gwlist, routeid, description, etc.
20   # and/or setting description from the phone_name column
21 ;
22
23 %info = (
24   'svc'      => 'svc_phone',
25   'desc'     => 'Export DIDs to OpenSIPs dr_rules table',
26   'options'  => \%options,
27   'notes'    => 'Export DIDs to OpenSIPs dr_rules table',
28 );
29
30 sub rebless { shift; }
31
32 sub _export_insert {
33   my($self, $svc_x) = (shift, shift);
34
35   my $conf = new FS::Conf;
36   my $agentnum = $svc_x->cust_svc->cust_pkg->cust_main->agentnum || 0;
37   my $gwlist = $conf->config('opensips_gwlist',$agentnum) || '';
38   my $description = $conf->config('opensips_description',$agentnum) || '';
39   my $route = $conf->config('opensips_route',$agentnum) || '';
40
41   my $dbh = $self->opensips_connect;
42   my $sth = $dbh->prepare("insert into dr_rules ".
43             "( groupid, prefix, timerec, routeid, gwlist, description ) ".
44             " values ( ?, ?, ?, ?, ?, ? )") or die $dbh->errstr;
45   $sth->execute('0',$svc_x->phonenum,'',$route,$gwlist,$description)
46             or die $sth->errstr;
47   $dbh->disconnect;
48   $self->dr_reload; # XXX: if this fails, do we delete what we just inserted?
49 }
50
51 sub opensips_connect {
52     my $self = shift;
53     DBI->connect($self->option('datasrc'),$self->option('username'),
54                         $self->option('password')) or die $DBI::errstr;
55 }
56
57 sub _export_replace {
58    '';
59 }
60
61 sub _export_suspend {
62   my( $self, $svc_phone ) = (shift, shift);
63   '';
64 }
65
66 sub _export_unsuspend {
67   my( $self, $svc_phone ) = (shift, shift);
68   '';
69 }
70
71 sub _export_delete {
72   my( $self, $svc_x ) = (shift, shift);
73   my $dbh = $self->opensips_connect;
74   my $sth = $dbh->prepare("delete from dr_rules where prefix = ?")
75     or die $dbh->errstr;
76   $sth->execute($svc_x->phonenum) or die $sth->errstr;
77   $dbh->disconnect;
78   $self->dr_reload; # XXX: if this fails, do we re-insert what we just deleted?
79 }
80
81 sub dr_reload {
82     my $self = shift;
83     my $reqxml = "<?xml version=\"1.0\"?>
84 <methodCall>
85   <methodName>dr_reload</methodName>
86 </methodCall>";
87     my $ua = LWP::UserAgent->new;
88     my $resp = $ua->post(   $self->option('xmlrpc_url'),
89                             Content_Type => 'text/xml', 
90                             Content => $reqxml );
91     return "invalid HTTP response from OpenSIPS: " . $resp->status_line
92         unless $resp->is_success;
93     '';
94 }
95