summaryrefslogtreecommitdiff
path: root/FS/bin/freeside-cdrrewrited
blob: 0b7f6883fbcd1b47eef43a56ea0a53a6c1c018ba (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/perl -w

use strict;
use vars qw( $conf );
use FS::Daemon ':all'; #daemonize1 drop_root daemonize2 myexit logfile sig*
use FS::UID qw( adminsuidsetup );
use FS::Record qw( qsearch ); #qsearchs);
#use FS::cdr;
#use FS::cust_pkg;
#use FS::queue;

my $user = shift or die &usage;

#daemonize1('freeside-sprepaidd', $user); #keep unique pid files w/multi installs
daemonize1('freeside-cdrrewrited');

drop_root();

adminsuidsetup($user);

logfile( "%%%FREESIDE_LOG%%%/cdrrewrited-log.". $FS::UID::datasrc );

daemonize2();

$conf = new FS::Conf;

die "not running; cdr-asterisk_forward_rewrite and cdr-charged_party_rewrite ".
    " conf options are both off\n"
  unless _shouldrun();

#--

while (1) {

  #hmm... don't want to do an expensive search with an ever-growing bunch
  # of unprocessed CDRs during the month... better to mark them all as
  # rewritten "skipped", i.e. why we're a daemon in the first place
  # instead of just doing this search like normal CDRs

  my $found = 0;
  foreach my $cdr ( 
    qsearch( {
      'table'     => 'cdr',
      'extra_sql' => 'FOR UPDATE',
      'hashref'   => {},
      'extra_sql' => 'WHERE freesidestatus IS NULL'.
                     ' AND freesiderewritestatus IS NULL'.
                     ' LIMIT 1024', #arbitrary, but don't eat too much memory
    } )
  ) {

    $found = 1;
    my @status = ();

    if ( $conf->exists('cdr-asterisk_forward_rewrite')
         && $cdr->dstchannel =~ /^Local\/(\d+)/i && $1 ne $cdr->dst
       )
    {

      my $dst = $1;

      warn "dst ". $cdr->dst. " does not match dstchannel $dst ".
           "(". $cdr->dstchannel. "); rewriting CDR as a forwarded call";

      $cdr->charged_party($cdr->dst);
      $cdr->dst($dst);
      $cdr->amaflags(2);

      push @status, 'asterisk_forward';

    }

    if ( $conf->exists('cdr-charged_party_rewrite') && ! $cdr->charged_party ) {

      $cdr->set_charged_party;
      push @status, 'charged_party';

    }

    $cdr->freesiderewritestatus(
      scalar(@status) ? join('/', @status) : 'skipped'
    );

    my $error = $cdr->replace;

    if ( $error ) {
      warn "WARNING: error rewriting CDR (will retry in 30 seconds):".
           " $error\n";
      sleep 30; #i dunno, wait and see if the database comes back?
    }

  }

  myexit() if sigterm() || sigint();
  #sleep 1 unless $found;
  sleep 5 unless $found;

}

#--

sub _shouldrun {
     $conf->exists('cdr-asterisk_forward_rewrite')
  || $conf->exists('cdr-charged_party_rewrite');
}

sub usage { 
  die "Usage:\n\n  freeside-cdrrewrited user\n";
}

=head1 NAME

freeside-cdrrewrited - Real-time daemon for CDR rewriting

=head1 SYNOPSIS

  freeside-cdrrewrited

=head1 DESCRIPTION

Runs continuously, searches for CDRs and does forwarded-call rewriting if the
"cdr-asterisk_forward_rewrite" or "cdr-charged_party_rewrite" config option is
enabled.

=head1 SEE ALSO

=cut

1;