#!/usr/bin/python """ Automated Faq Poster version: 0.3 Usage: /usr/bin/python usenetposter.py -s -f [-l login] [-p password] Notice that the faq file should have at least the following valid usenet message headers: From: test@example.com Subject: test Newsgroups: pt.internet.usenet """ import sys import os import time import nntplib import getopt import StringIO def usage(): print "Usage: %s -s -f [-l login] [-p password]" % sys.argv[0] def main(): server = '' faq = '' port = 119 login = '' password = '' print "Automated Faq Poster" # Check and get options try: opts, args = getopt.getopt(sys.argv[1:],"s:f:l:p:") for x,y in opts: if x == '-s': server = y if x == '-f': faq = y if x == '-l': login = y if x == '-p': password = y except: usage() sys.exit(2) if server == '' or faq == '': usage() sys.exit(2) # Check faq access if os.access(faq,os.F_OK) and os.access(faq,os.R_OK): f = open(faq).read() # Date: // # User-Agent: Automated Faq Poster v0.3 f = "User-Agent: Automated Faq Poster v0.3\r\n" + "Date: " + time.strftime('%Y/%m/%d',time.localtime(time.time())) + "\r\n" + f st = StringIO.StringIO(f) print "Opening connection..." try: if login != '': s = nntplib.NNTP(server,port,login,password) else: s = nntplib.NNTP(server) # debug level - 2 for extensive debug s.set_debuglevel(0) print s.getwelcome() print "Posting message..." s.post(st) s.quit() print "Done!" except Exception, inst: # Hint: # all exceptions have Exception class as their base class # __str__ allows arguments to be printed directly print inst else: usage() if __name__ == '__main__': main()