DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Subscribe Drupal Users To Mailman List

04.07.2009
| 2650 views |
  • submit to reddit
        redacted code which goes through a Drupal database subscribing all new users to a Mailman list on a separate server.  Relies upon Drupal Drush module and Mailman XMLRPC2 patch.

import os
import xmlrpclib

proxy = xmlrpclib.ServerProxy("https://lists.mydomain.org/mailman/RPC2")

def nicepass(alpha=6,numeric=2):
    """
    returns a human-readble password (say rol86din instead of 
    a difficult to remember K8Yn9muL ) 
    """
    import string
    import random
    vowels = ['a','e','i','o','u']
    consonants = [a for a in string.ascii_lowercase if a not in vowels]
    digits = string.digits
    
    ####utility functions
    def a_part(slen):
        ret = ''
        for i in range(slen):                   
            if i%2 ==0:
                randid = random.randint(0,20) #number of consonants
                ret += consonants[randid]
            else:
                randid = random.randint(0,4) #number of vowels
                ret += vowels[randid]
        return ret
    
    def n_part(slen):
        ret = ''
        for i in range(slen):
            randid = random.randint(0,9) #number of digits
            ret += digits[randid]
        return ret
        
    ####        
    fpl = alpha/2               
    if alpha % 2 :
        fpl = int(alpha/2) + 1                                  
    lpl = alpha - fpl   
    
    start = a_part(fpl)
    mid = n_part(numeric)
    end = a_part(lpl)
    
    return "%s%s%s" % (start,mid,end)

if __name__ == "__main__":
        os.system(" cd /home/myuser/public_html/ && /usr/local/php5/bin/php drush sql query \"select mail from users
 where status=1 and unix_timestamp(date_sub(now(), interval 24 hour)) <= created\" | sed 1d > /home/qwep/newusers")
        for line in open("/home/myuser/newusers"):
                print line[:-1]
                try:
                        proxy.Mailman.addMember("oursite-announce", "mypassword", line[:-1], "auto-subscribed user", nicepass(6,2), 'false', 'false')
                except xmlrpclib.Fault:
                        print "unexpected error, probably already a member"
        try:
                os.remove("/home/myuser/newusers")
        except OSError:
                raise OSError("Couldn't remove the working file")