7 from getopt import getopt, GetoptError
8 from sys import stdin, stdout, stderr, argv, exit
9 from cStringIO import StringIO
14 print >>stderr, '%s (fatal): %s' % (prog, msg)
17 print >>stderr, '%s: %s' % (prog, msg)
21 ('Usage: %s [-d DIST] [-h HOST] [-r REMOTE] [-p PATH] GROUP <MESSAGE' %
29 if line == '' or line == '\n':
33 bad('unexpected continuation')
43 bad('failed to parse header')
44 return v[0].strip().lower(), v[1].strip()
46 remote = ('localhost', 119)
49 host = OS.popen('hostname -f').read().strip()
57 global approved, remote, host, dist, path, group
59 opts, args = getopt(argv[1:], 'a:d:h:r:p:',
60 ['approved=', 'distribution=',
61 'hostname=', 'remote=', 'path='])
65 if o in ('-a', '--approved'):
67 elif o in ('-d', '--distribution'):
69 elif o in ('-h', '--hostname'):
71 elif o in ('-r', '--remote'):
72 remote = (lambda addr, port = 119: (addr, int(port)))(*a.split(':'))
77 rx_msgid = RX.compile(r'^\<\S+@\S+\>$')
80 def __init__(me, addr):
81 me.sk = S.socket(S.AF_INET, S.SOCK_STREAM)
83 me.f = me.sk.makefile()
86 die('unable to contact server: %s %s' % (rc, msg))
92 me.f.write(stuff + '\r\n')
95 rc, msg = (lambda rc, msg = '.': (rc, msg.strip())) \
96 (*me.f.readline().split(None, 1))
97 if rc.startswith('5'):
98 die('server hated me: %s %s' % (rc, msg))
99 return rc, msg.strip()
103 hdr.write('Path: newsgate\r\n'
104 'Distribution: mail\r\n'
107 % (group, approved or 'newsgate@%s' % host))
110 lines xref newsgroups path distribution approved received
114 for h in headers(stdin):
117 h = 'X-Newsgate-' + h
118 elif h.startswith('.'):
121 if h.endswith('\r\n'):
123 elif h.endswith('\n'):
128 if 'message-id' not in seen:
129 seen['message-id'] = ('<newsgate-%s@%s>'
130 % (OS.popen('gorp 128').read().strip(),
132 hdr.write('Message-ID: %s\r\n' % seen['message-id'])
133 if 'date' not in seen:
134 hdr.write('Date: %s\r\n'
135 % (T.strftime('%a, %d %b %Y %H:%M:%S %Z')))
136 if 'subject' not in seen:
137 hdr.write('Subject: (no subject)\r\n')
140 msgid = seen['message-id']
141 if not rx_msgid.match(msgid):
142 bad('invalid message-id %s' % msgid)
145 nntp.cmd('IHAVE %s' % msgid)
146 rc, msg = nntp.reply()
148 nntp.write(hdr.getvalue())
150 if i.startswith('.'):
152 if i.endswith('\r\n'):
154 elif i.endswith('\n'):
161 rc, msg = nntp.reply()
163 ## doesn't want my article; pretend all is fine: I don't care
166 die('failed to send article: %s %s' % (rc, msg))
168 bad('server rejected article: %s %s' % (rc, msg))
169 elif not rc.startswith('2'):
170 die('unexpected response from server: %s %s' % (rc, msg))
180 # except Exception, exc:
181 # die('unhandled exception: %s, %s' % (exc.__class__.__name__,