More functions for MailingList
[platal.git] / include / mailinglist.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 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 General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 // {{{ class MailingList
23
24 class MailingList
25 {
26 public $address; // Fully qualified address of the list
27 public $mbox; // mailbox for the list
28 public $domain; // domain for the list
29 protected $mmclient; // The XML-RPC client for Mailman requests
30
31 public function __construct($mbox, $domain, $user, $pass='')
32 {
33 $this->mbox = $mbox;
34 $this->domain = $domain;
35 $this->address = "$mbox@$domain";
36
37 if ($user instanceof PlUser) {
38 $this->mmclient = new MMList($user, $this->domain);
39 } else {
40 $this->mmclient = new MMList($user, $pass, $this->domain);
41 }
42 }
43
44 /** Instantiate a MailingList from its address.
45 *
46 * $user and $pass are connection parameters for MailMan.
47 */
48 public static function fromAddress($address, $user, $pass='')
49 {
50 if (strstr($address, '@') !== false) {
51 list($mbox, $domain) = explode('@', $address);
52 } else {
53 global $globals;
54 $mbox = $address;
55 $domain = $globals->mail->domain;
56 }
57 return new MailingList($mbox, $domain, $user, $pass);
58 }
59
60 /** Retrieve the MailingList associated with a given promo.
61 *
62 * $user and $pass are connection parameters for MailMan.
63 */
64 public static function promo($promo, $user, $pass='')
65 {
66 global $globals;
67 $mail_domain = $globals->mail->domain;
68 return new MailingList('promo', "$promo.$mail_domain", $user, $pass);
69 }
70
71 const KIND_BOUNCE = 'bounces';
72 const KIND_OWNER = 'owner';
73 public function getAddress($kind)
74 {
75 return $this->mbox . '-' . $kind . '@' . $this->domain;
76 }
77
78 /** Subscribe the current user to the list
79 */
80 public function subscribe()
81 {
82 return $this->mmclient->subscribe($this->mbox);
83 }
84
85 /** Subscribe a batch of users to the list
86 */
87 public function subscribeBulk($members)
88 {
89 return $this->mmclient->mass_subscribe($this->mbox, $members);
90 }
91
92 /** Unsubscribe the current user from the list
93 */
94 public function unsubscribe()
95 {
96 return $this->mmclient->unsubscribe($this->mbox);
97 }
98
99 /** Unsubscribe a batch of users from the list
100 */
101 public function unsubscribeBulk($members)
102 {
103 return $this->mmclient->mass_unsubscribe($this->mbox, $members);
104 }
105
106 /** Retrieve owners for the list.
107 *
108 * TODO: document the return type
109 */
110 public function getOwners()
111 {
112 return $this->mmclient->get_owners($this->mbox);
113 }
114
115 /** Add an owner to the list
116 */
117 public function addOwner($email)
118 {
119 return $this->mmclient->add_owner($this->mbox, $email);
120 }
121
122 /** Remove an owner from the list
123 */
124 public function removeOwner($email)
125 {
126 return $this->mmclient->del_owner($this->mbox, $email);
127 }
128
129 /** Retrieve members of the list.
130 *
131 * TODO: document the return type
132 */
133 public function getMembers()
134 {
135 return $this->mmclient->get_members($this->mbox);
136 }
137
138 /** Retrieve a subset of list members.
139 *
140 * TODO: document the return type
141 */
142 public function getMembersLimit($page, $number_per_page)
143 {
144 return $this->mmclient->get_members_limit($this->mbox, $page, $number_per_page);
145 }
146
147 /** Fetch pending list operations.
148 *
149 * TODO: document the return type
150 */
151 public function getPendingOps()
152 {
153 return $this->mmclient->get_pending_ops($this->mbox);
154 }
155
156 const REQ_REJECT = 2;
157 const REQ_SUBSCRIBE = 4;
158
159 /** Handle a mailing list request
160 */
161 public function handleRequest($kind, $value, $comment='')
162 {
163 return $this->mmclient->handle_request($this->mbox, $value, $kind,
164 utf8_decode($comment));
165 }
166
167 /** Retrieve the current status of a pending subscription request
168 */
169 public function getPendingSubscription($email)
170 {
171 return $this->mmclient->get_pending_sub($this->mbox, $email);
172 }
173
174 /** Create a list
175 */
176 public function create($description, $advertise,
177 $moderation_level, $subscription_level, $owners, $members)
178 {
179 return $this->mmclient->create_list($this->mbox, utf8_decode($description),
180 $advertise, $moderation_level, $subscription_level,
181 $owners, $members);
182 }
183
184 /** Delete a list
185 */
186 public function delete($remove_archives=false)
187 {
188 return $this->mmclient->delete_list($this->mbox, $remove_archives);
189 }
190
191 /** Set antispam level.
192 */
193 public function setBogoLevel($level)
194 {
195 return $this->mmclient->set_bogo_level($this->mbox);
196 }
197
198 /** Get antispam level.
199 *
200 * @return int
201 */
202 public function getBogoLevel()
203 {
204 $bogo = $this->mmclient->get_bogo_level($this->mbox);
205 return $bogo;
206 }
207
208 /** Set public options.
209 *
210 * @param $options array
211 */
212 public function setOwnerOptions($options)
213 {
214 return $this->mmclient->set_owner_options($this->mbox, $options);
215 }
216
217 /** Retrieve owner options
218 *
219 * @return array
220 */
221 public function getOwnerOptions()
222 {
223 return $this->mmclient->get_owner_options($this->mbox);
224 }
225
226 /** Set admin options.
227 *
228 * @param $options array
229 */
230 public function setAdminOptions($options)
231 {
232 return $this->mmclient->set_admin_options($this->mbox, $options);
233 }
234
235 /** Retrieve admin options
236 *
237 * @return array
238 */
239 public function getAdminOptions()
240 {
241 return $this->mmclient->get_admin_options($this->mbox);
242 }
243
244 /** Check options, optionnally fixing them.
245 */
246 public function checkOptions($fix=false)
247 {
248 return $this->mmclient->check_options($this->mbox, $fix);
249 }
250
251 /** Add an email to the list of whitelisted senders
252 */
253 public function whitelistAdd($email)
254 {
255 return $this->mmclient->add_to_wl($this->mbox, $email);
256 }
257
258 /** Remove an email from the list of whitelisted senders
259 */
260 public function whitelistRemove($email)
261 {
262 return $this->mmclient->del_from_wl($this->mbox, $email);
263 }
264 }
265
266 // }}}
267
268 // vim:set et sw=4 sts=4 sws=4 enc=utf-8:
269 ?>