source: mailjam/mailjam/cli.py

Last change on this file was 25:536c135ab269, checked in by Borja Lopez <borja@…>, 12 years ago

Added support for adding members to a given mailing list, and to get a list
of members from a given mailing list, to the CLI client.

File size: 7.3 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 usage = 'usage: add_mailing_list_member member_address list_address'
149 if not line:
150 print usage
151 else:
152 params = line.split(' ')
153 list_addr = params.pop()
154 if params:
155 member_addr = params.pop()
156 result = self.rpc.members.add(member_addr, list_addr)
157 print result
158 else:
159 # we need the member adddress too
160 print usage
161
162 def do_amlm(self, line):
163 """
164 amlm member_address list_address
165 Alias for the add_mailing_list_member command. To learn more type:
166
167 help add_mailing_list_member
168 """
169 self.do_add_mailing_list_member(line)
170
171 def do_list_mailing_list_members(self, line):
172 """
173 list_mailing_list_members address
174 List all the members from the given list.
175
176 Alias: lmlm
177 """
178 if not line:
179 print 'usage: list_mailing_list_members address'
180 else:
181 members = self.rpc.members.list(line)
182 if not members:
183 print 'No members found in list', line
184 else:
185 print len(members), 'member(s) found in list', line
186 for m in members:
187 print m
188
189 def do_lmlm(self, line):
190 """
191 lmlm address
192 Alias for the list_mailing_list_members command. To learn more type:
193
194 help list_mailing_lists
195 """
196 self.do_list_mailing_list_members(line)
197
198 def do_EOF(self, line):
199 return True
200
201 def precmd(self, line):
202 # save the commands to the history, if it is enabled in the config file
203 if self.config['history']['enabled']:
204 self._history.append(line)
205 return line
206
207 def postloop(self):
208 # save history to a file, if it is enabled in the config file.
209 if self.config['history']['enabled']:
210 history_file_name = self.config['history']['path']
211 if '~' in history_file_name:
212 # we have to replace that with the proper home path
213 # FIXME: we should call some "sanitize" function here, to
214 # get rid of dangerous paths in the file name
215 home = os.path.expanduser("~")
216 history_file_name = history_file_name.replace('~', home)
217 if not os.path.exists(history_file_name):
218 try:
219 os.makedirs(os.path.dirname(history_file_name))
220 except OSError, e:
221 # If the dir already exists do not complain, if it is
222 # any other error, raise the exception
223 if e.errno != errno.EEXIST:
224 raise
225 history_file = open(history_file_name, 'a')
226 for i in self._history:
227 if 'EOF' not in i:
228 history_file.write(i+'\n')
229 history_file.close()
230 print 'Bye!'
231
232 def emptyline(self):
233 pass
Note: See TracBrowser for help on using the repository browser.