1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Copyright (c) 2008 Asbjørn Zweidorff Kjær
7 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "text_object.h"
32 #include <sys/types.h>
35 #include <libxml/parser.h>
36 #include <libxml/tree.h>
37 #include <libxml/xmlwriter.h>
39 #include <curl/curl.h>
40 #include <curl/types.h>
41 #include <curl/easy.h>
46 #define EVE_UPDATE_DELAY 60
75 Character eveCharacters[MAXCHARS];
77 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
80 struct xmlData *data = 0;
81 data = (struct xmlData *)stream;
82 realsize = size * nmemb;
84 data->data = (char *)realloc(data->data, data->size + realsize + 1);
86 memcpy(&(data->data[data->size]), ptr, realsize);
87 data->size += realsize;
88 data->data[data->size] = '\0';
94 int parseTrainingXml(char *data, Character * s)
96 char *skill, *level, *ends, *cache;
100 struct tm end_tm, cache_tm;
105 doc = xmlReadMemory(data, strlen(data), "", NULL, 0);
106 root = xmlDocGetRootElement(doc);
107 for (n = root->children; n; n = n->next) {
108 if (n->type == XML_ELEMENT_NODE) {
109 if (!strcasecmp((const char *)n->name, "error")) {
111 } else if (!strcasecmp((const char *)n->name, "result")) {
113 for (c = n->children; c; c = c->next) {
114 if (!strcasecmp((const char *)c->name, "trainingEndTime")) {
115 ends = (char *)c->children->content;
116 } else if (!strcasecmp((const char *)c->name, "trainingTypeID")) {
117 if (c->children->content)
118 skill = (char *)c->children->content;
119 } else if (!strcasecmp((const char *)c->name, "trainingToLevel")) {
120 level = (char *)c->children->content;
123 } else if (!strcasecmp((const char *)n->name, "cachedUntil")) {
124 cache = (char *)n->children->content;
129 strptime(ends, "%Y-%m-%d %H:%M:%S", &end_tm);
130 strptime(cache, "%Y-%m-%d %H:%M:%S", &cache_tm);
131 s->skill = atoi(skill);
132 s->level = atoi(level);
140 static char *getXmlFromAPI(const char *userid, const char *apikey, const char *charid, const char *url)
142 struct curl_httppost *post = NULL;
143 struct curl_httppost *last = NULL;
153 curl_global_init(CURL_GLOBAL_ALL);
154 curl_handle = curl_easy_init();
155 curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1);
156 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
157 curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);
158 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
159 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chr);
161 if (userid != NULL && apikey != NULL && charid != NULL) {
162 curl_formadd(&post, &last, CURLFORM_COPYNAME, "userID", CURLFORM_COPYCONTENTS, userid, CURLFORM_END);
163 curl_formadd(&post, &last, CURLFORM_COPYNAME, "apiKey", CURLFORM_COPYCONTENTS, apikey, CURLFORM_END);
164 curl_formadd(&post, &last, CURLFORM_COPYNAME, "characterID", CURLFORM_COPYCONTENTS, charid, CURLFORM_END);
166 curl_easy_setopt(curl_handle, CURLOPT_HTTPPOST, post);
169 if ((rc = curl_easy_perform(curl_handle)) != CURLE_OK) {
173 content = strdup(chr.data);
174 curl_easy_cleanup(curl_handle);
179 static void init_eve(void)
183 for (i = 0; i < MAXCHARS; i++) {
184 eveCharacters[i].charid = NULL;
185 eveCharacters[i].skillname = NULL;
186 eveCharacters[i].time = NULL;
187 eveCharacters[i].level = 0;
188 eveCharacters[i].skill = 0;
189 eveCharacters[i].delay = 0;
193 static int isCacheValid(struct tm cached)
202 gettimeofday(&tv, &tz);
203 offset = (double)(tz.tz_minuteswest * 60);
205 cache = mktime(&cached);
206 diff = difftime(cache, now);
214 static char *formatTime(struct tm *ends)
225 gettimeofday(&tv, &tz);
226 offset = (double)(tz.tz_minuteswest * 60);
228 tEnds = mktime(ends);
235 int days = (int)(diff / 60 / 60 / 24);
236 int hours = (int)((diff / 60 / 60) - (days * 24));
237 int minutes = (int)((diff / 60) - ((hours * 60) + (days * 60 * 24)));
238 int seconds = (int)(diff - ((minutes * 60) + (hours * 60 * 60) + (days * 60 * 60 * 24)));
239 char *output = malloc(100 * sizeof(char));
242 sprintf(output, "%dd, %dh, %02dm and %02ds", days, hours, minutes, seconds);
244 sprintf(output, "%dh, %02dm and %02ds", hours, minutes, seconds);
246 sprintf(output, "%02dm and %02ds", minutes, seconds);
250 char *output = strdup("Done");
255 static int file_exists(const char *filename)
259 if ((stat(filename, &fi)) == 0) {
268 static void writeSkilltree(char *content, const char *filename)
270 FILE *fp = fopen(filename, "w");
271 fwrite(content, sizeof(char), strlen(content), fp);
275 static char *getSkillname(const char *file, int skillid)
283 if (!file_exists(file)) {
284 skilltree = getXmlFromAPI(NULL, NULL, NULL, EVEURL_SKILLTREE);
285 writeSkilltree(skilltree, file);
289 doc = xmlReadFile(file, NULL, 0);
293 root = xmlDocGetRootElement(doc);
295 for (n = root->children; n; n = n->next) {
297 for (o = n->children; o; o = o->next) {
299 for (p = o->children; p; p = p->next) {
301 for (q = p->children; q; q = q->next) {
303 for (r = q->children; r; r = r->next) {
304 xmlElementPtr ele = (xmlElementPtr) r;
305 xmlAttrPtr attr = (xmlAttrPtr) ele->attributes;
307 int id, assigned = 0;
309 while (attr != NULL) {
310 if (!strcasecmp((const char *)attr->name, "typeName")) {
311 mySkill = strdup((const char *)attr->children->content);
313 } else if (!strcasecmp((const char *)attr->name, "typeID")) {
314 id = atoi((const char *)attr->children->content);
320 skill = strdup(mySkill);
337 static char *eve(char *userid, char *apikey, char *charid)
339 Character *chr = NULL;
340 const char *skillfile = "/tmp/.cesf";
350 for (i = 0; i < MAXCHARS; i++) {
351 if (eveCharacters[i].charid != NULL) {
352 if (strcasecmp(eveCharacters[i].charid, charid) == 0) {
353 chr = &eveCharacters[i];
360 if (num_chars == MAXCHARS - 1)
362 chr = &eveCharacters[num_chars];
363 chr->charid = strdup(charid);
367 if (chr->delay > 0) {
369 if (now < chr->delay) {
370 output = strdup("Server error");
376 if (isCacheValid(chr->cache)) {
377 output = (char *)malloc(200 * sizeof(char));
378 timel = strdup(formatTime(&chr->ends));
379 sprintf(output, EVE_OUTPUT_FORMAT, chr->skillname, chr->level, timel);
383 content = getXmlFromAPI(userid, apikey, charid, EVEURL_TRAINING);
384 if (content == NULL) {
385 error = strdup("Server error");
387 now += (time_t) 1800;
392 if (parseTrainingXml(content, chr)) {
393 output = (char *)malloc(200 * sizeof(char));
394 sprintf(output, "API error");
398 output = (char *)malloc(200 * sizeof(char));
399 timel = formatTime(&chr->ends);
400 skill = getSkillname(skillfile, chr->skill);
402 chr->skillname = strdup(skill);
404 sprintf(output, EVE_OUTPUT_FORMAT, chr->skillname, chr->level, timel);
411 void scan_eve(struct text_object *obj, const char *arg)
416 ed = malloc(sizeof(struct eve_data));
417 memset(ed, 0, sizeof(struct eve_data));
419 argc = sscanf(arg, "%20s %64s %20s", ed->userid, ed->apikey, ed->charid);
422 obj->data.opaque = ed;
425 void print_eve(struct text_object *obj, char *p, int p_max_size)
427 struct eve_data *ed = obj->data.opaque;
432 snprintf(p, p_max_size, "%s", eve(ed->userid, ed->apikey, ed->charid));
435 void free_eve(struct text_object *obj)
437 if (obj->data.opaque) {
438 free(obj->data.opaque);
439 obj->data.opaque = NULL;