sendxmpp logger for incoming calls
[auerswald-callnotifier.git] / src / callnotifier / Logger / CallSendXmpp.php
1 <?php
2 namespace callnotifier;
3
4 /**
5  * Notify people via XMPP about incoming calls
6  * Utilizes the "sendxmpp" tool.
7  */
8 class Logger_CallSendXmpp extends Logger_CallBase
9 {
10     protected $recipients;
11     protected $debug = false;
12
13     public function __construct($recipients, $callTypes = 'i', $msns = array())
14     {
15         parent::__construct($callTypes, $msns);
16         $this->recipients = $recipients;
17     }
18
19     public function log($type, $arData)
20     {
21         $call = $arData['call'];
22         if (!$this->hasValidType($call)) {
23             return;
24         }
25         if (!$this->hasValidMsn($call)) {
26             return;
27         }
28
29         if ($type != 'startingCall') {
30             return;
31         }
32         $this->displayStart($arData['call']);
33     }
34
35
36     protected function displayStart(CallMonitor_Call $call)
37     {
38         $this->addUnsetVars($call);
39
40         $type = 'from';
41         $varNumber   = $type;
42         $varName     = $type . 'Name';
43         $varLocation = $type . 'Location';
44
45         $str = "Incoming call:\n";
46         if ($call->$varName !== null) {
47             $str .= $call->$varName . "\n";
48         } else {
49             $str .= "*unknown*\n";
50         }
51         if ($call->$varLocation !== null) {
52             $str .= '' . $call->$varLocation . "\n";
53         }
54         $str .= $this->getNumber($call->$varNumber) . "\n";
55
56         $this->notify($str);
57     }
58
59     protected function notify($msg)
60     {
61         $runInBackground = ' > /dev/null 2>&1 &';
62         if ($this->debug) {
63             $runInBackground = '';
64             echo $msg . "\n";
65         }
66
67         foreach ((array)$this->recipients as $recipient) {
68             //use system instead of exec to make debugging possible
69             system(
70                 'echo ' . escapeshellarg($msg)
71                 . ' | sendxmpp'
72                 . ' --message-type=headline'//no offline storage
73                 . ' --resource callnotifier'
74                 . ' ' . escapeshellarg($recipient)
75                 . $runInBackground
76             );
77         }
78     }
79 }
80 ?>