-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.c
85 lines (71 loc) · 2.39 KB
/
report.c
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright 2007 Pascal Terjan <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* 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., 59 Temple Street #330, Boston, MA 02111-1307, USA.
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <glib/gbase64.h>
#include <glib/gmem.h>
#include <glib/gmessages.h>
gboolean send_report(char *message, const char *user, const char *password);
gboolean send_report(char *message, const char *user, const char *password)
{
CURL *curl;
CURLcode res;
char *post, *msg, *userpwd;
long response;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (!curl) {
free(message);
g_critical("signal-spam: CURL initialisation failed.");
return FALSE;
}
curl_easy_setopt(curl, CURLOPT_URL, "https://www.signal-spam.fr/api/signaler");
msg = g_base64_encode(message, strlen(message));
free(message);
post = (char*)malloc(strlen("message=")+strlen(msg)+1);
sprintf(post, "message=%s", msg);
g_free(msg);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post);
userpwd = (char*)malloc(strlen(user)+strlen(password)+2);
sprintf(userpwd, "%s:%s", user, password);
curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd);
res = curl_easy_perform(curl);
free(post);
free(userpwd);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
curl_easy_cleanup(curl);
switch (response) {
case 202: /* Accepted */
break;
case 400: /* Bad Request */
g_warning("signal-spam: Bad Request");
return FALSE;
case 401: /* Not Authorized */
g_warning("signal-spam: Wrong login or password");
return FALSE;
default:
g_warning("signal-spam: unexpected response %ld", response);
return (res==0);
}
return TRUE;
}