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 );
12 my $user = shift or die &usage;
14 #daemonize1('freeside-sprepaidd', $user); #keep unique pid files w/multi installs
15 daemonize1('freeside-cdrrewrited');
19 adminsuidsetup($user);
21 logfile( "%%%FREESIDE_LOG%%%/cdrrewrited-log.". $FS::UID::datasrc );
27 die "not running; cdr-asterisk_forward_rewrite, cdr-charged_party_rewrite ".
28 " and cdr-taqua-accountcode_rewrite conf options are all off\n"
33 my %accountcode_unmatch = ();
34 my $accountcode_retry = 4 * 60 * 60; # 4 hours
35 my $accountcode_giveup = 4 * 24 * 60 * 60; # 4 days
37 my %cdr_type = map { lc($_->cdrtypename) => $_->cdrtypenum }
38 qsearch('cdr_type',{});
42 #hmm... don't want to do an expensive search with an ever-growing bunch
43 # of unprocessed CDRs during the month... better to mark them all as
44 # rewritten "skipped", i.e. why we're a daemon in the first place
45 # instead of just doing this search like normal CDRs
48 my @recent = grep { ($accountcode_unmatch{$_} + $accountcode_retry) > time }
49 keys %accountcode_unmatch;
50 my $extra_sql = scalar(@recent)
51 ? ' AND acctid NOT IN ('. join(',', @recent). ') '
61 'extra_sql' => 'FOR UPDATE',
63 'extra_sql' => 'WHERE freesidestatus IS NULL '.
64 ' AND freesiderewritestatus IS NULL '.
66 ' LIMIT 1024', #arbitrary, but don't eat too much memory
70 next if $skip{$cdr->acctid};
75 if ( $conf->exists('cdr-asterisk_forward_rewrite')
76 && $cdr->dstchannel =~ /^Local\/(\d+)/i && $1 ne $cdr->dst
82 warn "dst ". $cdr->dst. " does not match dstchannel $dst ".
83 "(". $cdr->dstchannel. "); rewriting CDR as a forwarded call";
85 $cdr->charged_party($cdr->dst);
89 push @status, 'asterisk_forward';
93 # XXX weird special case stuff--can we modularize this somehow?
95 if ( $conf->exists('cdr-asterisk_australia_rewrite') and
96 $cdr->disposition eq 'ANSWERED' ) {
99 if ( $dst =~ /^0?(12|13|1800|1900|0055)/ ) {
100 # toll free or smart numbers, any length
102 $cdr->charged_party($dst);
104 elsif ( $dst =~ /^(11|0011)/ ) {
105 # will be followed by country code
106 $type = 'international';
107 $dst =~ s/^$1/0011/; #standardize
110 elsif ( length($dst) == 10 and$dst =~ /^04/ ) {
113 elsif ( length($dst) == 10 and $dst =~ /^02|03|07|08/ ) {
116 elsif ( length($dst) == 8 ) {
117 # local call, no area code
123 if ( $type and exists($cdr_type{$type}) ) {
124 $cdr->cdrtypenum($cdr_type{$type});
125 push @status, 'asterisk_australia';
128 $warning{"no CDR type defined for $type calls"}++;
132 if ( $conf->exists('cdr-charged_party_rewrite') && ! $cdr->charged_party ) {
134 $cdr->set_charged_party;
135 push @status, 'charged_party';
139 if ( $conf->exists('cdr-taqua-accountcode_rewrite')
140 && $cdr->lastapp eq 'acctcode' && $cdr->cdrtypenum == 1
144 #find the matching CDR
145 my $primary = qsearchs('cdr', {
146 'sessionnum' => $cdr->sessionnum,
147 'src' => $cdr->subscriber,
148 #'accountcode' => '',
151 unless ( $primary ) {
153 my $cantfind = "can't find primary CDR with session ". $cdr->sessionnum.
154 ", src ". $cdr->subscriber;
155 if ( $cdr->calldate_unix + $accountcode_giveup < time ) {
156 warn "ERROR: $cantfind; giving up\n";
157 push @status, 'taqua-accountcode-NOTFOUND';
158 $cdr->status('done'); #so it doesn't try to rate
159 delete $accountcode_unmatch{$cdr->acctid}; #so it doesn't suck mem
161 warn "WARNING: $cantfind; will keep trying\n";
162 $accountcode_unmatch{$cdr->acctid} = time;
168 $primary->accountcode( $cdr->lastdata );
169 #$primary->freesiderewritestatus( 'taqua-accountcode-primary' );
170 my $error = $primary->replace;
172 warn "WARNING: error rewriting primary CDR (will retry): $error\n";
175 $skip{$primary->acctid} = 1;
177 push @status, 'taqua-accountcode';
178 $cdr->status('done'); #so it doesn't try to rate
184 $cdr->freesiderewritestatus(
185 scalar(@status) ? join('/', @status) : 'skipped'
188 my $error = $cdr->replace;
191 warn "WARNING: error rewriting CDR (will retry in 30 seconds):".
193 sleep 30; #i dunno, wait and see if the database comes back?
196 last if sigterm() || sigint();
200 foreach (sort keys %warning) {
201 warn "WARNING: $_ (x $warning{$_})\n";
205 myexit() if sigterm() || sigint();
206 #sleep 1 unless $found;
207 sleep 5 unless $found;
214 $conf->exists('cdr-asterisk_forward_rewrite')
215 || $conf->exists('cdr-asterisk_australia_rewrite')
216 || $conf->exists('cdr-charged_party_rewrite')
217 || $conf->exists('cdr-taqua-accountcode_rewrite');
221 die "Usage:\n\n freeside-cdrrewrited user\n";
226 freeside-cdrrewrited - Real-time daemon for CDR rewriting
234 Runs continuously, searches for CDRs and does forwarded-call rewriting if any
235 of the "cdr-asterisk_forward_rewrite", "cdr-charged_party_rewrite" or
236 "cdr-taqua-accountcode_rewrite" config options are enabled.