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 _m('Private messages'),
259 array(Group_privacy_settings::SOMETIMES => _m('Sometimes'),
260 Group_privacy_settings::ALWAYS => _m('Always'),
261 Group_privacy_settings::NEVER => _m('Never')),
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 _m('Private senders'),
269 array(Group_privacy_settings::EVERYONE => _m('Everyone'),
270 Group_privacy_settings::MEMBER => _m('Member'),
271 Group_privacy_settings::ADMIN => _m('Admin')),
272 _m('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 Widget $widget The showgroup action being shown
352 * @param User_group $group The current group
354 * @return boolean hook value
356 function onEndGroupActionsList($widget, $group)
358 $cur = common_current_user();
359 $action = $widget->out;
366 Group_privacy_settings::ensurePost($cur, $group);
367 } catch (Exception $e) {
371 $action->elementStart('li', 'entity_send-a-message');
372 $action->element('a', array('href' => common_local_url('newgroupmessage', array('nickname' => $group->nickname)),
373 'title' => _m('Send a direct message to this group.')),
375 // $form = new GroupMessageForm($action, $group);
376 // $form->hidden = true;
378 $action->elementEnd('li');
383 * When saving a notice, check its groups. If any of them has
384 * privacy == always, force a group private message to all mentioned groups.
385 * If any of the groups disallows private messages, skip it.
391 function onStartNoticeSave(&$notice) {
393 // Look for group tags
394 // FIXME: won't work for remote groups
395 // @fixme if Notice::saveNew is refactored so we can just pull its list
396 // of groups between processing and saving, make use of it
398 $count = preg_match_all('/(?:^|\s)!(' . Nickname::DISPLAY_FMT . ')/',
399 strtolower($notice->content),
405 $forcePrivate = false;
406 $profile = $notice->getProfile();
410 /* Add them to the database */
412 foreach (array_unique($match[1]) as $nickname) {
414 $group = User_group::getForNickname($nickname, $profile);
420 $gps = Group_privacy_settings::forGroup($group);
422 switch ($gps->allow_privacy) {
423 case Group_privacy_settings::ALWAYS:
424 $forcePrivate = true;
426 case Group_privacy_settings::SOMETIMES:
429 case Group_privacy_settings::NEVER:
437 foreach ($ignored as $group) {
438 common_log(LOG_NOTICE,
439 "Notice forced to group direct message ".
440 "but group ".$group->nickname." does not allow them.");
443 $user = User::staticGet('id', $notice->profile_id);
446 common_log(LOG_WARNING,
447 "Notice forced to group direct message ".
448 "but profile ".$notice->profile_id." is not a local user.");
450 foreach ($groups as $group) {
451 Group_message::send($user, $group, $notice->content);
455 // Don't save the notice!
456 // FIXME: this is probably cheating.
457 throw new ClientException(sprintf(_m('Forced notice to private group message.')),
466 * Show an indicator that the group is (essentially) private on the group page
468 * @param Action $action The action being shown
469 * @param User_group $group The group being shown
471 * @return boolean hook value
474 function onEndGroupProfileElements($action, $group)
476 $gps = Group_privacy_settings::forGroup($group);
478 if ($gps->allow_privacy == Group_privacy_settings::ALWAYS) {
479 $action->element('p', 'privategroupindicator', _m('Private'));
485 function onStartShowExportData($action)
487 if ($action instanceof ShowgroupAction) {
488 $gps = Group_privacy_settings::forGroup($action->group);
490 if ($gps->allow_privacy == Group_privacy_settings::ALWAYS) {
497 function onPluginVersion(&$versions)
499 $versions[] = array('name' => 'GroupPrivateMessage',
500 'version' => STATUSNET_VERSION,
501 'author' => 'Evan Prodromou',
502 'homepage' => 'http://status.net/wiki/Plugin:GroupPrivateMessage',
504 _m('Allow posting private messages to groups.'));