7 from getopt
import getopt
, GetoptError
8 from sys
import stdin
, stdout
, stderr
, argv
, exit
9 from cStringIO
import StringIO
15 print >>stderr
, '%s (fatal): %s' %
(prog
, msg
)
18 print >>stderr
, '%s: %s' %
(prog
, msg
)
22 ('Usage: %s [-d DIST] [-h HOST] [-r REMOTE] [-p PATH] GROUP <MESSAGE' %
30 if line
== '' or line
== '\n':
34 bad('unexpected continuation')
44 bad('failed to parse header')
45 return v
[0].strip().lower(), v
[1].strip()
47 remote
= ('localhost', 119)
50 host
= OS
.popen('hostname -f').read().strip()
55 sender
= env
.get('SENDER')
56 recip
= env
.get('RECIPIENT')
60 global approved
, remote
, host
, dist
, path
, group
, sender
, recip
62 opts
, args
= getopt(argv
[1:], 'a:d:h:r:p:S:R:',
63 ['approved=', 'distribution=',
64 'sender=', 'recipient=',
65 'hostname=', 'remote=', 'path='])
69 if o
in ('-a', '--approved'):
71 elif o
in ('-d', '--distribution'):
73 elif o
in ('-h', '--hostname'):
75 elif o
in ('-r', '--remote'):
76 remote
= (lambda addr
, port
= 119: (addr
, int(port
)))(*a
.split(':'))
77 elif o
in ('-R', '--recipient'):
79 elif o
in ('-S', '--sender'):
85 rx_msgid
= RX
.compile(r
'^\<\S+@\S+\>$')
88 def __init__(me
, addr
):
89 me
.sk
= S
.socket(S
.AF_INET
, S
.SOCK_STREAM
)
91 me
.f
= me
.sk
.makefile()
94 die('unable to contact server: %s %s' %
(rc
, msg
))
100 me
.f
.write(stuff
+ '\r\n')
103 rc
, msg
= (lambda rc
, msg
= '.': (rc
, msg
.strip())) \
104 (*me
.f
.readline().split(None, 1))
105 if rc
.startswith('5'):
106 die('server hated me: %s %s' %
(rc
, msg
))
107 return rc
, msg
.strip()
112 hdr
.write('Path: newsgate\r\n'
113 'Distribution: mail\r\n'
116 if approved
: hdr
.write('Approved: %s\r\n' % approved
)
117 if sender
: hdr
.write('Return-Path: <%s>\r\n' % sender
)
118 if recip
: hdr
.write('Delivered-To: %s\r\n' % recip
)
121 lines xref newsgroups path distribution approved received
125 for h
in headers(stdin
):
128 h
= 'X-Newsgate-' + h
129 elif h
.startswith('.'):
132 if h
.endswith('\r\n'):
134 elif h
.endswith('\n'):
139 if 'message-id' not in seen
:
140 seen
['message-id'] = ('<newsgate-%s@%s>'
141 %
(OS
.popen('gorp 128').read().strip(),
143 hdr
.write('Message-ID: %s\r\n' % seen
['message-id'])
144 if 'date' not in seen
:
145 hdr
.write('Date: %s\r\n'
146 %
(T
.strftime('%a, %d %b %Y %H:%M:%S %Z')))
147 if 'subject' not in seen
:
148 hdr
.write('Subject: (no subject)\r\n')
150 msgid
= seen
['message-id']
151 if not rx_msgid
.match(msgid
):
152 bad('invalid message-id %s' % msgid
)
155 nntp
.cmd('IHAVE %s' % msgid
)
156 rc
, msg
= nntp
.reply()
160 if i
.startswith('.'):
162 if i
.endswith('\r\n'):
164 elif i
.endswith('\n'):
170 hdr
.write('Lines: %d\r\n' % n
)
172 nntp
.write(hdr
.getvalue())
173 nntp
.write(body
.getvalue())
176 rc
, msg
= nntp
.reply()
178 ## doesn't want my article; pretend all is fine: I don't care
181 die('failed to send article: %s %s' %
(rc
, msg
))
183 bad('server rejected article: %s %s' %
(rc
, msg
))
184 elif not rc
.startswith('2'):
185 die('unexpected response from server: %s %s' %
(rc
, msg
))
195 # except Exception, exc:
196 # die('unhandled exception: %s, %s' % (exc.__class__.__name__,