X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Fmboxscan.c;h=c9aef48dda53c13dc512d153d18236f133558409;hb=349f5fc1c69755e942eb1616501fada58f17136c;hp=7387441a5a8e0bcf517220c9e3175d153c65f3bc;hpb=0ab1d8b96ad5925c497855c4e09fcde1dc24ef74;p=monky diff --git a/src/mboxscan.c b/src/mboxscan.c index 7387441..c9aef48 100644 --- a/src/mboxscan.c +++ b/src/mboxscan.c @@ -1,29 +1,40 @@ -/* - * $Id$ - * - * Licence: see LICENSE - * author: mac@calmar.ws Marco Candrian - * - * Modified for use in Conky by Brenden Matthews +/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*- + * vim: ts=4 sw=4 noet ai cindent syntax=c * - * Description: - * scanning from top to bottom on a mbox - * The output as follows: - * F: FROM_LENGHT S: SUBJECT_LENGHT + * Conky, a system monitor, based on torsmo + * + * 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) 2006 Marco Candrian + * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al. + * (see AUTHORS) + * 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 + * along with this program. If not, see . * - * Usage: ${mboxscan [-n ] - * [-fw ] [-sw ] - * [-t "mbox" } */ #include "conky.h" +#include "logging.h" +#include "mail.h" +#include "text_object.h" #include #include #include -#include -#include -#include -#include "mboxscan.h" #define FROM_WIDTH 10 #define SUBJECT_WIDTH 22 @@ -44,35 +55,19 @@ static double last_update; static int args_ok = 0; static int from_width; static int subject_width; -static int print_mails; +static int print_num_mails; static int time_delay; -/* - * I don't know what to use: TEXT_BUFFER_SIZE or text_buffer_size - * text_buffer_size is the maximum output in chars - * TEXT_BUFFER_SIZE actually something like a allowed size - * for things in the config, below 'TEXT'. Or what is more probably - * max_user_text. Anyway, I used TEXT_BUFFER_SIZE for not 'output' things here - * -- calmar - * - * To clarify, TEXT_BUFFER_SIZE is used for buffers of fixed size, and - * text_buffer_size is used for buffers which can change in size. - * text_buffer_size is just defined as TEXT_BUFFER_SIZE to start, - * so its okay for most things, however if something is allocated - * with text_buffer_size and then text_buffer_size changes but - * the array doesn't, you might have some issues if you are using - * text_buffer_size to determine the size of the array. - * -- brenden - */ - -static char mbox_mail_spool[TEXT_BUFFER_SIZE]; +static char mbox_mail_spool[DEFAULT_TEXT_BUFFER_SIZE]; -void mbox_scan(char *args, char *output, size_t max_len) +static void mbox_scan(char *args, char *output, size_t max_len) { int i, u, flag; int force_rescan = 0; char buf[text_buffer_size]; struct stat statbuf; + struct ring_list *curr = 0, *prev = 0, *startlist = 0; + FILE *fp; /* output was set to 1 after malloc'ing in conky.c */ /* -> beeing able to test it here for catching SIGUSR1 */ @@ -84,15 +79,17 @@ void mbox_scan(char *args, char *output, size_t max_len) if (!args_ok || force_rescan) { char *substr = strstr(args, "-n"); + if (substr) { - if (sscanf(substr, "-n %i", &print_mails) != 1) { - print_mails = PRINT_MAILS; + if (sscanf(substr, "-n %i", &print_num_mails) != 1) { + print_num_mails = PRINT_MAILS; } } else { - print_mails = PRINT_MAILS; + print_num_mails = PRINT_MAILS; + } + if (print_num_mails < 1) { + print_num_mails = 1; } - if (print_mails < 1) - print_mails = 1; substr = strstr(args, "-t"); if (substr) { @@ -122,25 +119,30 @@ void mbox_scan(char *args, char *output, size_t max_len) } /* encapsulated with "'s find first occurrence of " */ if (args[strlen(args) - 1] == '"') { - strncpy(mbox_mail_spool, args, TEXT_BUFFER_SIZE); - char *start = strchr(mbox_mail_spool, '"') + 1; - start[(long)(strrchr(mbox_mail_spool, '"') - start)] = '\0'; - strncpy(mbox_mail_spool, start, TEXT_BUFFER_SIZE); + char *start; + strncpy(mbox_mail_spool, args, DEFAULT_TEXT_BUFFER_SIZE); + start = strchr(mbox_mail_spool, '"') + 1; + + start[(long) (strrchr(mbox_mail_spool, '"') - start)] = '\0'; + strncpy(mbox_mail_spool, start, DEFAULT_TEXT_BUFFER_SIZE); } else { - char *copy_args = strdup(args); + char *copy_args = strndup(args, text_buffer_size); char *tmp = strtok(copy_args, " "); char *start = tmp; + while (tmp) { tmp = strtok(NULL, " "); if (tmp) { start = tmp; } } - strncpy(mbox_mail_spool, start, TEXT_BUFFER_SIZE); + strncpy(mbox_mail_spool, start, DEFAULT_TEXT_BUFFER_SIZE); free(copy_args); } if (strlen(mbox_mail_spool) < 1) { - CRIT_ERR("Usage: ${mboxscan [-n ] [-fw ] [-sw ] [-t mbox}"); + CRIT_ERR(NULL, NULL, "Usage: ${mboxscan [-n ] " + "[-fw ] [-sw ] " + "[-t mbox]}"); } /* allowing $MAIL in the config */ @@ -149,44 +151,46 @@ void mbox_scan(char *args, char *output, size_t max_len) } if (stat(mbox_mail_spool, &statbuf)) { - CRIT_ERR("can't stat %s: %s", mbox_mail_spool, strerror(errno)); + CRIT_ERR(NULL, NULL, "can't stat %s: %s", mbox_mail_spool, strerror(errno)); } args_ok = 1; /* args-computing necessary only once */ } /* if time_delay not yet reached, then return */ - if (current_update_time - last_update < time_delay && !force_rescan) + if (current_update_time - last_update < time_delay && !force_rescan) { return; + } last_update = current_update_time; /* mbox still exists? and get stat-infos */ if (stat(mbox_mail_spool, &statbuf)) { - ERR("can't stat %s: %s", mbox_mail_spool, strerror(errno)); + NORM_ERR("can't stat %s: %s", mbox_mail_spool, strerror(errno)); output[0] = '\0'; /* delete any output */ return; } /* modification time has not changed, so skip scanning the box */ - if (statbuf.st_ctime == last_ctime && statbuf.st_mtime == last_mtime && !force_rescan) { + if (statbuf.st_ctime == last_ctime && statbuf.st_mtime == last_mtime + && !force_rescan) { return; } last_ctime = statbuf.st_ctime; last_mtime = statbuf.st_mtime; - /* build up double-linked ring-list to hold data, while scanning down * the mbox */ - struct ring_list *curr = 0, *prev = 0, *startlist = 0; - - for (i = 0; i < print_mails; i++) { - curr = (struct ring_list *)malloc(sizeof(struct ring_list)); - curr->from = (char *)malloc(sizeof(char[from_width + 1])); - curr->subject = (char *)malloc(sizeof(char[subject_width + 1])); + /* build up double-linked ring-list to hold data, while scanning down the + * mbox */ + for (i = 0; i < print_num_mails; i++) { + curr = (struct ring_list *) malloc(sizeof(struct ring_list)); + curr->from = (char *) malloc(sizeof(char[from_width + 1])); + curr->subject = (char *) malloc(sizeof(char[subject_width + 1])); curr->from[0] = '\0'; curr->subject[0] = '\0'; - if (i == 0) + if (i == 0) { startlist = curr; + } if (i > 0) { curr->previous = prev; prev->next = curr; @@ -199,24 +203,25 @@ void mbox_scan(char *args, char *output, size_t max_len) curr->next = startlist; /* mbox */ - FILE *fp; - fp = fopen(mbox_mail_spool, "r"); if (!fp) { return; } - flag = 1; /* first find a "From " to set it to 0 for header-sarchings */ + /* first find a "From " to set it to 0 for header-sarchings */ + flag = 1; while (!feof(fp)) { - if (fgets(buf, text_buffer_size, fp) == NULL) + if (fgets(buf, text_buffer_size, fp) == NULL) { break; + } if (strncmp(buf, "From ", 5) == 0) { curr = curr->next; /* skip until \n */ - while (strchr(buf, '\n') == NULL && !feof(fp)) + while (strchr(buf, '\n') == NULL && !feof(fp)) { fgets(buf, text_buffer_size, fp); + } flag = 0; /* in the headers now */ continue; @@ -230,23 +235,26 @@ void mbox_scan(char *args, char *output, size_t max_len) /* beyond the headers now (empty line), skip until \n */ /* then search for new mail ("From ") */ - while (strchr(buf, '\n') == NULL && !feof(fp)) + while (strchr(buf, '\n') == NULL && !feof(fp)) { fgets(buf, text_buffer_size, fp); + } flag = 1; /* in the body now */ continue; } if ((strncmp(buf, "X-Status: ", 10) == 0) - || (strncmp(buf, "Status: R", 9) == 0)) { + || (strncmp(buf, "Status: R", 9) == 0)) { /* Mail was read or something, so skip that message */ flag = 1; /* search for next From */ curr->subject[0] = '\0'; curr->from[0] = '\0'; - curr = curr->previous; /* (will get current again on new * 'From ' finding) */ + /* (will get current again on new 'From ' finding) */ + curr = curr->previous; /* Skip until \n */ - while (strchr(buf, '\n') == NULL && !feof(fp)) + while (strchr(buf, '\n') == NULL && !feof(fp)) { fgets(buf, text_buffer_size, fp); + } continue; } @@ -262,12 +270,14 @@ void mbox_scan(char *args, char *output, size_t max_len) continue; } - if (buf[u] == '<' && i > 1) { /* some are: From: * */ + /* some are: From: */ + if (buf[u] == '<' && i > 1) { curr->from[i] = '\0'; /* skip until \n */ - while (strchr(buf, '\n') == NULL && !feof(fp)) + while (strchr(buf, '\n') == NULL && !feof(fp)) { fgets(buf, text_buffer_size, fp); + } break; } @@ -284,8 +294,9 @@ void mbox_scan(char *args, char *output, size_t max_len) if (i >= from_width) { curr->from[i] = '\0'; /* skip until \n */ - while (strchr(buf, '\n') == NULL && !feof(fp)) + while (strchr(buf, '\n') == NULL && !feof(fp)) { fgets(buf, text_buffer_size, fp); + } break; } @@ -313,8 +324,9 @@ void mbox_scan(char *args, char *output, size_t max_len) curr->subject[i] = '\0'; /* skip until \n */ - while (strchr(buf, '\n') == NULL && !feof(fp)) + while (strchr(buf, '\n') == NULL && !feof(fp)) { fgets(buf, text_buffer_size, fp); + } break; } @@ -322,22 +334,23 @@ void mbox_scan(char *args, char *output, size_t max_len) curr->subject[i++] = buf[u++]; } } - } fclose(fp); output[0] = '\0'; - struct ring_list *tmp; - i = print_mails; + + i = print_num_mails; while (i) { + struct ring_list *tmp; if (curr->from[0] != '\0') { - if (i != print_mails) { - snprintf(buf, text_buffer_size, "\nF: %-*s S: %-*s", from_width, curr->from, subject_width, curr->subject); + if (i != print_num_mails) { + snprintf(buf, text_buffer_size, "\nF: %-*s S: %-*s", from_width, + curr->from, subject_width, curr->subject); } else { /* first time - no \n in front */ - snprintf(buf, text_buffer_size, "F: %-*s S: %-*s", from_width, curr->from, subject_width, curr->subject); + snprintf(buf, text_buffer_size, "F: %-*s S: %-*s", from_width, + curr->from, subject_width, curr->subject); } - } else { snprintf(buf, text_buffer_size, "\n"); } @@ -352,3 +365,49 @@ void mbox_scan(char *args, char *output, size_t max_len) i--; } } + +struct mboxscan_data { + char *args; + char *output; +}; + +void parse_mboxscan_arg(struct text_object *obj, const char *arg) +{ + struct mboxscan_data *msd; + + msd = malloc(sizeof(struct mboxscan_data)); + memset(msd, 0, sizeof(struct mboxscan_data)); + + msd->args = strndup(arg, text_buffer_size); + msd->output = (char *) malloc(text_buffer_size); + /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */ + msd->output[0] = 1; + + obj->data.opaque = msd; +} + +void print_mboxscan(struct text_object *obj, char *p, int p_max_size) +{ + struct mboxscan_data *msd = obj->data.opaque; + + if (!msd) + return; + + mbox_scan(msd->args, msd->output, text_buffer_size); + snprintf(p, p_max_size, "%s", msd->output); +} + +void free_mboxscan(struct text_object *obj) +{ + struct mboxscan_data *msd = obj->data.opaque; + + if (!msd) + return; + if (msd->args) + free(msd->args); + if (msd->output) + free(msd->output); + free(obj->data.opaque); + obj->data.opaque = NULL; +} +