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 return _('New message');
75 * Handle input, produce output
77 * @param array $args $_REQUEST contents
82 function handle($args)
84 parent::handle($args);
86 if (!common_logged_in()) {
87 $this->clientError(_('Not logged in.'), 403);
88 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
89 $this->saveNewMessage();
95 function prepare($args)
97 parent::prepare($args);
99 $user = common_current_user();
102 /* Go log in, and then come back. */
103 common_set_returnto($_SERVER['REQUEST_URI']);
104 common_redirect(common_local_url('login'));
108 $this->content = $this->trimmed('content');
109 $this->to = $this->trimmed('to');
113 $this->other = User::staticGet('id', $this->to);
116 $this->clientError(_('No such user'), 404);
120 if (!$user->mutuallySubscribed($this->other)) {
121 $this->clientError(_('You can\'t send a message to this user.'), 404);
129 function saveNewMessage()
133 $token = $this->trimmed('token');
134 if (!$token || $token != common_session_token()) {
135 $this->showForm(_('There was a problem with your session token. ' .
136 'Try again, please.'));
140 $user = common_current_user();
141 assert($user); // XXX: maybe an error instead...
143 if (!$this->content) {
144 $this->showForm(_('No content!'));
147 $content_shortened = common_shorten_links($this->content);
149 if (Message::contentTooLong($content_shortened)) {
150 $this->showForm(sprintf(_('That\'s too long. ' .
151 'Max message size is %d chars.'),
152 Message::maxContent()));
158 $this->showForm(_('No recipient specified.'));
160 } else if (!$user->mutuallySubscribed($this->other)) {
161 $this->clientError(_('You can\'t send a message to this user.'), 404);
163 } else if ($user->id == $this->other->id) {
164 $this->clientError(_('Don\'t send a message to yourself; ' .
165 'just say it to yourself quietly instead.'), 403);
169 $message = Message::saveNew($user->id, $this->other->id, $this->content, 'web');
171 if (is_string($message)) {
172 $this->showForm($message);
176 $this->notify($user, $this->other, $message);
178 if ($this->boolean('ajax')) {
179 $this->startHTML('text/xml;charset=utf-8');
180 $this->elementStart('head');
181 $this->element('title', null, _('Message sent'));
182 $this->elementEnd('head');
183 $this->elementStart('body');
184 $this->element('p', array('id' => 'command_result'),
185 sprintf(_('Direct message to %s sent'),
186 $this->other->nickname));
187 $this->elementEnd('body');
188 $this->elementEnd('html');
190 $url = common_local_url('outbox',
191 array('nickname' => $user->nickname));
192 common_redirect($url, 303);
197 * Show an Ajax-y error message
199 * Goes back to the browser, where it's shown in a popup.
201 * @param string $msg Message to show
206 function ajaxErrorMsg($msg)
208 $this->startHTML('text/xml;charset=utf-8', true);
209 $this->elementStart('head');
210 $this->element('title', null, _('Ajax Error'));
211 $this->elementEnd('head');
212 $this->elementStart('body');
213 $this->element('p', array('id' => 'error'), $msg);
214 $this->elementEnd('body');
215 $this->elementEnd('html');
218 function showForm($msg = null)
220 if ($msg && $this->boolean('ajax')) {
221 $this->ajaxErrorMsg($msg);
226 if ($this->trimmed('ajax')) {
227 header('Content-Type: text/xml;charset=utf-8');
228 $this->xw->startDocument('1.0', 'UTF-8');
229 $this->elementStart('html');
230 $this->elementStart('head');
231 $this->element('title', null, _('New message'));
232 $this->elementEnd('head');
233 $this->elementStart('body');
234 $this->showNoticeForm();
235 $this->elementEnd('body');
243 function showPageNotice()
246 $this->element('p', 'error', $this->msg);
250 function notify($from, $to, $message)
252 mail_notify_message($message, $from, $to);
253 // XXX: Jabber, SMS notifications... probably queued
256 // Do nothing (override)
258 function showNoticeForm()
260 $message_form = new MessageForm($this, $this->other, $this->content);
261 $message_form->show();