aboutsummaryrefslogtreecommitdiffstats
path: root/news2mail.py
blob: 29f4271824354430d374bdf5c02365c15d66aa67 (plain) (blame)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python3
"""News to mail gateway script. Copyright 2000 Cosimo Alfarano

Author: Cosimo Alfarano
Date: June 11 2000

news2mail.py - (C) 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.

class news2mail is hopefully conform to rfc822.

normal (what pygs does) operations flow is:
1) reads from stdin NNTP article (readfile)
2) divide headers and body (parsearticle)
3) merges NNTP and SMTP heads into a unique heads
4) adds, renames and removes some heads
5) sorts remaining headers starting at top with Received: From: To: Subject:
    Date:, normal headers ending with X-* and Resent-* headers.

"""
import argparse
import os
import os.path
import collections
import email
from mail2news import VERSION, DESC
import smtplib
import socket
import sys
import time

import whitelist

# logging.basicConfig(level=logging.DEBUG)
class news2mail(object):
    """news to mail gateway class"""

    def __init__(self, options):
        self.wlfile = None
        self.logfile = None
        self.verbose = options.verbose

        self.sender = ''
        self.rcpt = ''
        self.envelope = ''

        self.smtpserver = 'localhost'

        self.hostname = socket.gethostbyaddr(socket.gethostname())[0]

        self.heads_dict = {}
        self.article, self.headers, self.body = [], [], []

        if options.input == '':
            self.message = self.__addheads(email.message_from_file(sys.stdin))
        else:
            with open(options.input, 'r') as inp_stream:
                self.message = self.__addheads(email.message_from_file(inp_stream))

    def __addheads(self, msg):
        """add new header like X-Gateway: Received:
        """

        msg['X-Gateway'] = 'pyg {0} {1}'.format(VERSION, DESC)

        # to make Received: header
        t = time.ctime(time.time())

        if time.daylight:
            tzone = time.tzname[1]
        else:
            tzone = time.tzname[0]

        # An example from debian-italian:
        # Received: from murphy.debian.org (murphy.debian.org [216.234.231.6])
        #        by smv04.iname.net (8.9.3/8.9.1SMV2) with SMTP id JAA26407
        #        for <kame.primo@innocent.com> sent by
        #        <debian-italian-request@lists.debian.org

        tmp = 'from GATEWAY by ' + self.hostname + \
            ' with pyg' + \
            '\n\tfor <' + self.rcpt + '> ; ' + \
            t + ' (' + tzone + ')\n'

        msg['Received'] = tmp

        return msg

    def __renameheads(self):
        """remove headers like Xref: Path: Lines:
           rename headers such as Newsgroups: to X-Newsgroups:

        headers renamed are useless or not rfc 822 copliant
        """
        try:
            if 'Newsgroups' in self.message:
                self.message['X-Newsgroups'] = \
                    self.message['Newsgroups']
                del self.message['Newsgroups']

            if 'NNTP-Posting-Host' in self.message:
                self.message['X-NNTP-Posting-Host'] = \
                    self.message['NNTP-Posting-Host']
                del self.message['NNTP-Posting-Host']
        except KeyError as ex:
            print(ex)

        try:
            # removing some others useless headers ....
            # that includes BOTH 'From ' and 'From'
            # 'Sender is usually set by INN, if ng is moderated...
            for key in ('Approved', 'From', 'Xref', 'Path', 'Lines', 'Sender'):
                if key in self.message:
                    del self.message[key]

            if 'Message-id' in self.message:
                msgid = self.message['Message-id']
                del self.message['Message-id']
                self.message['Message-Id'] = msgid
            else:
                # It should put a real user@domain
                self.heads_dict['Message-Id'] = 'pyg@puppapera.org'

            if 'References' in self.message and \
                    'In-Reply-To' not in self.message:
                refs = self.message['References'].split()
                self.message['In-Reply-To'] = refs[-1]

        except KeyError as message:
            print(message)

    def __sortheads(self):
        """make list sorting heads, Received: From: To: Subject: first,
           others, X-*, Resent-* last"""

        # put at top
        header_set = ('Received', 'From', 'To', 'Subject', 'Date')

        heads_dict = collections.OrderedDict(self.message)
        for hdr in self.message.keys():
            del self.message[hdr]

        for k in header_set:
            if k in heads_dict:
                self.message[k] = heads_dict[k]

        for k in heads_dict:
            if not k.startswith('X-') and not k.startswith('Resent-') \
                    and k not in header_set:
                self.message[k] = heads_dict[k]

        for k in heads_dict:
            if k.startswith('X-'):
                self.message[k] = heads_dict[k]

        for k in heads_dict:
            if k.startswith('Resent-'):
                self.message[k] = heads_dict[k]

    def process_message(self):
        """phase 3:
        format rfc 822 headers from input article
        """
        self.__renameheads()  # remove other heads
        self.__sortheads()

    def sendarticle(self):
        """Talk to SMTP server and try to send email."""
        s = smtplib.SMTP(self.smtpserver)
        s.set_debuglevel(self.verbose)

        s.sendmail(self.envelope, self.rcpt, self.message.as_bytes())

        s.quit()

