-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathread.py
executable file
·422 lines (352 loc) · 12.6 KB
/
read.py
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/python3 -bb
import re
import tempfile
import os
import ast
import xml.etree.ElementTree
import ftnconfig
import ftnexport
import ftnimport
import sys
import getopt
db=ftnconfig.connectdb()
my_id = ftnconfig.get_addr_id (db, db.FTN_domains["node"], ftnconfig.ADDRESS)
RE_s = re.compile("\s+")
RE_quote = re.compile("\s*(\S{0,2}?)(\>+)\s*(.*)$")
MAXW=79
def quote(qname, block):
if len(block) and block[-1].startswith(" * "):
block.pop(-1)
if len(block) and block[-1].startswith("---"):
block.pop(-1)
if len(block) and block[-1].startswith("..."):
block.pop(-1)
while len(block) and block[-1]=="":
block.pop(-1)
while len(block) and block[0]=="":
block.pop(0)
if len(block) and block[0].strip().startswith("Hello") or block[0].strip().startswith("Привет"):
block.pop(0)
while len(block) and block[0]=="":
block.pop(0)
# go line by line looking for quote level.
# reformat text in one quote level
outp = []
carry = None
prv_q = None
prv_t = None
pos=0
while pos<len(block) or prv_q:
if pos<len(block):
l = block[pos]
else:
l= ""
q = RE_quote.match(l)
if q:
qn, qc, qtext = q.groups()
qc += ">"
else:
qn = qname
qc = ">"
qtext = l
qtext = qtext.strip()
if prv_q:
if (qn, qc) != prv_q or qtext=="":
qn, qc = prv_q
qtext = prv_t
prv_q = None
prv_t = None
else:
qtext = prv_t +" "+qtext
prv_t = None
prv_q = None
pos += 1
else:
pos += 1
pre_len = 1+len(qn)+len(qc)+1
if pre_len + len(qtext) > MAXW:
spc = qtext.rfind(" ", 0, MAXW-pre_len+1)
if spc==-1:
spc = MAXW-pre_len+1
prv_t = qtext[spc:].lstrip()
prv_q = (qn, qc)
qtext = qtext[:spc]
outp.append(" "+qn+qc+" "+qtext+"\n")
#print(" "+qn+qc+" "+qtext)
return outp
txt=""" Приветствую Вас, Sergey!
23 фев 12 23:54, Sergey Dorofeev wrote to Alex Barinov:
AB>> С уходом Юры Богоявленского информаци по бекбону 5020 не
AB>> публиковалась, впрочем, ничего в ней с тех пор и не поменялось.
AB>> Разве что кто-то из хабов сеть покинул... Если есть необходимость,
AB>> могу восстановить публикацию этой информации, предварительно её
AB>> обновив. А так - спрашивай, отвечу. :-)
SD> Было бы неплохо обновить, поскольку изменения такие, что /400 свалил,
SD> /758 написал о полном автопилоте. В общем освежить картинку
Угу. Эхолист начал актуализировать. Хабов тоже обновлю как руки дойдут. Кстати, /400, таки вернулся и пока стабильно работает, так что не вижу смысла гнать его из хабов, тем более, что многие вторичники с него кормятся.
Алексей Баринов
E-Mail: bav (at) sirena-travel.ru ICQ: 24466689 Skype: huba-huba
[Team Бородатые]
--- GoldED+/W32-MSVC 1.1.5-20051112
* Origin: Alex at Work (2:5020/715.1)
"""
#for l in quote("AB", txt.splitlines()):
# print (l.rstrip())
#exit()
def submit(file, sendmode=None):
doheader = True
for l in file.splitlines():
l=l.rstrip()
if doheader:
if l=="":
doheader = False
body = []
continue
if l.startswith("From: "):
fromname = ast.literal_eval(l[6:])
elif l.startswith("To: "):
toname = ast.literal_eval(l[4:])
elif l.startswith("Subject: "):
subj = ast.literal_eval(l[9:])
elif l.startswith("ReplyTo: "):
msgid = ast.literal_eval(l[9:])
elif l.startswith("Destination: "):
dest = ast.literal_eval(l[13:])
elif l.startswith("Attr: "):
flags = ast.literal_eval(l[6:])
else:
if l[0]!="#":
raise Exception("invalid header %s"%repr(l))
else:
body.append(l)
with ftnimport.session(db) as sess:
sess.send_message(("node", ftnconfig.ADDRESS), fromname, dest, toname, msgid, subj, "\n".join(body), flags, sendmode)
def reply(srcid, dstid, msgid, header, body):
nameparts = RE_s.split(header.find("sendername").text.strip())
senderfirstname = nameparts[0]
if senderfirstname:
qname = senderfirstname[0]
else:
qname = ''
if len(nameparts)>1:
qname += nameparts[-1][0]
origdom, origtext = ftnconfig.get_addr(db, srcid)
origdom = db.FTN_backdomains[origdom]
destdom, desttext = ftnconfig.get_addr(db, dstid)
destdom = db.FTN_backdomains[destdom]
tpl = ["From: " + repr(ftnconfig.SYSOP) + "\n"]
tpl.append("To: " + repr(header.find("sendername").text) + "\n")
tpl.append("Subject: " + repr(header.find("subject").text) + "\n")
tpl.append("ReplyTo: " + repr(msgid) + "\n")
if destdom == 'echo':
tpl.append("Destination: " + repr((destdom, desttext)) + "\n")
tpl.append("#Destination: " + repr((origdom, origtext)) + "\n")
else:
tpl.append("Destination: " + repr((origdom, origtext)) + "\n")
tpl.append("Attr: []\n")
tpl.append("\n")
tpl.append(" ".join(("Hello", senderfirstname))+",\n")
tpl.append("\n")
tpl.append("orig.message to %s %s on "%(destdom, desttext) + header.find("date").text.strip() + "\n")
tpl.extend(quote(qname, body.splitlines()))
tpl.append("\n")
tpl.append("Sergey\n")
tpl.append("\n")
tpl.append("... vim\n")
newmsg = "".join(tpl)
(fh, fname) = tempfile.mkstemp()
fo = os.fdopen(fh, "w")
fo.write(newmsg)
fo.close()
os.system ("vi " + fname)
editedmsg = open(fname).read()
if newmsg == editedmsg:
print("no changes")
r = False
else:
submit(editedmsg)
r = True
os.unlink(fname)
return r
def inval(s):
if s.startswith("---"):
return "-+-"+s[3:]
if s.startswith(" * Origin"):
return " + Origin"+s[9:]
return s
def forward(srcid, dstid, msgid, header, body, tosrc=False, newsubj=None):
origdom, origtext = ftnconfig.get_addr(db, srcid)
origdom = db.FTN_backdomains[origdom]
destdom, desttext = ftnconfig.get_addr(db, dstid)
destdom = db.FTN_backdomains[destdom]
tpl = ["From: " + repr(ftnconfig.SYSOP) + "\n"]
if tosrc:
tpl.append("To: "+repr(header.find("sendername").text)+"\n")
else:
tpl.append("To: ''\n")
if newsubj:
tpl.append("Subject: " + repr(newsubj) + "\n")
else:
tpl.append("Subject: " + repr(header.find("subject").text) + "\n")
tpl.append("ReplyTo: " + repr(None) + "\n")
if tosrc:
tpl.append("Destination: (%s, %s)\n"%(repr(origdom), repr(origtext)))
else:
tpl.append("Destination: ('', '')\n")
tpl.append("Attr: []\n")
tpl.append("\n")
if tosrc:
tpl.append("Hello "+(header.find("sendername").text or "Sysop")+",\n")
else:
tpl.append("Hello ,\n")
tpl.append("\n")
tpl.append(" *** Forwarded message:\n")
tpl.append("="*79+"\n")
tpl.append("From: %s, %s %s\n"%(header.find("sendername").text or '', origdom, origtext))
tpl.append("To : %s, %s %s\n"%(header.find("recipientname").text or '', destdom, desttext))
tpl.append("Subj: %s\n"%(header.find("subject").text or ''))
tpl.append("Date: %s\n"%(header.find("date").text or ''))
tpl.append("-"*79+"\n")
tpl.extend(map(inval, body.splitlines(True)))
tpl.append("="*79+"\n")
tpl.append("\n")
tpl.append("... vim\n")
newmsg = "".join(tpl)
(fh, fname) = tempfile.mkstemp()
fo = os.fdopen(fh, "w")
fo.write(newmsg)
fo.close()
os.system ("vi " + fname)
editedmsg = open(fname).read()
if newmsg == editedmsg:
print("no changes")
r = False
else:
submit(editedmsg)
r = True
os.unlink(fname)
return r
def view(mid, srcid, dstid, header, body):
print ("="*10 + " %20d "%mid + "="*47)
print ("From: %-36s, %s"%(header.find("sendername").text, str(ftnconfig.get_addr(db, srcid))))
print ("To : %-36s, %s"%(header.find("recipientname").text, str(ftnconfig.get_addr(db, dstid))))
print ("Subj: %s"%header.find("subject").text)
print ("Date: %s"%header.find("date").text)
print ("-"*79)
ftn=header.find("FTN")
for kl in ftn.findall("KLUDGE"):
print ("@"+kl.get("name"), kl.get("value"))
print (body)
print ("="*79)
def save(srcid, dstid, msgid, header, body):
tr = {}
for a in msgid:
if not a.isalpha() and not a.isdigit():
tr[ord(a)]="_"
fn = "save-%d-%s.txt"%(srcid, msgid.translate(tr))
f=open(fn, "wb")
f.write(xml.etree.ElementTree.tostring(header, encoding="utf-8"))
f.write(b"\n\n")
f.write(body.encode("utf-8"))
f.close()
return True
USERNAME = ftnconfig.SYSOP
def list_echoes():
ftnexport.get_targets("echo")
opts=getopt.getopt(sys.argv[1:], "ndbv")
optflags = set(map(lambda x: x[0], opts[0]))
optdata = opts[1]
print (optflags, optdata)
if "-n" in optflags: # new
tpl = ["From: " + repr(ftnconfig.SYSOP) + "\n"]
tpl.append("To: ''\n")
tpl.append("Subject: ''\n")
tpl.append("Destination: ('', '')\n")
tpl.append("ReplyTo: None\n")
tpl.append("Attr: []\n")
tpl.append("\n")
tpl.append("Hello ,\n")
tpl.append("\n")
tpl.append("\n")
tpl.append("\n")
tpl.append("... vim\n")
newmsg = "".join(tpl)
(fh, fname) = tempfile.mkstemp()
fo = os.fdopen(fh, "w")
fo.write(newmsg)
fo.close()
os.system ("vi " + fname)
editedmsg = open(fname).read()
if newmsg == editedmsg:
print("no changes")
else:
submit(editedmsg, sendmode="direct" if "-d" in optflags else None)
os.unlink(fname)
exit()
elif "-b" in optflags: # bounce message #
mid = int(optdata[0])
srcid, dstid, msgid, header, body, origcharset, receivedfrom = \
db.prepare("select source, destination, msgid, header, body, origcharset, receivedfrom from messages where id=$1").first(mid)
if forward(srcid, dstid, msgid, header, body, tosrc=True, newsubj="message bounced at "+ftnconfig.ADDRESS):
db.prepare("update messages set processed=6 where id=$1")(mid)
exit()
elif "-v" in optflags: # view
mid = int(optdata[0])
mid, srcid, dstid, msgid, header, body, origcharset, receivedfrom = \
db.prepare("select id, source, destination, msgid, header, body, origcharset, receivedfrom from messages where id=$1").first(mid)
view(mid, srcid, dstid, header, body)
exit()
committer = ftnexport.netmailcommitter()
for mid, srcid, dstid, msgid, header, body, origcharset, receivedfrom in ftnexport.get_subscriber_messages_n(db, my_id, db.FTN_domains["node"]):
view(mid, srcid, dstid, header, body)
cmd = None
while not cmd:
x = input("Commit, Reply, Quit, Forward> ")
if x in ("c", "r", "q", "f", "s"):
cmd = x
elif x == "":
cmd = "m" # more
if cmd == "q":
break
elif cmd == "c":
committer.add ((mid, False))
elif cmd == "r":
if reply (srcid, dstid, msgid, header, body):
committer.add ((mid, False))
elif cmd == "f":
if forward (srcid, dstid, msgid, header, body):
committer.add ((mid, False))
elif cmd == "s":
if save (srcid, dstid, msgid, header, body):
committer.add ((mid, False))
print ()
committer.commit()
committer = ftnexport.echomailcommitter()
from ftnexport import get_subscriber_messages_e
for mid, srcid, dstid, msgid, header, body, origcharset, receivedfrom, subsid, processed in get_subscriber_messages_e(db, my_id, db.FTN_domains["echo"]):
if processed == 5:
print("skip archived msg #%d"%mid)
continue # archived
view(mid, srcid, dstid, header, body)
committer.add ((subsid, mid))
cmd = None
while not cmd:
x = input("Commit, Reply, Forward, Quit> ")
if x in ("c", "r", "q", "f"):
cmd = x
elif x == "":
cmd = "m" # more
if cmd == "q":
break
elif cmd == "c":
committer.commit()
elif cmd == "r":
if reply (srcid, dstid, msgid, header, body):
committer.commit()
elif cmd == "f":
if forward (srcid, dstid, msgid, header, body):
committer.commit()
print ()
x = input("Commit echomail> ")
if x=="c":
committer.commit()