summaryrefslogtreecommitdiffstats
path: root/rtemstoolkit/mailer.py
blob: 085a2cee4efb3d31f28653ada045c1b1d8218d75 (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
#
# RTEMS Tools Project (http://www.rtems.org/)
# Copyright 2013-2016 Chris Johns (chrisj@rtems.org)
# Copyright (C) 2021 On-Line Applications Research Corporation (OAR)
# All rights reserved.
#
# This file is part of the RTEMS Tools package in 'rtems-tools'.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

#
# Manage emailing results or reports.
#

from __future__ import print_function

import os
import smtplib
import socket

from rtemstoolkit import error
from rtemstoolkit import execute
from rtemstoolkit import options
from rtemstoolkit import path

_options = {
    '--mail'         : 'Send email report or results.',
    '--use-gitconfig': 'Use mail configuration from git config.',
    '--mail-to'      : 'Email address to send the email to.',
    '--mail-from'    : 'Email address the report is from.',
    '--smtp-host'    : 'SMTP host to send via.',
    '--smtp-port'    : 'SMTP port to send via.',
    '--smtp-user'    : 'User for SMTP authentication.',
    '--smtp-password': 'Password for SMTP authentication.'
}

def append_options(opts):
    for o in _options:
        opts[o] = _options[o]

def add_arguments(argsp):
    argsp.add_argument('--mail', help = _options['--mail'], action = 'store_true')
    argsp.add_argument('--use-gitconfig', help = _options['--use-gitconfig'], action = 'store_true')
    no_add = ['--mail', '--use-gitconfig']
    for o in [opt for opt in list(_options) if opt not in no_add]:
        argsp.add_argument(o, help = _options[o], type = str)

class mail:
    def __init__(self, opts):
        self.opts = opts
        self.gitconfig_lines = None
        if opts.find_arg('--use-gitconfig') is not None:
            # Read the output of `git config --list` instead of reading the
            # .gitconfig file directly because Python 2 ConfigParser does not
            # accept tabs at the beginning of lines.
            e = execute.capture_execution()
            exit_code, proc, output = e.open('git config --list', shell=True)
            if exit_code == 0:
                self.gitconfig_lines = output.split(os.linesep)

    def _args_are_macros(self):
        return isinstance(self.opts, options.command_line)

    def _get_arg(self, arg):
        if self._args_are_macros():
            value = self.opts.find_arg(arg)
            if value is not None:
                value = self.opts.find_arg(arg)[1]
        else:
            if arg.startswith('--'):
                arg = arg[2:]
            arg = arg.replace('-', '_')
            if arg in vars(self.opts):
                value = vars(self.opts)[arg]
            else:
                value = None
        return value

    def _get_from_gitconfig(self, variable_name):
        if self.gitconfig_lines is None:
            return None

        for line in self.gitconfig_lines:
            if line.startswith(variable_name):
                ls = line.split('=')
                if len(ls) >= 2:
                    return ls[1]

    def from_address(self):

        def _clean(l):
            if '#' in l:
                l = l[:l.index('#')]
            if '\r' in l:
                l = l[:l.index('r')]
            if '\n' in l:
                l = l[:l.index('\n')]
            return l.strip()

        addr = self._get_arg('--mail-from')
        if addr is not None:
            return addr
        addr = self._get_from_gitconfig('user.email')
        if addr is not None:
            name = self._get_from_gitconfig('user.name')
            if name is not None:
                addr = '%s <%s>' % (name, addr)
            return addr
        mailrc = None
        if 'MAILRC' in os.environ:
            mailrc = os.environ['MAILRC']
        if mailrc is None and 'HOME' in os.environ:
            mailrc = path.join(os.environ['HOME'], '.mailrc')
        if mailrc is not None and path.exists(mailrc):
            # set from="Joe Blow <joe@blow.org>"
            try:
                with open(mailrc, 'r') as mrc:
                    lines = mrc.readlines()
            except IOError as err:
                raise error.general('error reading: %s' % (mailrc))
            for l in lines:
                l = _clean(l)
                if 'from' in l:
                    fa = l[l.index('from') + len('from'):]
                    if '=' in fa:
                        addr = fa[fa.index('=') + 1:].replace('"', ' ').strip()
            if addr is not None:
                return addr
        if self._args_are_macros():
            addr = self.opts.defaults.get_value('%{_sbgit_mail}')
        else:
            raise error.general('no valid from address for mail')
        return addr

    def smtp_host(self):
        host = self._get_arg('--smtp-host')
        if host is not None:
            return host
        host = self._get_from_gitconfig('sendemail.smtpserver')
        if host is not None:
            return host
        if self._args_are_macros():
            host = self.opts.defaults.get_value('%{_mail_smtp_host}')
        if host is not None:
            return host
        return 'localhost'

    def smtp_port(self):
        port = self._get_arg('--smtp-port')
        if port is not None:
            return port
        port = self._get_from_gitconfig('sendemail.smtpserverport')
        if port is not None:
            return port
        if self._args_are_macros():
            port = self.opts.defaults.get_value('%{_mail_smtp_port}')
        return port

    def smtp_user(self):
        user = self._get_arg('--smtp-user')
        if user is not None:
            return user
        user = self._get_from_gitconfig('sendemail.smtpuser')
        return user

    def smtp_password(self):
        password = self._get_arg('--smtp-password')
        if password is not None:
            return password
        password = self._get_from_gitconfig('sendemail.smtppass')
        return password

    def send(self, to_addr, subject, body):
        from_addr = self.from_address()
        msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % \
            (from_addr, to_addr, subject) + body
        port = self.smtp_port()

        try:
            s = smtplib.SMTP(self.smtp_host(), port, timeout=10)

            password = self.smtp_password()
            # If a password is provided, assume that authentication is required.
            if password is not None:
                user = self.smtp_user()
                if user is None:
                    user = from_addr
                s.starttls()
                s.login(user, password)

            s.sendmail(from_addr, [to_addr], msg)
        except smtplib.SMTPException as se:
            raise error.general('sending mail: %s' % (str(se)))
        except socket.error as se:
            raise error.general('sending mail: %s' % (str(se)))

    def send_file_as_body(self, to_addr, subject, name, intro = None):
        try:
            with open(name, 'r') as f:
                body = f.readlines()
        except IOError as err:
            raise error.general('error reading mail body: %s' % (name))
        if intro is not None:
            body = intro + body
        self.send(to_addr, from_addr, body)

if __name__ == '__main__':
    import sys
    from rtemstoolkit import macros
    optargs = {}
    rtdir = 'rtemstoolkit'
    defaults = '%s/defaults.mc' % (rtdir)
    append_options(optargs)
    opts = options.command_line(base_path = '.',
                                argv = sys.argv,
                                optargs = optargs,
                                defaults = macros.macros(name = defaults, rtdir = rtdir),
                                command_path = '.')
    options.load(opts)
    m = mail(opts)
    print('From: %s' % (m.from_address()))
    print('SMTP Host: %s' % (m.smtp_host()))
    if '--mail' in sys.argv:
        m.send(m.from_address(), 'Test mailer.py', 'This is a test')