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