[2] | 1 | # -*- coding: utf-8 -*-
|
---|
| 2 |
|
---|
[20] | 3 | """
|
---|
| 4 | The mailjam project - daemon.py
|
---|
| 5 |
|
---|
| 6 | This file is released under the BSD license, see LICENSE for
|
---|
| 7 | more information.
|
---|
| 8 |
|
---|
| 9 | Francisco de Borja Lopez Rio - <borja@codigo23.net>
|
---|
| 10 | Soluciones Informaticas Codigo23 S.L.U. - http://codigo23.net
|
---|
| 11 | """
|
---|
| 12 |
|
---|
[6] | 13 | import os, inspect, logging
|
---|
| 14 | from SimpleXMLRPCServer import SimpleXMLRPCServer, list_public_methods
|
---|
[2] | 15 |
|
---|
[16] | 16 | from mailjam import config
|
---|
| 17 | from mailjam.models import Member, MailingList
|
---|
| 18 | from mailjam.storage import JsonStorage as Storage
|
---|
[2] | 19 |
|
---|
| 20 |
|
---|
[16] | 21 | class Mailjam():
|
---|
[2] | 22 |
|
---|
| 23 | def __init__(self, configfile=None):
|
---|
[20] | 24 | self.configfile=configfile
|
---|
[9] | 25 | self.storage_config = config.get_config_parameters('storage',
|
---|
| 26 | configfile)
|
---|
| 27 | self.archive_config = config.get_config_parameters('archive',
|
---|
| 28 | configfile)
|
---|
[2] | 29 |
|
---|
| 30 | # lists were the currently managed mailing lists information is going
|
---|
| 31 | # to be saved
|
---|
| 32 | self.mailings = {}
|
---|
| 33 | self.mailings_addresses = []
|
---|
| 34 |
|
---|
| 35 | # the files were internal information (like active mailing lists,
|
---|
| 36 | # members, etc) is saved
|
---|
[9] | 37 | self.dbs = {'mailings': Storage(self.storage_config['lists_db']),
|
---|
| 38 | 'members': Storage(self.storage_config['members_db'])}
|
---|
[2] | 39 |
|
---|
| 40 | def save(self):
|
---|
| 41 | """
|
---|
| 42 | Save all the current managed data to disk
|
---|
| 43 | """
|
---|
| 44 | if self.mailings:
|
---|
[6] | 45 | # Save the config file from where we can reload information about
|
---|
[16] | 46 | # the mailing lists managed by this mailjam instance
|
---|
[2] | 47 | self.dbs['mailings'].write(self.mailings_addresses)
|
---|
| 48 | # Save each mailing list data into its separated persistence file
|
---|
| 49 | for m in self.mailings.keys():
|
---|
| 50 | self.mailings[m].save()
|
---|
[5] | 51 | return True
|
---|
| 52 | return False
|
---|
[2] | 53 |
|
---|
| 54 | def load(self):
|
---|
| 55 | """
|
---|
| 56 | Load all data from the storage files
|
---|
| 57 | """
|
---|
| 58 | if self.dbs['mailings'].exists():
|
---|
| 59 | # load the list of managed mailing lists
|
---|
| 60 | # FIXME: This is quite naive, we do not perform any check here after
|
---|
| 61 | # loading the data from the json file, which can be modified by
|
---|
| 62 | # untrustred users.
|
---|
| 63 | self.mailings_addresses = self.dbs['mailings'].read()
|
---|
| 64 |
|
---|
| 65 | # now load all the mailing objects:
|
---|
| 66 | for address in self.mailings_addresses:
|
---|
[20] | 67 | mailing = MailingList(name=address, address=address,
|
---|
| 68 | configfile=self.configfile)
|
---|
[2] | 69 | mailing.load()
|
---|
| 70 | self.mailings[address] = mailing
|
---|
[5] | 71 | return True
|
---|
| 72 | return False
|
---|
[2] | 73 |
|
---|
[5] | 74 | def clear(self):
|
---|
| 75 | """
|
---|
| 76 | Delete all stored data from disk (useful for testing).
|
---|
| 77 | DANGER: Calling this method will remove all data from disk, leaving the
|
---|
[16] | 78 | mailjam instance with no persistence data, if the mailjam process die,
|
---|
[5] | 79 | before another .save() call is made, all data will be lost.
|
---|
| 80 | """
|
---|
| 81 | if self.dbs['mailings'].exists():
|
---|
| 82 | # We do not delete each mailing list file, but only the file
|
---|
| 83 | # containing the list of existing mailing lists
|
---|
| 84 | self.dbs['mailings'].delete()
|
---|
| 85 | return True
|
---|
| 86 | return False
|
---|
| 87 |
|
---|
| 88 | def add_mailing_list(self, info={}):
|
---|
[2] | 89 | """
|
---|
[16] | 90 | Add a new mailing list to this mailjam instance. expects one parameter,
|
---|
[5] | 91 | info, which is a dictionary that should contain, at least, the
|
---|
[2] | 92 | following keys:
|
---|
| 93 |
|
---|
| 94 | - name: (string) the name we will give to the list
|
---|
| 95 | - address: (string) the email address of the list
|
---|
| 96 | - members: (list) a list of email adddress of the list members
|
---|
| 97 |
|
---|
| 98 | """
|
---|
[5] | 99 | if not isinstance(info, dict):
|
---|
| 100 | raise TypeError(info, ' is not a valid dictionary')
|
---|
[2] | 101 |
|
---|
[5] | 102 | if 'name' not in info.keys() or \
|
---|
| 103 | 'address' not in info.keys() or \
|
---|
| 104 | 'members' not in info.keys() or \
|
---|
[10] | 105 | 'configfile' not in info.keys():
|
---|
[5] | 106 | raise ValueError(info, ' does not seem to be a valid configuration')
|
---|
[2] | 107 |
|
---|
[5] | 108 | if info['address'] in self.mailings_addresses:
|
---|
| 109 | raise IndexError(info['address'],
|
---|
[16] | 110 | ' has been already added to mailjam')
|
---|
[2] | 111 |
|
---|
[5] | 112 | mailing = MailingList(info['name'], info['address'],
|
---|
[10] | 113 | info['members'], info['configfile'])
|
---|
[2] | 114 | self.mailings[mailing.address] = mailing
|
---|
| 115 | self.mailings_addresses.append(mailing.address)
|
---|
| 116 | # After adding new mailings, save them to disk
|
---|
| 117 | self.save()
|
---|
[5] | 118 | return True
|
---|
[2] | 119 |
|
---|
| 120 | def add_mailing_member(self, member_addr=None, list_addr=None):
|
---|
| 121 | """
|
---|
| 122 | Add a new member for the mailing list represented by list_addr (a string
|
---|
[16] | 123 | containing the email address of any mailing list managed by this mailjam
|
---|
[2] | 124 | instance). member_addr is a string representing the email address of the
|
---|
| 125 | new member
|
---|
| 126 | """
|
---|
| 127 |
|
---|
| 128 | if not member_addr:
|
---|
| 129 | raise ValueError(member_addr, 'missing member address')
|
---|
| 130 |
|
---|
| 131 | if not list_addr:
|
---|
| 132 | raise ValueError(list_addr, 'missing list address')
|
---|
| 133 |
|
---|
| 134 | if list_addr not in self.mailings_addresses:
|
---|
| 135 | # FIXME: Perhaps we should add it, perhaps not (mispelled address?)
|
---|
| 136 | raise IndexError(list_addr, ' is not a valid mailing list')
|
---|
| 137 |
|
---|
[5] | 138 | added = self.mailings[list_addr].add_member_by_address(member_addr)
|
---|
| 139 | if added:
|
---|
| 140 | self.save()
|
---|
| 141 | return added
|
---|
[6] | 142 |
|
---|
| 143 |
|
---|
[16] | 144 | class MailjamXMLRPC():
|
---|
[6] | 145 | """
|
---|
| 146 | This class is a wrapper we will use to limit the methods that will be
|
---|
| 147 | published through the XMLRPC link. Only the methods from this class
|
---|
| 148 | will be available through that link.
|
---|
| 149 |
|
---|
| 150 | As we use dotted names to separate xmlrpc-exported methods into different
|
---|
| 151 | namespaces, this class contains nothing, it will be used only for
|
---|
| 152 | method-registering purposes. The MailingListXMLRPC and MemberXMLRPC classes
|
---|
| 153 | contain the actual methods that are published.
|
---|
| 154 |
|
---|
| 155 | More information on this approach here:
|
---|
[2] | 156 |
|
---|
[6] | 157 | http://www.doughellmann.com/PyMOTW/SimpleXMLRPCServer/#exposing-methods-of-objects
|
---|
| 158 | """
|
---|
| 159 |
|
---|
[20] | 160 | def __init__(self, configfile=None):
|
---|
| 161 | self.configfile = configfile
|
---|
| 162 |
|
---|
[6] | 163 | def _listMethods(self):
|
---|
[7] | 164 | public_methods = []
|
---|
[23] | 165 | public_methods += ['lists.'+i for i in dir(MailingListXMLRPC) \
|
---|
| 166 | if '_' not in i]
|
---|
| 167 | public_methods += ['members.'+i for i in dir(MemberXMLRPC) \
|
---|
| 168 | if '_' not in i]
|
---|
[7] | 169 | return public_methods
|
---|
[6] | 170 |
|
---|
| 171 | def _methodHelp(self, method):
|
---|
| 172 | f = getattr(self, method)
|
---|
| 173 | return inspect.getdoc(f)
|
---|
| 174 |
|
---|
| 175 |
|
---|
| 176 | class MailingListXMLRPC():
|
---|
[20] | 177 | def __init__(self, configfile=None):
|
---|
| 178 | self.mailjam = Mailjam(configfile=configfile)
|
---|
[16] | 179 | self.mailjam.load()
|
---|
[6] | 180 | def add(self, info={}):
|
---|
[23] | 181 | try:
|
---|
| 182 | self.mailjam.add_mailing_list(info)
|
---|
| 183 | except IndexError, e:
|
---|
| 184 | return str(e)
|
---|
| 185 | return 'Added mailing list ' + info['address']
|
---|
[7] | 186 | def addresses(self):
|
---|
[23] | 187 | return self.mailjam.mailings_addresses
|
---|
[24] | 188 | def info(self, address):
|
---|
| 189 | if address not in self.mailjam.mailings_addresses:
|
---|
| 190 | return 'Mailing list not found - ' + address
|
---|
| 191 | return self.mailjam.mailings[address].info()
|
---|
[2] | 192 |
|
---|
| 193 |
|
---|
[6] | 194 | class MemberXMLRPC():
|
---|
[20] | 195 | def __init__(self, configfile=None):
|
---|
| 196 | self.mailjam = Mailjam(configfile=configfile)
|
---|
[16] | 197 | self.mailjam.load()
|
---|
[6] | 198 | def add(self, member_addr=None, list_addr=None):
|
---|
[31] | 199 | # reload data to ensure we have recently added lists available
|
---|
| 200 | self.mailjam.load()
|
---|
[23] | 201 | try:
|
---|
| 202 | self.mailjam.add_mailing_member(member_addr, list_addr)
|
---|
| 203 | except IndexError, e:
|
---|
| 204 | return str(e)
|
---|
| 205 | return 'Added member ' + member_addr + ' to ' + list_addr
|
---|
[13] | 206 | def list(self, mailing):
|
---|
[16] | 207 | if mailing in self.mailjam.mailings_addresses:
|
---|
| 208 | return self.mailjam.mailings[mailing].members_addresses()
|
---|
[25] | 209 | return []
|
---|
[2] | 210 |
|
---|
[13] | 211 |
|
---|
[16] | 212 | class MailjamDaemon():
|
---|
[6] | 213 | def __init__(self, configfile=None):
|
---|
[20] | 214 | self.configfile = configfile
|
---|
[9] | 215 | self.config = config.get_config_parameters('xmlrpc_server', configfile)
|
---|
| 216 | self.address = self.config.get('address', 'localhost')
|
---|
| 217 | self.port = int(self.config.get('port', 9876))
|
---|
| 218 | self.logfile = self.config.get('logfile',
|
---|
| 219 | os.path.join(os.path.dirname(__file__),
|
---|
| 220 | 'server.log'))
|
---|
[6] | 221 | logging.basicConfig(filename=self.logfile, level=logging.DEBUG)
|
---|
| 222 | self.server = None
|
---|
| 223 | self.ready_to_serve = False
|
---|
| 224 |
|
---|
| 225 | def create_server(self):
|
---|
| 226 | """
|
---|
| 227 | If there is no server initialized in self.server, create an instance
|
---|
| 228 | of SimpleXMLRPCServer in that attribute. If there is already a server
|
---|
| 229 | initialized there, simply return True
|
---|
[7] | 230 | """
|
---|
[6] | 231 | if not self.server:
|
---|
[7] | 232 | msg = 'Creating XMLRPC server object on {}:{}'.format(self.address,
|
---|
| 233 | self.port)
|
---|
[6] | 234 | logging.info(msg)
|
---|
[7] | 235 | self.server = SimpleXMLRPCServer((self.address, self.port),
|
---|
| 236 | allow_none=True,
|
---|
[33] | 237 | logRequests=True)
|
---|
[6] | 238 | self.server.register_introspection_functions()
|
---|
| 239 | return True
|
---|
[2] | 240 |
|
---|
[6] | 241 | def add_methods(self):
|
---|
| 242 | """
|
---|
| 243 | Check if there is an initialized server (initialize it if there is none)
|
---|
[16] | 244 | and then register all the Mailjam public methods to be served through
|
---|
[6] | 245 | the xml-rpc link
|
---|
| 246 |
|
---|
| 247 | Once the methods are registered set self.ready_to_serve to True
|
---|
| 248 | """
|
---|
| 249 | if not self.server:
|
---|
| 250 | # ensure there is an XMLRPC server initialized
|
---|
| 251 | self.create_server()
|
---|
| 252 | msg = 'Registering public methods'
|
---|
| 253 | logging.info(msg)
|
---|
[20] | 254 | root = MailjamXMLRPC(self.configfile)
|
---|
| 255 | root.lists = MailingListXMLRPC(self.configfile)
|
---|
| 256 | root.members = MemberXMLRPC(self.configfile)
|
---|
[6] | 257 | self.server.register_instance(root, allow_dotted_names=True)
|
---|
| 258 | self.ready_to_serve = True
|
---|
[7] | 259 | return self.ready_to_serve
|
---|
[6] | 260 |
|
---|
| 261 | def run(self):
|
---|
| 262 | """
|
---|
| 263 | Run the xmlrpc daemon. If self.ready_to_serve is False, call
|
---|
| 264 | self.add_methods, which will initialize the server and will register all
|
---|
| 265 | the public methods into that server
|
---|
| 266 | """
|
---|
| 267 | if not self.ready_to_serve:
|
---|
| 268 | self.add_methods()
|
---|
[7] | 269 | msg = 'Starting XMLRPC server on {}:{}'.format(self.address,
|
---|
| 270 | self.port)
|
---|
[6] | 271 | logging.info(msg)
|
---|
[7] | 272 | try:
|
---|
| 273 | self.server.serve_forever()
|
---|
| 274 | except KeyboardInterrupt:
|
---|
| 275 | msg = 'Stopping server'
|
---|
| 276 | logging.info(msg)
|
---|