aboutsummaryrefslogtreecommitdiff
blob: 47293afc9aaadaf965c888ccc3a25138a63a40ff (plain)
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
# elog/mod_mail.py - elog dispatch module
# Copyright 2006-2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

import portage.mail
import socket

from portage.exception import PortageException
from portage.localization import _
from portage.util import writemsg


def process(mysettings, key, logentries, fulltext):
    if "PORTAGE_ELOG_MAILURI" in mysettings:
        myrecipient = mysettings["PORTAGE_ELOG_MAILURI"].split()[0]
    else:
        myrecipient = "root@localhost"

    myfrom = mysettings["PORTAGE_ELOG_MAILFROM"]
    myfrom = myfrom.replace("${HOST}", socket.getfqdn())
    mysubject = mysettings["PORTAGE_ELOG_MAILSUBJECT"]
    mysubject = mysubject.replace("${PACKAGE}", key)
    mysubject = mysubject.replace("${HOST}", socket.getfqdn())

    # look at the phases listed in our logentries to figure out what action was performed
    action = _("merged")
    for phase in logentries:
        # if we found a *rm phase assume that the package was unmerged
        if phase in ["postrm", "prerm"]:
            action = _("unmerged")
    # if we think that the package was unmerged, make sure there was no unexpected
    # phase recorded to avoid misinformation
    if action == _("unmerged"):
        for phase in logentries:
            if phase not in ["postrm", "prerm", "other"]:
                action = _("unknown")

    mysubject = mysubject.replace("${ACTION}", action)

    mymessage = portage.mail.create_message(myfrom, myrecipient, mysubject, fulltext)
    try:
        portage.mail.send_mail(mysettings, mymessage)
    except PortageException as e:
        writemsg(f"{str(e)}\n", noiselevel=-1)