summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/phone_sqlopensips.pm
blob: c281787b46d1e5743cbb4769090ad1ff46d197a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package FS::part_export::phone_sqlopensips;

use vars qw(@ISA @EXPORT_OK %info %options);
use Exporter;
use Tie::IxHash;
use FS::Record qw( dbh qsearch qsearchs );
use FS::part_export;
use FS::svc_phone;
use FS::export_svc;
use LWP::UserAgent;
use FS::DBI;

@ISA = qw(FS::part_export);

tie %options, 'Tie::IxHash',
  'datasrc'  => { label=>'DBI data source ' },
  'username' => { label=>'Database username' },
  'password' => { label=>'Database password' },
  'xmlrpc_url' => { label=>'XMLRPC URL' },
  # XXX: in future, add non-agent-virtualized config, i.e. per-export setting of gwlist, routeid, description, etc.
  # and/or setting description from the phone_name column
;

%info = (
  'svc'        => 'svc_phone',
  'desc'       => 'Export DIDs to OpenSIPs dr_rules table',
  'options'    => \%options,
  'no_machine' => 1,
  'notes'      => 'Export DIDs to OpenSIPs dr_rules table',
);

sub rebless { shift; }

sub _export_insert {
  my($self, $svc_x) = (shift, shift);

  my $conf = new FS::Conf;
  my $agentnum = $svc_x->cust_svc->cust_pkg->cust_main->agentnum || 0;
  my $gwlist = $conf->config('opensips_gwlist',$agentnum) || '';
  my $description = $conf->config('opensips_description',$agentnum) || '';
  my $route = $conf->config('opensips_route',$agentnum) || '';

  my $dbh = $self->opensips_connect;
  my $sth = $dbh->prepare("insert into dr_rules ".
	    "( groupid, prefix, timerec, routeid, gwlist, description ) ".
	    " values ( ?, ?, ?, ?, ?, ? )") or die $dbh->errstr;
  $sth->execute('0',$svc_x->phonenum,'',$route,$gwlist,$description)
	    or die $sth->errstr;
  $dbh->disconnect;
  $self->dr_reload; # XXX: if this fails, do we delete what we just inserted?
}

sub opensips_connect {
    my $self = shift;
    FS::DBI->connect($self->option('datasrc'),$self->option('username'),
			$self->option('password')) or die $FS::DBI::errstr;
}

sub _export_replace {
   '';
}

sub _export_suspend {
  my( $self, $svc_phone ) = (shift, shift);
  '';
}

sub _export_unsuspend {
  my( $self, $svc_phone ) = (shift, shift);
  '';
}

sub _export_delete {
  my( $self, $svc_x ) = (shift, shift);
  my $dbh = $self->opensips_connect;
  my $sth = $dbh->prepare("delete from dr_rules where prefix = ?")
    or die $dbh->errstr;
  $sth->execute($svc_x->phonenum) or die $sth->errstr;
  $dbh->disconnect;
  $self->dr_reload; # XXX: if this fails, do we re-insert what we just deleted?
}

sub dr_reload {
    my $self = shift;
    my $reqxml = "<?xml version=\"1.0\"?>
<methodCall>
  <methodName>dr_reload</methodName>
</methodCall>";
    my $ua = LWP::UserAgent->new;
    my $resp = $ua->post(   $self->option('xmlrpc_url'),
			    Content_Type => 'text/xml', 
			    Content => $reqxml );
    return "invalid HTTP response from OpenSIPS: " . $resp->status_line
	unless $resp->is_success;
    '';
}

1;