summaryrefslogtreecommitdiffstats
path: root/lib/Plugins/StrataSignature.cpp
blob: 5f525fd137b587564e1b3f156d425c0209c8ec5d (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
/*
    StrataSignature.cpp

    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 "abrtlib.h"
#include "abrt_xmlrpc.h" /* for xcurl_easy_init */
#include "StrataSignature.h"
#include "DebugDump.h"
#include "ABRTException.h"
#include "CommLayerInner.h"

#include "strata_client.h"

using namespace std;


CStrataSignature::CStrataSignature()
{}

CStrataSignature::~CStrataSignature()
{}

string CStrataSignature::Report(const map_crash_data_t& pCrashData,
                const map_plugin_settings_t& pSettings,
                const char *pArgs)
{
    string URL;

    map_plugin_settings_t settings = parse_settings(pSettings);
    if (!settings.empty())
    {
        URL = settings["URL"];
    }
    else
    {
        URL = m_sURL;
    }
    update_client(_("Creating a signature..."));

    reportfile_t* file = reportfile_start(1);
    if (!file)
    {
	throw CABRTException(EXCEP_PLUGIN, "%s", strata_client_strerror());
    }

    // Copy each entry into the tarball root.
    // Files are simply copied, strings are written to a file
    // TODO: some files are totally useless:
    // "Reported", "Message" (plugin's output), "DumpDir",
    // "Description" (package description) - maybe skip those?
    int rc;
    map_crash_data_t::const_iterator it;
    for (it = pCrashData.begin(); it != pCrashData.end(); it++)
    {
        const char *content = it->second[CD_CONTENT].c_str();
        if (it->second[CD_TYPE] == CD_TXT)
	{
	  rc = reportfile_add_binding_from_string(file, 
						  it->first.c_str(), 
						  content, 0, 0);
	    if (rc < 0) 
	    {
		throw CABRTException(EXCEP_PLUGIN, "%s", strata_client_strerror());
	    }
        }
        else if (it->second[CD_TYPE] == CD_BIN)
        {
	    rc = reportfile_add_binding_from_namedfile(file, 
						       it->first.c_str(),
						       content, 1, content);
	    if (rc < 0) 
	    {
		throw CABRTException(EXCEP_PLUGIN, "%s", strata_client_strerror());
	    } 
        }
    }

    rc = reportfile_end(file);
    if (rc < 0) 
    {
	throw CABRTException(EXCEP_PLUGIN, "%s", strata_client_strerror());
    } 
    
    char* signature = reportfile_as_string(file);
    if (!signature) 
    {
	throw CABRTException(EXCEP_PLUGIN, "%s", strata_client_strerror());
    } 
  
    rc = reportfile_free(file);
    if (rc < 0)
    {
	throw CABRTException(EXCEP_PLUGIN, "%s", strata_client_strerror());
    } 

    update_client(_("Creating a signature..."));
    const char* p = post_signature(URL.c_str(), signature);
    if (!p)
    {
	throw CABRTException(EXCEP_PLUGIN, "%s", strata_client_strerror());
    }

    string msg = p;
    free((void*)p);
    return msg;
}

void CStrataSignature::SetSettings(const map_plugin_settings_t& pSettings)
{
  log("entering CStrataSignature::SetSettings");
    m_pSettings = pSettings;

    map_plugin_settings_t::const_iterator end = pSettings.end();
    map_plugin_settings_t::const_iterator it;
    it = pSettings.find("URL");
    if (it != end)
    {
        m_sURL = it->second;
    }
}

const map_plugin_settings_t& CStrataSignature::GetSettings()
{
    m_pSettings["URL"] = m_sURL;
    return m_pSettings;
}

//todo: make static
map_plugin_settings_t CStrataSignature::parse_settings(const map_plugin_settings_t& pSettings)
{
    map_plugin_settings_t plugin_settings;

    map_plugin_settings_t::const_iterator end = pSettings.end();
    map_plugin_settings_t::const_iterator it;

    it = pSettings.find("URL");
    if (it == end)
    {
        plugin_settings.clear();
        return plugin_settings;
    }
    plugin_settings["URL"] = it->second;
    VERB1 log("User settings ok, using them instead of defaults");
    return plugin_settings;
}

PLUGIN_INFO(REPORTER,
            CStrataSignature,
            "StrataSignature",
            "0.0.1",
            "Sends a signature to a Strata signature service.",
            "gavin@redhat.com",
            "https://fedorahosted.org/abrt/wiki",
            PLUGINS_LIB_DIR"/StrataSignature.GTKBuilder");