123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- require_once(INSTALLDIR.'/lib/channel.php');
- class Command
- {
- protected $scoped = null;
- var $user = null;
- function __construct($user=null)
- {
- $this->scoped = empty($user)?null:$user->getProfile();
- $this->user = $user;
- }
-
- public function execute($channel)
- {
- try {
- $this->handle($channel);
- } catch (CommandException $e) {
- $channel->error($this->user, $e->getMessage());
- } catch (Exception $e) {
- common_log(LOG_ERR, "Error handling " . get_class($this) . ": " . $e->getMessage());
- $channel->error($this->user, $e->getMessage());
- }
- }
-
- function handle($channel)
- {
- return false;
- }
-
- function getNotice($arg)
- {
- $notice = null;
- if (Event::handle('StartCommandGetNotice', array($this, $arg, &$notice))) {
- if(substr($this->other,0,1)=='#'){
-
- $notice = Notice::getKV(substr($arg,1));
- if (!$notice) {
-
- throw new CommandException(_('Notice with that id does not exist.'));
- }
- }
- if (Validate::uri($this->other)) {
-
- $notice = Notice::getKV('uri', $arg);
- }
- if (!$notice) {
-
-
- $recipient = $this->getProfile($arg);
- $notice = $recipient->getCurrentNotice();
- if (!$notice) {
-
- throw new CommandException(_('User has no last notice.'));
- }
- }
- }
- Event::handle('EndCommandGetNotice', array($this, $arg, &$notice));
- if (!$notice) {
-
- throw new CommandException(_('Notice with that id does not exist.'));
- }
- return $notice;
- }
-
- function getProfile($arg)
- {
- $profile = null;
- if (Event::handle('StartCommandGetProfile', array($this, $arg, &$profile))) {
- $profile =
- common_relative_profile($this->user, common_canonical_nickname($arg));
- }
- Event::handle('EndCommandGetProfile', array($this, $arg, &$profile));
- if (!$profile) {
-
-
- throw new CommandException(sprintf(_('Could not find a user with nickname %s.'), $arg));
- }
- return $profile;
- }
-
- function getUser($arg)
- {
- $user = null;
- if (Event::handle('StartCommandGetUser', array($this, $arg, &$user))) {
- $user = User::getKV('nickname', Nickname::normalize($arg));
- }
- Event::handle('EndCommandGetUser', array($this, $arg, &$user));
- if (!$user){
-
-
- throw new CommandException(sprintf(_('Could not find a local user with nickname %s.'),
- $arg));
- }
- return $user;
- }
-
- function getGroup($arg)
- {
- $group = null;
- if (Event::handle('StartCommandGetGroup', array($this, $arg, &$group))) {
- $group = User_group::getForNickname($arg, $this->user->getProfile());
- }
- Event::handle('EndCommandGetGroup', array($this, $arg, &$group));
- if (!$group) {
-
- throw new CommandException(_('No such group.'));
- }
- return $group;
- }
- }
- class CommandException extends Exception
- {
- }
- class UnimplementedCommand extends Command
- {
- function handle($channel)
- {
-
- $channel->error($this->user, _('Sorry, this command is not yet implemented.'));
- }
- }
- class TrackingCommand extends UnimplementedCommand
- {
- }
- class TrackOffCommand extends UnimplementedCommand
- {
- }
- class TrackCommand extends UnimplementedCommand
- {
- var $word = null;
- function __construct($user, $word)
- {
- parent::__construct($user);
- $this->word = $word;
- }
- }
- class UntrackCommand extends UnimplementedCommand
- {
- var $word = null;
- function __construct($user, $word)
- {
- parent::__construct($user);
- $this->word = $word;
- }
- }
- class NudgeCommand extends Command
- {
- var $other = null;
- function __construct($user, $other)
- {
- parent::__construct($user);
- $this->other = $other;
- }
- function handle($channel)
- {
- $recipient = $this->getUser($this->other);
- if ($recipient->id == $this->user->id) {
-
- throw new CommandException(_('It does not make a lot of sense to nudge yourself!'));
- } else {
- if ($recipient->email && $recipient->emailnotifynudge) {
- mail_notify_nudge($this->user, $recipient);
- }
-
-
-
-
- $channel->output($this->user, sprintf(_('Nudge sent to %s.'),
- $recipient->nickname));
- }
- }
- }
- class InviteCommand extends UnimplementedCommand
- {
- var $other = null;
- function __construct($user, $other)
- {
- parent::__construct($user);
- $this->other = $other;
- }
- }
- class StatsCommand extends Command
- {
- function handle($channel)
- {
- $profile = $this->user->getProfile();
- $subs_count = $profile->subscriptionCount();
- $subbed_count = $profile->subscriberCount();
- $notice_count = $profile->noticeCount();
-
-
-
-
- $channel->output($this->user, sprintf(_("Subscriptions: %1\$s\n".
- "Subscribers: %2\$s\n".
- "Notices: %3\$s"),
- $subs_count,
- $subbed_count,
- $notice_count));
- }
- }
- class JoinCommand extends Command
- {
- var $other = null;
- function __construct($user, $other)
- {
- parent::__construct($user);
- $this->other = $other;
- }
- function handle($channel)
- {
- $group = $this->getGroup($this->other);
- $cur = $this->user;
- if ($cur->isMember($group)) {
-
- $channel->error($cur, _('You are already a member of that group.'));
- return;
- }
- if (Group_block::isBlocked($group, $cur->getProfile())) {
-
- $channel->error($cur, _('You have been blocked from that group by the admin.'));
- return;
- }
- try {
- $cur->joinGroup($group);
- } catch (Exception $e) {
-
-
- $channel->error($cur, sprintf(_('Could not join user %1$s to group %2$s.'),
- $cur->nickname, $group->nickname));
- return;
- }
-
-
- $channel->output($cur, sprintf(_('%1$s joined group %2$s.'),
- $cur->nickname,
- $group->nickname));
- }
- }
- class DropCommand extends Command
- {
- var $other = null;
- function __construct($user, $other)
- {
- parent::__construct($user);
- $this->other = $other;
- }
- function handle($channel)
- {
- $group = $this->getGroup($this->other);
- $cur = $this->user;
- if (!$group) {
-
- $channel->error($cur, _('No such group.'));
- return;
- }
- if (!$cur->isMember($group)) {
-
- $channel->error($cur, _('You are not a member of that group.'));
- return;
- }
- try {
- $cur->leaveGroup($group);
- } catch (Exception $e) {
-
-
- $channel->error($cur, sprintf(_('Could not remove user %1$s from group %2$s.'),
- $cur->nickname, $group->nickname));
- return;
- }
-
-
- $channel->output($cur, sprintf(_('%1$s left group %2$s.'),
- $cur->nickname,
- $group->nickname));
- }
- }
- class TagCommand extends Command
- {
- var $other = null;
- var $tags = null;
- function __construct($user, $other, $tags)
- {
- parent::__construct($user);
- $this->other = $other;
- $this->tags = $tags;
- }
- function handle($channel)
- {
- $profile = $this->getProfile($this->other);
- $cur = $this->user->getProfile();
- if (!$profile) {
-
- $channel->error($cur, _('No such profile.'));
- return;
- }
- if (!$cur->canTag($profile)) {
-
- $channel->error($cur, _('You cannot tag this user.'));
- return;
- }
- $privs = array();
- $tags = preg_split('/[\s,]+/', $this->tags);
- $clean_tags = array();
- foreach ($tags as $tag) {
- $private = @$tag[0] === '.';
- $tag = $clean_tags[] = common_canonical_tag($tag);
- if (!common_valid_profile_tag($tag)) {
-
-
- $channel->error($cur, sprintf(_('Invalid tag: "%s".'), $tag));
- return;
- }
- $privs[$tag] = $private;
- }
- try {
- foreach ($clean_tags as $tag) {
- Profile_tag::setTag($cur->id, $profile->id, $tag, null, $privs[$tag]);
- }
- } catch (Exception $e) {
-
-
- $channel->error($cur, sprintf(_('Error tagging %1$s: %2$s'),
- $profile->nickname, $e->getMessage()));
- return;
- }
-
-
-
- $channel->output($cur, sprintf(_m('%1$s was tagged %2$s',
- '%1$s was tagged %2$s',
- count($clean_tags)),
- $profile->nickname,
-
- implode(_(', '), $clean_tags)));
- }
- }
- class UntagCommand extends TagCommand
- {
- function handle($channel)
- {
- $profile = $this->getProfile($this->other);
- $cur = $this->user->getProfile();
- if (!$profile) {
-
- $channel->error($cur, _('No such profile.'));
- return;
- }
- if (!$cur->canTag($profile)) {
-
- $channel->error($cur, _('You cannot tag this user.'));
- return;
- }
- $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $this->tags));
- foreach ($tags as $tag) {
- if (!common_valid_profile_tag($tag)) {
-
-
- $channel->error($cur, sprintf(_('Invalid tag: "%s"'), $tag));
- return;
- }
- }
- try {
- foreach ($tags as $tag) {
- Profile_tag::unTag($cur->id, $profile->id, $tag);
- }
- } catch (Exception $e) {
-
-
- $channel->error($cur, sprintf(_('Error untagging %1$s: %2$s'),
- $profile->nickname, $e->getMessage()));
- return;
- }
-
-
-
- $channel->output($cur, sprintf(_m('The following tag was removed from user %1$s: %2$s.',
- 'The following tags were removed from user %1$s: %2$s.',
- count($tags)),
- $profile->nickname,
-
- implode(_(', '), $tags)));
- }
- }
- class WhoisCommand extends Command
- {
- var $other = null;
- function __construct($user, $other)
- {
- parent::__construct($user);
- $this->other = $other;
- }
- function handle($channel)
- {
- $recipient = $this->getProfile($this->other);
-
-
- $whois = sprintf(_m('WHOIS',"%1\$s (%2\$s)"), $recipient->nickname,
- $recipient->profileurl);
- if ($recipient->fullname) {
-
- $whois .= "\n" . sprintf(_('Fullname: %s'), $recipient->fullname);
- }
- if ($recipient->location) {
-
- $whois .= "\n" . sprintf(_('Location: %s'), $recipient->location);
- }
- if ($recipient->homepage) {
-
- $whois .= "\n" . sprintf(_('Homepage: %s'), $recipient->homepage);
- }
- if ($recipient->bio) {
-
- $whois .= "\n" . sprintf(_('About: %s'), $recipient->bio);
- }
- $channel->output($this->user, $whois);
- }
- }
- class ReplyCommand extends Command
- {
- var $other = null;
- var $text = null;
- function __construct($user, $other, $text)
- {
- parent::__construct($user);
- $this->other = $other;
- $this->text = $text;
- }
- function handle($channel)
- {
- $notice = $this->getNotice($this->other);
- $recipient = $notice->getProfile();
- $len = mb_strlen($this->text);
- if ($len == 0) {
-
- $channel->error($this->user, _('No content!'));
- return;
- }
- $this->text = $this->user->shortenLinks($this->text);
- if (Notice::contentTooLong($this->text)) {
-
-
-
- $channel->error($this->user, sprintf(_m('Notice too long - maximum is %1$d character, you sent %2$d.',
- 'Notice too long - maximum is %1$d characters, you sent %2$d.',
- Notice::maxContent()),
- Notice::maxContent(), mb_strlen($this->text)));
- return;
- }
- $notice = Notice::saveNew($this->user->id, $this->text, $channel->source(),
- array('reply_to' => $notice->id));
- if ($notice) {
-
-
- $channel->output($this->user, sprintf(_('Reply to %s sent.'), $recipient->nickname));
- } else {
-
- $channel->error($this->user, _('Error saving notice.'));
- }
- }
- }
- class GetCommand extends Command
- {
- var $other = null;
- function __construct($user, $other)
- {
- parent::__construct($user);
- $this->other = $other;
- }
- function handle($channel)
- {
- $target = $this->getProfile($this->other);
- $notice = $target->getCurrentNotice();
- if (!$notice) {
-
- $channel->error($this->user, _('User has no last notice.'));
- return;
- }
- $notice_content = $notice->content;
- $channel->output($this->user, $target->nickname . ": " . $notice_content);
- }
- }
- class SubCommand extends Command
- {
- var $other = null;
- function __construct($user, $other)
- {
- parent::__construct($user);
- $this->other = $other;
- }
- function handle($channel)
- {
- if (!$this->other) {
-
- $channel->error($this->user, _('Specify the name of the user to subscribe to.'));
- return;
- }
- $target = $this->getProfile($this->other);
- try {
- Subscription::start($this->user->getProfile(), $target);
-
-
- $channel->output($this->user, sprintf(_('Subscribed to %s.'), $this->other));
- } catch (Exception $e) {
- $channel->error($this->user, $e->getMessage());
- }
- }
- }
- class UnsubCommand extends Command
- {
- var $other = null;
- function __construct($user, $other)
- {
- parent::__construct($user);
- $this->other = $other;
- }
- function handle($channel)
- {
- if(!$this->other) {
-
- $channel->error($this->user, _('Specify the name of the user to unsubscribe from.'));
- return;
- }
- $target = $this->getProfile($this->other);
- try {
- Subscription::cancel($this->user->getProfile(), $target);
-
-
- $channel->output($this->user, sprintf(_('Unsubscribed from %s.'), $this->other));
- } catch (Exception $e) {
- $channel->error($this->user, $e->getMessage());
- }
- }
- }
- class OffCommand extends Command
- {
- var $other = null;
- function __construct($user, $other=null)
- {
- parent::__construct($user);
- $this->other = $other;
- }
- function handle($channel)
- {
- if ($this->other) {
-
- $channel->error($this->user, _("Command not yet implemented."));
- } else {
- if ($channel->off($this->user)) {
-
- $channel->output($this->user, _('Notification off.'));
- } else {
-
- $channel->error($this->user, _('Can\'t turn off notification.'));
- }
- }
- }
- }
- class OnCommand extends Command
- {
- var $other = null;
- function __construct($user, $other=null)
- {
- parent::__construct($user);
- $this->other = $other;
- }
- function handle($channel)
- {
- if ($this->other) {
-
- $channel->error($this->user, _("Command not yet implemented."));
- } else {
- if ($channel->on($this->user)) {
-
- $channel->output($this->user, _('Notification on.'));
- } else {
-
- $channel->error($this->user, _('Can\'t turn on notification.'));
- }
- }
- }
- }
- class LoginCommand extends Command
- {
- function handle($channel)
- {
- $disabled = common_config('logincommand','disabled');
- $disabled = isset($disabled) && $disabled;
- if($disabled) {
-
- $channel->error($this->user, _('Login command is disabled.'));
- return;
- }
- try {
- $login_token = Login_token::makeNew($this->user);
- } catch (Exception $e) {
- $channel->error($this->user, $e->getMessage());
- }
- $channel->output($this->user,
-
-
- sprintf(_('This link is useable only once and is valid for only 2 minutes: %s.'),
- common_local_url('otp',
- array('user_id' => $login_token->user_id, 'token' => $login_token->token))));
- }
- }
- class LoseCommand extends Command
- {
- var $other = null;
- function __construct($user, $other)
- {
- parent::__construct($user);
- $this->other = $other;
- }
- function execute($channel)
- {
- if(!$this->other) {
-
- $channel->error($this->user, _('Specify the name of the user to unsubscribe from.'));
- return;
- }
- $result = Subscription::cancel($this->getProfile($this->other), $this->user->getProfile());
- if ($result) {
-
-
- $channel->output($this->user, sprintf(_('Unsubscribed %s.'), $this->other));
- } else {
- $channel->error($this->user, $result);
- }
- }
- }
- class SubscriptionsCommand extends Command
- {
- function handle($channel)
- {
- $profile = $this->user->getSubscribed(0);
- $nicknames=array();
- while ($profile->fetch()) {
- $nicknames[]=$profile->nickname;
- }
- if(count($nicknames)==0){
-
- $out=_('You are not subscribed to anyone.');
- }else{
-
-
-
- $out = _m('You are subscribed to this person:',
- 'You are subscribed to these people:',
- count($nicknames));
- $out .= ' ';
- $out .= implode(', ',$nicknames);
- }
- $channel->output($this->user,$out);
- }
- }
- class SubscribersCommand extends Command
- {
- function handle($channel)
- {
- $profile = $this->user->getSubscribers();
- $nicknames=array();
- while ($profile->fetch()) {
- $nicknames[]=$profile->nickname;
- }
- if(count($nicknames)==0){
-
-
- $out=_('No one is subscribed to you.');
- }else{
-
-
-
- $out = _m('This person is subscribed to you:',
- 'These people are subscribed to you:',
- count($nicknames));
- $out .= ' ';
- $out .= implode(', ',$nicknames);
- }
- $channel->output($this->user,$out);
- }
- }
- class GroupsCommand extends Command
- {
- function handle($channel)
- {
- $group = $this->user->getGroups();
- $groups=array();
- while ($group instanceof User_group && $group->fetch()) {
- $groups[]=$group->nickname;
- }
- if(count($groups)==0){
-
-
- $out=_('You are not a member of any groups.');
- }else{
-
-
-
- $out = _m('You are a member of this group:',
- 'You are a member of these groups:',
- count($nicknames));
- $out.=implode(', ',$groups);
- }
- $channel->output($this->user,$out);
- }
- }
- class HelpCommand extends Command
- {
- function handle($channel)
- {
-
- $out = array(_m('COMMANDHELP', "Commands:"));
- $commands = array(
- "on" => _m('COMMANDHELP', "turn on notifications"),
-
- "off" => _m('COMMANDHELP', "turn off notifications"),
-
- "help" => _m('COMMANDHELP', "show this help"),
-
- "follow <nickname>" => _m('COMMANDHELP', "subscribe to user"),
-
- "groups" => _m('COMMANDHELP', "lists the groups you have joined"),
-
- "tag <nickname> <tags>" => _m('COMMANDHELP',"tag a user"),
-
- "untag <nickname> <tags>" => _m('COMMANDHELP',"untag a user"),
-
- "subscriptions" => _m('COMMANDHELP', "list the people you follow"),
-
- "subscribers" => _m('COMMANDHELP', "list the people that follow you"),
-
- "leave <nickname>" => _m('COMMANDHELP', "unsubscribe from user"),
-
- "d <nickname> <text>" => _m('COMMANDHELP', "direct message to user"),
-
- "get <nickname>" => _m('COMMANDHELP', "get last notice from user"),
-
- "whois <nickname>" => _m('COMMANDHELP', "get profile info on user"),
-
- "lose <nickname>" => _m('COMMANDHELP', "force user to stop following you"),
-
- "reply #<notice_id>" => _m('COMMANDHELP', "reply to notice with a given id"),
-
- "reply <nickname>" => _m('COMMANDHELP', "reply to the last notice from user"),
-
- "join <group>" => _m('COMMANDHELP', "join group"),
-
- "login" => _m('COMMANDHELP', "Get a link to login to the web interface"),
-
- "drop <group>" => _m('COMMANDHELP', "leave group"),
-
- "stats" => _m('COMMANDHELP', "get your stats"),
-
- "stop" => _m('COMMANDHELP', "same as 'off'"),
-
- "quit" => _m('COMMANDHELP', "same as 'off'"),
-
- "sub <nickname>" => _m('COMMANDHELP', "same as 'follow'"),
-
- "unsub <nickname>" => _m('COMMANDHELP', "same as 'leave'"),
-
- "last <nickname>" => _m('COMMANDHELP', "same as 'get'"),
-
- "on <nickname>" => _m('COMMANDHELP', "not yet implemented."),
-
- "off <nickname>" => _m('COMMANDHELP', "not yet implemented."),
-
- "nudge <nickname>" => _m('COMMANDHELP', "remind a user to update."),
-
- "invite <phone number>" => _m('COMMANDHELP', "not yet implemented."),
-
- "track <word>" => _m('COMMANDHELP', "not yet implemented."),
-
- "untrack <word>" => _m('COMMANDHELP', "not yet implemented."),
-
- "track off" => _m('COMMANDHELP', "not yet implemented."),
-
- "untrack all" => _m('COMMANDHELP', "not yet implemented."),
-
- "tracks" => _m('COMMANDHELP', "not yet implemented."),
-
- "tracking" => _m('COMMANDHELP', "not yet implemented."));
-
- Event::handle('HelpCommandMessages', array($this, &$commands));
- ksort($commands);
- foreach ($commands as $command => $help) {
- $out[] = "$command - $help";
- }
- $channel->output($this->user, implode("\n", $out));
- }
- }
|