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
65 $schema->ensureTable('group_privacy_settings',
66 array(new ColumnDef('group_id',
71 new ColumnDef('allow_privacy',
73 new ColumnDef('allow_sender',
75 new ColumnDef('created',
77 new ColumnDef('modified',
80 $schema->ensureTable('group_message',
81 array(new ColumnDef('id',
91 new ColumnDef('from_profile',
96 new ColumnDef('to_group',
101 new ColumnDef('content',
103 new ColumnDef('rendered',
110 new ColumnDef('created',
113 $schema->ensureTable('group_message_profile',
114 array(new ColumnDef('to_profile',
119 new ColumnDef('group_message_id',
124 new ColumnDef('created',
131 * Load related modules when needed
133 * @param string $cls Name of the class to be loaded
135 * @return boolean hook value
137 function onAutoload($cls)
139 $dir = dirname(__FILE__);
143 case 'GroupinboxAction':
144 case 'ShowgroupmessageAction':
145 case 'NewgroupmessageAction':
146 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
148 case 'Group_privacy_settings':
149 case 'Group_message':
150 case 'Group_message_profile':
151 include_once $dir . '/'.$cls.'.php';
153 case 'GroupMessageCommand':
154 case 'GroupMessageList':
155 case 'GroupMessageListItem':
156 case 'GroupMessageForm':
157 include_once $dir . '/'.strtolower($cls).'.php';
165 * Map URLs to actions
167 * @param Net_URL_Mapper $m path-to-action mapper
169 * @return boolean hook value
171 function onRouterInitialized($m)
173 $m->connect('group/:nickname/inbox',
174 array('action' => 'groupinbox'),
175 array('nickname' => Nickname::DISPLAY_FMT));
177 $m->connect('group/message/:id',
178 array('action' => 'showgroupmessage'),
179 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
181 $m->connect('group/:nickname/message/new',
182 array('action' => 'newgroupmessage'),
183 array('nickname' => Nickname::DISPLAY_FMT));
189 * Add group inbox to the menu
191 * @param Action $action The current action handler. Use this to
194 * @return boolean hook value; true means continue processing, false means stop.
198 function onEndGroupGroupNav($groupnav)
200 $action = $groupnav->action;
201 $group = $groupnav->group;
203 $action->menuItem(common_local_url('groupinbox',
204 array('nickname' => $group->nickname)),
205 // TRANS: Menu item in group page.
207 // TRANS: Menu title in group page.
208 _m('Private messages for this group.'),
209 $action->trimmed('action') == 'groupinbox',
215 * Create default group privacy settings at group create time
217 * @param User_group $group Group that was just created
219 * @result boolean hook value
221 function onEndGroupSave($group)
223 $gps = new Group_privacy_settings();
225 $gps->group_id = $group->id;
226 $gps->allow_privacy = Group_privacy_settings::SOMETIMES;
227 $gps->allow_sender = Group_privacy_settings::MEMBER;
228 $gps->created = common_sql_now();
229 $gps->modified = $gps->created;
231 // This will throw an exception on error
239 * Show group privacy controls on group edit form
241 * @param GroupEditForm $form form being shown
243 function onEndGroupEditFormData($form)
247 if (!empty($form->group)) {
248 $gps = Group_privacy_settings::staticGet('group_id', $form->group->id);
251 $form->out->elementStart('li');
252 $form->out->dropdown('allow_privacy',
253 // TRANS: Dropdown label in group settings page for if group allows private messages.
254 _m('Private messages'),
255 // TRANS: Dropdown option in group settings page for allowing private messages.
256 array(Group_privacy_settings::SOMETIMES => _m('Sometimes'),
257 // TRANS: Dropdown option in group settings page for allowing private messages.
258 Group_privacy_settings::ALWAYS => _m('Always'),
259 // TRANS: Dropdown option in group settings page for allowing private messages.
260 Group_privacy_settings::NEVER => _m('Never')),
261 // TRANS: Dropdown title in group settings page for if group allows private messages.
262 _m('Whether to allow private messages to this group.'),
264 (empty($gps)) ? Group_privacy_settings::SOMETIMES : $gps->allow_privacy);
265 $form->out->elementEnd('li');
266 $form->out->elementStart('li');
267 $form->out->dropdown('allow_sender',
268 // TRANS: Dropdown label in group settings page for who can send private messages to the group.
269 _m('Private senders'),
270 // TRANS: Dropdown option in group settings page for who can send private messages.
271 array(Group_privacy_settings::EVERYONE => _m('Everyone'),
272 // TRANS: Dropdown option in group settings page for who can send private messages.
273 Group_privacy_settings::MEMBER => _m('Member'),
274 // TRANS: Dropdown option in group settings page for who can send private messages.
275 Group_privacy_settings::ADMIN => _m('Admin')),
276 // TRANS: Dropdown title in group settings page for who can send private messages to the group.
277 _m('Who can send private messages to the group.'),
279 (empty($gps)) ? Group_privacy_settings::MEMBER : $gps->allow_sender);
280 $form->out->elementEnd('li');
284 function onEndGroupSaveForm($action)
288 if (!empty($action->group)) {
289 $gps = Group_privacy_settings::staticGet('group_id', $action->group->id);
295 $gps = new Group_privacy_settings();
296 $gps->group_id = $action->group->id;
301 $gps->allow_privacy = $action->trimmed('allow_privacy');
302 $gps->allow_sender = $action->trimmed('allow_sender');
305 $gps->created = common_sql_now();
315 * Overload 'd' command to send private messages to groups.
317 * 'd !group word word word' will send the private message
318 * 'word word word' to the group 'group'.
320 * @param string $cmd Command being run
321 * @param string $arg Rest of the message (including address)
322 * @param User $user User sending the message
323 * @param Command &$result The resulting command object to be run.
325 * @return boolean hook value
327 function onStartIntepretCommand($cmd, $arg, $user, &$result)
329 if ($cmd == 'd' || $cmd == 'dm') {
331 $this->debug('Got a d command');
333 // Break off the first word as the address
335 $pieces = explode(' ', $arg, 2);
337 if (count($pieces) == 1) {
341 list($addr, $msg) = $pieces;
343 if (!empty($addr) && $addr[0] == '!') {
344 $result = new GroupMessageCommand($user, substr($addr, 1), $msg);
345 Event::handle('EndInterpretCommand', array($cmd, $arg, $user, $result));
354 * To add a "Message" button to the group profile page
356 * @param Widget $widget The showgroup action being shown
357 * @param User_group $group The current group
359 * @return boolean hook value
361 function onEndGroupActionsList($widget, $group)
363 $cur = common_current_user();
364 $action = $widget->out;
371 Group_privacy_settings::ensurePost($cur, $group);
372 } catch (Exception $e) {
376 $action->elementStart('li', 'entity_send-a-message');
377 $action->element('a', array('href' => common_local_url('newgroupmessage', array('nickname' => $group->nickname)),
378 // TRANS: Title for action in group actions list.
379 'title' => _m('Send a direct message to this group.')),
380 // TRANS: Link text for action in group actions list to send a private message to a group.
381 _m('LINKTEXT','Message'));
382 // $form = new GroupMessageForm($action, $group);
383 // $form->hidden = true;
385 $action->elementEnd('li');
390 * When saving a notice, check its groups. If any of them has
391 * privacy == always, force a group private message to all mentioned groups.
392 * If any of the groups disallows private messages, skip it.
396 function onStartNoticeSave(&$notice) {
397 // Look for group tags
398 // FIXME: won't work for remote groups
399 // @fixme if Notice::saveNew is refactored so we can just pull its list
400 // of groups between processing and saving, make use of it
402 $count = preg_match_all('/(?:^|\s)!(' . Nickname::DISPLAY_FMT . ')/',
403 strtolower($notice->content),
409 $forcePrivate = false;
410 $profile = $notice->getProfile();
413 /* Add them to the database */
415 foreach (array_unique($match[1]) as $nickname) {
416 $group = User_group::getForNickname($nickname, $profile);
422 $gps = Group_privacy_settings::forGroup($group);
424 switch ($gps->allow_privacy) {
425 case Group_privacy_settings::ALWAYS:
426 $forcePrivate = true;
428 case Group_privacy_settings::SOMETIMES:
431 case Group_privacy_settings::NEVER:
438 foreach ($ignored as $group) {
439 common_log(LOG_NOTICE,
440 "Notice forced to group direct message ".
441 "but group ".$group->nickname." does not allow them.");
444 $user = User::staticGet('id', $notice->profile_id);
447 common_log(LOG_WARNING,
448 "Notice forced to group direct message ".
449 "but profile ".$notice->profile_id." is not a local user.");
451 foreach ($groups as $group) {
452 Group_message::send($user, $group, $notice->content);
456 // Don't save the notice!
457 // FIXME: this is probably cheating.
458 // TRANS: Client exception thrown when a private group message has to be forced.
459 throw new ClientException(sprintf(_m('Forced notice to private group message.')),
468 * Show an indicator that the group is (essentially) private on the group page
470 * @param Action $action The action being shown
471 * @param User_group $group The group being shown
473 * @return boolean hook value
475 function onEndGroupProfileElements($action, $group)
477 $gps = Group_privacy_settings::forGroup($group);
479 if ($gps->allow_privacy == Group_privacy_settings::ALWAYS) {
480 // TRANS: Indicator on the group page that the group is (essentially) private.
481 $action->element('p', 'privategroupindicator', _m('Private'));
487 function onStartShowExportData($action)
489 if ($action instanceof ShowgroupAction) {
490 $gps = Group_privacy_settings::forGroup($action->group);
492 if ($gps->allow_privacy == Group_privacy_settings::ALWAYS) {
499 function onPluginVersion(&$versions)
501 $versions[] = array('name' => 'GroupPrivateMessage',
502 'version' => STATUSNET_VERSION,
503 'author' => 'Evan Prodromou',
504 'homepage' => 'http://status.net/wiki/Plugin:GroupPrivateMessage',
506 // TRANS: Plugin description.
507 _m('Allow posting private messages to groups.'));