56e33355e86703ecc5d356ccf911d46a07704141
[quix0rs-gnu-social.git] / plugins / ForceGroup / ForceGroupPlugin.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /**
21  * @package ForceGroupPlugin
22  * @maintainer Brion Vibber <brion@status.net>
23  */
24
25 if (!defined('STATUSNET')) { exit(1); }
26
27 class ForceGroupPlugin extends Plugin
28 {
29     /**
30      * Members of these groups will have all their posts mirrored into
31      * the group even if they don't explicitly mention it.
32      *
33      * List by local nickname.
34      */
35     public $post = array();
36
37     /**
38      * New user registrations will automatically join these groups on
39      * registration. They're not prevented from leaving, however.
40      *
41      * List by local nickname.
42      */
43     public $join = array();
44
45     /**
46      * If poster is in one of the forced groups, make sure their notice
47      * gets saved into that group even if not explicitly mentioned.
48      *
49      * @param Notice $notice
50      * @return boolean event hook return
51      */
52     function onStartNoticeDistribute($notice)
53     {
54         $profile = $notice->getProfile();
55
56         $isRemote = !(User::getKV('id', $profile->id));
57         if ($isRemote) {
58             /*
59              * Notices from remote users on other sites
60              * will normally not end up here unless they're
61              * specifically directed here, e.g.: via explicit
62              * post to a remote (to them) group. But remote
63              * notices can also be `pulled in' as a result of
64              * local users subscribing to the remote user;
65              * from the remote user's perspective, this results
66              * in group-forcing appearing effectively random.
67              * So let's be consistent, and just never force
68              * incoming remote notices into a ForceGroup:
69              */
70             return true;
71         }
72
73         foreach ($this->post as $nickname) {
74             $group = User_group::getForNickname($nickname);
75             if ($group && $profile->isMember($group)) {
76                 $notice->addToGroupInbox($group);
77             }
78         }
79         return true;
80     }
81
82     public function onEndUserRegister(Profile $profile)
83     {
84         foreach ($this->join as $nickname) {
85             $group = User_group::getForNickname($nickname);
86             if ($group && !$profile->isMember($group)) {
87                 try {
88                     $profile->joinGroup($group);
89                 } catch (Exception $e) {
90                     // TRANS: Server exception.
91                     // TRANS: %1$s is a user nickname, %2$s is a group nickname.
92                     throw new ServerException(sprintf(_m('Could not join user %1$s to group %2$s.'),
93                                                $profile->nickname, $group->nickname));
94                 }
95             }
96         }
97     }
98
99     /**
100      * Provide plugin version information.
101      *
102      * This data is used when showing the version page.
103      *
104      * @param array &$versions array of version data arrays; see EVENTS.txt
105      *
106      * @return boolean hook value
107      */
108     function onPluginVersion(array &$versions)
109     {
110         $url = 'http://status.net/wiki/Plugin:ForceGroup';
111
112         $versions[] = array('name' => 'ForceGroup',
113             'version' => GNUSOCIAL_VERSION,
114             'author' => 'Brion Vibber',
115             'homepage' => $url,
116             'rawdescription' =>
117             // TRANS: Plugin description.
118             _m('Allows forced group memberships and forces all notices to appear in groups that users were forced in.'));
119
120         return true;
121     }
122 }