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: Client error displayed trying to create a new direct message while not logged in.
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 $this->showForm(_('There was a problem with your session token. ' .
141 'Try again, please.'));
145 $user = common_current_user();
146 assert($user); // XXX: maybe an error instead...
148 if (!$this->content) {
149 // TRANS: Form validator error displayed trying to send a direct message without content.
150 $this->showForm(_('No content!'));
153 $content_shortened = $user->shortenLinks($this->content);
155 if (Message::contentTooLong($content_shortened)) {
156 // TRANS: Form validation error displayed when message content is too long.
157 // TRANS: %d is the maximum number of characters for a message.
158 $this->showForm(sprintf(_m('That\'s too long. Maximum message size is %d character.',
159 'That\'s too long. Maximum message size is %d characters.',
160 Message::maxContent()),
161 Message::maxContent()));
167 // TRANS: Form validation error displayed trying to send a direct message without specifying a recipient.
168 $this->showForm(_('No recipient specified.'));
170 } else if (!$user->mutuallySubscribed($this->other)) {
171 // TRANS: Client error displayed trying to send a direct message to a user while sender and
172 // TRANS: receiver are not subscribed to each other.
173 $this->clientError(_('You cannot send a message to this user.'), 404);
175 } else if ($user->id == $this->other->id) {
176 // TRANS: Client error displayed trying to send a direct message to self.
177 $this->clientError(_('Don\'t send a message to yourself; ' .
178 'just say it to yourself quietly instead.'), 403);
182 $message = Message::saveNew($user->id, $this->other->id, $this->content, 'web');
184 if (is_string($message)) {
185 $this->showForm($message);
191 if ($this->boolean('ajax')) {
192 $this->startHTML('text/xml;charset=utf-8');
193 $this->elementStart('head');
194 // TRANS: Page title after sending a direct message.
195 $this->element('title', null, _('Message sent'));
196 $this->elementEnd('head');
197 $this->elementStart('body');
198 $this->element('p', array('id' => 'command_result'),
199 // TRANS: Confirmation text after sending a direct message.
200 // TRANS: %s is the direct message recipient.
201 sprintf(_('Direct message to %s sent.'),
202 $this->other->nickname));
203 $this->elementEnd('body');
204 $this->elementEnd('html');
206 $url = common_local_url('outbox',
207 array('nickname' => $user->nickname));
208 common_redirect($url, 303);
213 * Show an Ajax-y error message
215 * Goes back to the browser, where it's shown in a popup.
217 * @param string $msg Message to show
222 function ajaxErrorMsg($msg)
224 $this->startHTML('text/xml;charset=utf-8', true);
225 $this->elementStart('head');
226 // TRANS: Page title after an AJAX error occurred on the "send direct message" page.
227 $this->element('title', null, _('Ajax Error'));
228 $this->elementEnd('head');
229 $this->elementStart('body');
230 $this->element('p', array('id' => 'error'), $msg);
231 $this->elementEnd('body');
232 $this->elementEnd('html');
235 function showForm($msg = null)
237 if ($msg && $this->boolean('ajax')) {
238 $this->ajaxErrorMsg($msg);
243 if ($this->trimmed('ajax')) {
244 header('Content-Type: text/xml;charset=utf-8');
245 $this->xw->startDocument('1.0', 'UTF-8');
246 $this->elementStart('html');
247 $this->elementStart('head');
248 // TRANS: Page title on page for sending a direct message.
249 $this->element('title', null, _('New message'));
250 $this->elementEnd('head');
251 $this->elementStart('body');
252 $this->showNoticeForm();
253 $this->elementEnd('body');
261 function showPageNotice()
264 $this->element('p', 'error', $this->msg);
268 // Do nothing (override)
270 function showNoticeForm()
272 $message_form = new MessageForm($this, $this->other, $this->content);
273 $message_form->show();