import torrus 1.0.9
[freeside.git] / FS / bin / freeside-cdrrewrited
1 #!/usr/bin/perl -w
2
3 use strict;
4 use vars qw( $conf );
5 use FS::Daemon ':all'; #daemonize1 drop_root daemonize2 myexit logfile sig*
6 use FS::UID qw( adminsuidsetup );
7 use FS::Record qw( qsearch ); #qsearchs);
8 #use FS::cdr;
9 #use FS::cust_pkg;
10 #use FS::queue;
11
12 my $user = shift or die &usage;
13
14 #daemonize1('freeside-sprepaidd', $user); #keep unique pid files w/multi installs
15 daemonize1('freeside-cdrrewrited');
16
17 drop_root();
18
19 adminsuidsetup($user);
20
21 logfile( "%%%FREESIDE_LOG%%%/cdrrewrited-log.". $FS::UID::datasrc );
22
23 daemonize2();
24
25 $conf = new FS::Conf;
26
27 die "not running; cdr-asterisk_forward_rewrite and cdr-charged_party_rewrite ".
28     " conf options are both off\n"
29   unless _shouldrun();
30
31 #--
32
33 while (1) {
34
35   #hmm... don't want to do an expensive search with an ever-growing bunch
36   # of unprocessed CDRs during the month... better to mark them all as
37   # rewritten "skipped", i.e. why we're a daemon in the first place
38   # instead of just doing this search like normal CDRs
39
40   my $found = 0;
41   foreach my $cdr ( 
42     qsearch( {
43       'table'     => 'cdr',
44       'extra_sql' => 'FOR UPDATE',
45       'hashref'   => {},
46       'extra_sql' => 'WHERE freesidestatus IS NULL'.
47                      ' AND freesiderewritestatus IS NULL'.
48                      ' LIMIT 1024', #arbitrary, but don't eat too much memory
49     } )
50   ) {
51
52     $found = 1;
53     my @status = ();
54
55     if ( $conf->exists('cdr-asterisk_forward_rewrite')
56          && $cdr->dstchannel =~ /^Local\/(\d+)/i && $1 ne $cdr->dst
57        )
58     {
59
60       my $dst = $1;
61
62       warn "dst ". $cdr->dst. " does not match dstchannel $dst ".
63            "(". $cdr->dstchannel. "); rewriting CDR as a forwarded call";
64
65       $cdr->charged_party($cdr->dst);
66       $cdr->dst($dst);
67       $cdr->amaflags(2);
68
69       push @status, 'asterisk_forward';
70
71     }
72
73     if ( $conf->exists('cdr-charged_party_rewrite') && ! $cdr->charged_party ) {
74
75       $cdr->set_charged_party;
76       push @status, 'charged_party';
77
78     }
79
80     $cdr->freesiderewritestatus(
81       scalar(@status) ? join('/', @status) : 'skipped'
82     );
83
84     my $error = $cdr->replace;
85
86     if ( $error ) {
87       warn "WARNING: error rewriting CDR (will retry in 30 seconds):".
88            " $error\n";
89       sleep 30; #i dunno, wait and see if the database comes back?
90     }
91
92   }
93
94   myexit() if sigterm() || sigint();
95   #sleep 1 unless $found;
96   sleep 5 unless $found;
97
98 }
99
100 #--
101
102 sub _shouldrun {
103      $conf->exists('cdr-asterisk_forward_rewrite')
104   || $conf->exists('cdr-charged_party_rewrite');
105 }
106
107 sub usage { 
108   die "Usage:\n\n  freeside-cdrrewrited user\n";
109 }
110
111 =head1 NAME
112
113 freeside-cdrrewrited - Real-time daemon for CDR rewriting
114
115 =head1 SYNOPSIS
116
117   freeside-cdrrewrited
118
119 =head1 DESCRIPTION
120
121 Runs continuously, searches for CDRs and does forwarded-call rewriting if the
122 "cdr-asterisk_forward_rewrite" or "cdr-charged_party_rewrite" config option is
123 enabled.
124
125 =head1 SEE ALSO
126
127 =cut
128
129 1;