# -*- coding: utf-8 -*- """ The mailjam project - cli.py This file is released under the BSD license, see LICENSE for more information. Francisco de Borja Lopez Rio - Soluciones Informaticas Codigo23 S.L.U. - http://codigo23.net """ import os, cmd, xmlrpclib from mailjam.config import CLIClientConfig class CLIClient(cmd.Cmd): def __init__(self, configfile=None, *args, **kwargs): cmd.Cmd.__init__(self) cli_config = CLIClientConfig(configfile=configfile) cli_config.load() self.config = cli_config.config self.rpc = xmlrpclib.ServerProxy(self.config['server']['uri']) self.prompt = "mailjam-cli > " self.intro = "Welcome to Mailjam's CLI client - type help for a list of commands" self._history = [] def do_history(self, line): """ history Show a list of recently executed commands Alias: h """ if not self._history: print 'No history data available' else: for i in self._history: print self._history.index(i), ' - ', i def do_h(self, line): """ h Alias for the history command. To learn more type: help history """ self.do_history(line) def do_add_mailing_list(self, line): """ add_mailing_list [name] address Add a new mailing list to the remote mailjam server. You have to provide a valid email address for the mailing list to be added. If no name is provided, the address itself will be used. Alias: aml """ if not line: print 'usage: add_mailing_list [name] address' else: params = line.split(' ') address = params.pop() if not params: name = address else: name = ' '.join(params) info = {'name': name, 'address': address, 'members': {}, 'configfile': self.config['server']['configfile']} result = self.rpc.lists.add(info) print result def do_aml(self, line): """ aml [name] address Alias for the add_mailing_list command. To learn more type: help add_mailing_list """ self.do_add_mailing_list(line) def do_list_mailing_lists(self, line): """ list_mailing_lists [text] List the mailing lists available in the remote mailjam server. If text is provided, only mailing lists whose address contains such text will be shown. Otherwise all the available email addresses are listed. Alias: lml """ addresses = self.rpc.lists.addresses() if not line: for address in addresses: print address else: for address in addresses: if line in address: print address def do_lml(self, line): """ lml [text] Alias for the list_mailing_lists command. To learn more type: help list_mailing_lists """ self.do_list_mailing_lists(line) def do_show_mailing_list(self, line): """ show_mailing_list address Show detailed information of the mailing list represented by address. Alias: sml """ if not line: print 'usage: show_mailing_list address' else: result = self.rpc.lists.info(line) if isinstance(result, dict): print 'Mailing list information:' print ' - name:', result['name'] print ' - address:', result['address'] print ' - members:', result['members'] else: print result def do_sml(self, line): """ sml address Alias for the show_mailing_list command. To learn more type: help show_mailing_list """ self.do_show_mailing_list(line) def do_add_mailing_list_member(self, line): """ add_mailing_list_member member_address list_address Add a new member to a given mailing list. You have to provide valid email addresses for both the member and the list. The mailing list must be handled by the remote mailjam server for this operation to work. Alias: amlm """ usage = 'usage: add_mailing_list_member member_address list_address' if not line: print usage else: params = line.split(' ') list_addr = params.pop() if params: member_addr = params.pop() result = self.rpc.members.add(member_addr, list_addr) print result else: # we need the member adddress too print usage def do_amlm(self, line): """ amlm member_address list_address Alias for the add_mailing_list_member command. To learn more type: help add_mailing_list_member """ self.do_add_mailing_list_member(line) def do_list_mailing_list_members(self, line): """ list_mailing_list_members address List all the members from the given list. Alias: lmlm """ if not line: print 'usage: list_mailing_list_members address' else: members = self.rpc.members.list(line) if not members: print 'No members found in list', line else: print len(members), 'member(s) found in list', line for m in members: print m def do_lmlm(self, line): """ lmlm address Alias for the list_mailing_list_members command. To learn more type: help list_mailing_lists """ self.do_list_mailing_list_members(line) def do_EOF(self, line): return True def precmd(self, line): # save the commands to the history, if it is enabled in the config file if self.config['history']['enabled']: self._history.append(line) return line def postloop(self): # save history to a file, if it is enabled in the config file. if self.config['history']['enabled']: history_file_name = self.config['history']['path'] if '~' in history_file_name: # we have to replace that with the proper home path # FIXME: we should call some "sanitize" function here, to # get rid of dangerous paths in the file name home = os.path.expanduser("~") history_file_name = history_file_name.replace('~', home) if not os.path.exists(history_file_name): try: os.makedirs(os.path.dirname(history_file_name)) except OSError, e: # If the dir already exists do not complain, if it is # any other error, raise the exception if e.errno != errno.EEXIST: raise history_file = open(history_file_name, 'a') for i in self._history: if 'EOF' not in i: history_file.write(i+'\n') history_file.close() print 'Bye!' def emptyline(self): pass