+
+ /**
+ * Look up a notice from an argument, by poster's name to get last post
+ * or notice_id prefixed with #.
+ *
+ * @return Notice
+ * @throws CommandException
+ */
+ function getNotice($arg)
+ {
+ $notice = null;
+ if (Event::handle('StartCommandGetNotice', array($this, $arg, &$notice))) {
+ if(substr($this->other,0,1)=='#'){
+ // A specific notice_id #123
+
+ $notice = Notice::staticGet(substr($arg,1));
+ if (!$notice) {
+ throw new CommandException(_('Notice with that id does not exist'));
+ }
+ }
+
+ if (Validate::uri($this->other)) {
+ // A specific notice by URI lookup
+ $notice = Notice::staticGet('uri', $arg);
+ }
+
+ if (!$notice) {
+ // Local or remote profile name to get their last notice.
+ // May throw an exception and report 'no such user'
+ $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;
+ }
+
+ /**
+ * Look up a local or remote profile by nickname.
+ *
+ * @return Profile
+ * @throws CommandException
+ */
+ 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;
+ }
+
+ /**
+ * Get a local user by name
+ * @return User
+ * @throws CommandException
+ */
+ function getUser($arg)
+ {
+ $user = null;
+ if (Event::handle('StartCommandGetUser', array($this, $arg, &$user))) {
+ $user = User::staticGet('nickname', $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;
+ }
+
+ /**
+ * Get a local or remote group by name.
+ * @return User_group
+ * @throws CommandException
+ */
+ 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
+{