3 * Laconica, the distributed open-source microblogging tool
5 * Handler for posting new notices
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@controlyourself.ca>
25 * @author Zach Copley <zach@controlyourself.ca>
26 * @author Sarven Capadisli <csarven@controlyourself.ca>
27 * @copyright 2008-2009 Control Yourself, Inc.
28 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29 * @link http://laconi.ca/
32 if (!defined('LACONICA')) {
37 * Action for posting new direct messages
41 * @author Evan Prodromou <evan@controlyourself.ca>
42 * @author Zach Copley <zach@controlyourself.ca>
43 * @author Sarven Capadisli <csarven@controlyourself.ca>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://laconi.ca/
48 class NewmessageAction extends Action
52 * Error message, if any
60 * Note that this usually doesn't get called unless something went wrong
62 * @return string page title
67 return _('New message');
71 * Handle input, produce output
73 * @param array $args $_REQUEST contents
78 function handle($args)
80 parent::handle($args);
82 if (!common_logged_in()) {
83 $this->clientError(_('Not logged in.'), 403);
84 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
85 $this->saveNewMessage();
91 function saveNewMessage()
93 $user = common_current_user();
94 assert($user); // XXX: maybe an error instead...
98 $token = $this->trimmed('token');
99 if (!$token || $token != common_session_token()) {
100 $this->showForm(_('There was a problem with your session token. ' .
101 'Try again, please.'));
105 $content = $this->trimmed('content');
106 $to = $this->trimmed('to');
109 $this->showForm(_('No content!'));
112 $content_shortened = common_shorten_links($content);
114 if (mb_strlen($content_shortened) > 140) {
115 common_debug("Content = '$content_shortened'", __FILE__);
116 common_debug("mb_strlen(\$content) = " .
117 mb_strlen($content_shortened),
119 $this->showForm(_('That\'s too long. ' .
120 'Max message size is 140 chars.'));
125 $other = User::staticGet('id', $to);
128 $this->showForm(_('No recipient specified.'));
130 } else if (!$user->mutuallySubscribed($other)) {
131 $this->clientError(_('You can\'t send a message to this user.'), 404);
133 } else if ($user->id == $other->id) {
134 $this->clientError(_('Don\'t send a message to yourself; ' .
135 'just say it to yourself quietly instead.'), 403);
139 $message = Message::saveNew($user->id, $other->id, $content, 'web');
141 if (is_string($message)) {
142 $this->showForm($message);
146 $this->notify($user, $other, $message);
148 $url = common_local_url('outbox', array('nickname' => $user->nickname));
150 common_redirect($url, 303);
153 function showForm($msg = null)
155 $content = $this->trimmed('content');
156 $user = common_current_user();
158 $to = $this->trimmed('to');
160 $other = User::staticGet('id', $to);
163 $this->clientError(_('No such user'), 404);
167 if (!$user->mutuallySubscribed($other)) {
168 $this->clientError(_('You can\'t send a message to this user.'), 404);
177 function notify($from, $to, $message)
179 mail_notify_message($message, $from, $to);
180 // XXX: Jabber, SMS notifications... probably queued