Add new MailingList class
authorRaphaël Barrois <raphael.barrois@polytechnique.org>
Sun, 4 Nov 2012 19:17:40 +0000 (20:17 +0100)
committerRaphaël Barrois <raphael.barrois@polytechnique.org>
Sat, 10 Nov 2012 16:04:43 +0000 (17:04 +0100)
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
include/mailinglist.inc.php [new file with mode: 0644]

diff --git a/include/mailinglist.inc.php b/include/mailinglist.inc.php
new file mode 100644 (file)
index 0000000..c78b751
--- /dev/null
@@ -0,0 +1,126 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+// {{{ class MailingList
+
+class MailingList
+{
+    public $address;        // Fully qualified address of the list
+    public $mbox;           // mailbox for the list
+    public $domain;         // domain for the list
+    protected $mmclient;    // The XML-RPC client for Mailman requests
+
+    public function __construct($mbox, $domain, $user, $pass='')
+    {
+        $this->mbox = $mbox;
+        $this->domain = $domain;
+        $this->address = "$mbox@$domain";
+
+        if ($user instanceof PlUser) {
+            $this->mmclient = new MMList($user, $this->domain);
+        } else {
+            $this->mmclient = new MMList($user, $pass, $this->domain);
+        }
+    }
+
+    /** Instantiate a MailingList from its address.
+     *
+     * $user and $pass are connection parameters for MailMan.
+     */
+    public static function fromAddress($address, $user, $pass='')
+    {
+        if (strstr($address, '@') !== false) {
+            list($mbox, $domain) = explode('@', $address);
+        } else {
+            global $globals;
+            $mbox = $address;
+            $domain = $globals->mail->domain;
+        }
+        return new MailingList($mbox, $domain, $user, $pass);
+    }
+
+    /** Retrieve the MailingList associated with a given promo.
+     *
+     * $user and $pass are connection parameters for MailMan.
+     */
+    public static function promo($promo, $user, $pass='')
+    {
+        global $globals;
+        $mail_domain = $globals->mail->domain;
+        return new MailingList('promo', "$promo.$mail_domain", $user, $pass);
+    }
+
+    /** Subscribe the current user to the list
+     */
+    public function subscribe()
+    {
+        return $this->mmclient->subscribe($this->mbox);
+    }
+
+    /** Unsubscribe the current user from the list
+     */
+    public function unsubscribe()
+    {
+        return $this->mmclient->unsubscribe($this->mbox);
+    }
+
+    /** Retrieve owners for the list.
+     *
+     * TODO: document the return type
+     */
+    public function getOwners()
+    {
+        return $this->mmclient->get_owners($this->mbox);
+    }
+
+    /** Retrieve members of the list.
+     *
+     * TODO: document the return type
+     */
+    public function getMembers()
+    {
+        return $this->mmclient->get_members($this->mbox);
+    }
+
+    /** Retrieve a subset of list members.
+     *
+     * TODO: document the return type
+     */
+    public function getMembersLimit($page, $number_per_page)
+    {
+        return $this->mmclient->get_members_limit($this->mbox, $page, $number_per_page);
+    }
+
+    /** Create a list
+     */
+    public function create($description, $advertise,
+        $moderation_level, $subscription_level, $owners, $members)
+    {
+        return $this->mmclient->create_list($this->mbox, utf8_decode($description),
+            $advertise, $moderation_level, $subscription_level,
+            $owners, $members);
+    }
+}
+
+// }}}
+
+// vim:set et sw=4 sts=4 sws=4 enc=utf-8:
+?>