source: mailjam/mailjam/cli.py@ 24:dc78a59d357e

Last change on this file since 24:dc78a59d357e was 24:dc78a59d357e, checked in by Borja Lopez <borja@…>, 12 years ago

Added a new XMLRPC method to the mailjam server, lists.info(), which returns
detailed information about a given mailing lists, provided an address.

Added support listing and getting detailed information of mailing lists to
the CLI client

File size: 6.0 KB
Line 
1# -*- coding: utf-8 -*-
2
3"""
4The mailjam project - cli.py
5
6This file is released under the BSD license, see LICENSE for
7more information.
8
9Francisco de Borja Lopez Rio - <borja@codigo23.net>
10Soluciones Informaticas Codigo23 S.L.U. - http://codigo23.net
11"""
12
13import os, cmd, xmlrpclib
14from mailjam.config import CLIClientConfig
15
16class CLIClient(cmd.Cmd):
17
18 def __init__(self, configfile=None, *args, **kwargs):
19 cmd.Cmd.__init__(self)
20 cli_config = CLIClientConfig(configfile=configfile)
21 cli_config.load()
22 self.config = cli_config.config
23 self.rpc = xmlrpclib.ServerProxy(self.config['server']['uri'])
24 self.prompt = "mailjam-cli > "
25 self.intro = "Welcome to Mailjam's CLI client - type help for a list of commands"
26 self._history = []
27
28 def do_history(self, line):
29 """
30 history
31 Show a list of recently executed commands
32
33 Alias: h
34 """
35 if not self._history:
36 print 'No history data available'
37 else:
38 for i in self._history:
39 print self._history.index(i), ' - ', i
40
41 def do_h(self, line):
42 """
43 h
44 Alias for the history command. To learn more type:
45
46 help history
47 """
48 self.do_history(line)
49
50 def do_add_mailing_list(self, line):
51 """
52 add_mailing_list [name] address
53 Add a new mailing list to the remote mailjam server. You have to
54 provide a valid email address for the mailing list to be added. If no
55 name is provided, the address itself will be used.
56
57 Alias: aml
58 """
59 if not line:
60 print 'usage: add_mailing_list [name] address'
61 else:
62 params = line.split(' ')
63 address = params.pop()
64 if not params:
65 name = address
66 else:
67 name = ' '.join(params)
68 info = {'name': name, 'address': address, 'members': {},
69 'configfile': self.config['server']['configfile']}
70 result = self.rpc.lists.add(info)
71 print result
72
73 def do_aml(self, line):
74 """
75 aml [name] address
76 Alias for the add_mailing_list command. To learn more type:
77
78 help add_mailing_list
79 """
80 self.do_add_mailing_list(line)
81
82 def do_list_mailing_lists(self, line):
83 """
84 list_mailing_lists [text]
85 List the mailing lists available in the remote mailjam server.
86 If text is provided, only mailing lists whose address contains such
87 text will be shown. Otherwise all the available email addresses are
88 listed.
89
90 Alias: lml
91 """
92 addresses = self.rpc.lists.addresses()
93 if not line:
94 for address in addresses:
95 print address
96 else:
97 for address in addresses:
98 if line in address:
99 print address
100
101 def do_lml(self, line):
102 """
103 lml [text]
104 Alias for the list_mailing_lists command. To learn more type:
105
106 help list_mailing_lists
107 """
108 self.do_list_mailing_lists(line)
109
110 def do_show_mailing_list(self, line):
111 """
112 show_mailing_list address
113 Show detailed information of the mailing list represented by address.
114
115 Alias: sml
116 """
117 if not line:
118 print 'usage: show_mailing_list address'
119 else:
120 result = self.rpc.lists.info(line)
121 if isinstance(result, dict):
122 print 'Mailing list information:'
123 print ' - name:', result['name']
124 print ' - address:', result['address']
125 print ' - members:', result['members']
126 else:
127 print result
128
129 def do_sml(self, line):
130 """
131 sml address
132 Alias for the show_mailing_list command. To learn more type:
133
134 help show_mailing_list
135 """
136 self.do_show_mailing_list(line)
137
138 def do_add_mailing_list_member(self, line):
139 """
140 add_mailing_list_member member_address list_address
141 Add a new member to a given mailing list. You have to provide valid
142 email addresses for both the member and the list. The mailing list
143 must be handled by the remote mailjam server for this operation to
144 work.
145
146 Alias: amlm
147 """
148 pass
149
150 def do_show_mailing_list_members(self, line):
151 """
152 show_mailing_list_members address
153 """
154 pass
155
156 def do_EOF(self, line):
157 return True
158
159 def precmd(self, line):
160 # save the commands to the history, if it is enabled in the config file
161 if self.config['history']['enabled']:
162 self._history.append(line)
163 return line
164
165 def postloop(self):
166 # save history to a file, if it is enabled in the config file.
167 if self.config['history']['enabled']:
168 history_file_name = self.config['history']['path']
169 if '~' in history_file_name:
170 # we have to replace that with the proper home path
171 # FIXME: we should call some "sanitize" function here, to
172 # get rid of dangerous paths in the file name
173 home = os.path.expanduser("~")
174 history_file_name = history_file_name.replace('~', home)
175 if not os.path.exists(history_file_name):
176 try:
177 os.makedirs(os.path.dirname(history_file_name))
178 except OSError, e:
179 # If the dir already exists do not complain, if it is
180 # any other error, raise the exception
181 if e.errno != errno.EEXIST:
182 raise
183 history_file = open(history_file_name, 'a')
184 for i in self._history:
185 if 'EOF' not in i:
186 history_file.write(i+'\n')
187 history_file.close()
188 print 'Bye!'
189
190 def emptyline(self):
191 pass
Note: See TracBrowser for help on using the repository browser.