3 * StatusNet, the distributed open-source microblogging tool
5 * Handler for posting new messages
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@status.net>
25 * @author Zach Copley <zach@status.net>
26 * @author Sarven Capadisli <csarven@status.net>
27 * @copyright 2008-2009 StatusNet, Inc.
28 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29 * @link http://status.net/
32 if (!defined('STATUSNET') && !defined('LACONICA')) {
37 * Action for posting new direct messages
41 * @author Evan Prodromou <evan@status.net>
42 * @author Zach Copley <zach@status.net>
43 * @author Sarven Capadisli <csarven@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
48 class NewmessageAction extends Action
52 * Error message, if any
64 * Note that this usually doesn't get called unless something went wrong
66 * @return string page title
71 // TRANS: Page title for new direct message page.
72 return _('New message');
76 * Handle input, produce output
78 * @param array $args $_REQUEST contents
83 function handle($args)
85 parent::handle($args);
87 if (!common_logged_in()) {
88 // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
89 $this->clientError(_('Not logged in.'), 403);
90 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
91 $this->saveNewMessage();
97 function prepare($args)
99 parent::prepare($args);
101 $user = common_current_user();
104 /* Go log in, and then come back. */
105 common_set_returnto($_SERVER['REQUEST_URI']);
106 common_redirect(common_local_url('login'));
110 $this->content = $this->trimmed('content');
111 $this->to = $this->trimmed('to');
115 $this->other = User::staticGet('id', $this->to);
118 // TRANS: Client error displayed trying to send a direct message to a non-existing user.
119 $this->clientError(_('No such user.'), 404);
123 if (!$user->mutuallySubscribed($this->other)) {
124 // TRANS: Client error displayed trying to send a direct message to a user while sender and
125 // TRANS: receiver are not subscribed to each other.
126 $this->clientError(_('You cannot send a message to this user.'), 404);
134 function saveNewMessage()
138 $token = $this->trimmed('token');
139 if (!$token || $token != common_session_token()) {
140 // TRANS: Client error displayed when the session token does not match or is not given.
141 $this->showForm(_('There was a problem with your session token. ' .
142 'Try again, please.'));
146 $user = common_current_user();
147 assert($user); // XXX: maybe an error instead...
149 if (!$this->content) {
150 // TRANS: Form validator error displayed trying to send a direct message without content.
151 $this->showForm(_('No content!'));
154 $content_shortened = $user->shortenLinks($this->content);
156 if (Message::contentTooLong($content_shortened)) {
157 // TRANS: Form validation error displayed when message content is too long.
158 // TRANS: %d is the maximum number of characters for a message.
159 $this->showForm(sprintf(_m('That\'s too long. Maximum message size is %d character.',
160 'That\'s too long. Maximum message size is %d characters.',
161 Message::maxContent()),
162 Message::maxContent()));
168 // TRANS: Form validation error displayed trying to send a direct message without specifying a recipient.
169 $this->showForm(_('No recipient specified.'));
171 } else if (!$user->mutuallySubscribed($this->other)) {
172 // TRANS: Client error displayed trying to send a direct message to a user while sender and
173 // TRANS: receiver are not subscribed to each other.
174 $this->clientError(_('You cannot send a message to this user.'), 404);
176 } else if ($user->id == $this->other->id) {
177 // TRANS: Client error displayed trying to send a direct message to self.
178 $this->clientError(_('Do not send a message to yourself; ' .
179 'just say it to yourself quietly instead.'), 403);
183 $message = Message::saveNew($user->id, $this->other->id, $this->content, 'web');
185 if (is_string($message)) {
186 $this->showForm($message);
192 if ($this->boolean('ajax')) {
193 $this->startHTML('text/xml;charset=utf-8');
194 $this->elementStart('head');
195 // TRANS: Page title after sending a direct message.
196 $this->element('title', null, _('Message sent'));
197 $this->elementEnd('head');
198 $this->elementStart('body');
199 $this->element('p', array('id' => 'command_result'),
200 // TRANS: Confirmation text after sending a direct message.
201 // TRANS: %s is the direct message recipient.
202 sprintf(_('Direct message to %s sent.'),
203 $this->other->nickname));
204 $this->elementEnd('body');
205 $this->elementEnd('html');
207 $url = common_local_url('outbox',
208 array('nickname' => $user->nickname));
209 common_redirect($url, 303);
214 * Show an Ajax-y error message
216 * Goes back to the browser, where it's shown in a popup.
218 * @param string $msg Message to show
223 function ajaxErrorMsg($msg)
225 $this->startHTML('text/xml;charset=utf-8', true);
226 $this->elementStart('head');
227 // TRANS: Page title after an AJAX error occurred on the "send direct message" page.
228 $this->element('title', null, _('Ajax Error'));
229 $this->elementEnd('head');
230 $this->elementStart('body');
231 $this->element('p', array('id' => 'error'), $msg);
232 $this->elementEnd('body');
233 $this->elementEnd('html');
236 function showForm($msg = null)
238 if ($msg && $this->boolean('ajax')) {
239 $this->ajaxErrorMsg($msg);
244 if ($this->trimmed('ajax')) {
245 header('Content-Type: text/xml;charset=utf-8');
246 $this->xw->startDocument('1.0', 'UTF-8');
247 $this->elementStart('html');
248 $this->elementStart('head');
249 // TRANS: Page title on page for sending a direct message.
250 $this->element('title', null, _('New message'));
251 $this->elementEnd('head');
252 $this->elementStart('body');
253 $this->showNoticeForm();
254 $this->elementEnd('body');
262 function showPageNotice()
265 $this->element('p', 'error', $this->msg);
269 // Do nothing (override)
271 function showNoticeForm()
273 $message_form = new MessageForm($this, $this->other, $this->content);
274 $message_form->show();