summaryrefslogtreecommitdiff
path: root/rtclient.c
blob: a0c7c8f35676882f3b4764e0a1781d871ddc3ca9 (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
#ifdef DEBUG
#ifdef ANDROID
#include <android/log.h>
#else
#include <stdio.h>
#endif // ANDROID
#endif // DEBUG
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
#include "rtclient.h"

typedef struct rt_user rt_user;
static CURL *curl = NULL;
static char *server_url = NULL;

bool rtclient_init(const char *url)
{
	curl_global_init(CURL_GLOBAL_SSL);
	curl = curl_easy_init();
	if (curl) {
		curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
		curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
		curl_easy_setopt(curl, CURLOPT_REFERER, url);
#ifdef DEBUG
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
#endif // DEBUG
		server_url = malloc(strlen(url) + 1);
		strcpy(server_url, url);
	}

	return (bool)curl;
}

void rtclient_login(const char *name, const char *password)
{
	struct curl_httppost *post, *last = NULL;
	curl_formadd(&post, &last,
			CURLFORM_COPYNAME, "user",
			CURLFORM_PTRCONTENTS, name,
			CURLFORM_END);
	curl_formadd(&post, &last,
			CURLFORM_COPYNAME, "pass",
			CURLFORM_PTRCONTENTS, password,
			CURLFORM_END);
	last = NULL;

	curl_easy_setopt(curl, CURLOPT_URL, server_url);
	curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
#ifdef DEBUG
	CURLcode res =
#endif // DEBUG
		curl_easy_perform(curl);
#ifdef DEBUG
	if (res != CURLE_OK) {
		const char *error = curl_easy_strerror(res);
#ifdef ANDROID
		__android_log_print(ANDROID_LOG_ERROR, "librtclient.so", "cURL perform error: %s", error);
#else
		fprintf(stderr, "cURL perform error: %s\n", error);
#endif // ANDROID
	}
#endif // DEBUG
}

static size_t
user_callback(void *contents, size_t size, size_t nmemb, void *writedata)
{
	size_t realsize = size * nmemb;
	char response[realsize + 1];
	memcpy(&response[0], contents, realsize);
	response[realsize] = '\0';

	static const unsigned short nproperties = 24;
	char *lines[nproperties];
	char *line = strtok(response, "\n");
	if (strstr(line, "200 Ok")) {
		rt_user *user = (rt_user *)writedata;
		user = malloc(sizeof(rt_user));
		line = strtok(NULL, "\n");
		unsigned short i = 0;
		while (line && i < nproperties) {
			lines[i++] = line;
			line = strtok(NULL, "\n");
		}
		for (unsigned short i = 0; i < nproperties; i++) {
			char *token = strtok(lines[i], ":");
#ifdef DEBUG
#ifdef ANDROID
			__android_log_print(ANDROID_LOG_DEBUG, "librtclient.so"
					, "Key: %s", token);
#else
			fprintf(stderr, "Key: %s\n", token);
#endif // ANDROID
#endif // DEBUG
			token = strtok(NULL, ":");
#ifdef DEBUG
#ifdef ANDROID
			__android_log_print(ANDROID_LOG_DEBUG, "librtclient.so"
					, "Value: %s", ++token);
#else
			fprintf(stderr, "Value: %s\n", ++token);
#endif // ANDROID
#endif // DEBUG
		}
		free(user);
	}

	return realsize;
}

static inline void request(const char *path, const char *suffix)
{
	char url[strlen(server_url) + strlen(path) + strlen(suffix) + 1];
	sprintf(url, "%s%s%s", server_url, path, suffix);
	curl_easy_setopt(curl, CURLOPT_URL, url);
	curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
	curl_easy_perform(curl);
}

rt_user *rtclient_user(const char *name)
{
	rt_user *user = NULL;
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, user);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, user_callback);
	request("/REST/1.0/user/", name);
	return user;
}

void rtclient_search(const char *query)
{
	request("/REST/1.0/search/ticket?query=", query);
}

void rtclient_cleanup()
{
	if (curl) {
		free(server_url);
		curl_easy_cleanup(curl);
	}
	curl_global_cleanup();
}