- function __construct($resource=null, $daemonize=true)
- {
- parent::__construct($daemonize);
-
- static $attrs = array('server', 'port', 'user', 'password', 'host');
-
- foreach ($attrs as $attr)
- {
- $this->$attr = common_config('xmpp', $attr);
- }
-
- if ($resource) {
- $this->resource = $resource . 'daemon';
- } else {
- $this->resource = common_config('xmpp', 'resource') . 'daemon';
- }
-
- $this->jid = $this->user.'@'.$this->server.'/'.$this->resource;
-
- $this->log(LOG_INFO, "INITIALIZE XMPPDaemon {$this->jid}");
- }
-
- function connect()
- {
- $connect_to = ($this->host) ? $this->host : $this->server;
-
- $this->log(LOG_INFO, "Connecting to $connect_to on port $this->port");
-
- $this->conn = jabber_connect($this->resource);
-
- if (!$this->conn) {
- return false;
- }
-
- $this->log(LOG_INFO, "Connected");
-
- $this->conn->setReconnectTimeout(600);
-
- $this->log(LOG_INFO, "Sending initial presence.");
-
- jabber_send_presence("Send me a message to post a notice", 'available',
- null, 'available', 100);
-
- $this->log(LOG_INFO, "Done connecting.");
-
- return !$this->conn->isDisconnected();
- }
-
- function name()
- {
- return strtolower('xmppdaemon.'.$this->resource);
- }
-
- function run()
- {
- if ($this->connect()) {
-
- $this->log(LOG_DEBUG, "Initializing stanza handlers.");
-
- $this->conn->addEventHandler('message', 'handle_message', $this);
- $this->conn->addEventHandler('presence', 'handle_presence', $this);
- $this->conn->addEventHandler('reconnect', 'handle_reconnect', $this);
-
- $this->log(LOG_DEBUG, "Beginning processing loop.");
-
- while ($this->conn->processTime(60)) {
- $this->sendPing();
- }
- }
- }
-
- function sendPing()
- {
- if (!isset($this->pingid)) {
- $this->pingid = 0;
- } else {
- $this->pingid++;
- }
-
- $this->log(LOG_DEBUG, "Sending ping #{$this->pingid}");
-
- $this->conn->send("<iq from='{$this->jid}' to='{$this->server}' id='ping_{$this->pingid}' type='get'><ping xmlns='urn:xmpp:ping'/></iq>");
- }
-
- function handle_reconnect(&$pl)
- {
- $this->log(LOG_DEBUG, "Got reconnection callback.");
- $this->conn->processUntil('session_start');
- $this->log(LOG_DEBUG, "Sending reconnection presence.");
- $this->conn->presence('Send me a message to post a notice', 'available', null, 'available', 100);
- unset($pl['xml']);
- $pl['xml'] = null;
-
- $pl = null;
- unset($pl);
- }
-
- function get_user($from)
- {
- $user = User::staticGet('jabber', jabber_normalize_jid($from));
- return $user;
- }
-
- function handle_message(&$pl)
- {
- $from = jabber_normalize_jid($pl['from']);
-
- if ($pl['type'] != 'chat') {
- $this->log(LOG_WARNING, "Ignoring message of type ".$pl['type']." from $from.");
- return;
- }
-
- if (mb_strlen($pl['body']) == 0) {
- $this->log(LOG_WARNING, "Ignoring message with empty body from $from.");
- return;
- }