messagecommand.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. class MessageCommand extends Command
  3. {
  4. var $other = null;
  5. var $text = null;
  6. function __construct($user, $other, $text)
  7. {
  8. parent::__construct($user);
  9. $this->other = $other;
  10. $this->text = $text;
  11. }
  12. function handle($channel)
  13. {
  14. try {
  15. $other = $this->getUser($this->other)->getProfile();
  16. } catch (CommandException $e) {
  17. try {
  18. $profile = $this->getProfile($this->other);
  19. } catch (CommandException $f) {
  20. throw $e;
  21. }
  22. // TRANS: Command exception text shown when trying to send a direct message to a remote user (a user not registered at the current server).
  23. // TRANS: %s is a remote profile.
  24. throw new CommandException(sprintf(_('%s is a remote profile; you can only send direct messages to users on the same server.'), $this->other));
  25. }
  26. $len = mb_strlen($this->text);
  27. if ($len == 0) {
  28. // TRANS: Command exception text shown when trying to send a direct message to another user without content.
  29. $channel->error($this->user, _('No content!'));
  30. return;
  31. }
  32. $this->text = $this->user->shortenLinks($this->text);
  33. if (Message::contentTooLong($this->text)) {
  34. // XXX: i18n. Needs plural support.
  35. // TRANS: Message given if content is too long. %1$sd is used for plural.
  36. // TRANS: %1$d is the maximum number of characters, %2$d is the number of submitted characters.
  37. $channel->error($this->user, sprintf(_m('Message too long - maximum is %1$d character, you sent %2$d.',
  38. 'Message too long - maximum is %1$d characters, you sent %2$d.',
  39. Message::maxContent()),
  40. Message::maxContent(), mb_strlen($this->text)));
  41. return;
  42. }
  43. if (!$other instanceof Profile) {
  44. // TRANS: Error text shown when trying to send a direct message to a user that does not exist.
  45. $channel->error($this->user, _('No such user.'));
  46. return;
  47. } else if (!$this->user->mutuallySubscribed($other)) {
  48. // TRANS: Error text shown when trying to send a direct message to a user without a mutual subscription (each user must be subscribed to the other).
  49. $channel->error($this->user, _('You can\'t send a message to this user.'));
  50. return;
  51. } else if ($this->user->id == $other->id) {
  52. // TRANS: Error text shown when trying to send a direct message to self.
  53. $channel->error($this->user, _('Do not send a message to yourself; just say it to yourself quietly instead.'));
  54. return;
  55. }
  56. try {
  57. $message = Message::saveNew($this->user->id, $other->id, $this->text, $channel->source());
  58. $message->notify();
  59. // TRANS: Message given have sent a direct message to another user.
  60. // TRANS: %s is the name of the other user.
  61. $channel->output($this->user, sprintf(_('Direct message to %s sent.'), $this->other));
  62. } catch (Exception $e) {
  63. // TRANS: Error text shown sending a direct message fails with an unknown reason.
  64. $channel->error($this->user, $e->getMessage());
  65. }
  66. }
  67. }