diff options
| author | bwarsaw | 1999-12-10 20:35:28 +0000 |
|---|---|---|
| committer | bwarsaw | 1999-12-10 20:35:28 +0000 |
| commit | 0806bbf250fc5b10b2cf2df341b2c99b2d62856d (patch) | |
| tree | 05de20059506968fb8c30811842112026af7bd67 /Mailman/Handlers/ToUsenet.py | |
| parent | 14ac79204bbab36993c3a4637c167d162069b7e2 (diff) | |
| download | mailman-0806bbf250fc5b10b2cf2df341b2c99b2d62856d.tar.gz mailman-0806bbf250fc5b10b2cf2df341b2c99b2d62856d.tar.zst mailman-0806bbf250fc5b10b2cf2df341b2c99b2d62856d.zip | |
Diffstat (limited to 'Mailman/Handlers/ToUsenet.py')
| -rw-r--r-- | Mailman/Handlers/ToUsenet.py | 161 |
1 files changed, 85 insertions, 76 deletions
diff --git a/Mailman/Handlers/ToUsenet.py b/Mailman/Handlers/ToUsenet.py index 3cebda464..763c5aeb4 100644 --- a/Mailman/Handlers/ToUsenet.py +++ b/Mailman/Handlers/ToUsenet.py @@ -38,82 +38,91 @@ def process(mlist, msg): if not mlist.nntp_host: error.append('no NNTP host') if error: - msg = 'NNTP gateway improperly configured: ' + \ - string.join(error, ', ') + msg = 'NNTP gateway improperly configured: ' + string.join(error, ', ') mlist.LogMsg('error', msg) return # Fork in case the nntp connection hangs. - if not os.fork(): - # child - import nntplib - # Ok, munge headers, etc. - subj = msg.getheader('subject') - if subj: - subjpref = mlist.subject_prefix - if not re.match('(re:? *)?' + re.escape(subjpref), subj, re.I): - msg['Subject'] = subjpref + subj - else: - msg['Subject'] = subjpref + '(no subject)' - if mlist.reply_goes_to_list: - del msg['reply-to'] - msg.headers.append('Reply-To: %s\n' % mlist.GetListEmail()) - # if we already have a sender header, don't add another one; use - # the header that's already there. - if not msg.getheader('sender'): - msg.headers.append('Sender: %s\n' % mlist.GetAdminEmail()) - msg.headers.append('Errors-To: %s\n' % mlist.GetAdminEmail()) - msg.headers.append('X-BeenThere: %s\n' % mlist.GetListEmail()) - ngheader = msg.getheader('newsgroups') - if ngheader is not None: - # see if the Newsgroups: header already contains our - # linked_newsgroup. If so, don't add it again. If not, - # append our linked_newsgroup to the end of the header list - ngroups = map(string.strip, string.split(ngheader, ',')) - if mlist.linked_newsgroup not in ngroups: - ngroups.append(mlist.linked_newsgroup) - ngheader = string.join(ngroups, ',') - # subtitute our new header for the old one. XXX Message - # class should have a __setitem__() - del msg['newsgroups'] - msg.headers.append('Newsgroups: %s\n' % ngroups) - else: - # Newsgroups: isn't in the message - msg.headers.append('Newsgroups: %s\n' % mlist.linked_newsgroup) - # Note: Need to be sure 2 messages aren't ever sent to the same - # list in the same process, since message ID's need to be unique. - # Could make the ID be mm.listname.postnum instead if that happens - if msg.getheader('Message-ID') is None: - msg.headers.append('Message-ID: <mm.%s.%s@%s>\n' % - (time.time(), os.getpid(), mlist.host_name)) - if msg.getheader('Lines') is None: - msg.headers.append('Lines: %s\n' % - len(string.split(msg.body,"\n"))) - del msg['received'] - # TBD: Gross hack to ensure that we have only one - # content-transfer-encoding header. More than one barfs NNTP. I - # don't know why we sometimes have more than one such header, and it - # probably isn't correct to take the value of just the first one. - # What if there are conflicting headers??? - # - # This relies on the new interface for getaddrlist() returning values - # for all present headers, and the fact that the legal values are - # usually not parseable as addresses. Yes this is another bogosity. - cteheaders = msg.getaddrlist('content-transfer-encoding') - if cteheaders: - ctetuple = cteheaders[0] - ctevalue = ctetuple[1] - del msg['content-transfer-encoding'] - msg['content-transfer-encoding'] = ctevalue - # NNTP is strict about spaces after the colon in headers. - for n in range(len(msg.headers)): - line = msg.headers[n] - i = string.find(line,":") - if i <> -1 and line[i+1] <> ' ': - msg.headers[n] = line[:i+1] + ' ' + line[i+1:] - # flatten the message object, stick it in a StringIO object and post - # that resulting thing to the newsgroup - fp = StringIO(str(msg)) - conn = nntplib.NNTP(mlist.nntp_host) - conn.post(fp) - conn.quit() - os._exit(0) + pid = os.fork() + if not pid: + do_child(mlist, msg) + # TBD: we probably want to reap all those children, but do it in a way + # that doesn't keep the MailList object locked. Problem is that we don't + # know what other handlers are going to execute. Handling children should + # be pushed up into a higher module + + + +def do_child(mlist, msg): + # child + import nntplib + # Ok, munge headers, etc. + subj = msg.getheader('subject') + if subj: + subjpref = mlist.subject_prefix + if not re.match('(re:? *)?' + re.escape(subjpref), subj, re.I): + msg['Subject'] = subjpref + subj + else: + msg['Subject'] = subjpref + '(no subject)' + if mlist.reply_goes_to_list: + del msg['reply-to'] + msg.headers.append('Reply-To: %s\n' % mlist.GetListEmail()) + # if we already have a sender header, don't add another one; use + # the header that's already there. + if not msg.getheader('sender'): + msg.headers.append('Sender: %s\n' % mlist.GetAdminEmail()) + msg.headers.append('Errors-To: %s\n' % mlist.GetAdminEmail()) + msg.headers.append('X-BeenThere: %s\n' % mlist.GetListEmail()) + ngheader = msg.getheader('newsgroups') + if ngheader is not None: + # see if the Newsgroups: header already contains our + # linked_newsgroup. If so, don't add it again. If not, + # append our linked_newsgroup to the end of the header list + ngroups = map(string.strip, string.split(ngheader, ',')) + if mlist.linked_newsgroup not in ngroups: + ngroups.append(mlist.linked_newsgroup) + ngheader = string.join(ngroups, ',') + # subtitute our new header for the old one. XXX Message + # class should have a __setitem__() + del msg['newsgroups'] + msg.headers.append('Newsgroups: %s\n' % ngroups) + else: + # Newsgroups: isn't in the message + msg.headers.append('Newsgroups: %s\n' % mlist.linked_newsgroup) + # Note: Need to be sure 2 messages aren't ever sent to the same + # list in the same process, since message ID's need to be unique. + # Could make the ID be mm.listname.postnum instead if that happens + if msg.getheader('Message-ID') is None: + msg.headers.append('Message-ID: <mm.%s.%s@%s>\n' % + (time.time(), os.getpid(), mlist.host_name)) + if msg.getheader('Lines') is None: + msg.headers.append('Lines: %s\n' % + len(string.split(msg.body,"\n"))) + del msg['received'] + # TBD: Gross hack to ensure that we have only one + # content-transfer-encoding header. More than one barfs NNTP. I + # don't know why we sometimes have more than one such header, and it + # probably isn't correct to take the value of just the first one. + # What if there are conflicting headers??? + # + # This relies on the new interface for getaddrlist() returning values + # for all present headers, and the fact that the legal values are + # usually not parseable as addresses. Yes this is another bogosity. + cteheaders = msg.getaddrlist('content-transfer-encoding') + if cteheaders: + ctetuple = cteheaders[0] + ctevalue = ctetuple[1] + del msg['content-transfer-encoding'] + msg['content-transfer-encoding'] = ctevalue + # NNTP is strict about spaces after the colon in headers. + for n in range(len(msg.headers)): + line = msg.headers[n] + i = string.find(line,":") + if i <> -1 and line[i+1] <> ' ': + msg.headers[n] = line[:i+1] + ' ' + line[i+1:] + # flatten the message object, stick it in a StringIO object and post + # that resulting thing to the newsgroup + fp = StringIO(str(msg)) + conn = nntplib.NNTP(mlist.nntp_host) + conn.post(fp) + conn.quit() + os._exit(0) |
