add channels and use command interpreter in different channels
darcs-hash:20081004163213-5ed1f-684ecb464e843b1bbe456c348e56b40a39a83ecd.gz
This commit is contained in:
parent
c8fd8fa00f
commit
f072147e4e
|
@ -50,6 +50,15 @@ class NewnoticeAction extends Action {
|
|||
return;
|
||||
}
|
||||
|
||||
$inter = new CommandInterpreter();
|
||||
|
||||
$cmd = $inter->handle_command($user, $text);
|
||||
|
||||
if ($cmd) {
|
||||
$cmd->execute(new WebChannel());
|
||||
return;
|
||||
}
|
||||
|
||||
$notice = Notice::saveNew($user->id, $content, 'web');
|
||||
|
||||
if (is_string($notice)) {
|
||||
|
|
|
@ -22,14 +22,135 @@ if (!defined('LACONICA')) { exit(1); }
|
|||
class Channel {
|
||||
|
||||
function on($user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
function off($user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
function output($user) {
|
||||
function output($user, $text) {
|
||||
return false;
|
||||
}
|
||||
|
||||
function error($user) {
|
||||
function error($user, $text) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class XMPPChannel extends Channel {
|
||||
|
||||
var $conn = NULL;
|
||||
|
||||
function __construct($conn) {
|
||||
$this->conn = $conn;
|
||||
}
|
||||
|
||||
function on($user) {
|
||||
return $this->set_notify($user, 1);
|
||||
}
|
||||
|
||||
function off($user) {
|
||||
return $this->set_notify($user, 0);
|
||||
}
|
||||
|
||||
function output($user, $text) {
|
||||
$text = '['.common_config('site', 'name') . '] ' . $text;
|
||||
jabber_send_message($user->jabber, $text);
|
||||
}
|
||||
|
||||
function error($user, $text) {
|
||||
$text = '['.common_config('site', 'name') . '] ' . $text;
|
||||
jabber_send_message($user->jabber, $text);
|
||||
}
|
||||
|
||||
function set_notify(&$user, $notify) {
|
||||
$orig = clone($user);
|
||||
$user->jabbernotify = $notify;
|
||||
$result = $user->update($orig);
|
||||
if (!$result) {
|
||||
$last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
|
||||
common_log(LOG_ERR,
|
||||
'Could not set notify flag to ' . $notify .
|
||||
' for user ' . common_log_objstring($user) .
|
||||
': ' . $last_error->message);
|
||||
return false;
|
||||
} else {
|
||||
common_log(LOG_INFO,
|
||||
'User ' . $user->nickname . ' set notify flag to ' . $notify);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class WebChannel extends Channel {
|
||||
|
||||
function on($user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
function off($user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
function output($user, $text) {
|
||||
# XXX: buffer all output and send it at the end
|
||||
# XXX: even better, redirect to appropriate page
|
||||
# depending on what command was run
|
||||
common_show_header(_('Command results'));
|
||||
common_element('p', NULL, $text);
|
||||
common_show_footer();
|
||||
}
|
||||
|
||||
function error($user, $text) {
|
||||
common_client_error($text);
|
||||
}
|
||||
}
|
||||
|
||||
class MailChannel extends Channel {
|
||||
|
||||
var $addr = NULL;
|
||||
|
||||
function __construct($addr=NULL) {
|
||||
$this->addr = $addr;
|
||||
}
|
||||
|
||||
function on($user) {
|
||||
return $this->set_notify($user, 1);
|
||||
}
|
||||
|
||||
function off($user) {
|
||||
return $this->set_notify($user, 0);
|
||||
}
|
||||
|
||||
function output($user, $text) {
|
||||
|
||||
$headers['From'] = $user->incomingemail;
|
||||
$headers['To'] = $this->addr;
|
||||
|
||||
$headers['Subject'] = _('Command complete');
|
||||
|
||||
return mail_send(array($this->addr), $headers, $text);
|
||||
}
|
||||
|
||||
function error($user, $text) {
|
||||
|
||||
$headers['From'] = $user->incomingemail;
|
||||
$headers['To'] = $this->addr;
|
||||
|
||||
$headers['Subject'] = _('Command failed');
|
||||
|
||||
return mail_send(array($this->addr), $headers, $text);
|
||||
}
|
||||
|
||||
function set_notify($user, $value) {
|
||||
$orig = clone($user);
|
||||
$user->smsnotify = $value;
|
||||
$result = $user->update($orig);
|
||||
if (!$result) {
|
||||
common_log_db_error($user, 'UPDATE', __FILE__);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -299,7 +299,11 @@ class OffCommand extends Command {
|
|||
if ($other) {
|
||||
$channel->error($this->user, _("Command not yet implemented."));
|
||||
} else {
|
||||
$channel->off($this->user);
|
||||
if ($channel->off($this->user)) {
|
||||
$channel->output(_('Notification off.'));
|
||||
} else {
|
||||
$channel->error(_('Can\'t turn off notification.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -315,7 +319,11 @@ class OnCommand extends Command {
|
|||
if ($other) {
|
||||
$channel->error($this->user, _("Command not yet implemented."));
|
||||
} else {
|
||||
$channel->on($this->user);
|
||||
if ($channel->on($this->user)) {
|
||||
$channel->output(_('Notification on.'));
|
||||
} else {
|
||||
$channel->error(_('Can\'t turn on notification.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,9 +58,8 @@ class MailerDaemon {
|
|||
$this->error($from, _('Sorry, no incoming email allowed.'));
|
||||
return false;
|
||||
}
|
||||
$response = $this->handle_command($user, $msg);
|
||||
$response = $this->handle_command($user, $from, $msg);
|
||||
if ($response) {
|
||||
$this->respond($from, $to, $response);
|
||||
return true;
|
||||
}
|
||||
$msg = $this->cleanup_msg($msg);
|
||||
|
@ -97,31 +96,16 @@ class MailerDaemon {
|
|||
return false;
|
||||
}
|
||||
|
||||
function handle_command($user, $msg) {
|
||||
$cmd = trim(strtolower($msg));
|
||||
switch ($cmd) {
|
||||
case 'off':
|
||||
$this->set_notify($user, 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;
|
||||
case 'on':
|
||||
$this->set_notify($user, true);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function set_notify($user, $value) {
|
||||
$orig = clone($user);
|
||||
$user->smsnotify = $value;
|
||||
$result = $user->update($orig);
|
||||
if (!$result) {
|
||||
common_log_db_error($user, 'UPDATE', __FILE__);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function respond($from, $to, $response) {
|
||||
|
||||
$headers['From'] = $to;
|
||||
|
|
|
@ -224,76 +224,14 @@ class XMPPDaemon extends Daemon {
|
|||
}
|
||||
|
||||
function handle_command($user, $body) {
|
||||
# XXX: localise
|
||||
$p=explode(' ',$body);
|
||||
if(count($p)>2)
|
||||
return false;
|
||||
switch($p[0]) {
|
||||
case 'help':
|
||||
if(count($p)!=1)
|
||||
return false;
|
||||
$this->from_site($user->jabber, "Commands:\n on - turn on notifications\n off - turn off notifications\n help - show this help \n sub - subscribe to user\n unsub - unsubscribe from user\n d - direct message to user\n");
|
||||
return true;
|
||||
case 'on':
|
||||
if(count($p)!=1)
|
||||
return false;
|
||||
$this->set_notify($user, true);
|
||||
$this->from_site($user->jabber, 'notifications on');
|
||||
$inter = new CommandInterpreter();
|
||||
$cmd = $inter->handle_command($user, $body);
|
||||
if ($cmd) {
|
||||
$chan = new XMPPChannel($this->conn);
|
||||
$cmd->execute($chan);
|
||||
return true;
|
||||
case 'off':
|
||||
if(count($p)!=1)
|
||||
return false;
|
||||
$this->set_notify($user, false);
|
||||
$this->from_site($user->jabber, 'notifications off');
|
||||
return true;
|
||||
case 'sub':
|
||||
if(count($p)==1) {
|
||||
$this->from_site($user->jabber, 'Specify the name of the user to subscribe to');
|
||||
return true;
|
||||
}
|
||||
$result=subs_subscribe_user($user, $p[1]);
|
||||
if($result=='true')
|
||||
$this->from_site($user->jabber, 'Subscribed to ' . $p[1]);
|
||||
else
|
||||
$this->from_site($user->jabber, $result);
|
||||
return true;
|
||||
case 'unsub':
|
||||
if(count($p)==1) {
|
||||
$this->from_site($user->jabber, 'Specify the name of the user to unsubscribe from');
|
||||
return true;
|
||||
}
|
||||
$result=subs_unsubscribe_user($user, $p[1]);
|
||||
if($result=='true')
|
||||
$this->from_site($user->jabber, 'Unsubscribed from ' . $p[1]);
|
||||
else
|
||||
$this->from_site($user->jabber, $result);
|
||||
return true;
|
||||
case 'last':
|
||||
if(count($p)==1) {
|
||||
# I think this might be AWFUL english
|
||||
$this->from_site($user->jabber, 'Specify the name of the user to get the last notice of');
|
||||
return true;
|
||||
}
|
||||
$this->get_last($user, $p[1], $user->jabber);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function set_notify(&$user, $notify) {
|
||||
$orig = clone($user);
|
||||
$user->jabbernotify = $notify;
|
||||
$result = $user->update($orig);
|
||||
if (!$result) {
|
||||
$last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
|
||||
$this->log(LOG_ERR,
|
||||
'Could not set notify flag to ' . $notify .
|
||||
' for user ' . common_log_objstring($user) .
|
||||
': ' . $last_error->message);
|
||||
} else {
|
||||
$this->log(LOG_INFO,
|
||||
'User ' . $user->nickname . ' set notify flag to ' . $notify);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -310,55 +248,6 @@ class XMPPDaemon extends Daemon {
|
|||
unset($notice);
|
||||
}
|
||||
|
||||
function get_last(&$user, $target_nickname, $from) {
|
||||
$target = User::staticGet('nickname', $target_nickname);
|
||||
if (!$target) {
|
||||
$this->from_site($from,_('No such user.'));
|
||||
return;
|
||||
}
|
||||
|
||||
$notice = $target->getCurrentNotice();
|
||||
if (!$notice) {
|
||||
$this->from_site($from, "User has no last notice");
|
||||
return;
|
||||
}
|
||||
|
||||
$notice_content = $notice->content;
|
||||
$this->from_site($from, $target_nickname . ": " . $notice_content);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
function add_direct(&$user, $body, $to, $from) {
|
||||
|
||||
$other = User::staticGet('nickname', $to);
|
||||
|
||||
$this->log(LOG_INFO, 'Direct message to' . $to);
|
||||
$len = mb_strlen($body);
|
||||
if ($len == 0) {
|
||||
$this->from_site($from, _('No content!'));
|
||||
return;
|
||||
} else if ($len > 140) {
|
||||
$this->from_site($from, 'Message too long - maximum is 140 characters, you sent ' . $len);
|
||||
return;
|
||||
} else if (!$other) {
|
||||
$this->from_site($from,_('No such user.'));
|
||||
return;
|
||||
} else if (!$user->mutuallySubscribed($other)) {
|
||||
$this->from_site($from, _('You can\'t send a message to this user.'));
|
||||
return;
|
||||
} else if ($user->id == $other->id) {
|
||||
$this->from_site($from, _('Don\'t send a message to yourself; just say it to yourself quietly instead.'));
|
||||
return;
|
||||
}
|
||||
$this->from_site($from, "Direct message to " . $to . " sent");
|
||||
$message = Message::saveNew($user->id, $other->id, $body, 'xmpp');
|
||||
|
||||
# XXX : Need to notify the other person
|
||||
}
|
||||
|
||||
function handle_presence(&$pl) {
|
||||
$from = jabber_normalize_jid($pl['from']);
|
||||
switch ($pl['type']) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user