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/
50 class GroupPrivateMessagePlugin extends Plugin
53 * Database schema setup
58 * @return boolean hook value
61 function onCheckSchema()
63 $schema = Schema::get();
65 // For storing user-submitted flags on profiles
67 $schema->ensureTable('group_privacy_settings',
68 array(new ColumnDef('group_id',
73 new ColumnDef('allow_privacy',
75 new ColumnDef('allow_sender',
77 new ColumnDef('created',
79 new ColumnDef('modified',
82 $schema->ensureTable('group_message',
83 array(new ColumnDef('id',
93 new ColumnDef('from_profile',
98 new ColumnDef('to_group',
103 new ColumnDef('content',
105 new ColumnDef('rendered',
112 new ColumnDef('created',
115 $schema->ensureTable('group_message_profile',
116 array(new ColumnDef('to_profile',
121 new ColumnDef('group_message_id',
126 new ColumnDef('created',
133 * Load related modules when needed
135 * @param string $cls Name of the class to be loaded
137 * @return boolean hook value
140 function onAutoload($cls)
142 $dir = dirname(__FILE__);
146 case 'GroupinboxAction':
147 case 'ShowgroupmessageAction':
148 case 'NewgroupmessageAction':
149 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
151 case 'Group_privacy_settings':
152 case 'Group_message':
153 case 'Group_message_profile':
154 include_once $dir . '/'.$cls.'.php';
156 case 'GroupMessageCommand':
157 case 'GroupMessageList':
158 case 'GroupMessageListItem':
159 case 'GroupMessageForm':
160 include_once $dir . '/'.strtolower($cls).'.php';
168 * Map URLs to actions
170 * @param Net_URL_Mapper $m path-to-action mapper
172 * @return boolean hook value
175 function onRouterInitialized($m)
177 $m->connect('group/:nickname/inbox',
178 array('action' => 'groupinbox'),
179 array('nickname' => Nickname::DISPLAY_FMT));
181 $m->connect('group/message/:id',
182 array('action' => 'showgroupmessage'),
183 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
185 $m->connect('group/:nickname/message/new',
186 array('action' => 'newgroupmessage'),
187 array('nickname' => Nickname::DISPLAY_FMT));
193 * Add group inbox to the menu
195 * @param Action $action The current action handler. Use this to
198 * @return boolean hook value; true means continue processing, false means stop.
203 function onEndGroupGroupNav($groupnav)
205 $action = $groupnav->action;
206 $group = $groupnav->group;
208 $action->menuItem(common_local_url('groupinbox',
209 array('nickname' => $group->nickname)),
211 _m('Private messages for this group'),
212 $action->trimmed('action') == 'groupinbox',
218 * Create default group privacy settings at group create time
220 * @param User_group $group Group that was just created
222 * @result boolean hook value
225 function onEndGroupSave($group)
227 $gps = new Group_privacy_settings();
229 $gps->group_id = $group->id;
230 $gps->allow_privacy = Group_privacy_settings::SOMETIMES;
231 $gps->allow_sender = Group_privacy_settings::MEMBER;
232 $gps->created = common_sql_now();
233 $gps->modified = $gps->created;
235 // This will throw an exception on error
243 * Show group privacy controls on group edit form
245 * @param GroupEditForm $form form being shown
248 function onEndGroupEditFormData($form)
252 if (!empty($form->group)) {
253 $gps = Group_privacy_settings::staticGet('group_id', $form->group->id);
256 $form->out->elementStart('li');
257 $form->out->dropdown('allow_privacy',
258 _('Private messages'),
259 array(Group_privacy_settings::SOMETIMES => _('Sometimes'),
260 Group_privacy_settings::ALWAYS => _('Always'),
261 Group_privacy_settings::NEVER => _('Never')),
262 _('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',
269 array(Group_privacy_settings::EVERYONE => _('Everyone'),
270 Group_privacy_settings::MEMBER => _('Member'),
271 Group_privacy_settings::ADMIN => _('Admin')),
272 _('Who can send private messages to the group'),
274 (empty($gps)) ? Group_privacy_settings::MEMBER : $gps->allow_sender);
275 $form->out->elementEnd('li');
279 function onEndGroupSaveForm($action)
283 if (!empty($action->group)) {
284 $gps = Group_privacy_settings::staticGet('group_id', $action->group->id);
290 $gps = new Group_privacy_settings();
291 $gps->group_id = $action->group->id;
296 $gps->allow_privacy = $action->trimmed('allow_privacy');
297 $gps->allow_sender = $action->trimmed('allow_sender');
300 $gps->created = common_sql_now();
310 * Overload 'd' command to send private messages to groups.
312 * 'd !group word word word' will send the private message
313 * 'word word word' to the group 'group'.
315 * @param string $cmd Command being run
316 * @param string $arg Rest of the message (including address)
317 * @param User $user User sending the message
318 * @param Command &$result The resulting command object to be run.
320 * @return boolean hook value
322 function onStartIntepretCommand($cmd, $arg, $user, &$result)
324 if ($cmd == 'd' || $cmd == 'dm') {
326 $this->debug('Got a d command');
328 // Break off the first word as the address
330 $pieces = explode(' ', $arg, 2);
332 if (count($pieces) == 1) {
336 list($addr, $msg) = $pieces;
338 if (!empty($addr) && $addr[0] == '!') {
339 $result = new GroupMessageCommand($user, substr($addr, 1), $msg);
340 Event::handle('EndInterpretCommand', array($cmd, $arg, $user, $result));
349 * To add a "Message" button to the group profile page
351 * @param Action $action The showgroup action being shown
352 * @param User_group $group The current group
354 * @return boolean hook value
356 function onEndGroupActionsList($action, $group)
358 $cur = common_current_user();
365 Group_privacy_settings::ensurePost($cur, $group);
366 } catch (Exception $e) {
370 $action->elementStart('li', 'entity_send-a-message');
371 $action->element('a', array('href' => common_local_url('newgroupmessage', array('nickname' => $group->nickname)),
372 'title' => _('Send a direct message to this group')),
374 // $form = new GroupMessageForm($action, $group);
375 // $form->hidden = true;
377 $action->elementEnd('li');
382 * When saving a notice, check its groups. If any of them has
383 * privacy == always, force a group private message to all mentioned groups.
384 * If any of the groups disallows private messages, skip it.
390 function onStartNoticeSave(&$notice) {
392 // Look for group tags
393 // FIXME: won't work for remote groups
394 // @fixme if Notice::saveNew is refactored so we can just pull its list
395 // of groups between processing and saving, make use of it
397 $count = preg_match_all('/(?:^|\s)!(' . Nickname::DISPLAY_FMT . ')/',
398 strtolower($notice->content),
404 $forcePrivate = false;
408 /* Add them to the database */
410 foreach (array_unique($match[1]) as $nickname) {
412 $group = User_group::getForNickname($nickname, $profile);
418 $gps = Group_privacy_settings::forGroup($group);
420 switch ($gps->allow_privacy) {
421 case Group_privacy_settings::ALWAYS:
422 $forcePrivate = true;
424 case Group_privacy_settings::SOMETIMES:
427 case Group_privacy_settings::NEVER:
435 foreach ($ignored as $group) {
436 common_log(LOG_NOTICE,
437 "Notice forced to group direct message ".
438 "but group ".$group->nickname." does not allow them.");
441 $user = User::staticGet('id', $notice->profile_id);
444 common_log(LOG_WARNING,
445 "Notice forced to group direct message ".
446 "but profile ".$notice->profile_id." is not a local user.");
448 foreach ($groups as $group) {
449 Group_message::send($user, $group, $notice->content);
453 // Don't save the notice!
454 // FIXME: this is probably cheating.
455 throw new ClientException(sprintf(_('Forced notice to private group message.')),
464 * Show an indicator that the group is (essentially) private on the group page
466 * @param Action $action The action being shown
467 * @param User_group $group The group being shown
469 * @return boolean hook value
472 function onEndGroupProfileElements($action, $group)
474 $gps = Group_privacy_settings::forGroup($group);
476 if ($gps->allow_privacy == Group_privacy_settings::ALWAYS) {
477 $action->element('p', 'privategroupindicator', _('Private'));
483 function onStartShowExportData($action)
485 if ($action instanceof ShowgroupAction) {
486 $gps = Group_privacy_settings::forGroup($action->group);
488 if ($gps->allow_privacy == Group_privacy_settings::ALWAYS) {
495 function onPluginVersion(&$versions)
497 $versions[] = array('name' => 'GroupPrivateMessage',
498 'version' => STATUSNET_VERSION,
499 'author' => 'Evan Prodromou',
500 'homepage' => 'http://status.net/wiki/Plugin:GroupPrivateMessage',
502 _m('Allow posting DMs to a group.'));