sendxmpp logger for incoming calls
authorChristian Weiske <cweiske@cweiske.de>
Fri, 28 Nov 2014 17:46:54 +0000 (18:46 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 28 Nov 2014 17:46:54 +0000 (18:46 +0100)
scripts/test-xmpp.php [new file with mode: 0644]
src/callnotifier/Logger/CallSendXmpp.php [new file with mode: 0644]

diff --git a/scripts/test-xmpp.php b/scripts/test-xmpp.php
new file mode 100644 (file)
index 0000000..b59d9f1
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+namespace callnotifier;
+require_once __DIR__ . '/../tests/bootstrap.php';
+
+$l = new Logger_CallSendXmpp('cweiske@cweiske.de', 'i', array('12345'));
+
+$call = new CallMonitor_Call();
+$call->type  = 'i';
+$call->from  = '03411234567';
+//$call->fromName = 'Foo Bar';
+$call->fromLocation = 'Leipzig';
+$call->to    = '12345';
+$call->start = strtotime('2013-08-03 20:11');
+$call->end   = strtotime('2013-08-03 20:12');
+$l->log('startingCall', array('call' => $call));
+
+?>
diff --git a/src/callnotifier/Logger/CallSendXmpp.php b/src/callnotifier/Logger/CallSendXmpp.php
new file mode 100644 (file)
index 0000000..c970430
--- /dev/null
@@ -0,0 +1,80 @@
+<?php
+namespace callnotifier;
+
+/**
+ * Notify people via XMPP about incoming calls
+ * Utilizes the "sendxmpp" tool.
+ */
+class Logger_CallSendXmpp extends Logger_CallBase
+{
+    protected $recipients;
+    protected $debug = false;
+
+    public function __construct($recipients, $callTypes = 'i', $msns = array())
+    {
+        parent::__construct($callTypes, $msns);
+        $this->recipients = $recipients;
+    }
+
+    public function log($type, $arData)
+    {
+        $call = $arData['call'];
+        if (!$this->hasValidType($call)) {
+            return;
+        }
+        if (!$this->hasValidMsn($call)) {
+            return;
+        }
+
+        if ($type != 'startingCall') {
+            return;
+        }
+        $this->displayStart($arData['call']);
+    }
+
+
+    protected function displayStart(CallMonitor_Call $call)
+    {
+        $this->addUnsetVars($call);
+
+        $type = 'from';
+        $varNumber   = $type;
+        $varName     = $type . 'Name';
+        $varLocation = $type . 'Location';
+
+        $str = "Incoming call:\n";
+        if ($call->$varName !== null) {
+            $str .= $call->$varName . "\n";
+        } else {
+            $str .= "*unknown*\n";
+        }
+        if ($call->$varLocation !== null) {
+            $str .= '' . $call->$varLocation . "\n";
+        }
+        $str .= $this->getNumber($call->$varNumber) . "\n";
+
+        $this->notify($str);
+    }
+
+    protected function notify($msg)
+    {
+        $runInBackground = ' > /dev/null 2>&1 &';
+        if ($this->debug) {
+            $runInBackground = '';
+            echo $msg . "\n";
+        }
+
+        foreach ((array)$this->recipients as $recipient) {
+            //use system instead of exec to make debugging possible
+            system(
+                'echo ' . escapeshellarg($msg)
+                . ' | sendxmpp'
+                . ' --message-type=headline'//no offline storage
+                . ' --resource callnotifier'
+                . ' ' . escapeshellarg($recipient)
+                . $runInBackground
+            );
+        }
+    }
+}
+?>