chiark / gitweb /
mail: Send mail with the correct envelope sender.
[newsgate] / bin / inject
index 62a75fd3d83114f9915fc49a6eacc909eb2e7615..f6e360bc1cf9ef15762ea116776c93d326943c02 100755 (executable)
@@ -7,6 +7,7 @@ import socket as S
 from getopt import getopt, GetoptError
 from sys import stdin, stdout, stderr, argv, exit
 from cStringIO import StringIO
+env = OS.environ
 
 prog = argv[0]
 
@@ -51,13 +52,16 @@ except:
   host = 'localhost'
 dist = 'mail'
 path = 'newsgate'
+sender = env.get('SENDER')
+recip = env.get('RECIPIENT')
 group = None
 
 def opts():
-  global approved, remote, host, dist, path, group
+  global approved, remote, host, dist, path, group, sender, recip
   try:
-    opts, args = getopt(argv[1:], 'a:d:h:r:p:',
+    opts, args = getopt(argv[1:], 'a:d:h:r:p:S:R:',
                         ['approved=', 'distribution=',
+                         'sender=', 'recipient=',
                          'hostname=', 'remote=', 'path='])
   except GetoptError:
     usage()
@@ -70,6 +74,10 @@ def opts():
       host = a
     elif o in ('-r', '--remote'):
       remote = (lambda addr, port = 119: (addr, int(port)))(*a.split(':'))
+    elif o in ('-R', '--recipient'):
+      recip = a
+    elif o in ('-S', '--sender'):
+      sender = a
   if len(args) != 1:
     usage()
   group, = args
@@ -100,11 +108,14 @@ class NNTP (object):
 
 def send():
   hdr = StringIO()
+  body = StringIO()
   hdr.write('Path: newsgate\r\n'
             'Distribution: mail\r\n'
             'Newsgroups: %s\r\n'
-            'Approved: %s\r\n'
-            % (group, approved or 'newsgate@%s' % host))
+            % group)
+  if approved: hdr.write('Approved: %s\r\n' % approved)
+  if sender: hdr.write('Return-Path: <%s>\r\n' % sender)
+  if recip: hdr.write('Delivered-To: %s\r\n' % recip)
   xify = {}
   for h in '''
     lines xref newsgroups path distribution approved received
@@ -135,7 +146,6 @@ def send():
               % (T.strftime('%a, %d %b %Y %H:%M:%S %Z')))
   if 'subject' not in seen:
     hdr.write('Subject: (no subject)\r\n')
-  hdr.write('\r\n')
 
   msgid = seen['message-id']
   if not rx_msgid.match(msgid):
@@ -145,7 +155,7 @@ def send():
   nntp.cmd('IHAVE %s' % msgid)
   rc, msg = nntp.reply()
   if rc == '335':
-    nntp.write(hdr.getvalue())
+    n = 0
     for i in stdin:
       if i.startswith('.'):
         i = '.' + i
@@ -155,7 +165,12 @@ def send():
         i = i[:-1] + '\r\n'
       else:
         i = i + '\r\n'
-      nntp.write(i)
+      body.write(i)
+      n += 1
+    hdr.write('Lines: %d\r\n' % n)
+    hdr.write('\r\n')
+    nntp.write(hdr.getvalue())
+    nntp.write(body.getvalue())
     nntp.write('.\r\n')
     nntp.flush()
     rc, msg = nntp.reply()