all loggers support call type and MSN filtering now
[auerswald-callnotifier.git] / src / callnotifier / Logger / CallNotifySend.php
1 <?php
2 namespace callnotifier;
3
4 class Logger_CallNotifySend extends Logger_CallBase
5 {
6     public function log($type, $arData)
7     {
8         switch ($type) {
9         case 'startingCall':
10             $displayMethod = 'displayStart';
11             break;
12         case 'finishedCall':
13             $displayMethod = 'displayFinished';
14             break;
15         default:
16             return;
17         }
18
19         $call = $arData['call'];
20         if (!$this->hasValidType($call)) {
21             return;
22         }
23         if (!$this->hasValidMsn($call)) {
24             return;
25         }
26         $this->$displayMethod($arData['call']);
27     }
28
29
30     protected function displayStart(CallMonitor_Call $call)
31     {
32         $this->addUnsetVars($call);
33         if ($call->type == CallMonitor_Call::INCOMING) {
34             $this->notify(
35                 trim($this->getNumberString($call, 'from')),
36                 'Incoming call'
37             );
38         } else {
39             $this->notify(
40                 trim($this->getNumberString($call, 'to')),
41                 'Outgoing call'
42             );
43         }
44     }
45
46     protected function displayFinished(CallMonitor_Call $call)
47     {
48         $this->addUnsetVars($call);
49         if ($call->type == CallMonitor_Call::INCOMING) {
50             $title = trim($this->getNumberString($call, 'from'));
51             $msg   = 'End of incoming call';
52         } else {
53             $title = trim($this->getNumberString($call, 'to'));
54             $msg   = 'End of outgoing call';
55         }
56         $this->notify(
57             $title,
58             $msg
59             . ', length ' . date('H:i:s', $call->end - $call->start - 3600)
60         );
61     }
62
63     protected function notify($title, $msg)
64     {
65         exec(
66             'notify-send'
67             . ' -u low'
68             . ' --expire-time=5000'
69             . ' -i phone'
70             . ' -c callmonitor'
71             . ' ' . escapeshellarg($title)
72             . ' ' . escapeshellarg($msg)
73             . ' > /dev/null 2>&1 &'
74         );
75     }
76 }
77 ?>