aboutsummaryrefslogblamecommitdiffstats
path: root/pygm2n
blob: 428ca5b7674a5c539f1088e0280ad8a901db4edc (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
                     
 












                                                                          
 








































































































                                                                                                               
 
 
 
 
 

 
 
 
                                  



                                                    
   

 
    
 


                                              
 

                                   
 
























































                                                                                           
 


                                        
 


                                            
 





                                                         

                         

                                  
#!/usr/bin/env python

"""News to mail gateway script. Copyright 2000 Cosimo Alfarano

Author: Cosimo Alfarano
Date: June 11 2000

pygs - Copyright 2000 by Cosimo Alfarano <Alfarano@Students.CS.UniBo.It>
You can use this software under the terms of the GPL. If we meet some day,
and you think this stuff is worth it, you can buy me a beer in return.

Thanks to md for this useful formula. Beer is beer.

Gets news article and sends it via SMTP.
"""

import sys, os
import getopt
from string import split

sys.path.append('/usr/lib/pyg')

import pyginfo
import mail2news 

	
def parse_cmdline(gw):
	"""Parses cmdline with getopt. and returns a dictionary with
       smtp new header
    """

	opt, arg = None, None
	test, verbose = 0, 0
#	retnull = (None, None)
	retnull = None

	retval = { 'test': 0, 
       		'verbose': 0 }
	
	try:
		opt, arg = getopt.getopt(sys.argv[1:],"a:s:n:u:p:P:hvVTM")
	except (getopt.error), message:
		print '%s: %s\n' % (sys.argv[0], message)
		sys.exit(1)
	
	if len(sys.argv) == 1 or opt == []:
		gw.smtpheads = None
		return retnull
	
	for i in range(len(opt)):
		if opt[i][0] == '-h':
			gw.nntpheads = None
			return retnull
		elif opt[i][0] == '-v':
			gw.nntpheads = None
			return retnull
		elif opt[i][0] == '-n':
			gw.nntpheads['Newsgroups:'] = opt[i][1] + '\n'
		elif opt[i][0] == '-a':
			gw.nntpheads['Approved:'] = opt[i][1] + '\n'
		elif opt[i][0] == '-s':
			gw.newsserver = opt[i][1]
		elif opt[i][0] == '-P':
			gw.port = int(opt[i][1])
#		elif opt[i][0] == '-d':
#			gw.debug = 1
		elif opt[i][0] == '-u':
			gw.user = opt[i][1]
		elif opt[i][0] == '-p':
			gw.password = opt[i][1]
		elif opt[i][0] == '-T':
			retval['test'] = 1
		elif opt[i][0] == '-V':
			retval['verbose'] = 1
		elif opt[i][0] == '-M':
			gw.reader = 1

	if not gw.nntpheads.has_key('Newsgroups:'):
		print 'Error: Missing Newsgroups\n'
		return retnull
	

#  By rfc822 [Resent-]Sender: should be ever set, unless == From: 
# (not this case). Should be a human, while [Resent-]From: may be a program.

#	if gw.rcpt == '' or gw.sender == '':
#		print 'missing command line option'
#		gw.smtpheads = None
#		return retnull

#	if gw.envelope == '' and gw.sender != '':
#		gw.smtpheads['Resent-From:'] = gw.sender + '\n'
#		gw.envelope = gw.sender
#	elif gw.envelope == -1:
#		gw.smtpheads = None
#		return retnull

	sys.argv[1:] = arg

#	return (test, verbose)
	return retval


def usage():
	i = pyginfo.pygsinfo()

	print '%s version %s - Copyright 2000 Cosimo Alfarano' % (i.PROGNAME, i.VERSION)
	print i.PROGDESC + ' - Mail to News'
	print 
	print 'usage: %s -n newsgroup [-h] [-a approver] [-n newsgroup] [-T] [-V]' % split(sys.argv[0],'/')[-1]
	print '-n newsgroup[s] (specified as comma separated without spaces list)'
	print '-u user | -p passwword (for auth to newsserver)'
	print '-a address of moderator/approver'
	print '-s servername'
#	print '-d for debug'
	print '-h or -v for this info'
	print '-T for test mode (not send article via NNTP)'
	print '-V for verbose output (usefull with -T option for debugging)'












"""main is structured in 4 phases:
	1) check and set pyg's internal variables
	2) check whitelist for users' permission
	3) format rfc 822 headers from input article
	4) open smtp connection and send e-mail
"""


try:

	"""phase 1:
	check and set pyg's internal variables
	"""

	m2n = mail2news.mail2news()
	owner = None

	opt = parse_cmdline(m2n)
	if(opt == None):
		usage()
		sys.exit(0)


	# check if m2n has some file prefercences set on commandline
#	if(m2n.wlfile == None):
#		wl = os.environ['HOME'] + '/pyg.whitelist'
#	else:
#		wl = m2n.wlfile
#	
#	if(m2n.logfile == None):
#		log = os.environ['HOME'] + '/pyg.log'
#	else:
#		log = m2n.logfile


#	wl = whitelist.whitelist(wl,log)

	# reads stdin and parses article separating head from body
	m2n.readfile()
	m2n.parseemail()



#	for line in m2n.email:
#		print line[:-1]

#	for line in m2n.smtpheads.keys():
#		print line
#		print m2n.smtpheads[line][:-1]

	"""phase 2:
	check whitelist for user's permission
	"""

	# make a first check of From: address
#	owner = wl.checkfrom(m2n.nntpheads['From:'])
#	if(owner == None):	
#		if(sys.stdin.isatty()==1 or test or verbose):
#			print ('"%s" is not in whitelist!' % (m2n.nntpheads['From:'][:-1]))
#		else:
#			wl.logmsg(m2n.nntpheads,wl.DENY)			
#		sys.exit(1)

	"""phase 3:
	format rfc 822 headers from input article
	"""

	m2n.mergeheads()	# make unique dict from NNTP and SMTP dicts
	
	m2n.addheads()		# add some important heads
	m2n.renameheads()	# rename useless heads
	m2n.removeheads()	# remove other heads

	m2n.sortheads()		# sort remaining heads :)

	if(opt['verbose']):
		for line in m2n.headers:
			print line[:-1]

	"""phase 4:
	open smtp connection and send e-mail
	"""

	if(len(m2n.headers) > 0 and len(m2n.body) > 0):
#		wl.logmsg(m2n.heads_dict,wl.ACCEPT,owner)
		if(not opt['test']):
			resp = m2n.sendemail()
			if resp:
				print resp

except KeyboardInterrupt:
	print 'Keyboard Interrupt'
	sys.exit(0)