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 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2006 Marco Candrian <mac@calmar.ws>
13 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 #include "text_object.h"
40 #define SUBJECT_WIDTH 22
47 struct ring_list *previous;
48 struct ring_list *next;
51 static time_t last_ctime; /* needed for mutt at least */
52 static time_t last_mtime; /* not sure what to test: testing both now */
53 static double last_update;
55 static int args_ok = 0;
56 static int from_width;
57 static int subject_width;
58 static int print_num_mails;
59 static int time_delay;
61 static char mbox_mail_spool[DEFAULT_TEXT_BUFFER_SIZE];
63 static void mbox_scan(char *args, char *output, size_t max_len)
67 char buf[text_buffer_size];
69 struct ring_list *curr = 0, *prev = 0, *startlist = 0;
72 /* output was set to 1 after malloc'ing in conky.c */
73 /* -> beeing able to test it here for catching SIGUSR1 */
79 if (!args_ok || force_rescan) {
81 char *substr = strstr(args, "-n");
84 if (sscanf(substr, "-n %i", &print_num_mails) != 1) {
85 print_num_mails = PRINT_MAILS;
88 print_num_mails = PRINT_MAILS;
90 if (print_num_mails < 1) {
94 substr = strstr(args, "-t");
96 if (sscanf(substr, "-t %i", &time_delay) != 1) {
97 time_delay = TIME_DELAY;
100 time_delay = TIME_DELAY;
103 substr = strstr(args, "-fw");
105 if (sscanf(substr, "-fw %i", &from_width) != 1) {
106 from_width = FROM_WIDTH;
109 from_width = FROM_WIDTH;
112 substr = strstr(args, "-sw");
114 if (sscanf(substr, "-sw %i", &subject_width) != 1) {
115 subject_width = SUBJECT_WIDTH;
118 subject_width = SUBJECT_WIDTH;
120 /* encapsulated with "'s find first occurrence of " */
121 if (args[strlen(args) - 1] == '"') {
123 strncpy(mbox_mail_spool, args, DEFAULT_TEXT_BUFFER_SIZE);
124 start = strchr(mbox_mail_spool, '"') + 1;
126 start[(long) (strrchr(mbox_mail_spool, '"') - start)] = '\0';
127 strncpy(mbox_mail_spool, start, DEFAULT_TEXT_BUFFER_SIZE);
129 char *copy_args = strndup(args, text_buffer_size);
130 char *tmp = strtok(copy_args, " ");
134 tmp = strtok(NULL, " ");
139 strncpy(mbox_mail_spool, start, DEFAULT_TEXT_BUFFER_SIZE);
142 if (strlen(mbox_mail_spool) < 1) {
143 CRIT_ERR(NULL, NULL, "Usage: ${mboxscan [-n <number of messages to print>] "
144 "[-fw <from width>] [-sw <subject width>] "
145 "[-t <delay in sec> mbox]}");
148 /* allowing $MAIL in the config */
149 if (!strcmp(mbox_mail_spool, "$MAIL")) {
150 strcpy(mbox_mail_spool, current_mail_spool);
153 if (stat(mbox_mail_spool, &statbuf)) {
154 CRIT_ERR(NULL, NULL, "can't stat %s: %s", mbox_mail_spool, strerror(errno));
156 args_ok = 1; /* args-computing necessary only once */
159 /* if time_delay not yet reached, then return */
160 if (current_update_time - last_update < time_delay && !force_rescan) {
164 last_update = current_update_time;
166 /* mbox still exists? and get stat-infos */
167 if (stat(mbox_mail_spool, &statbuf)) {
168 NORM_ERR("can't stat %s: %s", mbox_mail_spool, strerror(errno));
169 output[0] = '\0'; /* delete any output */
173 /* modification time has not changed, so skip scanning the box */
174 if (statbuf.st_ctime == last_ctime && statbuf.st_mtime == last_mtime
179 last_ctime = statbuf.st_ctime;
180 last_mtime = statbuf.st_mtime;
182 /* build up double-linked ring-list to hold data, while scanning down the
184 for (i = 0; i < print_num_mails; i++) {
185 curr = (struct ring_list *) malloc(sizeof(struct ring_list));
186 curr->from = (char *) malloc(sizeof(char[from_width + 1]));
187 curr->subject = (char *) malloc(sizeof(char[subject_width + 1]));
188 curr->from[0] = '\0';
189 curr->subject[0] = '\0';
195 curr->previous = prev;
201 /* connect end to start for an endless loop-ring */
202 startlist->previous = curr;
203 curr->next = startlist;
206 fp = fopen(mbox_mail_spool, "r");
211 /* first find a "From " to set it to 0 for header-sarchings */
214 if (fgets(buf, text_buffer_size, fp) == NULL) {
218 if (strncmp(buf, "From ", 5) == 0) {
222 while (strchr(buf, '\n') == NULL && !feof(fp)) {
223 fgets(buf, text_buffer_size, fp);
226 flag = 0; /* in the headers now */
230 if (flag == 1) { /* in the body, so skip */
234 if (buf[0] == '\n') {
235 /* beyond the headers now (empty line), skip until \n */
236 /* then search for new mail ("From ") */
238 while (strchr(buf, '\n') == NULL && !feof(fp)) {
239 fgets(buf, text_buffer_size, fp);
241 flag = 1; /* in the body now */
245 if ((strncmp(buf, "X-Status: ", 10) == 0)
246 || (strncmp(buf, "Status: R", 9) == 0)) {
248 /* Mail was read or something, so skip that message */
249 flag = 1; /* search for next From */
250 curr->subject[0] = '\0';
251 curr->from[0] = '\0';
252 /* (will get current again on new 'From ' finding) */
253 curr = curr->previous;
255 while (strchr(buf, '\n') == NULL && !feof(fp)) {
256 fgets(buf, text_buffer_size, fp);
261 /* that covers ^From: and ^from: ^From:<tab> */
262 if (strncmp(buf + 1, "rom:", 4) == 0) {
265 u = 6; /* no "From: " string needed, so skip */
268 if (buf[u] == '"') { /* no quotes around names */
273 /* some are: From: <foo@bar.com> */
274 if (buf[u] == '<' && i > 1) {
276 curr->from[i] = '\0';
278 while (strchr(buf, '\n') == NULL && !feof(fp)) {
279 fgets(buf, text_buffer_size, fp);
284 if (buf[u] == '\n') {
285 curr->from[i] = '\0';
289 if (buf[u] == '\0') {
290 curr->from[i] = '\0';
294 if (i >= from_width) {
295 curr->from[i] = '\0';
297 while (strchr(buf, '\n') == NULL && !feof(fp)) {
298 fgets(buf, text_buffer_size, fp);
303 /* nothing special so just set it */
304 curr->from[i++] = buf[u++];
308 /* that covers ^Subject: and ^subject: and ^Subjec:<tab> */
309 if (strncmp(buf + 1, "ubject:", 7) == 0) {
312 u = 9; /* no "Subject: " string needed, so skip */
315 if (buf[u] == '\n') {
316 curr->subject[i] = '\0';
319 if (buf[u] == '\0') {
320 curr->subject[i] = '\0';
323 if (i >= subject_width) {
324 curr->subject[i] = '\0';
327 while (strchr(buf, '\n') == NULL && !feof(fp)) {
328 fgets(buf, text_buffer_size, fp);
333 /* nothing special so just set it */
334 curr->subject[i++] = buf[u++];
345 struct ring_list *tmp;
346 if (curr->from[0] != '\0') {
347 if (i != print_num_mails) {
348 snprintf(buf, text_buffer_size, "\nF: %-*s S: %-*s", from_width,
349 curr->from, subject_width, curr->subject);
350 } else { /* first time - no \n in front */
351 snprintf(buf, text_buffer_size, "F: %-*s S: %-*s", from_width,
352 curr->from, subject_width, curr->subject);
355 snprintf(buf, text_buffer_size, "\n");
357 strncat(output, buf, max_len - strlen(output));
360 curr = curr->previous;
369 struct mboxscan_data {
374 void parse_mboxscan_arg(struct text_object *obj, const char *arg)
376 struct mboxscan_data *msd;
378 msd = malloc(sizeof(struct mboxscan_data));
379 memset(msd, 0, sizeof(struct mboxscan_data));
381 msd->args = strndup(arg, text_buffer_size);
382 msd->output = (char *) malloc(text_buffer_size);
383 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
386 obj->data.opaque = msd;
389 void print_mboxscan(struct text_object *obj, char *p, int p_max_size)
391 struct mboxscan_data *msd = obj->data.opaque;
396 mbox_scan(msd->args, msd->output, text_buffer_size);
397 snprintf(p, p_max_size, "%s", msd->output);
400 void free_mboxscan(struct text_object *obj)
402 struct mboxscan_data *msd = obj->data.opaque;
410 free(obj->data.opaque);
411 obj->data.opaque = NULL;