]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GroupPrivateMessage/GroupPrivateMessagePlugin.php
IMPORTANT: Making prev. Memcached_DataObject working again with schemaDef
[quix0rs-gnu-social.git] / plugins / GroupPrivateMessage / GroupPrivateMessagePlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Private groups for StatusNet 0.9.x
7  *
8  * PHP version 5
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  * @category  Privacy
24  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Private groups
39  *
40  * This plugin allows users to send private messages to a group.
41  *
42  * @category  Privacy
43  * @package   StatusNet
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/
48  */
49 class GroupPrivateMessagePlugin extends Plugin
50 {
51     /**
52      * Database schema setup
53      *
54      * @see Schema
55      * @see ColumnDef
56      *
57      * @return boolean hook value
58      */
59     function onCheckSchema()
60     {
61         $schema = Schema::get();
62
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());
67         return true;
68     }
69
70     /**
71      * Load related modules when needed
72      *
73      * @param string $cls Name of the class to be loaded
74      *
75      * @return boolean hook value
76      */
77     function onAutoload($cls)
78     {
79         $dir = dirname(__FILE__);
80
81         switch ($cls)
82         {
83         case 'GroupinboxAction':
84         case 'ShowgroupmessageAction':
85         case 'NewgroupmessageAction':
86             include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
87             return false;
88         case 'Group_privacy_settings':
89         case 'Group_message':
90         case 'Group_message_profile':
91             include_once $dir . '/'.$cls.'.php';
92             return false;
93         case 'GroupMessageCommand':
94         case 'GroupMessageList':
95         case 'GroupMessageListItem':
96         case 'GroupMessageForm':
97             include_once $dir . '/'.strtolower($cls).'.php';
98             return false;
99         default:
100             return true;
101         }
102     }
103
104     /**
105      * Map URLs to actions
106      *
107      * @param Net_URL_Mapper $m path-to-action mapper
108      *
109      * @return boolean hook value
110      */
111     function onRouterInitialized($m)
112     {
113         $m->connect('group/:nickname/inbox',
114                     array('action' => 'groupinbox'),
115                     array('nickname' => Nickname::DISPLAY_FMT));
116
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}'));
120
121         $m->connect('group/:nickname/message/new',
122                     array('action' => 'newgroupmessage'),
123                     array('nickname' => Nickname::DISPLAY_FMT));
124
125         return true;
126     }
127
128     /**
129      * Add group inbox to the menu
130      *
131      * @param Action $action The current action handler. Use this to
132      *                       do any output.
133      *
134      * @return boolean hook value; true means continue processing, false means stop.
135      *
136      * @see Action
137      */
138     function onEndGroupGroupNav($groupnav)
139     {
140         $action = $groupnav->action;
141         $group  = $groupnav->group;
142
143         $action->menuItem(common_local_url('groupinbox',
144                                            array('nickname' => $group->nickname)),
145                           // TRANS: Menu item in group page.
146                           _m('MENU','Inbox'),
147                           // TRANS: Menu title in group page.
148                           _m('Private messages for this group.'),
149                           $action->trimmed('action') == 'groupinbox',
150                           'nav_group_inbox');
151         return true;
152     }
153
154     /**
155      * Create default group privacy settings at group create time
156      *
157      * @param User_group $group Group that was just created
158      *
159      * @result boolean hook value
160      */
161     function onEndGroupSave($group)
162     {
163         $gps = new Group_privacy_settings();
164
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;
170
171         // This will throw an exception on error
172
173         $gps->insert();
174
175         return true;
176     }
177
178     /**
179      * Show group privacy controls on group edit form
180      *
181      * @param GroupEditForm $form form being shown
182      */
183     function onEndGroupEditFormData($form)
184     {
185         $gps = null;
186
187         if (!empty($form->group)) {
188             $gps = Group_privacy_settings::getKV('group_id', $form->group->id);
189         }
190
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.'),
203                              false,
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.'),
218                              false,
219                              (empty($gps)) ? Group_privacy_settings::MEMBER : $gps->allow_sender);
220         $form->out->elementEnd('li');
221         return true;
222     }
223
224     function onEndGroupSaveForm($action)
225     {
226         $gps = null;
227
228         if (!empty($action->group)) {
229             $gps = Group_privacy_settings::getKV('group_id', $action->group->id);
230         }
231
232         $orig = null;
233
234         if (empty($gps)) {
235             $gps = new Group_privacy_settings();
236             $gps->group_id = $action->group->id;
237         } else {
238             $orig = clone($gps);
239         }
240
241         $gps->allow_privacy = $action->trimmed('allow_privacy');
242         $gps->allow_sender  = $action->trimmed('allow_sender');
243
244         if (empty($orig)) {
245             $gps->created = common_sql_now();
246             $gps->insert();
247         } else {
248             $gps->update($orig);
249         }
250
251         return true;
252     }
253
254     /**
255      * Overload 'd' command to send private messages to groups.
256      *
257      * 'd !group word word word' will send the private message
258      * 'word word word' to the group 'group'.
259      *
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.
264      *
265      * @return boolean hook value
266      */
267     function onStartIntepretCommand($cmd, $arg, $user, &$result)
268     {
269         if ($cmd == 'd' || $cmd == 'dm') {
270
271             $this->debug('Got a d command');
272
273             // Break off the first word as the address
274
275             $pieces = explode(' ', $arg, 2);
276
277             if (count($pieces) == 1) {
278                 $pieces[] = null;
279             }
280
281             list($addr, $msg) = $pieces;
282
283             if (!empty($addr) && $addr[0] == '!') {
284                 $result = new GroupMessageCommand($user, substr($addr, 1), $msg);
285                 Event::handle('EndInterpretCommand', array($cmd, $arg, $user, $result));
286                 return false;
287             }
288         }
289
290         return true;
291     }
292
293     /**
294      * To add a "Message" button to the group profile page
295      *
296      * @param Widget     $widget The showgroup action being shown
297      * @param User_group $group  The current group
298      *
299      * @return boolean hook value
300      */
301     function onEndGroupActionsList($widget, $group)
302     {
303         $cur = common_current_user();
304         $action = $widget->out;
305
306         if (empty($cur)) {
307             return true;
308         }
309
310         try {
311             Group_privacy_settings::ensurePost($cur, $group);
312         } catch (Exception $e) {
313             return true;
314         }
315
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;
324         // $form->show();
325         $action->elementEnd('li');
326         return true;
327     }
328
329     /**
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.
333      *
334      * @param
335      */
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
341
342         $count = preg_match_all('/(?:^|\s)!(' . Nickname::DISPLAY_FMT . ')/',
343                                 strtolower($notice->content),
344                                 $match);
345
346         $groups = array();
347         $ignored = array();
348
349         $forcePrivate = false;
350         $profile = $notice->getProfile();
351
352         if ($count > 0) {
353             /* Add them to the database */
354
355             foreach (array_unique($match[1]) as $nickname) {
356                 $group = User_group::getForNickname($nickname, $profile);
357
358                 if (empty($group)) {
359                     continue;
360                 }
361
362                 $gps = Group_privacy_settings::forGroup($group);
363
364                 switch ($gps->allow_privacy) {
365                 case Group_privacy_settings::ALWAYS:
366                     $forcePrivate = true;
367                     // fall through
368                 case Group_privacy_settings::SOMETIMES:
369                     $groups[] = $group;
370                     break;
371                 case Group_privacy_settings::NEVER:
372                     $ignored[] = $group;
373                     break;
374                 }
375             }
376
377             if ($forcePrivate) {
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.");
382                 }
383
384                 $user = User::getKV('id', $notice->profile_id);
385
386                 if (empty($user)) {
387                     common_log(LOG_WARNING,
388                                "Notice forced to group direct message ".
389                                "but profile ".$notice->profile_id." is not a local user.");
390                 } else {
391                     foreach ($groups as $group) {
392                         Group_message::send($user, $group, $notice->content);
393                     }
394                 }
395
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.')),
400                                           200);
401             }
402         }
403
404         return true;
405     }
406
407     /**
408      * Show an indicator that the group is (essentially) private on the group page
409      *
410      * @param Action     $action The action being shown
411      * @param User_group $group  The group being shown
412      *
413      * @return boolean hook value
414      */
415     function onEndGroupProfileElements($action, $group)
416     {
417         $gps = Group_privacy_settings::forGroup($group);
418
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'));
422         }
423
424         return true;
425     }
426
427     function onStartShowExportData($action)
428     {
429         if ($action instanceof ShowgroupAction) {
430             $gps = Group_privacy_settings::forGroup($action->group);
431
432             if ($gps->allow_privacy == Group_privacy_settings::ALWAYS) {
433                 return false;
434             }
435         }
436         return true;
437     }
438
439     function onPluginVersion(&$versions)
440     {
441         $versions[] = array('name' => 'GroupPrivateMessage',
442                             'version' => STATUSNET_VERSION,
443                             'author' => 'Evan Prodromou',
444                             'homepage' => 'http://status.net/wiki/Plugin:GroupPrivateMessage',
445                             'rawdescription' =>
446                             // TRANS: Plugin description.
447                             _m('Allow posting private messages to groups.'));
448         return true;
449     }
450 }