summaryrefslogtreecommitdiffstats
path: root/lib/Plugins/Mailx.cpp
blob: 2ee9645574940d4e9acb006aa0e5e9ac6582a918 (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
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
/*
    Mailx.cpp

    Copyright (C) 2009  Zdenek Prikryl (zprikryl@redhat.com)
    Copyright (C) 2009  RedHat inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    */


#include <stdio.h>
#include "abrtlib.h"
#include "Mailx.h"
#include "DebugDump.h"
#include "ABRTException.h"
#include "CommLayerInner.h"

#define MAILX_COMMAND "/bin/mailx"

CMailx::CMailx() :
    m_sEmailFrom("user@localhost"),
    m_sEmailTo("root@localhost"),
    m_sSubject("[abrt] full crash report"),
    m_bSendBinaryData(false)
{}

static void exec_and_feed_input(uid_t uid, const char* pText, char **pArgs)
{
    int pipein[2];

    pid_t child = fork_execv_on_steroids(
		EXECFLG_INPUT | EXECFLG_QUIET | EXECFLG_SETGUID,
                pArgs,
                pipein,
                /*unsetenv_vec:*/ NULL,
                /*dir:*/ NULL,
                uid);

    full_write(pipein[1], pText, strlen(pText));
    close(pipein[1]);

    waitpid(child, NULL, 0); /* wait for command completion */
}

static char** append_str_to_vector(char **vec, unsigned &size, const char *str)
{
    //log("old vec: %p", vec);
    vec = (char**) xrealloc(vec, (size+2) * sizeof(vec[0]));
    vec[size] = xstrdup(str);
    //log("new vec: %p, added [%d] %p", vec, size, vec[size]);
    size++;
    vec[size] = NULL;
    return vec;
}

std::string CMailx::Report(const map_crash_data_t& pCrashData,
                const map_plugin_settings_t& pSettings,
                const char *pArgs)
{
    char **args = NULL;
    unsigned arg_size = 0;
    args = append_str_to_vector(args, arg_size, MAILX_COMMAND);

//TODO: move email body generation to make_descr.cpp
    std::string binaryFiles, commonFiles, additionalFiles, DUPHASHFile;
    map_crash_data_t::const_iterator it;
    for (it = pCrashData.begin(); it != pCrashData.end(); it++)
    {
        if (it->second[CD_TYPE] == CD_TXT)
        {
            if (it->first != CD_DUPHASH
             && it->first != FILENAME_ARCHITECTURE
             && it->first != FILENAME_KERNEL
             && it->first != FILENAME_PACKAGE
            ) {
                additionalFiles += it->first;
                additionalFiles += "\n-----\n";
                additionalFiles += it->second[CD_CONTENT];
                additionalFiles += "\n\n";
            }
            else if (it->first == CD_DUPHASH)
            {
                DUPHASHFile += it->first;
                DUPHASHFile += "\n-----\n";
                DUPHASHFile += it->second[CD_CONTENT];
                DUPHASHFile += "\n\n";
            }
            else
            {
                commonFiles += it->first;
                commonFiles += "\n-----\n";
                commonFiles += it->second[CD_CONTENT];
                commonFiles += "\n\n";
            }
        }
        if (it->second[CD_TYPE] == CD_BIN)
        {
            binaryFiles += " -a ";
            binaryFiles += it->second[CD_CONTENT];
            if (m_bSendBinaryData)
            {
                args = append_str_to_vector(args, arg_size, "-a");
                args = append_str_to_vector(args, arg_size, it->second[CD_CONTENT].c_str());
            }
        }
    }

    std::string emailBody = "Duplicate check\n";
    emailBody += "=====\n\n";
    emailBody += DUPHASHFile;
    emailBody += "\nCommon information\n";
    emailBody += "=====\n\n";
    emailBody += commonFiles;
    emailBody += "\nAdditional information\n";
    emailBody += "=====\n\n";
    emailBody += additionalFiles;
    emailBody += '\n';

    args = append_str_to_vector(args, arg_size, "-s");
    args = append_str_to_vector(args, arg_size, (pArgs[0] != '\0' ? pArgs : m_sSubject.c_str()));
    args = append_str_to_vector(args, arg_size, "-r");
    args = append_str_to_vector(args, arg_size, m_sEmailFrom.c_str());
    args = append_str_to_vector(args, arg_size, m_sEmailTo.c_str());

    update_client(_("Sending an email..."));
    const char *uid_str = get_crash_data_item_content(pCrashData, FILENAME_UID).c_str();
    exec_and_feed_input(xatoi_u(uid_str), emailBody.c_str(), args);

    while (*args)
    {
        free(*args++);
    }
    args -= arg_size;
    free(args);

    return "Email was sent to: " + m_sEmailTo;
}

void CMailx::SetSettings(const map_plugin_settings_t& pSettings)
{
    m_pSettings = pSettings;

    map_plugin_settings_t::const_iterator end = pSettings.end();
    map_plugin_settings_t::const_iterator it;
    it = pSettings.find("Subject");
    if (it != end)
    {
        m_sSubject = it->second;
    }
    it = pSettings.find("EmailFrom");
    if (it != end)
    {
        m_sEmailFrom = it->second;
    }
    it = pSettings.find("EmailTo");
    if (it != end)
    {
        m_sEmailTo = it->second;
    }
    it = pSettings.find("SendBinaryData");
    if (it != end)
    {
        m_bSendBinaryData = string_to_bool(it->second.c_str());
    }
}

//ok to delete?
//const map_plugin_settings_t& CMailx::GetSettings()
//{
//    m_pSettings["Subject"] = m_sSubject;
//    m_pSettings["EmailFrom"] = m_sEmailFrom;
//    m_pSettings["EmailTo"] = m_sEmailTo;
//    m_pSettings["SendBinaryData"] = m_bSendBinaryData ? "yes" : "no";
//
//    return m_pSettings;
//}

PLUGIN_INFO(REPORTER,
            CMailx,
            "Mailx",
            "0.0.2",
            "Sends an email with a report (via mailx command)",
            "zprikryl@redhat.com",
            "https://fedorahosted.org/abrt/wiki",
            PLUGINS_LIB_DIR"/Mailx.GTKBuilder");