3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
6 * Private groups for StatusNet 0.9.x
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2011 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
40 * This plugin allows users to send private messages to a group.
44 * @author Evan Prodromou <evan@status.net>
45 * @copyright 2011 StatusNet, Inc.
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
47 * @link http://status.net/
49 class GroupPrivateMessagePlugin extends Plugin
52 * Database schema setup
57 * @return boolean hook value
59 function onCheckSchema()
61 $schema = Schema::get();
63 // For storing user-submitted flags on profiles
64 $schema->ensureTable('group_privacy_settings', Group_privacy_settings::schemaDef());
65 $schema->ensureTable('group_message', Group_message::schemaDef());
66 $schema->ensureTable('group_message_profile', Group_message_profile::schemaDef());
73 * @param URLMapper $m path-to-action mapper
75 * @return boolean hook value
77 function onRouterInitialized(URLMapper $m)
79 $m->connect('group/:nickname/inbox',
80 array('action' => 'groupinbox'),
81 array('nickname' => Nickname::DISPLAY_FMT));
83 $m->connect('group/message/:id',
84 array('action' => 'showgroupmessage'),
85 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
87 $m->connect('group/:nickname/message/new',
88 array('action' => 'newgroupmessage'),
89 array('nickname' => Nickname::DISPLAY_FMT));
95 * Add group inbox to the menu
97 * @param Action $action The current action handler. Use this to
100 * @return boolean hook value; true means continue processing, false means stop.
104 function onEndGroupGroupNav(Menu $groupnav)
106 $action = $groupnav->action;
107 $group = $groupnav->group;
109 $action->menuItem(common_local_url('groupinbox',
110 array('nickname' => $group->nickname)),
111 // TRANS: Menu item in group page.
113 // TRANS: Menu title in group page.
114 _m('Private messages for this group.'),
115 $action->trimmed('action') == 'groupinbox',
121 * Create default group privacy settings at group create time
123 * @param User_group $group Group that was just created
125 * @result boolean hook value
127 function onEndGroupSave(User_group $group)
129 $gps = new Group_privacy_settings();
131 $gps->group_id = $group->id;
132 $gps->allow_privacy = Group_privacy_settings::SOMETIMES;
133 $gps->allow_sender = Group_privacy_settings::MEMBER;
134 $gps->created = common_sql_now();
135 $gps->modified = $gps->created;
137 // This will throw an exception on error
145 * Show group privacy controls on group edit form
147 * @param GroupEditForm $form form being shown
149 function onEndGroupEditFormData(GroupEditForm $form)
153 if (!empty($form->group)) {
154 $gps = Group_privacy_settings::getKV('group_id', $form->group->id);
157 $form->out->elementStart('li');
158 $form->out->dropdown('allow_privacy',
159 // TRANS: Dropdown label in group settings page for if group allows private messages.
160 _m('Private messages'),
161 // TRANS: Dropdown option in group settings page for allowing private messages.
162 array(Group_privacy_settings::SOMETIMES => _m('Sometimes'),
163 // TRANS: Dropdown option in group settings page for allowing private messages.
164 Group_privacy_settings::ALWAYS => _m('Always'),
165 // TRANS: Dropdown option in group settings page for allowing private messages.
166 Group_privacy_settings::NEVER => _m('Never')),
167 // TRANS: Dropdown title in group settings page for if group allows private messages.
168 _m('Whether to allow private messages to this group.'),
170 (empty($gps)) ? Group_privacy_settings::SOMETIMES : $gps->allow_privacy);
171 $form->out->elementEnd('li');
172 $form->out->elementStart('li');
173 $form->out->dropdown('allow_sender',
174 // TRANS: Dropdown label in group settings page for who can send private messages to the group.
175 _m('Private senders'),
176 // TRANS: Dropdown option in group settings page for who can send private messages.
177 array(Group_privacy_settings::EVERYONE => _m('Everyone'),
178 // TRANS: Dropdown option in group settings page for who can send private messages.
179 Group_privacy_settings::MEMBER => _m('Member'),
180 // TRANS: Dropdown option in group settings page for who can send private messages.
181 Group_privacy_settings::ADMIN => _m('Admin')),
182 // TRANS: Dropdown title in group settings page for who can send private messages to the group.
183 _m('Who can send private messages to the group.'),
185 (empty($gps)) ? Group_privacy_settings::MEMBER : $gps->allow_sender);
186 $form->out->elementEnd('li');
190 function onEndGroupSaveForm(Action $action)
192 // The Action class must contain this method
193 assert(is_callable(array($action, 'getGroup')));
196 $group = $action->getGroup();
198 if ($group instanceof User_group) {
199 $gps = Group_privacy_settings::getKV('group_id', $group->id);
205 $gps = new Group_privacy_settings();
206 $gps->group_id = $action->getGroup()->id;
211 $gps->allow_privacy = $action->trimmed('allow_privacy');
212 $gps->allow_sender = $action->trimmed('allow_sender');
215 $gps->created = common_sql_now();
225 * Overload 'd' command to send private messages to groups.
227 * 'd !group word word word' will send the private message
228 * 'word word word' to the group 'group'.
230 * @param string $cmd Command being run
231 * @param string $arg Rest of the message (including address)
232 * @param User $user User sending the message
233 * @param Command &$result The resulting command object to be run.
235 * @return boolean hook value
237 function onStartInterpretCommand($cmd, $arg, User $user, &$result)
239 if ($cmd == 'd' || $cmd == 'dm') {
241 $this->debug('Got a d command');
243 // Break off the first word as the address
245 $pieces = explode(' ', $arg, 2);
247 if (count($pieces) == 1) {
251 list($addr, $msg) = $pieces;
253 if (!empty($addr) && $addr[0] == '!') {
254 $result = new GroupMessageCommand($user, substr($addr, 1), $msg);
255 Event::handle('EndInterpretCommand', array($cmd, $arg, $user, $result));
264 * To add a "Message" button to the group profile page
266 * @param Widget $widget The showgroup action being shown
267 * @param User_group $group The current group
269 * @return boolean hook value
271 function onEndGroupActionsList(Widget $widget, User_group $group)
273 $cur = common_current_user();
274 $action = $widget->out;
281 Group_privacy_settings::ensurePost($cur, $group);
282 } catch (Exception $e) {
286 $action->elementStart('li', 'entity_send-a-message');
287 $action->element('a', array('href' => common_local_url('newgroupmessage', array('nickname' => $group->nickname)),
288 // TRANS: Title for action in group actions list.
289 'title' => _m('Send a direct message to this group.')),
290 // TRANS: Link text for action in group actions list to send a private message to a group.
291 _m('LINKTEXT','Message'));
292 // $form = new GroupMessageForm($action, $group);
293 // $form->hidden = true;
295 $action->elementEnd('li');
300 * When saving a notice, check its groups. If any of them has
301 * privacy == always, force a group private message to all mentioned groups.
302 * If any of the groups disallows private messages, skip it.
306 function onStartNoticeSave(Notice &$notice) {
307 // Look for group tags
308 // FIXME: won't work for remote groups
309 // @fixme if Notice::saveNew is refactored so we can just pull its list
310 // of groups between processing and saving, make use of it
312 $count = preg_match_all('/(?:^|\s)!(' . Nickname::DISPLAY_FMT . ')/',
313 strtolower($notice->content),
319 $forcePrivate = false;
320 $profile = $notice->getProfile();
323 /* Add them to the database */
325 foreach (array_unique($match[1]) as $nickname) {
326 $group = User_group::getForNickname($nickname, $profile);
328 if (!$group instanceof User_group) {
332 $gps = Group_privacy_settings::forGroup($group);
334 switch ($gps->allow_privacy) {
335 case Group_privacy_settings::ALWAYS:
336 $forcePrivate = true;
338 case Group_privacy_settings::SOMETIMES:
341 case Group_privacy_settings::NEVER:
348 foreach ($ignored as $group) {
349 common_log(LOG_NOTICE,
350 "Notice forced to group direct message ".
351 "but group ".$group->nickname." does not allow them.");
354 $user = User::getKV('id', $notice->profile_id);
357 common_log(LOG_WARNING,
358 "Notice forced to group direct message ".
359 "but profile ".$notice->profile_id." is not a local user.");
361 foreach ($groups as $group) {
362 Group_message::send($user, $group, $notice->content);
366 // Don't save the notice!
367 // FIXME: this is probably cheating.
368 // TRANS: Client exception thrown when a private group message has to be forced.
369 throw new ClientException(sprintf(_m('Forced notice to private group message.')),
378 * Show an indicator that the group is (essentially) private on the group page
380 * @param Action $action The action being shown
381 * @param User_group $group The group being shown
383 * @return boolean hook value
385 function onEndGroupProfileElements(Action $action, User_group $group)
387 $gps = Group_privacy_settings::forGroup($group);
389 if ($gps->allow_privacy == Group_privacy_settings::ALWAYS) {
390 // TRANS: Indicator on the group page that the group is (essentially) private.
391 $action->element('p', 'privategroupindicator', _m('Private'));
397 function onStartShowExportData(Action $action)
399 if ($action instanceof ShowgroupAction) {
400 $gps = Group_privacy_settings::forGroup($action->getGroup());
402 if ($gps->allow_privacy == Group_privacy_settings::ALWAYS) {
409 function onPluginVersion(array &$versions)
411 $versions[] = array('name' => 'GroupPrivateMessage',
412 'version' => GNUSOCIAL_VERSION,
413 'author' => 'Evan Prodromou',
414 'homepage' => 'http://status.net/wiki/Plugin:GroupPrivateMessage',
416 // TRANS: Plugin description.
417 _m('Allow posting private messages to groups.'));