]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GroupPrivateMessage/GroupPrivateMessagePlugin.php
[TRANSLATION] Update license and copyright notice in translation files
[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     const PLUGIN_VERSION = '2.0.0';
52
53     /**
54      * Database schema setup
55      *
56      * @see Schema
57      * @see ColumnDef
58      *
59      * @return boolean hook value
60      */
61     function onCheckSchema()
62     {
63         $schema = Schema::get();
64
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());
69         return true;
70     }
71
72     /**
73      * Map URLs to actions
74      *
75      * @param URLMapper $m path-to-action mapper
76      *
77      * @return boolean hook value
78      */
79     public function onRouterInitialized(URLMapper $m)
80     {
81         $m->connect('group/:nickname/inbox',
82                     array('action' => 'groupinbox'),
83                     array('nickname' => Nickname::DISPLAY_FMT));
84
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}'));
88
89         $m->connect('group/:nickname/message/new',
90                     array('action' => 'newgroupmessage'),
91                     array('nickname' => Nickname::DISPLAY_FMT));
92
93         return true;
94     }
95
96     /**
97      * Add group inbox to the menu
98      *
99      * @param Action $action The current action handler. Use this to
100      *                       do any output.
101      *
102      * @return boolean hook value; true means continue processing, false means stop.
103      *
104      * @see Action
105      */
106     function onEndGroupGroupNav(Menu $groupnav)
107     {
108         $action = $groupnav->action;
109         $group  = $groupnav->group;
110
111         $action->menuItem(common_local_url('groupinbox',
112                                            array('nickname' => $group->nickname)),
113                           // TRANS: Menu item in group page.
114                           _m('MENU','Inbox'),
115                           // TRANS: Menu title in group page.
116                           _m('Private messages for this group.'),
117                           $action->trimmed('action') == 'groupinbox',
118                           'nav_group_inbox');
119         return true;
120     }
121
122     /**
123      * Create default group privacy settings at group create time
124      *
125      * @param User_group $group Group that was just created
126      *
127      * @result boolean hook value
128      */
129     function onEndGroupSave($group)
130     {
131         $gps = new Group_privacy_settings();
132
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;
138
139         // This will throw an exception on error
140
141         $gps->insert();
142
143         return true;
144     }
145
146     /**
147      * Show group privacy controls on group edit form
148      *
149      * @param GroupEditForm $form form being shown
150      */
151     function onEndGroupEditFormData(GroupEditForm $form)
152     {
153         $gps = null;
154
155         if (!empty($form->group)) {
156             $gps = Group_privacy_settings::getKV('group_id', $form->group->id);
157         }
158
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.'),
171                              false,
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.'),
186                              false,
187                              (empty($gps)) ? Group_privacy_settings::MEMBER : $gps->allow_sender);
188         $form->out->elementEnd('li');
189         return true;
190     }
191
192     function onEndGroupSaveForm(Action $action)
193     {
194         // The Action class must contain this method
195         assert(is_callable(array($action, 'getGroup')));
196
197         $gps = null;
198
199         if ($action->getGroup() instanceof User_group) {
200             $gps = Group_privacy_settings::getKV('group_id', $action->getGroup()->id);
201         }
202
203         $orig = null;
204
205         if (empty($gps)) {
206             $gps = new Group_privacy_settings();
207             $gps->group_id = $action->getGroup()->id;
208         } else {
209             $orig = clone($gps);
210         }
211
212         $gps->allow_privacy = $action->trimmed('allow_privacy');
213         $gps->allow_sender  = $action->trimmed('allow_sender');
214
215         if (empty($orig)) {
216             $gps->created = common_sql_now();
217             $gps->insert();
218         } else {
219             $gps->update($orig);
220         }
221
222         return true;
223     }
224
225     /**
226      * Overload 'd' command to send private messages to groups.
227      *
228      * 'd !group word word word' will send the private message
229      * 'word word word' to the group 'group'.
230      *
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.
235      *
236      * @return boolean hook value
237      */
238     function onStartInterpretCommand($cmd, $arg, User $user, &$result)
239     {
240         if ($cmd == 'd' || $cmd == 'dm') {
241
242             $this->debug('Got a d command');
243
244             // Break off the first word as the address
245
246             $pieces = explode(' ', $arg, 2);
247
248             if (count($pieces) == 1) {
249                 $pieces[] = null;
250             }
251
252             list($addr, $msg) = $pieces;
253
254             if (!empty($addr) && $addr[0] == '!') {
255                 $result = new GroupMessageCommand($user, substr($addr, 1), $msg);
256                 Event::handle('EndInterpretCommand', array($cmd, $arg, $user, $result));
257                 return false;
258             }
259         }
260
261         return true;
262     }
263
264     /**
265      * To add a "Message" button to the group profile page
266      *
267      * @param Widget     $widget The showgroup action being shown
268      * @param User_group $group  The current group
269      *
270      * @return boolean hook value
271      */
272     function onEndGroupActionsList(Widget $widget, User_group $group)
273     {
274         $cur = common_current_user();
275         $action = $widget->out;
276
277         if (empty($cur)) {
278             return true;
279         }
280
281         try {
282             Group_privacy_settings::ensurePost($cur, $group);
283         } catch (Exception $e) {
284             return true;
285         }
286
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;
295         // $form->show();
296         $action->elementEnd('li');
297         return true;
298     }
299
300     /**
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.
304      *
305      * @param
306      */
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
312
313         $count = preg_match_all('/(?:^|\s)!(' . Nickname::DISPLAY_FMT . ')/',
314                                 strtolower($notice->content),
315                                 $match);
316
317         $groups = array();
318         $ignored = array();
319
320         $forcePrivate = false;
321         $profile = $notice->getProfile();
322
323         if ($count > 0) {
324             /* Add them to the database */
325
326             foreach (array_unique($match[1]) as $nickname) {
327                 $group = User_group::getForNickname($nickname, $profile);
328
329                 if (empty($group)) {
330                     continue;
331                 }
332
333                 $gps = Group_privacy_settings::forGroup($group);
334
335                 switch ($gps->allow_privacy) {
336                 case Group_privacy_settings::ALWAYS:
337                     $forcePrivate = true;
338                     // fall through
339                 case Group_privacy_settings::SOMETIMES:
340                     $groups[] = $group;
341                     break;
342                 case Group_privacy_settings::NEVER:
343                     $ignored[] = $group;
344                     break;
345                 }
346             }
347
348             if ($forcePrivate) {
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.");
353                 }
354
355                 $user = User::getKV('id', $notice->profile_id);
356
357                 if (empty($user)) {
358                     common_log(LOG_WARNING,
359                                "Notice forced to group direct message ".
360                                "but profile ".$notice->profile_id." is not a local user.");
361                 } else {
362                     foreach ($groups as $group) {
363                         Group_message::send($user, $group, $notice->content);
364                     }
365                 }
366
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.')),
371                                           200);
372             }
373         }
374
375         return true;
376     }
377
378     /**
379      * Show an indicator that the group is (essentially) private on the group page
380      *
381      * @param Action     $action The action being shown
382      * @param User_group $group  The group being shown
383      *
384      * @return boolean hook value
385      */
386     function onEndGroupProfileElements(Action $action, User_group $group)
387     {
388         $gps = Group_privacy_settings::forGroup($group);
389
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'));
393         }
394
395         return true;
396     }
397
398     function onStartShowExportData(Action $action)
399     {
400         if ($action instanceof ShowgroupAction) {
401             $gps = Group_privacy_settings::forGroup($action->getGroup());
402
403             if ($gps->allow_privacy == Group_privacy_settings::ALWAYS) {
404                 return false;
405             }
406         }
407         return true;
408     }
409
410     function onPluginVersion(array &$versions)
411     {
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',
416                             'rawdescription' =>
417                             // TRANS: Plugin description.
418                             _m('Allow posting private messages to groups.'));
419         return true;
420     }
421 }