summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorEvan Prodromou <git@evanprodromou.name>2009-01-23 08:58:31 +0100
committerEvan Prodromou <git@evanprodromou.name>2009-01-23 08:58:31 +0100
commita7c85bebd5be9ea019a8c80d74730d7eb28d4651 (patch)
treec3fdf9575a342624bc71aad1d439ae73594f558c /scripts
parent4873277b58941ae6ec16543f437f4267ccab5ac0 (diff)
parent7aa496cd8a939960eeaf79f3397f6fe94097e047 (diff)
Merge branch 'master' of /var/www/mublog
Conflicts: actions/api.php actions/deletenotice.php actions/recoverpassword.php actions/remotesubscribe.php actions/tag.php actions/tagrss.php actions/twitapiaccount.php actions/twitapiusers.php classes/Notice.php classes/User.php lib/common.php lib/language.php lib/subs.php lib/twitterapi.php lib/util.php scripts/inbox_users.php scripts/update_translations.php Merged development trunk into laconica head. woohoo!
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/enjitqueuehandler.php94
-rwxr-xr-xscripts/fixup_hashtags.php12
-rwxr-xr-xscripts/fixup_inboxes.php60
-rwxr-xr-xscripts/fixup_notices_rendered.php22
-rwxr-xr-xscripts/getpiddir.php4
-rwxr-xr-xscripts/jabberqueuehandler.php33
-rwxr-xr-xscripts/maildaemon.php361
-rwxr-xr-xscripts/ombqueuehandler.php62
-rwxr-xr-xscripts/publicqueuehandler.php35
-rwxr-xr-xscripts/sitemap.php607
-rwxr-xr-xscripts/smsqueuehandler.php41
-rwxr-xr-xscripts/synctwitterfriends.php28
-rwxr-xr-xscripts/update_facebook.php139
-rwxr-xr-xscripts/xmppconfirmhandler.php199
-rwxr-xr-xscripts/xmppdaemon.php527
15 files changed, 1217 insertions, 1007 deletions
diff --git a/scripts/enjitqueuehandler.php b/scripts/enjitqueuehandler.php
index 8538ae09a..3a4f8315d 100755
--- a/scripts/enjitqueuehandler.php
+++ b/scripts/enjitqueuehandler.php
@@ -20,8 +20,8 @@
# Abort if called from a web server
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
- print "This script must be run from the command line\n";
- exit();
+ print "This script must be run from the command line\n";
+ exit();
}
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
@@ -33,21 +33,25 @@ require_once(INSTALLDIR . '/lib/queuehandler.php');
set_error_handler('common_error_handler');
-class EnjitQueueHandler extends QueueHandler {
-
- function transport() {
- return 'enjit';
- }
+class EnjitQueueHandler extends QueueHandler
+{
+
+ function transport()
+ {
+ return 'enjit';
+ }
- function start() {
+ function start()
+ {
$this->log(LOG_INFO, "Starting EnjitQueueHandler");
$this->log(LOG_INFO, "Broadcasting to ".common_config('enjit', 'apiurl'));
- return true;
- }
+ return true;
+ }
- function handle_notice($notice) {
+ function handle_notice($notice)
+ {
- $profile = Profile::staticGet($notice->profile_id);
+ $profile = Profile::staticGet($notice->profile_id);
$this->log(LOG_INFO, "Posting Notice ".$notice->id." from ".$profile->nickname);
@@ -60,25 +64,25 @@ class EnjitQueueHandler extends QueueHandler {
#
# Build an Atom message from the notice
#
- $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
- $msg = $profile->nickname . ': ' . $notice->content;
-
- $atom = "<entry xmlns='http://www.w3.org/2005/Atom'>\n";
- $atom .= "<apisource>".common_config('enjit','source')."</apisource>\n";
- $atom .= "<source>\n";
- $atom .= "<title>" . $profile->nickname . " - " . common_config('site', 'name') . "</title>\n";
- $atom .= "<link href='" . $profile->profileurl . "'/>\n";
- $atom .= "<link rel='self' type='application/rss+xml' href='" . common_local_url('userrss', array('nickname' => $profile->nickname)) . "'/>\n";
- $atom .= "<author><name>" . $profile->nickname . "</name></author>\n";
- $atom .= "<icon>" . common_profile_avatar_url($profile, AVATAR_PROFILE_SIZE) . "</icon>\n";
- $atom .= "</source>\n";
- $atom .= "<title>" . htmlspecialchars($msg) . "</title>\n";
- $atom .= "<summary>" . htmlspecialchars($msg) . "</summary>\n";
- $atom .= "<link rel='alternate' href='" . $noticeurl . "' />\n";
- $atom .= "<id>". $notice->uri . "</id>\n";
- $atom .= "<published>".common_date_w3dtf($notice->created)."</published>\n";
- $atom .= "<updated>".common_date_w3dtf($notice->modified)."</updated>\n";
- $atom .= "</entry>\n";
+ $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
+ $msg = $profile->nickname . ': ' . $notice->content;
+
+ $atom = "<entry xmlns='http://www.w3.org/2005/Atom'>\n";
+ $atom .= "<apisource>".common_config('enjit','source')."</apisource>\n";
+ $atom .= "<source>\n";
+ $atom .= "<title>" . $profile->nickname . " - " . common_config('site', 'name') . "</title>\n";
+ $atom .= "<link href='" . $profile->profileurl . "'/>\n";
+ $atom .= "<link rel='self' type='application/rss+xml' href='" . common_local_url('userrss', array('nickname' => $profile->nickname)) . "'/>\n";
+ $atom .= "<author><name>" . $profile->nickname . "</name></author>\n";
+ $atom .= "<icon>" . common_profile_avatar_url($profile, AVATAR_PROFILE_SIZE) . "</icon>\n";
+ $atom .= "</source>\n";
+ $atom .= "<title>" . htmlspecialchars($msg) . "</title>\n";
+ $atom .= "<summary>" . htmlspecialchars($msg) . "</summary>\n";
+ $atom .= "<link rel='alternate' href='" . $noticeurl . "' />\n";
+ $atom .= "<id>". $notice->uri . "</id>\n";
+ $atom .= "<published>".common_date_w3dtf($notice->created)."</published>\n";
+ $atom .= "<updated>".common_date_w3dtf($notice->modified)."</updated>\n";
+ $atom .= "</entry>\n";
$url = common_config('enjit', 'apiurl') . "/submit/". common_config('enjit','apikey');
$data = "msg=$atom";
@@ -86,43 +90,43 @@ class EnjitQueueHandler extends QueueHandler {
#
# POST the message to $config['enjit']['apiurl']
#
- $ch = curl_init();
+ $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
+ curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POST, 1) ;
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+ curl_setopt($ch, CURLOPT_POST, 1) ;
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
# SSL and Debugging options
#
- # curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- # curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
+ # curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
+ # curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
# curl_setopt($ch, CURLOPT_VERBOSE, 1);
- $result = curl_exec($ch);
+ $result = curl_exec($ch);
- $code = curl_getinfo($ch, CURLINFO_HTTP_CODE );
+ $code = curl_getinfo($ch, CURLINFO_HTTP_CODE );
$this->log(LOG_INFO, "Response Code: $code");
- curl_close($ch);
+ curl_close($ch);
return $code;
- }
-
+ }
+
}
mb_internal_encoding('UTF-8');
-$id = ($argc > 1) ? $argv[1] : NULL;
+$id = ($argc > 1) ? $argv[1] : null;
$handler = new EnjitQueueHandler($id);
if ($handler->start()) {
- $handler->handle_queue();
+ $handler->handle_queue();
}
$handler->finish();
diff --git a/scripts/fixup_hashtags.php b/scripts/fixup_hashtags.php
index 88f385798..6f65c78a1 100755
--- a/scripts/fixup_hashtags.php
+++ b/scripts/fixup_hashtags.php
@@ -37,10 +37,10 @@ $cnt = $notice->find();
while ($notice->fetch()) {
common_log(LOG_INFO, 'Getting tags for notice #' . $notice->id);
$notice->saveTags();
- $original = clone($notice);
- $notice->rendered = common_render_content($notice->content, $notice);
- $result = $notice->update($original);
- if (!$result) {
- common_log_db_error($notice, 'UPDATE', __FILE__);
- }
+ $original = clone($notice);
+ $notice->rendered = common_render_content($notice->content, $notice);
+ $result = $notice->update($original);
+ if (!$result) {
+ common_log_db_error($notice, 'UPDATE', __FILE__);
+ }
}
diff --git a/scripts/fixup_inboxes.php b/scripts/fixup_inboxes.php
index 1715b0bc1..a5c8a0a5a 100755
--- a/scripts/fixup_inboxes.php
+++ b/scripts/fixup_inboxes.php
@@ -34,14 +34,14 @@ define('LACONICA', true);
require_once(INSTALLDIR . '/lib/common.php');
-$start_at = ($argc > 1) ? $argv[1] : NULL;
+$start_at = ($argc > 1) ? $argv[1] : null;
common_log(LOG_INFO, 'Updating user inboxes.');
$user = new User();
if ($start_at) {
- $user->whereAdd('id >= ' . $start_at);
+ $user->whereAdd('id >= ' . $start_at);
}
$cnt = $user->find();
@@ -49,32 +49,32 @@ $cache = common_memcache();
while ($user->fetch()) {
common_log(LOG_INFO, 'Updating inbox for user ' . $user->id);
- $user->query('BEGIN');
- $inbox = new Notice_inbox();
- $result = $inbox->query('INSERT LOW_PRIORITY INTO notice_inbox (user_id, notice_id, created) ' .
- 'SELECT ' . $user->id . ', notice.id, notice.created ' .
- 'FROM subscription JOIN notice ON subscription.subscribed = notice.profile_id ' .
- 'WHERE subscription.subscriber = ' . $user->id . ' ' .
- 'AND notice.created >= subscription.created ' .
- 'AND NOT EXISTS (SELECT user_id, notice_id ' .
- 'FROM notice_inbox ' .
- 'WHERE user_id = ' . $user->id . ' ' .
- 'AND notice_id = notice.id)');
- if (is_null($result) || $result === false) {
- common_log_db_error($inbox, 'INSERT', __FILE__);
- continue;
- }
- $orig = clone($user);
- $user->inboxed = 1;
- $result = $user->update($orig);
- if (!$result) {
- common_log_db_error($user, 'UPDATE', __FILE__);
- continue;
- }
- $user->query('COMMIT');
- $inbox->free();
- unset($inbox);
- if ($cache) {
- $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
- }
+ $user->query('BEGIN');
+ $inbox = new Notice_inbox();
+ $result = $inbox->query('INSERT LOW_PRIORITY INTO notice_inbox (user_id, notice_id, created) ' .
+ 'SELECT ' . $user->id . ', notice.id, notice.created ' .
+ 'FROM subscription JOIN notice ON subscription.subscribed = notice.profile_id ' .
+ 'WHERE subscription.subscriber = ' . $user->id . ' ' .
+ 'AND notice.created >= subscription.created ' .
+ 'AND NOT EXISTS (SELECT user_id, notice_id ' .
+ 'FROM notice_inbox ' .
+ 'WHERE user_id = ' . $user->id . ' ' .
+ 'AND notice_id = notice.id)');
+ if (is_null($result) || $result === false) {
+ common_log_db_error($inbox, 'INSERT', __FILE__);
+ continue;
+ }
+ $orig = clone($user);
+ $user->inboxed = 1;
+ $result = $user->update($orig);
+ if (!$result) {
+ common_log_db_error($user, 'UPDATE', __FILE__);
+ continue;
+ }
+ $user->query('COMMIT');
+ $inbox->free();
+ unset($inbox);
+ if ($cache) {
+ $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
+ }
}
diff --git a/scripts/fixup_notices_rendered.php b/scripts/fixup_notices_rendered.php
index c6c925729..c27185546 100755
--- a/scripts/fixup_notices_rendered.php
+++ b/scripts/fixup_notices_rendered.php
@@ -20,8 +20,8 @@
# Abort if called from a web server
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
- print "This script must be run from the command line\n";
- exit();
+ print "This script must be run from the command line\n";
+ exit();
}
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
@@ -31,20 +31,20 @@ require_once(INSTALLDIR . '/lib/common.php');
common_log(LOG_INFO, 'Starting to render old notices.');
-$start_at = ($argc > 1) ? $argv[1] : NULL;
+$start_at = ($argc > 1) ? $argv[1] : null;
$notice = new Notice();
if ($start_at) {
- $notice->whereAdd('id >= ' . $start_at);
+ $notice->whereAdd('id >= ' . $start_at);
}
$cnt = $notice->find();
while ($notice->fetch()) {
- common_log(LOG_INFO, 'Pre-rendering notice #' . $notice->id);
- $original = clone($notice);
- $notice->rendered = common_render_content($notice->content, $notice);
- $result = $notice->update($original);
- if (!$result) {
- common_log_db_error($notice, 'UPDATE', __FILE__);
- }
+ common_log(LOG_INFO, 'Pre-rendering notice #' . $notice->id);
+ $original = clone($notice);
+ $notice->rendered = common_render_content($notice->content, $notice);
+ $result = $notice->update($original);
+ if (!$result) {
+ common_log_db_error($notice, 'UPDATE', __FILE__);
+ }
}
diff --git a/scripts/getpiddir.php b/scripts/getpiddir.php
index b4dda2254..4f5704249 100755
--- a/scripts/getpiddir.php
+++ b/scripts/getpiddir.php
@@ -20,8 +20,8 @@
# Abort if called from a web server
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
- print "This script must be run from the command line\n";
- exit();
+ print "This script must be run from the command line\n";
+ exit();
}
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
diff --git a/scripts/jabberqueuehandler.php b/scripts/jabberqueuehandler.php
index 59cdb94ad..924fc4545 100755
--- a/scripts/jabberqueuehandler.php
+++ b/scripts/jabberqueuehandler.php
@@ -20,8 +20,8 @@
# Abort if called from a web server
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
- print "This script must be run from the command line\n";
- exit();
+ print "This script must be run from the command line\n";
+ exit();
}
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
@@ -33,22 +33,25 @@ require_once(INSTALLDIR . '/lib/xmppqueuehandler.php');
set_error_handler('common_error_handler');
-class JabberQueueHandler extends XmppQueueHandler {
+class JabberQueueHandler extends XmppQueueHandler
+{
- var $conn = NULL;
+ var $conn = null;
- function transport() {
- return 'jabber';
- }
+ function transport()
+ {
+ return 'jabber';
+ }
- function handle_notice($notice) {
- try {
- return jabber_broadcast_notice($notice);
- } catch (XMPPHP_Exception $e) {
- $this->log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
- exit(1);
- }
- }
+ function handle_notice($notice)
+ {
+ try {
+ return jabber_broadcast_notice($notice);
+ } catch (XMPPHP_Exception $e) {
+ $this->log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
+ exit(1);
+ }
+ }
}
ini_set("max_execution_time", "0");
diff --git a/scripts/maildaemon.php b/scripts/maildaemon.php
index 8b809f646..b9facec1a 100755
--- a/scripts/maildaemon.php
+++ b/scripts/maildaemon.php
@@ -20,8 +20,8 @@
# Abort if called from a web server
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
- print "This script must be run from the command line\n";
- exit();
+ print "This script must be run from the command line\n";
+ exit();
}
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
@@ -34,181 +34,194 @@ require_once('Mail/mimeDecode.php');
# FIXME: we use both Mail_mimeDecode and mailparse
# Need to move everything to mailparse
-class MailerDaemon {
-
- function __construct() {
- }
-
- function handle_message($fname='php://stdin') {
- list($from, $to, $msg) = $this->parse_message($fname);
- if (!$from || !$to || !$msg) {
- $this->error(NULL, _('Could not parse message.'));
- }
- common_log(LOG_INFO, "Mail from $from to $to: " .substr($msg, 0, 20));
- $user = $this->user_from($from);
- if (!$user) {
- $this->error($from, _('Not a registered user.'));
- return false;
- }
- if (!$this->user_match_to($user, $to)) {
- $this->error($from, _('Sorry, that is not your incoming email address.'));
- return false;
- }
- if (!$user->emailpost) {
- $this->error($from, _('Sorry, no incoming email allowed.'));
- return false;
- }
- $response = $this->handle_command($user, $from, $msg);
- if ($response) {
- return true;
- }
- $msg = $this->cleanup_msg($msg);
- $this->add_notice($user, $msg);
- }
-
- function error($from, $msg) {
- file_put_contents("php://stderr", $msg . "\n");
- exit(1);
- }
-
- function user_from($from_hdr) {
- $froms = mailparse_rfc822_parse_addresses($from_hdr);
- if (!$froms) {
- return NULL;
- }
- $from = $froms[0];
- $addr = common_canonical_email($from['address']);
- $user = User::staticGet('email', $addr);
- if (!$user) {
- $user = User::staticGet('smsemail', $addr);
- }
- return $user;
- }
-
- function user_match_to($user, $to_hdr) {
- $incoming = $user->incomingemail;
- $tos = mailparse_rfc822_parse_addresses($to_hdr);
- foreach ($tos as $to) {
- if (strcasecmp($incoming, $to['address']) == 0) {
- return true;
- }
- }
- return false;
- }
-
- function handle_command($user, $from, $msg) {
- $inter = new CommandInterpreter();
- $cmd = $inter->handle_command($user, $msg);
- if ($cmd) {
- $cmd->execute(new MailChannel($from));
- return true;
- }
- return false;
- }
-
- function respond($from, $to, $response) {
-
- $headers['From'] = $to;
- $headers['To'] = $from;
- $headers['Subject'] = "Command complete";
-
- return mail_send(array($from), $headers, $response);
- }
-
- function log($level, $msg) {
- common_log($level, 'MailDaemon: '.$msg);
- }
-
- function add_notice($user, $msg) {
+class MailerDaemon
+{
+
+ function __construct()
+ {
+ }
+
+ function handle_message($fname='php://stdin')
+ {
+ list($from, $to, $msg) = $this->parse_message($fname);
+ if (!$from || !$to || !$msg) {
+ $this->error(null, _('Could not parse message.'));
+ }
+ common_log(LOG_INFO, "Mail from $from to $to: " .substr($msg, 0, 20));
+ $user = $this->user_from($from);
+ if (!$user) {
+ $this->error($from, _('Not a registered user.'));
+ return false;
+ }
+ if (!$this->user_match_to($user, $to)) {
+ $this->error($from, _('Sorry, that is not your incoming email address.'));
+ return false;
+ }
+ if (!$user->emailpost) {
+ $this->error($from, _('Sorry, no incoming email allowed.'));
+ return false;
+ }
+ $response = $this->handle_command($user, $from, $msg);
+ if ($response) {
+ return true;
+ }
+ $msg = $this->cleanup_msg($msg);
+ $this->add_notice($user, $msg);
+ }
+
+ function error($from, $msg)
+ {
+ file_put_contents("php://stderr", $msg . "\n");
+ exit(1);
+ }
+
+ function user_from($from_hdr)
+ {
+ $froms = mailparse_rfc822_parse_addresses($from_hdr);
+ if (!$froms) {
+ return null;
+ }
+ $from = $froms[0];
+ $addr = common_canonical_email($from['address']);
+ $user = User::staticGet('email', $addr);
+ if (!$user) {
+ $user = User::staticGet('smsemail', $addr);
+ }
+ return $user;
+ }
+
+ function user_match_to($user, $to_hdr)
+ {
+ $incoming = $user->incomingemail;
+ $tos = mailparse_rfc822_parse_addresses($to_hdr);
+ foreach ($tos as $to) {
+ if (strcasecmp($incoming, $to['address']) == 0) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ function handle_command($user, $from, $msg)
+ {
+ $inter = new CommandInterpreter();
+ $cmd = $inter->handle_command($user, $msg);
+ if ($cmd) {
+ $cmd->execute(new MailChannel($from));
+ return true;
+ }
+ return false;
+ }
+
+ function respond($from, $to, $response)
+ {
+
+ $headers['From'] = $to;
+ $headers['To'] = $from;
+ $headers['Subject'] = "Command complete";
+
+ return mail_send(array($from), $headers, $response);
+ }
+
+ function log($level, $msg)
+ {
+ common_log($level, 'MailDaemon: '.$msg);
+ }
+
+ function add_notice($user, $msg)
+ {
// should test
// $msg_shortened = common_shorten_links($msg);
// if (mb_strlen($msg_shortened) > 140) ERROR and STOP
- $notice = Notice::saveNew($user->id, $msg, 'mail');
- if (is_string($notice)) {
- $this->log(LOG_ERR, $notice);
- return;
- }
- common_broadcast_notice($notice);
- $this->log(LOG_INFO,
- 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
- }
-
- function parse_message($fname) {
- $contents = file_get_contents($fname);
- $parsed = Mail_mimeDecode::decode(array('input' => $contents,
- 'include_bodies' => true,
- 'decode_headers' => true,
- 'decode_bodies' => true));
- if (!$parsed) {
- return NULL;
- }
-
- $from = $parsed->headers['from'];
-
- $to = $parsed->headers['to'];
-
- $type = $parsed->ctype_primary . '/' . $parsed->ctype_secondary;
-
- if ($parsed->ctype_primary == 'multipart') {
- foreach ($parsed->parts as $part) {
- if ($part->ctype_primary == 'text' &&
- $part->ctype_secondary == 'plain') {
- $msg = $part->body;
- break;
- }
- }
- } else if ($type == 'text/plain') {
- $msg = $parsed->body;
- } else {
- $this->unsupported_type($type);
- }
-
- return array($from, $to, $msg);
- }
-
- function unsupported_type($type) {
- $this->error(NULL, "Unsupported message type: " . $type);
- }
-
- function cleanup_msg($msg) {
- $lines = explode("\n", $msg);
-
- $output = '';
-
- foreach ($lines as $line) {
- // skip quotes
- if (preg_match('/^\s*>.*$/', $line)) {
- continue;
- }
- // skip start of quote
- if (preg_match('/^\s*On.*wrote:\s*$/', $line)) {
- continue;
- }
- // probably interesting to someone, not us
- if (preg_match('/^\s*Sent via/', $line)) {
- continue;
- }
- // skip everything after a sig
- if (preg_match('/^\s*--+\s*$/', $line) ||
- preg_match('/^\s*__+\s*$/', $line))
- {
- break;
- }
- // skip everything after Outlook quote
- if (preg_match('/^\s*-+\s*Original Message\s*-+\s*$/', $line)) {
- break;
- }
- // skip everything after weird forward
- if (preg_match('/^\s*Begin\s+forward/', $line)) {
- break;
- }
-
- $output .= ' ' . $line;
- }
-
- preg_replace('/\s+/', ' ', $output);
- return trim($output);
- }
+ $notice = Notice::saveNew($user->id, $msg, 'mail');
+ if (is_string($notice)) {
+ $this->log(LOG_ERR, $notice);
+ return;
+ }
+ common_broadcast_notice($notice);
+ $this->log(LOG_INFO,
+ 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
+ }
+
+ function parse_message($fname)
+ {
+ $contents = file_get_contents($fname);
+ $parsed = Mail_mimeDecode::decode(array('input' => $contents,
+ 'include_bodies' => true,
+ 'decode_headers' => true,
+ 'decode_bodies' => true));
+ if (!$parsed) {
+ return null;
+ }
+
+ $from = $parsed->headers['from'];
+
+ $to = $parsed->headers['to'];
+
+ $type = $parsed->ctype_primary . '/' . $parsed->ctype_secondary;
+
+ if ($parsed->ctype_primary == 'multipart') {
+ foreach ($parsed->parts as $part) {
+ if ($part->ctype_primary == 'text' &&
+ $part->ctype_secondary == 'plain') {
+ $msg = $part->body;
+ break;
+ }
+ }
+ } else if ($type == 'text/plain') {
+ $msg = $parsed->body;
+ } else {
+ $this->unsupported_type($type);
+ }
+
+ return array($from, $to, $msg);
+ }
+
+ function unsupported_type($type)
+ {
+ $this->error(null, "Unsupported message type: " . $type);
+ }
+
+ function cleanup_msg($msg)
+ {
+ $lines = explode("\n", $msg);
+
+ $output = '';
+
+ foreach ($lines as $line) {
+ // skip quotes
+ if (preg_match('/^\s*>.*$/', $line)) {
+ continue;
+ }
+ // skip start of quote
+ if (preg_match('/^\s*On.*wrote:\s*$/', $line)) {
+ continue;
+ }
+ // probably interesting to someone, not us
+ if (preg_match('/^\s*Sent via/', $line)) {
+ continue;
+ }
+ // skip everything after a sig
+ if (preg_match('/^\s*--+\s*$/', $line) ||
+ preg_match('/^\s*__+\s*$/', $line))
+ {
+ break;
+ }
+ // skip everything after Outlook quote
+ if (preg_match('/^\s*-+\s*Original Message\s*-+\s*$/', $line)) {
+ break;
+ }
+ // skip everything after weird forward
+ if (preg_match('/^\s*Begin\s+forward/', $line)) {
+ break;
+ }
+
+ $output .= ' ' . $line;
+ }
+
+ preg_replace('/\s+/', ' ', $output);
+ return trim($output);
+ }
}
$md = new MailerDaemon();
diff --git a/scripts/ombqueuehandler.php b/scripts/ombqueuehandler.php
index 1df816d14..cdcea51dc 100755
--- a/scripts/ombqueuehandler.php
+++ b/scripts/ombqueuehandler.php
@@ -20,8 +20,8 @@
# Abort if called from a web server
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
- print "This script must be run from the command line\n";
- exit();
+ print "This script must be run from the command line\n";
+ exit();
}
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
@@ -33,33 +33,39 @@ require_once(INSTALLDIR . '/lib/queuehandler.php');
set_error_handler('common_error_handler');
-class OmbQueueHandler extends QueueHandler {
-
- function transport() {
- return 'omb';
- }
-
- function start() {
- $this->log(LOG_INFO, "INITIALIZE");
- return true;
- }
+class OmbQueueHandler extends QueueHandler
+{
+
+ function transport()
+ {
+ return 'omb';
+ }
+
+ function start()
+ {
+ $this->log(LOG_INFO, "INITIALIZE");
+ return true;
+ }
- function handle_notice($notice) {
- if ($this->is_remote($notice)) {
- $this->log(LOG_DEBUG, 'Ignoring remote notice ' . $notice->id);
- return true;
- } else {
- return omb_broadcast_remote_subscribers($notice);
- }
- }
-
- function finish() {
- }
+ function handle_notice($notice)
+ {
+ if ($this->is_remote($notice)) {
+ $this->log(LOG_DEBUG, 'Ignoring remote notice ' . $notice->id);
+ return true;
+ } else {
+ return omb_broadcast_remote_subscribers($notice);
+ }
+ }
+
+ function finish()
+ {
+ }
- function is_remote($notice) {
- $user = User::staticGet($notice->profile_id);
- return is_null($user);
- }
+ function is_remote($notice)
+ {
+ $user = User::staticGet($notice->profile_id);
+ return is_null($user);
+ }
}
ini_set("max_execution_time", "0");
@@ -67,7 +73,7 @@ ini_set("max_input_time", "0");
set_time_limit(0);
mb_internal_encoding('UTF-8');
-$id = ($argc > 1) ? $argv[1] : NULL;
+$id = ($argc > 1) ? $argv[1] : null;
$handler = new OmbQueueHandler($id);
diff --git a/scripts/publicqueuehandler.php b/scripts/publicqueuehandler.php
index b1ae1d581..5075c12df 100755
--- a/scripts/publicqueuehandler.php
+++ b/scripts/publicqueuehandler.php
@@ -20,8 +20,8 @@
# Abort if called from a web server
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
- print "This script must be run from the command line\n";
- exit();
+ print "This script must be run from the command line\n";
+ exit();
}
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
@@ -33,20 +33,23 @@ require_once(INSTALLDIR . '/lib/xmppqueuehandler.php');
set_error_handler('common_error_handler');
-class PublicQueueHandler extends XmppQueueHandler {
-
- function transport() {
- return 'public';
- }
-
- function handle_notice($notice) {
- try {
- return jabber_public_notice($notice);
- } catch (XMPPHP_Exception $e) {
- $this->log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
- die($e->getMessage());
- }
- }
+class PublicQueueHandler extends XmppQueueHandler
+{
+
+ function transport()
+ {
+ return 'public';
+ }
+
+ function handle_notice($notice)
+ {
+ try {
+ return jabber_public_notice($notice);
+ } catch (XMPPHP_Exception $e) {
+ $this->log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
+ die($e->getMessage());
+ }
+ }
}
ini_set("max_execution_time", "0");
diff --git a/scripts/sitemap.php b/scripts/sitemap.php
index 6b845beae..51a9bbd75 100755
--- a/scripts/sitemap.php
+++ b/scripts/sitemap.php
@@ -18,189 +18,193 @@ index_map();
# ------------------------------------------------------------------------------
# Generate index sitemap of all other sitemaps.
-function index_map() {
- global $output_paths;
- $output_dir = $output_paths['output_dir'];
- $output_url = $output_paths['output_url'];
-
- foreach (glob("$output_dir*.xml") as $file_name) {
-
- # Just the file name please.
- $file_name = preg_replace("|$output_dir|", '', $file_name);
-
- $index_urls .= sitemap(
- array(
- 'url' => $output_url . $file_name,
- 'changefreq' => 'daily'
- )
- );
- }
-
- write_file($output_paths['index_file'], sitemapindex($index_urls));
+function index_map()
+{
+ global $output_paths;
+ $output_dir = $output_paths['output_dir'];
+ $output_url = $output_paths['output_url'];
+
+ foreach (glob("$output_dir*.xml") as $file_name) {
+
+ # Just the file name please.
+ $file_name = preg_replace("|$output_dir|", '', $file_name);
+
+ $index_urls .= sitemap(
+ array(
+ 'url' => $output_url . $file_name,
+ 'changefreq' => 'daily'
+ )
+ );
+ }
+
+ write_file($output_paths['index_file'], sitemapindex($index_urls));
}
# Generate sitemap of standard site elements.
-function standard_map() {
- global $output_paths;
-
- $standard_map_urls .= url(
- array(
- 'url' => common_local_url('public'),
- 'changefreq' => 'daily',
- 'priority' => '1',
- )
- );
-
- $standard_map_urls .= url(
- array(
- 'url' => common_local_url('publicrss'),
- 'changefreq' => 'daily',
- 'priority' => '0.3',
- )
- );
-
- $docs = array('about', 'faq', 'contact', 'im', 'openid', 'openmublog', 'privacy', 'source');
-
- foreach($docs as $title) {
- $standard_map_urls .= url(
- array(
- 'url' => common_local_url('doc', array('title' => $title)),
- 'changefreq' => 'monthly',
- 'priority' => '0.2',
- )
- );
- }
-
- $urlset_path = $output_paths['output_dir'] . 'standard.xml';
-
- write_file($urlset_path, urlset($standard_map_urls));
+function standard_map()
+{
+ global $output_paths;
+
+ $standard_map_urls .= url(
+ array(
+ 'url' => common_local_url('public'),
+ 'changefreq' => 'daily',
+ 'priority' => '1',
+ )
+ );
+
+ $standard_map_urls .= url(
+ array(
+ 'url' => common_local_url('publicrss'),
+ 'changefreq' => 'daily',
+ 'priority' => '0.3',
+ )
+ );
+
+ $docs = array('about', 'faq', 'contact', 'im', 'openid', 'openmublog', 'privacy', 'source');
+
+ foreach($docs as $title) {
+ $standard_map_urls .= url(
+ array(
+ 'url' => common_local_url('doc', array('title' => $title)),
+ 'changefreq' => 'monthly',
+ 'priority' => '0.2',
+ )
+ );
+ }
+
+ $urlset_path = $output_paths['output_dir'] . 'standard.xml';
+
+ write_file($urlset_path, urlset($standard_map_urls));
}
# Generate sitemaps of all notices.
-function notices_map() {
- global $output_paths;
-
- $notices = DB_DataObject::factory('notice');
-
- $notices->query('SELECT id, uri, url, modified FROM notice where is_local = 1');
-
- $notice_count = 0;
- $map_count = 1;
-
- while ($notices->fetch()) {
-
- # Maximum 50,000 URLs per sitemap file.
- if ($notice_count == 50000) {
- $notice_count = 0;
- $map_count++;
- }
-
- # remote notices have an URL
-
- if (!$notices->url && $notices->uri) {
- $notice = array(
- 'url' => ($notices->uri) ? $notices->uri : common_local_url('shownotice', array('notice' => $notices->id)),
- 'lastmod' => common_date_w3dtf($notices->modified),
- 'changefreq' => 'never',
- 'priority' => '1',
- );
-
- $notice_list[$map_count] .= url($notice);
- $notice_count++;
- }
- }
-
- # Make full sitemaps from the lists and save them.
- array_to_map($notice_list, 'notice');
+function notices_map()
+{
+ global $output_paths;
+
+ $notices = DB_DataObject::factory('notice');
+
+ $notices->query('SELECT id, uri, url, modified FROM notice where is_local = 1');
+
+ $notice_count = 0;
+ $map_count = 1;
+
+ while ($notices->fetch()) {
+
+ # Maximum 50,000 URLs per sitemap file.
+ if ($notice_count == 50000) {
+ $notice_count = 0;
+ $map_count++;
+ }
+
+ # remote notices have an URL
+
+ if (!$notices->url && $notices->uri) {
+ $notice = array(
+ 'url' => ($notices->uri) ? $notices->uri : common_local_url('shownotice', array('notice' => $notices->id)),
+ 'lastmod' => common_date_w3dtf($notices->modified),
+ 'changefreq' => 'never',
+ 'priority' => '1',
+ );
+
+ $notice_list[$map_count] .= url($notice);
+ $notice_count++;
+ }
+ }
+
+ # Make full sitemaps from the lists and save them.
+ array_to_map($notice_list, 'notice');
}
# Generate sitemaps of all users.
-function user_map() {
- global $output_paths;
-
- $users = DB_DataObject::factory('user');
-
- $users->query('SELECT id, nickname FROM user');
-
- $user_count = 0;
- $map_count = 1;
-
- while ($users->fetch()) {
-
- # Maximum 50,000 URLs per sitemap file.
- if ($user_count == 50000) {
- $user_count = 0;
- $map_count++;
- }
-
- $user_args = array('nickname' => $users->nickname);
-
- # Define parameters for generating <url></url> elements.
- $user = array(
- 'url' => common_local_url('showstream', $user_args),
- 'changefreq' => 'daily',
- 'priority' => '1',
- );
-
- $user_rss = array(
- 'url' => common_local_url('userrss', $user_args),
- 'changefreq' => 'daily',
- 'priority' => '0.3',
- );
-
- $all = array(
- 'url' => common_local_url('all', $user_args),
- 'changefreq' => 'daily',
- 'priority' => '1',
- );
-
- $all_rss = array(
- 'url' => common_local_url('allrss', $user_args),
- 'changefreq' => 'daily',
- 'priority' => '0.3',
- );
-
- $replies = array(
- 'url' => common_local_url('replies', $user_args),
- 'changefreq' => 'daily',
- 'priority' => '1',
- );
-
- $replies_rss = array(
- 'url' => common_local_url('repliesrss', $user_args),
- 'changefreq' => 'daily',
- 'priority' => '0.3',
- );
-
- $foaf = array(
- 'url' => common_local_url('foaf', $user_args),
- 'changefreq' => 'weekly',
- 'priority' => '0.5',
- );
-
- # Construct a <url></url> element for each user facet and add it
- # to our existing list of those.
- $user_list[$map_count] .= url($user);
- $user_rss_list[$map_count] .= url($user_rss);
- $all_list[$map_count] .= url($all);
- $all_rss_list[$map_count] .= url($all_rss);
- $replies_list[$map_count] .= url($replies);
- $replies_rss_list[$map_count] .= url($replies_rss);
- $foaf_list[$map_count] .= url($foaf);
-
- $user_count++;
- }
-
- # Make full sitemaps from the lists and save them.
- # Possible factoring: put all the lists into a master array, thus allowing
- # calling with single argument (i.e., array_to_map('user')).
- array_to_map($user_list, 'user');
- array_to_map($user_rss_list, 'user_rss');
- array_to_map($all_list, 'all');
- array_to_map($all_rss_list, 'all_rss');
- array_to_map($replies_list, 'replies');
- array_to_map($replies_rss_list, 'replies_rss');
- array_to_map($foaf_list, 'foaf');
+function user_map()
+{
+ global $output_paths;
+
+ $users = DB_DataObject::factory('user');
+
+ $users->query('SELECT id, nickname FROM user');
+
+ $user_count = 0;
+ $map_count = 1;
+
+ while ($users->fetch()) {
+
+ # Maximum 50,000 URLs per sitemap file.
+ if ($user_count == 50000) {
+ $user_count = 0;
+ $map_count++;
+ }
+
+ $user_args = array('nickname' => $users->nickname);
+
+ # Define parameters for generating <url></url> elements.
+ $user = array(
+ 'url' => common_local_url('showstream', $user_args),
+ 'changefreq' => 'daily',
+ 'priority' => '1',
+ );
+
+ $user_rss = array(
+ 'url' => common_local_url('userrss', $user_args),
+ 'changefreq' => 'daily',
+ 'priority' => '0.3',
+ );
+
+ $all = array(
+ 'url' => common_local_url('all', $user_args),
+ 'changefreq' => 'daily',
+ 'priority' => '1',
+ );
+
+ $all_rss = array(
+ 'url' => common_local_url('allrss', $user_args),
+ 'changefreq' => 'daily',
+ 'priority' => '0.3',
+ );
+
+ $replies = array(
+ 'url' => common_local_url('replies', $user_args),
+ 'changefreq' => 'daily',
+ 'priority' => '1',
+ );
+
+ $replies_rss = array(
+ 'url' => common_local_url('repliesrss', $user_args),
+ 'changefreq' => 'daily',
+ 'priority' => '0.3',
+ );
+
+ $foaf = array(
+ 'url' => common_local_url('foaf', $user_args),
+ 'changefreq' => 'weekly',
+ 'priority' => '0.5',
+ );
+
+ # Construct a <url></url> element for each user facet and add it
+ # to our existing list of those.
+ $user_list[$map_count] .= url($user);
+ $user_rss_list[$map_count] .= url($user_rss);
+ $all_list[$map_count] .= url($all);
+ $all_rss_list[$map_count] .= url($all_rss);
+ $replies_list[$map_count] .= url($replies);
+ $replies_rss_list[$map_count] .= url($replies_rss);
+ $foaf_list[$map_count] .= url($foaf);
+
+ $user_count++;
+ }
+
+ # Make full sitemaps from the lists and save them.
+ # Possible factoring: put all the lists into a master array, thus allowing
+ # calling with single argument (i.e., array_to_map('user')).
+ array_to_map($user_list, 'user');
+ array_to_map($user_rss_list, 'user_rss');
+ array_to_map($all_list, 'all');
+ array_to_map($all_rss_list, 'all_rss');
+ array_to_map($replies_list, 'replies');
+ array_to_map($replies_rss_list, 'replies_rss');
+ array_to_map($foaf_list, 'foaf');
}
# ------------------------------------------------------------------------------
@@ -208,88 +212,93 @@ function user_map() {
# ------------------------------------------------------------------------------
# Generate a <url></url> element.
-function url($url_args) {
- $url = preg_replace('/&/', '&amp;', $url_args['url']); # escape ampersands for XML
- $lastmod = $url_args['lastmod'];
- $changefreq = $url_args['changefreq'];
- $priority = $url_args['priority'];
+function url($url_args)
+{
+ $url = preg_replace('/&/', '&amp;', $url_args['url']); # escape ampersands for XML
+ $lastmod = $url_args['lastmod'];
+ $changefreq = $url_args['changefreq'];
+ $priority = $url_args['priority'];
- if (is_null($url)) {
- error("url() arguments require 'url' value.");
- }
+ if (is_null($url)) {
+ error("url() arguments require 'url' value.");
+ }
- $url_out = "\t<url>\n";
- $url_out .= "\t\t<loc>$url</loc>\n";
+ $url_out = "\t<url>\n";
+ $url_out .= "\t\t<loc>$url</loc>\n";
- if ($changefreq) {
- $url_out .= "\t\t<changefreq>$changefreq</changefreq>\n";
- }
+ if ($changefreq) {
+ $url_out .= "\t\t<changefreq>$changefreq</changefreq>\n";
+ }
- if ($lastmod) {
- $url_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
- }
+ if ($lastmod) {
+ $url_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
+ }
- if ($priority) {
- $url_out .= "\t\t<priority>$priority</priority>\n";
- }
+ if ($priority) {
+ $url_out .= "\t\t<priority>$priority</priority>\n";
+ }
- $url_out .= "\t</url>\n";
+ $url_out .= "\t</url>\n";
- return $url_out;
+ return $url_out;
}
-function sitemap($sitemap_args) {
- $url = preg_replace('/&/', '&amp;', $sitemap_args['url']); # escape ampersands for XML
- $lastmod = $sitemap_args['lastmod'];
+function sitemap($sitemap_args)
+{
+ $url = preg_replace('/&/', '&amp;', $sitemap_args['url']); # escape ampersands for XML
+ $lastmod = $sitemap_args['lastmod'];
- if (is_null($url)) {
- error("url() arguments require 'url' value.");
- }
+ if (is_null($url)) {
+ error("url() arguments require 'url' value.");
+ }
- $sitemap_out = "\t<sitemap>\n";
- $sitemap_out .= "\t\t<loc>$url</loc>\n";
+ $sitemap_out = "\t<sitemap>\n";
+ $sitemap_out .= "\t\t<loc>$url</loc>\n";
- if ($lastmod) {
- $sitemap_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
- }
+ if ($lastmod) {
+ $sitemap_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
+ }
- $sitemap_out .= "\t</sitemap>\n";
+ $sitemap_out .= "\t</sitemap>\n";
- return $sitemap_out;
+ return $sitemap_out;
}
# Generate a <urlset></urlset> element.
-function urlset($urlset_text) {
- $urlset = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
- '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
- $urlset_text .
- '</urlset>';
-
- return $urlset;
+function urlset($urlset_text)
+{
+ $urlset = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
+ '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
+ $urlset_text .
+ '</urlset>';
+
+ return $urlset;
}
# Generate a <urlset></urlset> element.
-function sitemapindex($sitemapindex_text) {
- $sitemapindex = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
- '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
- $sitemapindex_text .
- '</sitemapindex>';
-
- return $sitemapindex;
+function sitemapindex($sitemapindex_text)
+{
+ $sitemapindex = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
+ '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
+ $sitemapindex_text .
+ '</sitemapindex>';
+
+ return $sitemapindex;
}
# Generate a sitemap from an array containing <url></url> elements and write it to a file.
-function array_to_map($url_list, $filename_prefix) {
- global $output_paths;
-
- if ($url_list) {
- # $map_urls is a long string containing concatenated <url></url> elements.
- while (list($map_idx, $map_urls) = each($url_list)) {
- $urlset_path = $output_paths['output_dir'] . "$filename_prefix-$map_idx.xml";
-
- write_file($urlset_path, urlset($map_urls));
- }
- }
+function array_to_map($url_list, $filename_prefix)
+{
+ global $output_paths;
+
+ if ($url_list) {
+ # $map_urls is a long string containing concatenated <url></url> elements.
+ while (list($map_idx, $map_urls) = each($url_list)) {
+ $urlset_path = $output_paths['output_dir'] . "$filename_prefix-$map_idx.xml";
+
+ write_file($urlset_path, urlset($map_urls));
+ }
+ }
}
# ------------------------------------------------------------------------------
@@ -297,80 +306,84 @@ function array_to_map($url_list, $filename_prefix) {
# ------------------------------------------------------------------------------
# Parse command line arguments.
-function parse_args() {
- $args = getopt('f:d:u:');
-
- if (is_null($args[f]) && is_null($args[d]) && is_null($args[u])) {
- error('Mandatory arguments: -f <index file path> -d <output directory path> -u <URL of sitemaps directory>');
- }
-
- if (is_null($args[f])) {
- error('You must specify an index file name with the -f option.');
- }
-
- if (is_null($args[d])) {
- error('You must specify a directory for the output file with the -d option.');
- }
-
- if (is_null($args[u])) {
- error('You must specify a URL for the directory where the sitemaps will be kept with the -u option.');
- }
-
- $index_file = $args[f];
- $output_dir = $args[d];
- $output_url = $args[u];
-
- if (file_exists($output_dir)) {
- if (is_writable($output_dir) === FALSE) {
- error("$output_dir is not writable.");
- }
- } else {
- error("output directory $output_dir does not exist.");
- }
-
- $paths = array(
- 'index_file' => $index_file,
- 'output_dir' => trailing_slash($output_dir),
- 'output_url' => trailing_slash($output_url),
- );
-
- return $paths;
+function parse_args()
+{
+ $args = getopt('f:d:u:');
+
+ if (is_null($args[f]) && is_null($args[d]) && is_null($args[u])) {
+ error('Mandatory arguments: -f <index file path> -d <output directory path> -u <URL of sitemaps directory>');
+ }
+
+ if (is_null($args[f])) {
+ error('You must specify an index file name with the -f option.');
+ }
+
+ if (is_null($args[d])) {
+ error('You must specify a directory for the output file with the -d option.');
+ }
+
+ if (is_null($args[u])) {
+ error('You must specify a URL for the directory where the sitemaps will be kept with the -u option.');
+ }
+
+ $index_file = $args[f];
+ $output_dir = $args[d];
+ $output_url = $args[u];
+
+ if (file_exists($output_dir)) {
+ if (is_writable($output_dir) === false) {
+ error("$output_dir is not writable.");
+ }
+ } else {
+ error("output directory $output_dir does not exist.");
+ }
+
+ $paths = array(
+ 'index_file' => $index_file,
+ 'output_dir' => trailing_slash($output_dir),
+ 'output_url' => trailing_slash($output_url),
+ );
+
+ return $paths;
}
# Ensure paths end with a "/".
-function trailing_slash($path) {
- if (preg_match('/\/$/', $path) == 0) {
- $path .= '/';
- }
+function trailing_slash($path)
+{
+ if (preg_match('/\/$/', $path) == 0) {
+ $path .= '/';
+ }
- return $path;
+ return $path;
}
# Write data to disk.
-function write_file($path, $data) {
- if (is_null($path)) {
- error('No path specified for writing to.');
- } elseif (is_null($data)) {
- error('No data specified for writing.');
- }
-
- if (($fh_out = fopen($path,'w')) === FALSE) {
- error("couldn't open $path for writing.");
- }
-
- if (fwrite($fh_out, $data) === FALSE) {
- error("couldn't write to $path.");
- }
+function write_file($path, $data)
+{
+ if (is_null($path)) {
+ error('No path specified for writing to.');
+ } elseif (is_null($data)) {
+ error('No data specified for writing.');
+ }
+
+ if (($fh_out = fopen($path,'w')) === false) {
+ error("couldn't open $path for writing.");
+ }
+
+ if (fwrite($fh_out, $data) === false) {
+ error("couldn't write to $path.");
+ }
}
# Display an error message and exit.
-function error ($error_msg) {
- if (is_null($error_msg)) {
- $error_msg = 'error() was called without any explanation!';
- }
-
- echo "Error: $error_msg\n";
- exit(1);
+function error ($error_msg)
+{
+ if (is_null($error_msg)) {
+ $error_msg = 'error() was called without any explanation!';
+ }
+
+ echo "Error: $error_msg\n";
+ exit(1);
}
?> \ No newline at end of file
diff --git a/scripts/smsqueuehandler.php b/scripts/smsqueuehandler.php
index 8f0d02d9b..38f2f11fe 100755
--- a/scripts/smsqueuehandler.php
+++ b/scripts/smsqueuehandler.php
@@ -20,8 +20,8 @@
# Abort if called from a web server
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
- print "This script must be run from the command line\n";
- exit();
+ print "This script must be run from the command line\n";
+ exit();
}
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
@@ -33,23 +33,28 @@ require_once(INSTALLDIR . '/lib/queuehandler.php');
set_error_handler('common_error_handler');
-class SmsQueueHandler extends QueueHandler {
-
- function transport() {
- return 'sms';
- }
+class SmsQueueHandler extends QueueHandler
+{
+
+ function transport()
+ {
+ return 'sms';
+ }
- function start() {
- $this->log(LOG_INFO, "INITIALIZE");
- return true;
- }
+ function start()
+ {
+ $this->log(LOG_INFO, "INITIALIZE");
+ return true;
+ }
- function handle_notice($notice) {
- return mail_broadcast_notice_sms($notice);
- }
-
- function finish() {
- }
+ function handle_notice($notice)
+ {
+ return mail_broadcast_notice_sms($notice);
+ }
+
+ function finish()
+ {
+ }
}
ini_set("max_execution_time", "0");
@@ -57,7 +62,7 @@ ini_set("max_input_time", "0");
set_time_limit(0);
mb_internal_encoding('UTF-8');
-$id = ($argc > 1) ? $argv[1] : NULL;
+$id = ($argc > 1) ? $argv[1] : null;
$handler = new SmsQueueHandler($id);
diff --git a/scripts/synctwitterfriends.php b/scripts/synctwitterfriends.php
index 070eb9bbb..0ce34c2ae 100755
--- a/scripts/synctwitterfriends.php
+++ b/scripts/synctwitterfriends.php
@@ -11,17 +11,17 @@
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
# Abort if called from a web server
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
- print "This script must be run from the command line\n";
- exit();
+ print "This script must be run from the command line\n";
+ exit();
}
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
@@ -35,21 +35,21 @@ $flink->find();
while ($flink->fetch()) {
- if (($flink->friendsync & FOREIGN_FRIEND_RECV) == FOREIGN_FRIEND_RECV) {
+ if (($flink->friendsync & FOREIGN_FRIEND_RECV) == FOREIGN_FRIEND_RECV) {
- $user = User::staticGet($flink->user_id);
+ $user = User::staticGet($flink->user_id);
- print "Updating Twitter friends for user $user->nickname ($user->id)\n";
+ print "Updating Twitter friends for user $user->nickname ($user->id)\n";
- $fuser = $flink->getForeignUser();
+ $fuser = $flink->getForeignUser();
- $result = save_twitter_friends($user, $fuser->id, $fuser->nickname, $flink->credentials);
+ $result = save_twitter_friends($user, $fuser->id, $fuser->nickname, $flink->credentials);
- if ($result == false) {
- print "Problems updating Twitter friends! Check the log.\n";
- exit(1);
- }
- }
+ if ($result == false) {
+ print "Problems updating Twitter friends! Check the log.\n";
+ exit(1);
+ }
+ }
}
diff --git a/scripts/update_facebook.php b/scripts/update_facebook.php
new file mode 100755
index 000000000..d2c1c3ffb
--- /dev/null
+++ b/scripts/update_facebook.php
@@ -0,0 +1,139 @@
+#!/usr/bin/env php
+<?php
+/*
+ * Laconica - a distributed open-source microblogging tool
+ * Copyright (C) 2008, Controlez-Vous, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+# Abort if called from a web server
+if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
+ print "This script must be run from the command line\n";
+ exit();
+}
+
+define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
+define('LACONICA', true);
+
+require_once INSTALLDIR . '/lib/common.php';
+require_once INSTALLDIR . '/lib/facebookutil.php';
+
+// For storing the last run date-time
+$last_updated_file = INSTALLDIR . '/scripts/facebook_last_updated';
+
+// Lock file name
+$tmp_file = INSTALLDIR . '/scripts/update_facebook.lock';
+
+// Make sure only one copy of the script is running at a time
+if (!($tmp_file = @fopen($tmp_file, "w")))
+{
+ die("Can't open lock file. Script already running?");
+}
+
+$facebook = getFacebook();
+
+$current_time = time();
+
+$notice = getFacebookNotices(getLastUpdated());
+
+print date('r', $current_time) . " Looking for notices to send to Facebook...\n";
+
+$cnt = 0;
+
+while($notice->fetch()) {
+
+ $flink = Foreign_link::getByUserID($notice->profile_id, FACEBOOK_SERVICE);
+ $user = $flink->getUser();
+ $fbuid = $flink->foreign_id;
+
+ if (!userCanUpdate($fbuid)) {
+ continue;
+ }
+
+ $prefix = $facebook->api_client->data_getUserPreference(FACEBOOK_NOTICE_PREFIX, $fbuid);
+ $content = "$prefix $notice->content";
+
+ if (($flink->noticesync & FOREIGN_NOTICE_SEND) == FOREIGN_NOTICE_SEND) {
+
+ // If it's not a reply, or if the user WANTS to send replies...
+ if (!preg_match('/@[a-zA-Z0-9_]{1,15}\b/u', $content) ||
+ (($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) == FOREIGN_NOTICE_SEND_REPLY)) {
+
+ // Avoid a Loop
+ if ($notice->source != 'Facebook') {
+ updateStatus($fbuid, $content);
+ updateProfileBox($facebook, $flink, $notice);
+ $cnt++;
+ }
+ }
+ }
+}
+
+updateLastUpdated($current_time);
+
+print "Sent $cnt notices to Facebook.\n";
+
+exit(0);
+
+
+function userCanUpdate($fbuid) {
+
+ global $facebook;
+
+ $result = false;
+
+ try {
+ $result = $facebook->api_client->users_hasAppPermission('status_update', $fbuid);
+ } catch(FacebookRestClientException $e){
+ print_r($e);
+ }
+
+ return $result;
+}
+
+
+function updateStatus($fbuid, $content) {
+ global $facebook;
+
+ try {
+ $result = $facebook->api_client->users_setStatus($content, $fbuid, false, true);
+ } catch(FacebookRestClientException $e){
+ print_r($e);
+ }
+}
+
+function getLastUpdated(){
+ global $last_updated_file, $current_time;
+
+ $file = fopen($last_updated_file, 'r');
+
+ if ($file) {
+ $last = fgets($file);
+ } else {
+ print "Unable to read $last_updated_file. Using current time.\n";
+ return $current_time;
+ }
+
+ fclose($file);
+
+ return $last;
+}
+
+function updateLastUpdated($time){
+ global $last_updated_file;
+ $file = fopen($last_updated_file, 'w') or die("Can't open $last_updated_file for writing!");
+ fwrite($file, $time);
+ fclose($file);
+}
diff --git a/scripts/xmppconfirmhandler.php b/scripts/xmppconfirmhandler.php
index 8961b0b6e..2b8b085ce 100755
--- a/scripts/xmppconfirmhandler.php
+++ b/scripts/xmppconfirmhandler.php
@@ -20,8 +20,8 @@
# Abort if called from a web server
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
- print "This script must be run from the command line\n";
- exit();
+ print "This script must be run from the command line\n";
+ exit();
}
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
@@ -35,104 +35,109 @@ set_error_handler('common_error_handler');
define('CLAIM_TIMEOUT', 1200);
-class XmppConfirmHandler extends XmppQueueHandler {
+class XmppConfirmHandler extends XmppQueueHandler
+{
- var $_id = 'confirm';
-
- function class_name() {
- return 'XmppConfirmHandler';
- }
-
- function run() {
- if (!$this->start()) {
- return false;
- }
- $this->log(LOG_INFO, 'checking for queued confirmations');
- do {
- $confirm = $this->next_confirm();
- if ($confirm) {
- $this->log(LOG_INFO, 'Sending confirmation for ' . $confirm->address);
- $user = User::staticGet($confirm->user_id);
- if (!$user) {
- $this->log(LOG_WARNING, 'Confirmation for unknown user ' . $confirm->user_id);
- continue;
- }
- $success = jabber_confirm_address($confirm->code,
- $user->nickname,
- $confirm->address);
- if (!$success) {
- $this->log(LOG_ERR, 'Confirmation failed for ' . $confirm->address);
- # Just let the claim age out; hopefully things work then
- continue;
- } else {
- $this->log(LOG_INFO, 'Confirmation sent for ' . $confirm->address);
- # Mark confirmation sent; need a dupe so we don't have the WHERE clause
- $dupe = Confirm_address::staticGet('code', $confirm->code);
- if (!$dupe) {
- common_log(LOG_WARNING, 'Could not refetch confirm', __FILE__);
- continue;
- }
- $orig = clone($dupe);
- $dupe->sent = $dupe->claimed;
- $result = $dupe->update($orig);
- if (!$result) {
- common_log_db_error($dupe, 'UPDATE', __FILE__);
- # Just let the claim age out; hopefully things work then
- continue;
- }
- $dupe->free();
- unset($dupe);
- }
- $user->free();
- unset($user);
- $confirm->free();
- unset($confirm);
- $this->idle(0);
- } else {
-# $this->clear_old_confirm_claims();
- $this->idle(10);
- }
- } while (true);
- if (!$this->finish()) {
- return false;
- }
- return true;
- }
+ var $_id = 'confirm';
+
+ function class_name()
+ {
+ return 'XmppConfirmHandler';
+ }
+
+ function run()
+ {
+ if (!$this->start()) {
+ return false;
+ }
+ $this->log(LOG_INFO, 'checking for queued confirmations');
+ do {
+ $confirm = $this->next_confirm();
+ if ($confirm) {
+ $this->log(LOG_INFO, 'Sending confirmation for ' . $confirm->address);
+ $user = User::staticGet($confirm->user_id);
+ if (!$user) {
+ $this->log(LOG_WARNING, 'Confirmation for unknown user ' . $confirm->user_id);
+ continue;
+ }
+ $success = jabber_confirm_address($confirm->code,
+ $user->nickname,
+ $confirm->address);
+ if (!$success) {
+ $this->log(LOG_ERR, 'Confirmation failed for ' . $confirm->address);
+ # Just let the claim age out; hopefully things work then
+ continue;
+ } else {
+ $this->log(LOG_INFO, 'Confirmation sent for ' . $confirm->address);
+ # Mark confirmation sent; need a dupe so we don't have the WHERE clause
+ $dupe = Confirm_address::staticGet('code', $confirm->code);
+ if (!$dupe) {
+ common_log(LOG_WARNING, 'Could not refetch confirm', __FILE__);
+ continue;
+ }
+ $orig = clone($dupe);
+ $dupe->sent = $dupe->claimed;
+ $result = $dupe->update($orig);
+ if (!$result) {
+ common_log_db_error($dupe, 'UPDATE', __FILE__);
+ # Just let the claim age out; hopefully things work then
+ continue;
+ }
+ $dupe->free();
+ unset($dupe);
+ }
+ $user->free();
+ unset($user);
+ $confirm->free();
+ unset($confirm);
+ $this->idle(0);
+ } else {
+# $this->clear_old_confirm_claims();
+ $this->idle(10);
+ }
+ } while (true);
+ if (!$this->finish()) {
+ return false;
+ }
+ return true;
+ }
- function next_confirm() {
- $confirm = new Confirm_address();
- $confirm->whereAdd('claimed IS NULL');
- $confirm->whereAdd('sent IS NULL');
- # XXX: eventually we could do other confirmations in the queue, too
- $confirm->address_type = 'jabber';
- $confirm->orderBy('modified DESC');
- $confirm->limit(1);
- if ($confirm->find(TRUE)) {
- $this->log(LOG_INFO, 'Claiming confirmation for ' . $confirm->address);
- # working around some weird DB_DataObject behaviour
- $confirm->whereAdd(''); # clears where stuff
- $original = clone($confirm);
- $confirm->claimed = common_sql_now();
- $result = $confirm->update($original);
- if ($result) {
- $this->log(LOG_INFO, 'Succeeded in claim! '. $result);
- return $confirm;
- } else {
- $this->log(LOG_INFO, 'Failed in claim!');
- return false;
- }
- }
- return NULL;
- }
+ function next_confirm()
+ {
+ $confirm = new Confirm_address();
+ $confirm->whereAdd('claimed IS null');
+ $confirm->whereAdd('sent IS null');
+ # XXX: eventually we could do other confirmations in the queue, too
+ $confirm->address_type = 'jabber';
+ $confirm->orderBy('modified DESC');
+ $confirm->limit(1);
+ if ($confirm->find(true)) {
+ $this->log(LOG_INFO, 'Claiming confirmation for ' . $confirm->address);
+ # working around some weird DB_DataObject behaviour
+ $confirm->whereAdd(''); # clears where stuff
+ $original = clone($confirm);
+ $confirm->claimed = common_sql_now();
+ $result = $confirm->update($original);
+ if ($result) {
+ $this->log(LOG_INFO, 'Succeeded in claim! '. $result);
+ return $confirm;
+ } else {
+ $this->log(LOG_INFO, 'Failed in claim!');
+ return false;
+ }
+ }
+ return null;
+ }
- function clear_old_confirm_claims() {
- $confirm = new Confirm();
- $confirm->claimed = NULL;
- $confirm->whereAdd('now() - claimed > '.CLAIM_TIMEOUT);
- $confirm->update(DB_DATAOBJECT_WHEREADD_ONLY);
- $confirm->free();
- unset($confirm);
- }
+ function clear_old_confirm_claims()
+ {
+ $confirm = new Confirm();
+ $confirm->claimed = null;
+ $confirm->whereAdd('now() - claimed > '.CLAIM_TIMEOUT);
+ $confirm->update(DB_DATAOBJECT_WHEREADD_ONLY);
+ $confirm->free();
+ unset($confirm);
+ }
}
ini_set("max_execution_time", "0");
diff --git a/scripts/xmppdaemon.php b/scripts/xmppdaemon.php
index 9a60970a6..01fe8914f 100755
--- a/scripts/xmppdaemon.php
+++ b/scripts/xmppdaemon.php
@@ -20,8 +20,8 @@
# Abort if called from a web server
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
- print "This script must be run from the command line\n";
- exit();
+ print "This script must be run from the command line\n";
+ exit();
}
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
@@ -37,205 +37,221 @@ set_error_handler('common_error_handler');
# 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
-
- if ($this->is_self($from)) {
- $from = $this->get_ofrom($pl);
- if (is_null($from) || $this->is_self($from)) {
- return;
- }
- }
-
- $user = $this->get_user($from);
-
- if (!$user) {
- $this->from_site($from, 'Unknown user; go to ' .
- common_local_url('imsettings') .
- ' to add your address to your account');
- $this->log(LOG_WARNING, 'Message from unknown user ' . $from);
- return;
- }
- if ($this->handle_command($user, $pl['body'])) {
- return;
- } else if ($this->is_autoreply($pl['body'])) {
- $this->log(LOG_INFO, 'Ignoring auto reply from ' . $from);
- return;
- } else if ($this->is_otr($pl['body'])) {
- $this->log(LOG_INFO, 'Ignoring OTR from ' . $from);
- return;
- } else if ($this->is_direct($pl['body'])) {
- preg_match_all('/d[\ ]*([a-z0-9]{1,64})/', $pl['body'], $to);
-
- $to = preg_replace('/^d([\ ])*/', '', $to[0][0]);
- $body = preg_replace('/d[\ ]*('. $to .')[\ ]*/', '', $pl['body']);
- $this->add_direct($user, $body, $to, $from);
- } else {
- $len = mb_strlen($pl['body']);
- if($len > 140) {
- $this->from_site($from, 'Message too long - maximum is 140 characters, you sent ' . $len);
- return;
- }
- $this->add_notice($user, $pl);
- }
-
- $user->free();
- unset($user);
- }
-
- function is_self($from) {
- return preg_match('/^'.strtolower(jabber_daemon_address()).'/', strtolower($from));
- }
-
- function get_ofrom($pl) {
- $xml = $pl['xml'];
- $addresses = $xml->sub('addresses');
- if (!$addresses) {
- $this->log(LOG_WARNING, 'Forwarded message without addresses');
- return NULL;
- }
- $address = $addresses->sub('address');
- if (!$address) {
- $this->log(LOG_WARNING, 'Forwarded message without address');
- return NULL;
- }
- if (!array_key_exists('type', $address->attrs)) {
- $this->log(LOG_WARNING, 'No type for forwarded message');
- return NULL;
- }
- $type = $address->attrs['type'];
- if ($type != 'ofrom') {
- $this->log(LOG_WARNING, 'Type of forwarded message is not ofrom');
- return NULL;
- }
- if (!array_key_exists('jid', $address->attrs)) {
- $this->log(LOG_WARNING, 'No jid for forwarded message');
- return NULL;
- }
- $jid = $address->attrs['jid'];
- if (!$jid) {
- $this->log(LOG_WARNING, 'Could not get jid from address');
- return NULL;
- }
- $this->log(LOG_DEBUG, 'Got message forwarded from jid ' . $jid);
- return $jid;
- }
-
- function is_autoreply($txt) {
- if (preg_match('/[\[\(]?[Aa]uto[-\s]?[Rr]e(ply|sponse)[\]\)]/', $txt)) {
- return true;
- } else {
- return false;
- }
- }
-
- function is_otr($txt) {
- if (preg_match('/^\?OTR/', $txt)) {
- return true;
- } else {
- return false;
- }
- }
-
- function is_direct($txt) {
- if (strtolower(substr($txt, 0, 2))=='d ') {
- return true;
- } else {
- return false;
- }
- }
-
- function from_site($address, $msg) {
- $text = '['.common_config('site', 'name') . '] ' . $msg;
- jabber_send_message($address, $text);
- }
-
- function handle_command($user, $body) {
- $inter = new CommandInterpreter();
- $cmd = $inter->handle_command($user, $body);
- if ($cmd) {
- $chan = new XMPPChannel($this->conn);
- $cmd->execute($chan);
- return true;
- } else {
- return false;
- }
- }
-
- function add_notice(&$user, &$pl) {
+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
+
+ if ($this->is_self($from)) {
+ $from = $this->get_ofrom($pl);
+ if (is_null($from) || $this->is_self($from)) {
+ return;
+ }
+ }
+
+ $user = $this->get_user($from);
+
+ if (!$user) {
+ $this->from_site($from, 'Unknown user; go to ' .
+ common_local_url('imsettings') .
+ ' to add your address to your account');
+ $this->log(LOG_WARNING, 'Message from unknown user ' . $from);
+ return;
+ }
+ if ($this->handle_command($user, $pl['body'])) {
+ return;
+ } else if ($this->is_autoreply($pl['body'])) {
+ $this->log(LOG_INFO, 'Ignoring auto reply from ' . $from);
+ return;
+ } else if ($this->is_otr($pl['body'])) {
+ $this->log(LOG_INFO, 'Ignoring OTR from ' . $from);
+ return;
+ } else if ($this->is_direct($pl['body'])) {
+ preg_match_all('/d[\ ]*([a-z0-9]{1,64})/', $pl['body'], $to);
+
+ $to = preg_replace('/^d([\ ])*/', '', $to[0][0]);
+ $body = preg_replace('/d[\ ]*('. $to .')[\ ]*/', '', $pl['body']);
+ $this->add_direct($user, $body, $to, $from);
+ } else {
+ $len = mb_strlen($pl['body']);
+ if($len > 140) {
+ $this->from_site($from, 'Message too long - maximum is 140 characters, you sent ' . $len);
+ return;
+ }
+ $this->add_notice($user, $pl);
+ }
+
+ $user->free();
+ unset($user);
+ }
+
+ function is_self($from)
+ {
+ return preg_match('/^'.strtolower(jabber_daemon_address()).'/', strtolower($from));
+ }
+
+ function get_ofrom($pl)
+ {
+ $xml = $pl['xml'];
+ $addresses = $xml->sub('addresses');
+ if (!$addresses) {
+ $this->log(LOG_WARNING, 'Forwarded message without addresses');
+ return null;
+ }
+ $address = $addresses->sub('address');
+ if (!$address) {
+ $this->log(LOG_WARNING, 'Forwarded message without address');
+ return null;
+ }
+ if (!array_key_exists('type', $address->attrs)) {
+ $this->log(LOG_WARNING, 'No type for forwarded message');
+ return null;
+ }
+ $type = $address->attrs['type'];
+ if ($type != 'ofrom') {
+ $this->log(LOG_WARNING, 'Type of forwarded message is not ofrom');
+ return null;
+ }
+ if (!array_key_exists('jid', $address->attrs)) {
+ $this->log(LOG_WARNING, 'No jid for forwarded message');
+ return null;
+ }
+ $jid = $address->attrs['jid'];
+ if (!$jid) {
+ $this->log(LOG_WARNING, 'Could not get jid from address');
+ return null;
+ }
+ $this->log(LOG_DEBUG, 'Got message forwarded from jid ' . $jid);
+ return $jid;
+ }
+
+ function is_autoreply($txt)
+ {
+ if (preg_match('/[\[\(]?[Aa]uto[-\s]?[Rr]e(ply|sponse)[\]\)]/', $txt)) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ function is_otr($txt)
+ {
+ if (preg_match('/^\?OTR/', $txt)) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ function is_direct($txt)
+ {
+ if (strtolower(substr($txt, 0, 2))=='d ') {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ function from_site($address, $msg)
+ {
+ $text = '['.common_config('site', 'name') . '] ' . $msg;
+ jabber_send_message($address, $text);
+ }
+
+ function handle_command($user, $body)
+ {
+ $inter = new CommandInterpreter();
+ $cmd = $inter->handle_command($user, $body);
+ if ($cmd) {
+ $chan = new XMPPChannel($this->conn);
+ $cmd->execute($chan);
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ function add_notice(&$user, &$pl)
+ {
$body = trim($pl['body']);
$content_shortened = common_shorten_link($body);
if (mb_strlen($content_shortened) > 140) {
@@ -245,59 +261,62 @@ class XMPPDaemon extends Daemon {
else {
$content = $body;
}
- $notice = Notice::saveNew($user->id, $content, 'xmpp');
- if (is_string($notice)) {
- $this->log(LOG_ERR, $notice);
- return;
- }
- common_broadcast_notice($notice);
- $this->log(LOG_INFO,
- 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
- $notice->free();
- unset($notice);
- }
-
- function handle_presence(&$pl) {
- $from = jabber_normalize_jid($pl['from']);
- switch ($pl['type']) {
- case 'subscribe':
- # We let anyone subscribe
- $this->subscribed($from);
- $this->log(LOG_INFO,
- 'Accepted subscription from ' . $from);
- break;
- case 'subscribed':
- case 'unsubscribed':
- case 'unsubscribe':
- $this->log(LOG_INFO,
- 'Ignoring "' . $pl['type'] . '" from ' . $from);
- break;
- default:
- if (!$pl['type']) {
- $user = User::staticGet('jabber', $from);
- if (!$user) {
- $this->log(LOG_WARNING, 'Presence from unknown user ' . $from);
- return;
- }
- if ($user->updatefrompresence) {
- $this->log(LOG_INFO, 'Updating ' . $user->nickname .
- ' status from presence.');
- $this->add_notice($user, $pl);
- }
- $user->free();
- unset($user);
- }
- break;
- }
- }
-
- function log($level, $msg) {
- common_log($level, 'XMPPDaemon('.$this->resource.'): '.$msg);
- }
-
- function subscribed($to) {
- jabber_special_presence('subscribed', $to);
- }
+ $notice = Notice::saveNew($user->id, $content, 'xmpp');
+ if (is_string($notice)) {
+ $this->log(LOG_ERR, $notice);
+ return;
+ }
+ common_broadcast_notice($notice);
+ $this->log(LOG_INFO,
+ 'Added notice ' . $notice->id . ' from user ' . $user->nickname);
+ $notice->free();
+ unset($notice);
+ }
+
+ function handle_presence(&$pl)
+ {
+ $from = jabber_normalize_jid($pl['from']);
+ switch ($pl['type']) {
+ case 'subscribe':
+ # We let anyone subscribe
+ $this->subscribed($from);
+ $this->log(LOG_INFO,
+ 'Accepted subscription from ' . $from);
+ break;
+ case 'subscribed':
+ case 'unsubscribed':
+ case 'unsubscribe':
+ $this->log(LOG_INFO,
+ 'Ignoring "' . $pl['type'] . '" from ' . $from);
+ break;
+ default:
+ if (!$pl['type']) {
+ $user = User::staticGet('jabber', $from);
+ if (!$user) {
+ $this->log(LOG_WARNING, 'Presence from unknown user ' . $from);
+ return;
+ }
+ if ($user->updatefrompresence) {
+ $this->log(LOG_INFO, 'Updating ' . $user->nickname .
+ ' status from presence.');
+ $this->add_notice($user, $pl);
+ }
+ $user->free();
+ unset($user);
+ }
+ break;
+ }
+ }
+
+ function log($level, $msg)
+ {
+ common_log($level, 'XMPPDaemon('.$this->resource.'): '.$msg);
+ }
+
+ function subscribed($to)
+ {
+ jabber_special_presence('subscribed', $to);
+ }
}
ini_set("max_execution_time", "0");