1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
--- lib/pyzor/client.py Sun Sep 8 22:37:15 2002
+++ lib/pyzor/client.py Wed Apr 13 17:08:42 2005
@@ -8,6 +8,7 @@
import getopt
import tempfile
import mimetools
+import multifile
import sha
import pyzor
@@ -58,11 +57,6 @@
self.send(msg, address)
return self.read_response(msg.get_thread())
- def shutdown(self, address):
- msg = ShutdownRequest()
- self.send(msg, address)
- return self.read_response(msg.get_thread())
-
def build_socket(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@@ -132,39 +126,50 @@
def run(self):
debug = 0
- (options, args) = getopt.getopt(sys.argv[1:], 'dh:', ['homedir='])
- if len(args) < 1:
- self.usage()
-
specified_homedir = None
+ options = None
+ log = None
+
+ try:
+ (options, args) = getopt.getopt(sys.argv[1:], 'd', ['homedir=', 'log'])
+ except getopt.GetoptError:
+ self.usage()
+
+ if len(args) < 1:
+ self.usage()
for (o, v) in options:
if o == '-d':
debug = 1
- elif o == '-h':
- self.usage()
elif o == '--homedir':
specified_homedir = v
+ elif o == '--log':
+ log = 1
self.output = Output(debug=debug)
-
homedir = pyzor.get_homedir(specified_homedir)
-
+
+ if log:
+ sys.stderr = open(homedir + "/pyzor.log", 'a')
+ sys.stderr.write("\npyzor[" + repr (os.getpid()) + "]:\n")
+
config = pyzor.Config(homedir)
config.add_section('client')
- defaults = {'ServersFile': 'servers',
+ defaults = {'ServersFile': 'servers',
'DiscoverServersURL': ServerList.inform_url,
- 'AccountsFile' : 'accounts',
+ 'AccountsFile': 'accounts',
+ 'Timeout': str(Client.timeout),
}
for k, v in defaults.items():
config.set('client', k, v)
-
+
config.read(os.path.join(homedir, 'config'))
servers_fn = config.get_filename('client', 'ServersFile')
-
+ Client.timeout = config.getint('client', 'Timeout')
+
if not os.path.exists(homedir):
os.mkdir(homedir)
@@ -197,10 +202,13 @@
def usage(self, s=None):
if s is not None:
sys.stderr.write("%s\n" % s)
- sys.stderr.write("""usage: %s [-d] [--homedir dir] command [cmd_opts]
+ sys.stderr.write("""
+usage: %s [-d] [--homedir dir] command [cmd_opts]
command is one of: check, report, discover, ping, digest, predigest,
- genkey, shutdown
+ genkey
+
Data is read on standard input (stdin).
+
"""
% sys.argv[0])
sys.exit(2)
@@ -208,9 +216,9 @@
def ping(self, args):
- getopt.getopt(args[1:], '')
-
- if len(args) > 1:
+ try:
+ getopt.getopt(args[1:], '')
+ except getopt.GetoptError:
self.usage("%s does not take any non-option arguments" % args[0])
runner = ClientRunner(self.client.ping)
@@ -221,30 +229,23 @@
return runner.all_ok
- def shutdown(self, args):
- (opts, args2) = getopt.getopt(args[1:], '')
-
- if len(args2) > 1:
+ def info(self, args):
+ try:
+ (options, args2) = getopt.getopt(args[1:], '', ['mbox'])
+ except getopt.GetoptError:
self.usage("%s does not take any non-option arguments" % args[0])
- runner = ClientRunner(self.client.shutdown)
+ do_mbox = 'msg'
- for arg in args2:
- server = Address.from_str(arg)
- runner.run(server, (server,))
-
- return runner.all_ok
-
-
- def info(self, args):
- getopt.getopt(args[1:], '')
-
- if len(args) > 1:
- self.usage("%s does not take any non-option arguments" % args[0])
+ for (o, v) in options:
+ if o == '--mbox':
+ do_mbox = 'mbox'
runner = InfoClientRunner(self.client.info)
- for digest in FileDigester(sys.stdin, self.digest_spec):
+ for digest in get_input_handler(sys.stdin, self.digest_spec, do_mbox):
+ if digest is None:
+ continue
for server in self.servers:
response = runner.run(server, (digest, server))
@@ -252,34 +253,45 @@
def check(self, args):
- getopt.getopt(args[1:], '')
-
- if len(args) > 1:
+ try:
+ (options, args2) = getopt.getopt(args[1:], '', ['mbox'])
+ except getopt.GetoptError:
self.usage("%s does not take any non-option arguments" % args[0])
+ do_mbox = 'msg'
+
+ for (o, v) in options:
+ if o == '--mbox':
+ do_mbox = 'mbox'
+
runner = CheckClientRunner(self.client.check)
- for digest in FileDigester(sys.stdin, self.digest_spec):
+ for digest in get_input_handler(sys.stdin, self.digest_spec, do_mbox):
+ if digest is None:
+ continue
for server in self.servers:
- response = runner.run(server, (digest, server))
+ runner.run(server, (digest, server))
return (runner.found_hit and not runner.whitelisted)
def report(self, args):
- (options, args2) = getopt.getopt(args[1:], '', ['mbox'])
- do_mbox = False
-
- if len(args2) > 1:
+ try:
+ (options, args2) = getopt.getopt(args[1:], '', ['mbox'])
+ except getopt.GetoptError:
self.usage("%s does not take any non-option arguments" % args[0])
+ do_mbox = 'msg'
+
for (o, v) in options:
if o == '--mbox':
- do_mbox = True
+ do_mbox = 'mbox'
all_ok = True
- for digest in FileDigester(sys.stdin, self.digest_spec, do_mbox):
+ for digest in get_input_handler(sys.stdin, self.digest_spec, do_mbox):
+ if digest is None:
+ continue
if not self.send_digest(digest, self.digest_spec,
self.client.report):
all_ok = False
@@ -302,20 +314,22 @@
def whitelist(self, args):
- (options, args2) = getopt.getopt(args[1:], '', ['mbox'])
-
- if len(args2) > 1:
+ try:
+ (options, args2) = getopt.getopt(args[1:], '', ['mbox'])
+ except getopt.GetoptError:
self.usage("%s does not take any non-option arguments" % args[0])
- do_mbox = False
+ do_mbox = 'msg'
for (o, v) in options:
if o == '--mbox':
- do_mbox = True
+ do_mbox = 'mbox'
all_ok = True
- for digest in FileDigester(sys.stdin, self.digest_spec, do_mbox):
+ for digest in get_input_handler(sys.stdin, self.digest_spec, do_mbox):
+ if digest is None:
+ continue
if not self.send_digest(digest, self.digest_spec,
self.client.whitelist):
all_ok = False
@@ -324,28 +338,29 @@
def digest(self, args):
- (options, args2) = getopt.getopt(args[1:], '', ['mbox'])
-
- if len(args2) > 1:
+ try:
+ (options, args2) = getopt.getopt(args[1:], '', ['mbox'])
+ except getopt.GetoptError:
self.usage("%s does not take any non-option arguments" % args[0])
-
- do_mbox = False
+ do_mbox = 'msg'
for (o, v) in options:
if o == '--mbox':
- do_mbox = True
+ do_mbox = 'mbox'
- for digest in FileDigester(sys.stdin, self.digest_spec, do_mbox):
+ for digest in get_input_handler(sys.stdin, self.digest_spec, do_mbox):
+ if digest is None:
+ continue
sys.stdout.write("%s\n" % digest)
return True
def print_digested(self, args):
- getopt.getopt(args[1:], '')
-
- if len(args) > 1:
+ try:
+ getopt.getopt(args[1:], '')
+ except getopt.GetoptError:
self.usage("%s does not take any non-option arguments" % args[0])
def loop():
@@ -358,9 +373,9 @@
return True
def genkey(self, args):
- getopt.getopt(args[1:], '')
-
- if len(args) > 1:
+ try:
+ getopt.getopt(args[1:], '')
+ except getopt.GetoptError:
self.usage("%s does not take any non-option arguments" % args[0])
import getpass
@@ -414,7 +429,6 @@
'report': report,
'ping' : ping,
'genkey': genkey,
- 'shutdown': shutdown,
'info': info,
'whitelist': whitelist,
'digest': digest,
@@ -608,31 +622,37 @@
-class FileDigester(BasicIterator):
- __slots__ = ['digester']
-
- def __init__(self, fp, spec, mbox=False):
- self.digester = iter(get_file_digester(fp, spec, mbox))
- self.output = pyzor.Output()
-
- def next(self):
- digest = self.digester.next()
- self.output.debug("calculated digest: %s" % digest)
- return digest
-
-
-
-def get_file_digester(fp, spec, mbox, seekable=False):
+def get_input_handler(fp, spec, style='msg', seekable=False):
"""Return an object that can be iterated over
to get all the digests from fp according to spec.
mbox is a boolean"""
- if mbox:
+ if style == 'msg':
+ return filter(lambda x: x is not None,
+ (DataDigester(rfc822BodyCleaner(fp),
+ spec, seekable).get_digest(),)
+ )
+
+ elif style =='mbox':
return MailboxDigester(fp, spec)
- return (DataDigester(rfc822BodyCleaner(fp),
- spec, seekable).get_digest(),)
+ elif style == 'digests':
+ return JustDigestsIterator(fp)
+
+ raise ValueError, "unknown input style"
+class JustDigestsIterator(BasicIterator):
+ __slots__ = ['fp']
+
+ def __init__(self, fp):
+ self.fp = fp
+
+ def next(self):
+ l = fp.readline()
+ if not l:
+ raise StopIteration
+ return l.rstrip()
+
class MailboxDigester(BasicIterator):
__slots__ = ['mbox', 'digest_spec', 'seekable']
@@ -645,7 +665,12 @@
self.seekable = seekable
def next(self):
- next_msg = self.mbox.next()
+ try:
+ next_msg = self.mbox.next()
+ except IOError:
+ print "Error: Please feed mailbox files in on stdin, i.e."
+ print " pyzor digest --mbox < my_mbox_file"
+ next_msg = None
if next_msg is None:
raise StopIteration
return DataDigester(next_msg, self.digest_spec,
|