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