2008-02-20 20:30:45 +00:00
|
|
|
/* Conky, a system monitor, based on torsmo
|
2007-06-01 15:49:49 +00:00
|
|
|
*
|
2007-08-10 19:53:44 +00:00
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Toni Spets
|
2008-03-31 04:56:39 +00:00
|
|
|
* Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
|
2008-02-20 20:30:45 +00:00
|
|
|
* (see AUTHORS)
|
2007-08-10 19:53:44 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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 3 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
|
2008-02-20 20:30:45 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-08-10 19:53:44 +00:00
|
|
|
*
|
2008-02-20 20:30:45 +00:00
|
|
|
* $Id$ */
|
2007-05-06 12:59:31 +00:00
|
|
|
|
2008-03-29 12:44:29 +00:00
|
|
|
#include "conky.h"
|
|
|
|
#include "prss.h"
|
2007-06-01 10:42:57 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <assert.h>
|
2007-06-01 11:37:19 +00:00
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <curl/types.h>
|
|
|
|
#include <curl/easy.h>
|
2007-05-06 12:59:31 +00:00
|
|
|
|
2007-06-01 15:49:49 +00:00
|
|
|
#define MAX_FEEDS 16
|
2007-06-01 10:42:57 +00:00
|
|
|
|
2007-06-01 11:37:19 +00:00
|
|
|
struct MemoryStruct {
|
|
|
|
char *memory;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
2007-06-01 15:49:49 +00:00
|
|
|
typedef struct feed_ {
|
2008-02-20 20:30:45 +00:00
|
|
|
char *uri;
|
2007-06-01 15:49:49 +00:00
|
|
|
int last_update;
|
2008-02-20 20:30:45 +00:00
|
|
|
PRSS *data;
|
2007-06-01 15:49:49 +00:00
|
|
|
} feed;
|
|
|
|
|
|
|
|
int num_feeds = 0;
|
|
|
|
feed feeds[MAX_FEEDS];
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
|
2007-06-01 11:37:19 +00:00
|
|
|
{
|
|
|
|
size_t realsize = size * nmemb;
|
2008-02-20 20:30:45 +00:00
|
|
|
struct MemoryStruct *mem = (struct MemoryStruct *) data;
|
2007-06-01 11:37:19 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
mem->memory = (char *) realloc(mem->memory, mem->size + realsize + 1);
|
2007-06-01 11:37:19 +00:00
|
|
|
if (mem->memory) {
|
|
|
|
memcpy(&(mem->memory[mem->size]), ptr, realsize);
|
|
|
|
mem->size += realsize;
|
|
|
|
mem->memory[mem->size] = 0;
|
|
|
|
}
|
|
|
|
return realsize;
|
|
|
|
}
|
|
|
|
|
2008-06-19 06:17:53 +00:00
|
|
|
int rss_delay(int *wait_time, int delay)
|
2007-05-06 12:59:31 +00:00
|
|
|
{
|
2007-06-01 10:42:57 +00:00
|
|
|
time_t now = time(NULL);
|
2007-05-06 12:59:31 +00:00
|
|
|
|
2007-06-01 11:37:19 +00:00
|
|
|
// make it minutes
|
2008-02-20 20:30:45 +00:00
|
|
|
if (delay < 1) {
|
|
|
|
delay = 1;
|
|
|
|
}
|
2007-06-01 11:46:14 +00:00
|
|
|
delay *= 60;
|
2007-06-01 11:37:19 +00:00
|
|
|
|
2008-06-19 06:17:53 +00:00
|
|
|
if (!*wait_time) {
|
|
|
|
*wait_time = now + delay;
|
2007-06-01 10:42:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2007-05-06 12:59:31 +00:00
|
|
|
|
2008-06-19 06:17:53 +00:00
|
|
|
if (now >= *wait_time + delay) {
|
|
|
|
*wait_time = now + delay;
|
2007-06-01 10:42:57 +00:00
|
|
|
return 1;
|
2007-05-06 12:59:31 +00:00
|
|
|
}
|
|
|
|
|
2007-06-01 10:42:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
void init_rss_info(void)
|
2007-06-03 11:20:47 +00:00
|
|
|
{
|
|
|
|
int i;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
for (i = 0; i < MAX_FEEDS; i++) {
|
2007-06-03 11:20:47 +00:00
|
|
|
feeds[i].uri = NULL;
|
|
|
|
feeds[i].data = NULL;
|
|
|
|
feeds[i].last_update = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
void free_rss_info(void)
|
2007-06-03 11:20:47 +00:00
|
|
|
{
|
|
|
|
int i;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
for (i = 0; i < num_feeds; i++) {
|
|
|
|
if (feeds[i].uri != NULL) {
|
2007-06-03 11:20:47 +00:00
|
|
|
free(feeds[i].uri);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-03 11:20:47 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
PRSS *get_rss_info(char *uri, int delay)
|
2007-06-01 10:42:57 +00:00
|
|
|
{
|
2007-06-01 11:37:19 +00:00
|
|
|
CURL *curl = NULL;
|
|
|
|
CURLcode res;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2007-06-01 15:49:49 +00:00
|
|
|
// pointers to struct
|
|
|
|
feed *curfeed = NULL;
|
|
|
|
PRSS *curdata = NULL;
|
|
|
|
int *last_update = 0;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
2008-03-29 06:24:04 +00:00
|
|
|
// curl temps
|
|
|
|
struct MemoryStruct chunk;
|
|
|
|
|
|
|
|
chunk.memory = NULL;
|
|
|
|
chunk.size = 0;
|
|
|
|
|
2007-06-01 15:49:49 +00:00
|
|
|
// first seek for the uri in list
|
2008-02-20 20:30:45 +00:00
|
|
|
for (i = 0; i < num_feeds; i++) {
|
|
|
|
if (feeds[i].uri != NULL) {
|
|
|
|
if (!strcmp(feeds[i].uri, uri)) {
|
2007-06-03 11:20:47 +00:00
|
|
|
curfeed = &feeds[i];
|
|
|
|
break;
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-06-01 15:49:49 +00:00
|
|
|
}
|
2007-06-01 10:42:57 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (!curfeed) { // new feed
|
|
|
|
if (num_feeds == MAX_FEEDS - 1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-06-01 15:49:49 +00:00
|
|
|
curfeed = &feeds[num_feeds];
|
2008-04-02 18:44:49 +00:00
|
|
|
curfeed->uri = strndup(uri, text_buffer_size);
|
2007-06-01 15:49:49 +00:00
|
|
|
num_feeds++;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_update = &curfeed->last_update;
|
|
|
|
curdata = curfeed->data;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (!rss_delay(last_update, delay)) {
|
|
|
|
return curdata; // wait for delay to pass
|
|
|
|
}
|
2007-06-01 15:49:49 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (curdata != NULL) {
|
|
|
|
prss_free(curdata); // clean up old data
|
2007-06-03 08:58:05 +00:00
|
|
|
curdata = NULL;
|
|
|
|
}
|
2007-05-06 12:59:31 +00:00
|
|
|
|
2007-06-01 11:37:19 +00:00
|
|
|
curl = curl_easy_init();
|
2008-02-20 20:30:45 +00:00
|
|
|
if (curl) {
|
2007-06-01 11:37:19 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, uri);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
|
2008-02-20 20:30:45 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &chunk);
|
2007-06-01 11:37:19 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, "conky-rss/1.0");
|
|
|
|
|
|
|
|
res = curl_easy_perform(curl);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (chunk.size) {
|
2007-06-01 15:49:49 +00:00
|
|
|
curdata = prss_parse_data(chunk.memory);
|
2007-06-01 11:37:19 +00:00
|
|
|
free(chunk.memory);
|
2008-02-20 20:30:45 +00:00
|
|
|
} else {
|
2007-06-02 08:17:33 +00:00
|
|
|
ERR("No data from server");
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-06-01 11:37:19 +00:00
|
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
}
|
2007-05-06 12:59:31 +00:00
|
|
|
|
2007-06-03 08:58:05 +00:00
|
|
|
curfeed->data = curdata;
|
|
|
|
|
2007-06-01 15:49:49 +00:00
|
|
|
return curdata;
|
2007-05-06 12:59:31 +00:00
|
|
|
}
|