aboutsummaryrefslogtreecommitdiffstats
path: root/pygm2n
blob: 51869b36e0ef3c95827b93460a028fed8de5e8a1 (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""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.
"""
from __future__ import print_function

import logging
import sys
import argparse

import mail2news
import pyginfo


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


def parse_cmdline():
    i = pyginfo.pygsinfo()
    parser = argparse.ArgumentParser(
        description='%s version %s - Copyright 2000 Cosimo Alfarano\n%s' %
        (i.PROGNAME, i.VERSION, i.__doc__))

    parser.add_argument('-s', '--newsserver', default='')
    parser.add_argument('-a', '--approver', default='',
                        help="address of moderator/approver")
    parser.add_argument('-n', '--newsgroup', default='',
                        help='newsgroup[s] (specified as comma separated ' +
                        'without spaces list)', required=True)
    parser.add_argument('-u', '--user', default='')
    parser.add_argument('-p', '--password', default='',
                        help='password (for auth to newsserver)')
    parser.add_argument('-P', '--port', default='')
    parser.add_argument('-M', '--reader', default='')

    parser.add_argument('-e', '--envellope', default='')
    parser.add_argument('-t', '--to', dest='rcpt')
    parser.add_argument('-w', '--wlfile')
    parser.add_argument('-l', '--logfile')

    parser.add_argument('-T', '--test', action='store_true',
                        help='test mode (not send article via NNTP)')
    parser.add_argument('-d', '--debug', action='store_true')
    parser.add_argument('-V', '--verbose', action='store_true',
                        help='verbose output ' +
                        '(usefull with -T option for debugging)')

    args = parser.parse_args()

    if not args.newsgroup:
        raise argparse.ArgumentError('Error: Missing Newsgroups\n')

    return args


"""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()

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

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

    """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)

    """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)