3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('LACONICA')) { exit(1); }
22 class NewmessageAction extends Action {
24 function handle($args) {
25 parent::handle($args);
27 if (!common_logged_in()) {
28 $this->client_error(_('Not logged in.'), 403);
29 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
30 $this->save_new_message();
36 function save_new_message() {
38 $user = common_current_user();
39 assert($user); # XXX: maybe an error instead...
40 $content = $this->trimmed('content');
41 $to = $this->trimmed('to');
44 $this->show_form(_('No content!'));
46 } else if (mb_strlen($content) > 140) {
47 common_debug("Content = '$content'", __FILE__);
48 common_debug("mb_strlen(\$content) = " . mb_strlen($content), __FILE__);
49 $this->show_form(_('That\'s too long. Max message size is 140 chars.'));
53 $other = User::staticGet('id', $to);
56 $this->show_form(_('No recipient specified.'));
58 } else if (!$user->mutuallySubscribed($other)) {
59 $this->client_error(_('You can\'t send a message to this user.'), 404);
63 $message = Message::saveNew($user->id, $other->id, $content, 'web');
65 if (is_string($message)) {
66 $this->show_form($message);
70 $this->notify($user, $to, $message);
72 $url = common_local_url('showmessage',
73 array('message' => $message->id));
75 common_redirect($url, 303);
78 function show_top($params) {
80 list($content, $user, $to) = $params;
82 assert(!is_null($user));
84 common_element_start('form', array('id' => 'message_form',
86 'action' => $this->self_url()));
88 common_element_start('p');
90 $mutual_users = $user->mutuallySubscribedUsers();
94 while ($mutual_users->fetch()) {
95 $mutual[$mutual_users->id] = $mutual_users->nickname;
98 $mutual_users->free();
101 common_dropdown('to', _('To'), $mutual,
102 _('User you want to send a message to'), FALSE,
105 common_element('textarea', array('id' => 'content',
108 'name' => 'content'),
109 ($content) ? $content : '');
111 common_element('input', array('id' => 'message_send',
112 'name' => 'message_send',
114 'value' => _('Send')));
116 common_element_end('p');
117 common_element_end('form');
120 function show_form($msg=NULL) {
122 $content = $this->trimmed('content');
123 $user = common_current_user();
125 $to = common_canonical_nickname($this->trimmed('to'));
127 $other = User::staticGet('nickname', $to);
130 $this->client_error(_('No such user'), 404);
134 if (!$user->mutuallySubscribed($other)) {
135 $this->client_error(_('You can\'t send a message to this user.'), 404);
139 common_show_header(_('New message'), NULL,
140 array($content, $user, $to),
141 array($this, 'show_top'));
144 common_element('p', 'error', $msg);
147 common_show_footer();
150 function notify($from, $to, $message) {
151 mail_notify_message($message, $from, $to);
152 # XXX: Jabber, SMS notifications... probably queued