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());
71 * Load related modules when needed
73 * @param string $cls Name of the class to be loaded
75 * @return boolean hook value
77 function onAutoload($cls)
79 $dir = dirname(__FILE__);
83 case 'GroupinboxAction':
84 case 'ShowgroupmessageAction':
85 case 'NewgroupmessageAction':
86 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
88 case 'Group_privacy_settings':
90 case 'Group_message_profile':
91 include_once $dir . '/'.$cls.'.php';
93 case 'GroupMessageCommand':
94 case 'GroupMessageList':
95 case 'GroupMessageListItem':
96 case 'GroupMessageForm':
97 include_once $dir . '/'.strtolower($cls).'.php';
105 * Map URLs to actions
107 * @param Net_URL_Mapper $m path-to-action mapper
109 * @return boolean hook value
111 function onRouterInitialized($m)
113 $m->connect('group/:nickname/inbox',
114 array('action' => 'groupinbox'),
115 array('nickname' => Nickname::DISPLAY_FMT));
117 $m->connect('group/message/:id',
118 array('action' => 'showgroupmessage'),
119 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
121 $m->connect('group/:nickname/message/new',
122 array('action' => 'newgroupmessage'),
123 array('nickname' => Nickname::DISPLAY_FMT));
129 * Add group inbox to the menu
131 * @param Action $action The current action handler. Use this to
134 * @return boolean hook value; true means continue processing, false means stop.
138 function onEndGroupGroupNav($groupnav)
140 $action = $groupnav->action;
141 $group = $groupnav->group;
143 $action->menuItem(common_local_url('groupinbox',
144 array('nickname' => $group->nickname)),
145 // TRANS: Menu item in group page.
147 // TRANS: Menu title in group page.
148 _m('Private messages for this group.'),
149 $action->trimmed('action') == 'groupinbox',
155 * Create default group privacy settings at group create time
157 * @param User_group $group Group that was just created
159 * @result boolean hook value
161 function onEndGroupSave($group)
163 $gps = new Group_privacy_settings();
165 $gps->group_id = $group->id;
166 $gps->allow_privacy = Group_privacy_settings::SOMETIMES;
167 $gps->allow_sender = Group_privacy_settings::MEMBER;
168 $gps->created = common_sql_now();
169 $gps->modified = $gps->created;
171 // This will throw an exception on error
179 * Show group privacy controls on group edit form
181 * @param GroupEditForm $form form being shown
183 function onEndGroupEditFormData($form)
187 if (!empty($form->group)) {
188 $gps = Group_privacy_settings::getKV('group_id', $form->group->id);
191 $form->out->elementStart('li');
192 $form->out->dropdown('allow_privacy',
193 // TRANS: Dropdown label in group settings page for if group allows private messages.
194 _m('Private messages'),
195 // TRANS: Dropdown option in group settings page for allowing private messages.
196 array(Group_privacy_settings::SOMETIMES => _m('Sometimes'),
197 // TRANS: Dropdown option in group settings page for allowing private messages.
198 Group_privacy_settings::ALWAYS => _m('Always'),
199 // TRANS: Dropdown option in group settings page for allowing private messages.
200 Group_privacy_settings::NEVER => _m('Never')),
201 // TRANS: Dropdown title in group settings page for if group allows private messages.
202 _m('Whether to allow private messages to this group.'),
204 (empty($gps)) ? Group_privacy_settings::SOMETIMES : $gps->allow_privacy);
205 $form->out->elementEnd('li');
206 $form->out->elementStart('li');
207 $form->out->dropdown('allow_sender',
208 // TRANS: Dropdown label in group settings page for who can send private messages to the group.
209 _m('Private senders'),
210 // TRANS: Dropdown option in group settings page for who can send private messages.
211 array(Group_privacy_settings::EVERYONE => _m('Everyone'),
212 // TRANS: Dropdown option in group settings page for who can send private messages.
213 Group_privacy_settings::MEMBER => _m('Member'),
214 // TRANS: Dropdown option in group settings page for who can send private messages.
215 Group_privacy_settings::ADMIN => _m('Admin')),
216 // TRANS: Dropdown title in group settings page for who can send private messages to the group.
217 _m('Who can send private messages to the group.'),
219 (empty($gps)) ? Group_privacy_settings::MEMBER : $gps->allow_sender);
220 $form->out->elementEnd('li');
224 function onEndGroupSaveForm($action)
228 if (!empty($action->group)) {
229 $gps = Group_privacy_settings::getKV('group_id', $action->group->id);
235 $gps = new Group_privacy_settings();
236 $gps->group_id = $action->group->id;
241 $gps->allow_privacy = $action->trimmed('allow_privacy');
242 $gps->allow_sender = $action->trimmed('allow_sender');
245 $gps->created = common_sql_now();
255 * Overload 'd' command to send private messages to groups.
257 * 'd !group word word word' will send the private message
258 * 'word word word' to the group 'group'.
260 * @param string $cmd Command being run
261 * @param string $arg Rest of the message (including address)
262 * @param User $user User sending the message
263 * @param Command &$result The resulting command object to be run.
265 * @return boolean hook value
267 function onStartIntepretCommand($cmd, $arg, $user, &$result)
269 if ($cmd == 'd' || $cmd == 'dm') {
271 $this->debug('Got a d command');
273 // Break off the first word as the address
275 $pieces = explode(' ', $arg, 2);
277 if (count($pieces) == 1) {
281 list($addr, $msg) = $pieces;
283 if (!empty($addr) && $addr[0] == '!') {
284 $result = new GroupMessageCommand($user, substr($addr, 1), $msg);
285 Event::handle('EndInterpretCommand', array($cmd, $arg, $user, $result));
294 * To add a "Message" button to the group profile page
296 * @param Widget $widget The showgroup action being shown
297 * @param User_group $group The current group
299 * @return boolean hook value
301 function onEndGroupActionsList($widget, $group)
303 $cur = common_current_user();
304 $action = $widget->out;
311 Group_privacy_settings::ensurePost($cur, $group);
312 } catch (Exception $e) {
316 $action->elementStart('li', 'entity_send-a-message');
317 $action->element('a', array('href' => common_local_url('newgroupmessage', array('nickname' => $group->nickname)),
318 // TRANS: Title for action in group actions list.
319 'title' => _m('Send a direct message to this group.')),
320 // TRANS: Link text for action in group actions list to send a private message to a group.
321 _m('LINKTEXT','Message'));
322 // $form = new GroupMessageForm($action, $group);
323 // $form->hidden = true;
325 $action->elementEnd('li');
330 * When saving a notice, check its groups. If any of them has
331 * privacy == always, force a group private message to all mentioned groups.
332 * If any of the groups disallows private messages, skip it.
336 function onStartNoticeSave(&$notice) {
337 // Look for group tags
338 // FIXME: won't work for remote groups
339 // @fixme if Notice::saveNew is refactored so we can just pull its list
340 // of groups between processing and saving, make use of it
342 $count = preg_match_all('/(?:^|\s)!(' . Nickname::DISPLAY_FMT . ')/',
343 strtolower($notice->content),
349 $forcePrivate = false;
350 $profile = $notice->getProfile();
353 /* Add them to the database */
355 foreach (array_unique($match[1]) as $nickname) {
356 $group = User_group::getForNickname($nickname, $profile);
362 $gps = Group_privacy_settings::forGroup($group);
364 switch ($gps->allow_privacy) {
365 case Group_privacy_settings::ALWAYS:
366 $forcePrivate = true;
368 case Group_privacy_settings::SOMETIMES:
371 case Group_privacy_settings::NEVER:
378 foreach ($ignored as $group) {
379 common_log(LOG_NOTICE,
380 "Notice forced to group direct message ".
381 "but group ".$group->nickname." does not allow them.");
384 $user = User::getKV('id', $notice->profile_id);
387 common_log(LOG_WARNING,
388 "Notice forced to group direct message ".
389 "but profile ".$notice->profile_id." is not a local user.");
391 foreach ($groups as $group) {
392 Group_message::send($user, $group, $notice->content);
396 // Don't save the notice!
397 // FIXME: this is probably cheating.
398 // TRANS: Client exception thrown when a private group message has to be forced.
399 throw new ClientException(sprintf(_m('Forced notice to private group message.')),
408 * Show an indicator that the group is (essentially) private on the group page
410 * @param Action $action The action being shown
411 * @param User_group $group The group being shown
413 * @return boolean hook value
415 function onEndGroupProfileElements($action, $group)
417 $gps = Group_privacy_settings::forGroup($group);
419 if ($gps->allow_privacy == Group_privacy_settings::ALWAYS) {
420 // TRANS: Indicator on the group page that the group is (essentially) private.
421 $action->element('p', 'privategroupindicator', _m('Private'));
427 function onStartShowExportData($action)
429 if ($action instanceof ShowgroupAction) {
430 $gps = Group_privacy_settings::forGroup($action->group);
432 if ($gps->allow_privacy == Group_privacy_settings::ALWAYS) {
439 function onPluginVersion(&$versions)
441 $versions[] = array('name' => 'GroupPrivateMessage',
442 'version' => STATUSNET_VERSION,
443 'author' => 'Evan Prodromou',
444 'homepage' => 'http://status.net/wiki/Plugin:GroupPrivateMessage',
446 // TRANS: Plugin description.
447 _m('Allow posting private messages to groups.'));