-define('LACONICA', true);
-
-require_once(INSTALLDIR . '/lib/common.php');
-require_once(INSTALLDIR . '/lib/jabber.php');
-require_once(INSTALLDIR . '/lib/daemon.php');
-
-set_error_handler('common_error_handler');
-
-# This is kind of clunky; we create a class to call the global functions
-# in jabber.php, which create a new XMPP class. A more elegant (?) solution
-# might be to use make this a subclass of XMPP.
-
-class XMPPDaemon extends Daemon
-{
-
- function XMPPDaemon($resource=null)
- {
- static $attrs = array('server', 'port', 'user', 'password', 'host');
-
- foreach ($attrs as $attr)
- {
- $this->$attr = common_config('xmpp', $attr);
- }
-
- if ($resource) {
- $this->resource = $resource;
- } else {
- $this->resource = common_config('xmpp', 'resource') . 'daemon';
- }
-
- $this->log(LOG_INFO, "INITIALIZE XMPPDaemon {$this->user}@{$this->server}/{$this->resource}");
- }
-
- 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->conn->setReconnectTimeout(600);
-
- jabber_send_presence("Send me a message to post a notice", 'available',
- null, 'available', 100);
- return !$this->conn->isDisconnected();
- }
-
- function name()
- {
- return strtolower('xmppdaemon.'.$this->resource);
- }
-
- function run()
- {
- if ($this->connect()) {
-
- $this->conn->addEventHandler('message', 'handle_message', $this);
- $this->conn->addEventHandler('presence', 'handle_presence', $this);
- $this->conn->addEventHandler('reconnect', 'handle_reconnect', $this);
-
- $this->conn->process();
- }
- }
-
- function handle_reconnect(&$pl)
- {
- $this->conn->processUntil('session_start');
- $this->conn->presence('Send me a message to post a notice', 'available', null, 'available', 100);
- }
-
- function get_user($from)
- {
- $user = User::staticGet('jabber', jabber_normalize_jid($from));
- return $user;
- }
-
- function handle_message(&$pl)
- {
- if ($pl['type'] != 'chat') {
- return;
- }
- if (mb_strlen($pl['body']) == 0) {
- return;
- }
-
- $from = jabber_normalize_jid($pl['from']);
-
- # Forwarded from another daemon (probably a broadcaster) for
- # us to handle