link to my blog post
[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     public $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 "Message:\n" . $msg . "\n";
65             echo 'Sending to ' . count((array) $this->recipients)
66                 . " recipients\n";
67         }
68
69         foreach ((array)$this->recipients as $recipient) {
70             //use system instead of exec to make debugging possible
71             $cmd = 'echo ' . escapeshellarg($msg)
72                 . ' | sendxmpp'
73                 . ' --message-type=headline'//no offline storage
74                 . ' --resource callnotifier'
75                 . ' ' . escapeshellarg($recipient)
76                 . $runInBackground;
77             if ($this->debug) {
78                 echo "Executing:\n" . $cmd . "\n";
79             }
80             system($cmd, $retval);
81             if ($this->debug) {
82                 echo 'Exit code: ' . $retval . "\n";
83             }
84         }
85     }
86 }
87 ?>