def parse_cmdline(args):
    """
    set a dictionary with smtp new header in gw parameter (gw.smtpheads)
    return (test,verbose) boolean tuple
    """
    parser = argparse.ArgumentParser(
        description='pyg version %s - Copyright 2000 Cosimo Alfarano\n%s' %
        (VERSION, DESC))

    parser.add_argument('-H', '--smtpserver', default='')
    parser.add_argument('-s', '--sender', required=True, default='')
    parser.add_argument('-e', '--envelope', default='')
    parser.add_argument('-t', '--to', dest='rcpt', required=True)
    parser.add_argument('-w', '--wlfile')
    parser.add_argument('-i', '--input', default='')
    parser.add_argument('-l', '--logfile')

    parser.add_argument('-T', '--test',
                        help='test mode (not send article via SMTP)',
                        action='store_true')
    parser.add_argument('-v', '--verbose', help='verbose output',
                        action='store_true')

    opts = parser.parse_args(args)

#  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 opts.rcpt == '' or opts.sender == '':
        raise argparse.ArgumentError('missing command line option')

    if opts.envelope == '' and opts.sender != '':
        opts.envelope = opts.sender

    return opts

def main(args=sys.argv[1:]):
    """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
    """

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

    # it returns only test, other parms are set directly in the actual
    # parameter
    args = parse_cmdline(args)

    n2m = news2mail(args)
    owner = None

    # check if n2m has some file prefercences set on commandline
    if args.wlfile is None:
        wl = os.path.expanduser(os.path.join(os.path.dirname(__file__), 'pyg.whitelist'))
    else:
        wl = args.wlfile

    if args.logfile is None:
        log = os.path.expanduser(os.path.join(os.path.dirname(__file__), 'pyg.log'))
    else:
        log = args.logfile

    wl = whitelist.whitelist(wl, log)

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

    # make a first check of From: address
    owner = wl.checkfrom(n2m.message['From'])
    if owner is None:
        if sys.stdin.isatty() == 1 or args.test:
            out += str('"%s" is not in whitelist!' % (n2m.message['From'][:-1])) + '\n'
        else:
            wl.logmsg(n2m.nntpheads, wl.DENY)

        # if verbose, I want to print out headers, so I can't
        # exit now.
        if not args.verbose:
            sys.exit(1)

    # Reformat the message
    n2m.process_message()

    # prints formatted email message only (without send) if user wants
    if args.verbose:
        out += n2m.message.as_string() + '\n'

    if owner is None:
        sys.exit(1)

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

    wl.logmsg(n2m.heads_dict, wl.ACCEPT, owner)
    if not args.test:
        n2m.sendarticle()

    if args.input == '':
        print(out)
    else:
        return out