]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
add channels and use command interpreter in different channels
authorEvan Prodromou <evan@controlyourself.ca>
Sat, 4 Oct 2008 16:32:13 +0000 (12:32 -0400)
committerEvan Prodromou <evan@controlyourself.ca>
Sat, 4 Oct 2008 16:32:13 +0000 (12:32 -0400)
darcs-hash:20081004163213-5ed1f-684ecb464e843b1bbe456c348e56b40a39a83ecd.gz

actions/newnotice.php
classes/Channel.php
classes/Command.php
scripts/maildaemon.php
scripts/xmppdaemon.php

index cc40f89bc137a519843858e7fdc20f659447d0b0..e8fa832a6888fd9b0170a6e25bd1989e70e8998a 100644 (file)
@@ -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)) {
index f2e7ad34f2b08890a7c41943b09e7ee72206705b..ee0e57c89a62d5fa2d4cdf801abfa329d38b33b9 100644 (file)
@@ -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;
        }
-}
\ No newline at end of file
+}
+
+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;
+       }
+}
index 1a6d7cc0661600716fc0267555df945540a68e58..3e2afec6cdb4d2e28df28fb42a8ef0e3794ef836 100644 (file)
@@ -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.'));
+                       }
                }
        }
 }
index 3d3b3095140444294be1ba67efbee74c637f1865..db283c4a077da41a0da582c69e8d240432fe3f99 100755 (executable)
@@ -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;
index ce8a6e8967fd30514a65ca9dbff1ab40ef93f37e..14c07f000a789d585215c21af514d7c8c42aa703 100755 (executable)
@@ -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');
-                       return true;
-                case 'off':
-                       if(count($p)!=1)
-                               return false;
-                       $this->set_notify($user, false);
-                       $this->from_site($user->jabber, 'notifications off');
+               $inter = new CommandInterpreter();
+               $cmd = $inter->handle_command($user, $body);
+               if ($cmd) {
+                       $chan = new XMPPChannel($this->conn);
+                       $cmd->execute($chan);
                        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']) {