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
51 const PLUGIN_VERSION = '2.0.0';
54 * Database schema setup
59 * @return boolean hook value
61 function onCheckSchema()
63 $schema = Schema::get();
65 // For storing user-submitted flags on profiles
66 $schema->ensureTable('group_privacy_settings', Group_privacy_settings::schemaDef());
67 $schema->ensureTable('group_message', Group_message::schemaDef());
68 $schema->ensureTable('group_message_profile', Group_message_profile::schemaDef());
75 * @param URLMapper $m path-to-action mapper
77 * @return boolean hook value
79 public function onRouterInitialized(URLMapper $m)
81 $m->connect('group/:nickname/inbox',
82 array('action' => 'groupinbox'),
83 array('nickname' => Nickname::DISPLAY_FMT));
85 $m->connect('group/message/:id',
86 array('action' => 'showgroupmessage'),
87 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
89 $m->connect('group/:nickname/message/new',
90 array('action' => 'newgroupmessage'),
91 array('nickname' => Nickname::DISPLAY_FMT));
97 * Add group inbox to the menu
99 * @param Action $action The current action handler. Use this to
102 * @return boolean hook value; true means continue processing, false means stop.
106 function onEndGroupGroupNav(Menu $groupnav)
108 $action = $groupnav->action;
109 $group = $groupnav->group;
111 $action->menuItem(common_local_url('groupinbox',
112 array('nickname' => $group->nickname)),
113 // TRANS: Menu item in group page.
115 // TRANS: Menu title in group page.
116 _m('Private messages for this group.'),
117 $action->trimmed('action') == 'groupinbox',
123 * Create default group privacy settings at group create time
125 * @param User_group $group Group that was just created
127 * @result boolean hook value
129 function onEndGroupSave($group)
131 $gps = new Group_privacy_settings();
133 $gps->group_id = $group->id;
134 $gps->allow_privacy = Group_privacy_settings::SOMETIMES;
135 $gps->allow_sender = Group_privacy_settings::MEMBER;
136 $gps->created = common_sql_now();
137 $gps->modified = $gps->created;
139 // This will throw an exception on error
147 * Show group privacy controls on group edit form
149 * @param GroupEditForm $form form being shown
151 function onEndGroupEditFormData(GroupEditForm $form)
155 if (!empty($form->group)) {
156 $gps = Group_privacy_settings::getKV('group_id', $form->group->id);
159 $form->out->elementStart('li');
160 $form->out->dropdown('allow_privacy',
161 // TRANS: Dropdown label in group settings page for if group allows private messages.
162 _m('Private messages'),
163 // TRANS: Dropdown option in group settings page for allowing private messages.
164 array(Group_privacy_settings::SOMETIMES => _m('Sometimes'),
165 // TRANS: Dropdown option in group settings page for allowing private messages.
166 Group_privacy_settings::ALWAYS => _m('Always'),
167 // TRANS: Dropdown option in group settings page for allowing private messages.
168 Group_privacy_settings::NEVER => _m('Never')),
169 // TRANS: Dropdown title in group settings page for if group allows private messages.
170 _m('Whether to allow private messages to this group.'),
172 (empty($gps)) ? Group_privacy_settings::SOMETIMES : $gps->allow_privacy);
173 $form->out->elementEnd('li');
174 $form->out->elementStart('li');
175 $form->out->dropdown('allow_sender',
176 // TRANS: Dropdown label in group settings page for who can send private messages to the group.
177 _m('Private senders'),
178 // TRANS: Dropdown option in group settings page for who can send private messages.
179 array(Group_privacy_settings::EVERYONE => _m('Everyone'),
180 // TRANS: Dropdown option in group settings page for who can send private messages.
181 Group_privacy_settings::MEMBER => _m('Member'),
182 // TRANS: Dropdown option in group settings page for who can send private messages.
183 Group_privacy_settings::ADMIN => _m('Admin')),
184 // TRANS: Dropdown title in group settings page for who can send private messages to the group.
185 _m('Who can send private messages to the group.'),
187 (empty($gps)) ? Group_privacy_settings::MEMBER : $gps->allow_sender);
188 $form->out->elementEnd('li');
192 function onEndGroupSaveForm(Action $action)
194 // The Action class must contain this method
195 assert(is_callable(array($action, 'getGroup')));
199 if ($action->getGroup() instanceof User_group) {
200 $gps = Group_privacy_settings::getKV('group_id', $action->getGroup()->id);
206 $gps = new Group_privacy_settings();
207 $gps->group_id = $action->getGroup()->id;
212 $gps->allow_privacy = $action->trimmed('allow_privacy');
213 $gps->allow_sender = $action->trimmed('allow_sender');
216 $gps->created = common_sql_now();
226 * Overload 'd' command to send private messages to groups.
228 * 'd !group word word word' will send the private message
229 * 'word word word' to the group 'group'.
231 * @param string $cmd Command being run
232 * @param string $arg Rest of the message (including address)
233 * @param User $user User sending the message
234 * @param Command &$result The resulting command object to be run.
236 * @return boolean hook value
238 function onStartInterpretCommand($cmd, $arg, User $user, &$result)
240 if ($cmd == 'd' || $cmd == 'dm') {
242 $this->debug('Got a d command');
244 // Break off the first word as the address
246 $pieces = explode(' ', $arg, 2);
248 if (count($pieces) == 1) {
252 list($addr, $msg) = $pieces;
254 if (!empty($addr) && $addr[0] == '!') {
255 $result = new GroupMessageCommand($user, substr($addr, 1), $msg);
256 Event::handle('EndInterpretCommand', array($cmd, $arg, $user, $result));
265 * To add a "Message" button to the group profile page
267 * @param Widget $widget The showgroup action being shown
268 * @param User_group $group The current group
270 * @return boolean hook value
272 function onEndGroupActionsList(Widget $widget, User_group $group)
274 $cur = common_current_user();
275 $action = $widget->out;
282 Group_privacy_settings::ensurePost($cur, $group);
283 } catch (Exception $e) {
287 $action->elementStart('li', 'entity_send-a-message');
288 $action->element('a', array('href' => common_local_url('newgroupmessage', array('nickname' => $group->nickname)),
289 // TRANS: Title for action in group actions list.
290 'title' => _m('Send a direct message to this group.')),
291 // TRANS: Link text for action in group actions list to send a private message to a group.
292 _m('LINKTEXT','Message'));
293 // $form = new GroupMessageForm($action, $group);
294 // $form->hidden = true;
296 $action->elementEnd('li');
301 * When saving a notice, check its groups. If any of them has
302 * privacy == always, force a group private message to all mentioned groups.
303 * If any of the groups disallows private messages, skip it.
307 function onStartNoticeSave(Notice &$notice) {
308 // Look for group tags
309 // FIXME: won't work for remote groups
310 // @fixme if Notice::saveNew is refactored so we can just pull its list
311 // of groups between processing and saving, make use of it
313 $count = preg_match_all('/(?:^|\s)!(' . Nickname::DISPLAY_FMT . ')/',
314 strtolower($notice->content),
320 $forcePrivate = false;
321 $profile = $notice->getProfile();
324 /* Add them to the database */
326 foreach (array_unique($match[1]) as $nickname) {
327 $group = User_group::getForNickname($nickname, $profile);
333 $gps = Group_privacy_settings::forGroup($group);
335 switch ($gps->allow_privacy) {
336 case Group_privacy_settings::ALWAYS:
337 $forcePrivate = true;
339 case Group_privacy_settings::SOMETIMES:
342 case Group_privacy_settings::NEVER:
349 foreach ($ignored as $group) {
350 common_log(LOG_NOTICE,
351 "Notice forced to group direct message ".
352 "but group ".$group->nickname." does not allow them.");
355 $user = User::getKV('id', $notice->profile_id);
358 common_log(LOG_WARNING,
359 "Notice forced to group direct message ".
360 "but profile ".$notice->profile_id." is not a local user.");
362 foreach ($groups as $group) {
363 Group_message::send($user, $group, $notice->content);
367 // Don't save the notice!
368 // FIXME: this is probably cheating.
369 // TRANS: Client exception thrown when a private group message has to be forced.
370 throw new ClientException(sprintf(_m('Forced notice to private group message.')),
379 * Show an indicator that the group is (essentially) private on the group page
381 * @param Action $action The action being shown
382 * @param User_group $group The group being shown
384 * @return boolean hook value
386 function onEndGroupProfileElements(Action $action, User_group $group)
388 $gps = Group_privacy_settings::forGroup($group);
390 if ($gps->allow_privacy == Group_privacy_settings::ALWAYS) {
391 // TRANS: Indicator on the group page that the group is (essentially) private.
392 $action->element('p', 'privategroupindicator', _m('Private'));
398 function onStartShowExportData(Action $action)
400 if ($action instanceof ShowgroupAction) {
401 $gps = Group_privacy_settings::forGroup($action->getGroup());
403 if ($gps->allow_privacy == Group_privacy_settings::ALWAYS) {
410 function onPluginVersion(array &$versions)
412 $versions[] = array('name' => 'GroupPrivateMessage',
413 'version' => self::PLUGIN_VERSION,
414 'author' => 'Evan Prodromou',
415 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/GroupPrivateMessage',
417 // TRANS: Plugin description.
418 _m('Allow posting private messages to groups.'));