#include "eve.h"
#include "config.h"
+#include "text_object.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
+#define MAXCHARS 4
+#define EVE_UPDATE_DELAY 60
+
+typedef struct {
+ char *charid;
+ char *skillname;
+ char *time;
+ char *lastOutput;
+
+ struct tm ends;
+ struct tm cache;
+
+ time_t delay;
+
+ int level;
+ int skill;
+} Character;
+
+struct xmlData {
+ char *data;
+ size_t size;
+};
+
int num_chars = 0;
Character eveCharacters[MAXCHARS];
return 0;
}
-char *getXmlFromAPI(const char *userid, const char *apikey, const char *charid, const char *url)
+static char *getXmlFromAPI(const char *userid, const char *apikey, const char *charid, const char *url)
{
struct curl_httppost *post = NULL;
struct curl_httppost *last = NULL;
return content;
}
-void init_eve(void)
+static void init_eve(void)
{
int i;
}
}
-char *eve(char *userid, char *apikey, char *charid)
+static int isCacheValid(struct tm cached)
{
- Character *chr = NULL;
- const char *skillfile = "/tmp/.cesf";
- int i = 0;
- char *output = 0;
- char *timel = 0;
- char *skill = 0;
- char *content = 0;
+ struct timeval tv;
+ struct timezone tz;
+ double offset = 0;
time_t now = 0;
- char *error = 0;
-
-
- for (i = 0; i < MAXCHARS; i++) {
- if (eveCharacters[i].charid != NULL) {
- if (strcasecmp(eveCharacters[i].charid, charid) == 0) {
- chr = &eveCharacters[i];
- break;
- }
- }
- }
-
- if (!chr) {
- if (num_chars == MAXCHARS - 1)
- return NULL;
- chr = &eveCharacters[num_chars];
- chr->charid = strdup(charid);
- num_chars++;
- }
-
- if (chr->delay > 0) {
- now = time(NULL);
- if (now < chr->delay) {
- output = strdup("Server error");
- return output;
- } else
- chr->delay = 0;
- }
-
- if (isCacheValid(chr->cache)) {
- output = (char *)malloc(200 * sizeof(char));
- timel = strdup(formatTime(&chr->ends));
- sprintf(output, EVE_OUTPUT_FORMAT, chr->skillname, chr->level, timel);
- free(timel);
- return output;
- } else {
- content = getXmlFromAPI(userid, apikey, charid, EVEURL_TRAINING);
- if (content == NULL) {
- error = strdup("Server error");
- now = time(NULL);
- now += (time_t) 1800;
- chr->delay = now;
- return error;
- }
-
- if (parseTrainingXml(content, chr)) {
- output = (char *)malloc(200 * sizeof(char));
- sprintf(output, "API error");
- return output;
- }
-
- output = (char *)malloc(200 * sizeof(char));
- timel = formatTime(&chr->ends);
- skill = getSkillname(skillfile, chr->skill);
-
- chr->skillname = strdup(skill);
+ time_t cache = 0;
+ double diff = 0;
- sprintf(output, EVE_OUTPUT_FORMAT, chr->skillname, chr->level, timel);
- free(skill);
- return output;
- }
+ gettimeofday(&tv, &tz);
+ offset = (double)(tz.tz_minuteswest * 60);
+ now = time(NULL);
+ cache = mktime(&cached);
+ diff = difftime(cache, now);
+ if (diff < offset)
+ return 0;
+ else
+ return 1;
}
-char *formatTime(struct tm *ends)
+static char *formatTime(struct tm *ends)
{
struct timeval tv;
struct timezone tz;
}
}
-int isCacheValid(struct tm cached)
-{
- struct timeval tv;
- struct timezone tz;
- double offset = 0;
- time_t now = 0;
- time_t cache = 0;
- double diff = 0;
-
- gettimeofday(&tv, &tz);
- offset = (double)(tz.tz_minuteswest * 60);
- now = time(NULL);
- cache = mktime(&cached);
- diff = difftime(cache, now);
-
- if (diff < offset)
- return 0;
- else
- return 1;
-}
-
-int file_exists(const char *filename)
+static int file_exists(const char *filename)
{
struct stat fi;
return 0;
}
-void writeSkilltree(char *content, const char *filename)
+static void writeSkilltree(char *content, const char *filename)
{
FILE *fp = fopen(filename, "w");
fwrite(content, sizeof(char), strlen(content), fp);
fclose(fp);
}
-char *getSkillname(const char *file, int skillid)
+static char *getSkillname(const char *file, int skillid)
{
char *skilltree;
char *skill = NULL;
return skill;
}
+
+static char *eve(char *userid, char *apikey, char *charid)
+{
+ Character *chr = NULL;
+ const char *skillfile = "/tmp/.cesf";
+ int i = 0;
+ char *output = 0;
+ char *timel = 0;
+ char *skill = 0;
+ char *content = 0;
+ time_t now = 0;
+ char *error = 0;
+
+
+ for (i = 0; i < MAXCHARS; i++) {
+ if (eveCharacters[i].charid != NULL) {
+ if (strcasecmp(eveCharacters[i].charid, charid) == 0) {
+ chr = &eveCharacters[i];
+ break;
+ }
+ }
+ }
+
+ if (!chr) {
+ if (num_chars == MAXCHARS - 1)
+ return NULL;
+ chr = &eveCharacters[num_chars];
+ chr->charid = strdup(charid);
+ num_chars++;
+ }
+
+ if (chr->delay > 0) {
+ now = time(NULL);
+ if (now < chr->delay) {
+ output = strdup("Server error");
+ return output;
+ } else
+ chr->delay = 0;
+ }
+
+ if (isCacheValid(chr->cache)) {
+ output = (char *)malloc(200 * sizeof(char));
+ timel = strdup(formatTime(&chr->ends));
+ sprintf(output, EVE_OUTPUT_FORMAT, chr->skillname, chr->level, timel);
+ free(timel);
+ return output;
+ } else {
+ content = getXmlFromAPI(userid, apikey, charid, EVEURL_TRAINING);
+ if (content == NULL) {
+ error = strdup("Server error");
+ now = time(NULL);
+ now += (time_t) 1800;
+ chr->delay = now;
+ return error;
+ }
+
+ if (parseTrainingXml(content, chr)) {
+ output = (char *)malloc(200 * sizeof(char));
+ sprintf(output, "API error");
+ return output;
+ }
+
+ output = (char *)malloc(200 * sizeof(char));
+ timel = formatTime(&chr->ends);
+ skill = getSkillname(skillfile, chr->skill);
+
+ chr->skillname = strdup(skill);
+
+ sprintf(output, EVE_OUTPUT_FORMAT, chr->skillname, chr->level, timel);
+ free(skill);
+ return output;
+ }
+
+}
+
+void scan_eve(struct text_object *obj, const char *arg)
+{
+ int argc;
+ char *userid = (char *) malloc(20 * sizeof(char));
+ char *apikey = (char *) malloc(64 * sizeof(char));
+ char *charid = (char *) malloc(20 * sizeof(char));
+
+ argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
+ obj->data.eve.charid = charid;
+ obj->data.eve.userid = userid;
+ obj->data.eve.apikey = apikey;
+
+ init_eve();
+}
+
+void print_eve(struct text_object *obj, char *p, int p_max_size)
+{
+ snprintf(p, p_max_size, "%s",
+ eve(obj->data.eve.userid,
+ obj->data.eve.apikey, obj->data.eve.charid));
+}