aboutsummaryrefslogtreecommitdiff
path: root/src/callnotifier/Logger/CallSendXmpp.php
blob: 33fc6da07bfa86836ccb06d18ad07304ad51531a (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
<?php
namespace callnotifier;

/**
 * Notify people via XMPP about incoming calls
 * Utilizes the "sendxmpp" tool.
 */
class Logger_CallSendXmpp extends Logger_CallBase
{
    protected $recipients;
    public $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 "Message:\n" . $msg . "\n";
            echo 'Sending to ' . count((array) $this->recipients)
                . " recipients\n";
        }

        foreach ((array)$this->recipients as $recipient) {
            //use system instead of exec to make debugging possible
            $cmd = 'echo ' . escapeshellarg($msg)
                . ' | sendxmpp'
                . ' --message-type=headline'//no offline storage
                . ' --resource callnotifier'
                . ' ' . escapeshellarg($recipient)
                . $runInBackground;
            if ($this->debug) {
                echo "Executing:\n" . $cmd . "\n";
            }
            system($cmd, $retval);
            if ($this->debug) {
                echo 'Exit code: ' . $retval . "\n";
            }
        }
    }
}
?>