1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
#!/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 from stdin and sends it via SMTP.
"""
import sys, os
import getopt
from string import split
sys.path.append('/usr/lib/pyg')
import pyginfo
import whitelist
import news2mail
#import mail2news
def parse_cmdline(gw):
"""Parses cmdline with getopt.
set a dictionary with smtp new header in gw parameter (gw.smtpheads)
return (test,verbose) boolean tuple
"""
opt, arg = None, None
test, verbose = 0, 0
retnull = (None, None)
try:
opt, arg = getopt.getopt(sys.argv[1:],"H:t:s:e:w:l:hdvVT")
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.smtpheads = None
return retnull
elif opt[i][0] == '-v':
gw.smtpheads = None
return retnull
elif opt[i][0] == '-H':
gw.smtpserver = opt[i][1]
elif opt[i][0] == '-s':
gw.smtpheads['Resent-Sender:'] = opt[i][1] + '\n'
gw.sender = opt[i][1]
elif opt[i][0] == '-t' or opt[i][0] == '-r':
gw.smtpheads['To:'] = opt[i][1] + '\n'
gw.rcpt = opt[i][1]
elif opt[i][0] == '-e':
gw.smtpheads['Resent-From:'] = opt[i][1] + '\n' #envelope
gw.envelope = opt[i][1]
elif opt[i][0] == '-w':
gw.wlfile = opt[i][1]
elif opt[i][0] == '-l':
gw.logfile = opt[i][1]
elif opt[i][0] == '-d':
gw.debug = 1
elif opt[i][0] == '-T':
test = 1
elif opt[i][0] == '-V':
verbose = 1
# 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)
def usage():
i = pyginfo.pygsinfo()
print '%s version %s - Copyright 2000 Cosimo Alfarano' % (i.PROGNAME, i.VERSION)
print i.__doc__
print
print 'usage: %s [-h] [-d] [-T] [-V] [-H smtphost] [-l logfile] [-w whitelist] -t recipient@... -s sender@... [-e envelope@...]' % split(sys.argv[0],'/')[-1]
print '-t -s recipient, sender are necessary'
print '-e envelope [default: same of sender]'
print '-T for test mode (not send article via SMTP)'
print '-V for verbose output'
print '-d for debug'
print '-h or -v for this info'
"""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
"""
n2m = news2mail.news2mail()
owner = None
# it returns only test, other parms are set directly in the actual parameter
(test, verbose) = parse_cmdline(n2m)
if (test, verbose) == (None, None):
usage()
sys.exit(0)
# check if n2m has some file prefercences set on commandline
if(n2m.wlfile == None):
wl = os.environ['HOME'] + '/pyg.whitelist'
else:
wl = n2m.wlfile
if(n2m.logfile == None):
log = os.environ['HOME'] + '/pyg.log'
else:
log = n2m.logfile
# print 'using %s %s\n' % (wl,log)
wl = whitelist.whitelist(wl,log)
# reads stdin and parses article separating head from body
n2m.readfile()
n2m.parsearticle()
"""phase 2:
check whitelist for user's permission
"""
# make a first check of From: address
owner = wl.checkfrom(n2m.nntpheads['From:'])
if(owner == None):
if(sys.stdin.isatty()==1 or test):
print ('"%s" is not in whitelist!' % (n2m.nntpheads['From:'][:-1]))
else:
wl.logmsg(n2m.nntpheads,wl.DENY)
# if verbose, I want to print out headers, so I can't
# exit now.
if(not verbose):
sys.exit(1)
"""phase 3:
format rfc 822 headers from input article
"""
n2m.mergeheads() # make unique dict from NNTP and SMTP dicts
n2m.addheads() # add some important heads
n2m.renameheads() # rename useless heads
n2m.removeheads() # remove other heads
n2m.sortheads() # sort remaining heads :)
# prints formatted email message only (without send) if user wants
if(verbose):
for line in n2m.headers:
print line[:-1]
if(owner == None):
sys.exit(1)
"""phase 4:
open smtp connection and send e-mail
"""
if len(n2m.headers) > 0:
wl.logmsg(n2m.heads_dict,wl.ACCEPT,owner)
if(not test):
n2m.sendarticle()
else:
print 'Error: No Headers!!!'
except KeyboardInterrupt:
print 'Keyboard Interrupt'
sys.exit(1)
|