1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2006 Marco Candrian <mac@calmar.ws>
10 * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 #define SUBJECT_WIDTH 22
45 struct ring_list *previous;
46 struct ring_list *next;
49 static time_t last_ctime; /* needed for mutt at least */
50 static time_t last_mtime; /* not sure what to test: testing both now */
51 static double last_update;
53 static int args_ok = 0;
54 static int from_width;
55 static int subject_width;
56 static int print_mails;
57 static int time_delay;
59 static char mbox_mail_spool[DEFAULT_TEXT_BUFFER_SIZE];
61 void mbox_scan(char *args, char *output, size_t max_len)
65 char buf[text_buffer_size];
67 struct ring_list *curr = 0, *prev = 0, *startlist = 0;
70 /* output was set to 1 after malloc'ing in conky.c */
71 /* -> beeing able to test it here for catching SIGUSR1 */
77 if (!args_ok || force_rescan) {
79 char *substr = strstr(args, "-n");
82 if (sscanf(substr, "-n %i", &print_mails) != 1) {
83 print_mails = PRINT_MAILS;
86 print_mails = PRINT_MAILS;
88 if (print_mails < 1) {
92 substr = strstr(args, "-t");
94 if (sscanf(substr, "-t %i", &time_delay) != 1) {
95 time_delay = TIME_DELAY;
98 time_delay = TIME_DELAY;
101 substr = strstr(args, "-fw");
103 if (sscanf(substr, "-fw %i", &from_width) != 1) {
104 from_width = FROM_WIDTH;
107 from_width = FROM_WIDTH;
110 substr = strstr(args, "-sw");
112 if (sscanf(substr, "-sw %i", &subject_width) != 1) {
113 subject_width = SUBJECT_WIDTH;
116 subject_width = SUBJECT_WIDTH;
118 /* encapsulated with "'s find first occurrence of " */
119 if (args[strlen(args) - 1] == '"') {
121 strncpy(mbox_mail_spool, args, DEFAULT_TEXT_BUFFER_SIZE);
122 start = strchr(mbox_mail_spool, '"') + 1;
124 start[(long) (strrchr(mbox_mail_spool, '"') - start)] = '\0';
125 strncpy(mbox_mail_spool, start, DEFAULT_TEXT_BUFFER_SIZE);
127 char *copy_args = strdup(args);
128 char *tmp = strtok(copy_args, " ");
132 tmp = strtok(NULL, " ");
137 strncpy(mbox_mail_spool, start, DEFAULT_TEXT_BUFFER_SIZE);
140 if (strlen(mbox_mail_spool) < 1) {
141 CRIT_ERR("Usage: ${mboxscan [-n <number of messages to print>] "
142 "[-fw <from width>] [-sw <subject width>] "
143 "[-t <delay in sec> mbox]}");
146 /* allowing $MAIL in the config */
147 if (!strcmp(mbox_mail_spool, "$MAIL")) {
148 strcpy(mbox_mail_spool, current_mail_spool);
151 if (stat(mbox_mail_spool, &statbuf)) {
152 CRIT_ERR("can't stat %s: %s", mbox_mail_spool, strerror(errno));
154 args_ok = 1; /* args-computing necessary only once */
157 /* if time_delay not yet reached, then return */
158 if (current_update_time - last_update < time_delay && !force_rescan) {
162 last_update = current_update_time;
164 /* mbox still exists? and get stat-infos */
165 if (stat(mbox_mail_spool, &statbuf)) {
166 ERR("can't stat %s: %s", mbox_mail_spool, strerror(errno));
167 output[0] = '\0'; /* delete any output */
171 /* modification time has not changed, so skip scanning the box */
172 if (statbuf.st_ctime == last_ctime && statbuf.st_mtime == last_mtime
177 last_ctime = statbuf.st_ctime;
178 last_mtime = statbuf.st_mtime;
180 /* build up double-linked ring-list to hold data, while scanning down the
182 for (i = 0; i < print_mails; i++) {
183 curr = (struct ring_list *) malloc(sizeof(struct ring_list));
184 curr->from = (char *) malloc(sizeof(char[from_width + 1]));
185 curr->subject = (char *) malloc(sizeof(char[subject_width + 1]));
186 curr->from[0] = '\0';
187 curr->subject[0] = '\0';
193 curr->previous = prev;
199 /* connect end to start for an endless loop-ring */
200 startlist->previous = curr;
201 curr->next = startlist;
204 fp = fopen(mbox_mail_spool, "r");
209 /* first find a "From " to set it to 0 for header-sarchings */
212 if (fgets(buf, text_buffer_size, fp) == NULL) {
216 if (strncmp(buf, "From ", 5) == 0) {
220 while (strchr(buf, '\n') == NULL && !feof(fp)) {
221 fgets(buf, text_buffer_size, fp);
224 flag = 0; /* in the headers now */
228 if (flag == 1) { /* in the body, so skip */
232 if (buf[0] == '\n') {
233 /* beyond the headers now (empty line), skip until \n */
234 /* then search for new mail ("From ") */
236 while (strchr(buf, '\n') == NULL && !feof(fp)) {
237 fgets(buf, text_buffer_size, fp);
239 flag = 1; /* in the body now */
243 if ((strncmp(buf, "X-Status: ", 10) == 0)
244 || (strncmp(buf, "Status: R", 9) == 0)) {
246 /* Mail was read or something, so skip that message */
247 flag = 1; /* search for next From */
248 curr->subject[0] = '\0';
249 curr->from[0] = '\0';
250 /* (will get current again on new 'From ' finding) */
251 curr = curr->previous;
253 while (strchr(buf, '\n') == NULL && !feof(fp)) {
254 fgets(buf, text_buffer_size, fp);
259 /* that covers ^From: and ^from: ^From:<tab> */
260 if (strncmp(buf + 1, "rom:", 4) == 0) {
263 u = 6; /* no "From: " string needed, so skip */
266 if (buf[u] == '"') { /* no quotes around names */
271 /* some are: From: <foo@bar.com> */
272 if (buf[u] == '<' && i > 1) {
274 curr->from[i] = '\0';
276 while (strchr(buf, '\n') == NULL && !feof(fp)) {
277 fgets(buf, text_buffer_size, fp);
282 if (buf[u] == '\n') {
283 curr->from[i] = '\0';
287 if (buf[u] == '\0') {
288 curr->from[i] = '\0';
292 if (i >= from_width) {
293 curr->from[i] = '\0';
295 while (strchr(buf, '\n') == NULL && !feof(fp)) {
296 fgets(buf, text_buffer_size, fp);
301 /* nothing special so just set it */
302 curr->from[i++] = buf[u++];
306 /* that covers ^Subject: and ^subject: and ^Subjec:<tab> */
307 if (strncmp(buf + 1, "ubject:", 7) == 0) {
310 u = 9; /* no "Subject: " string needed, so skip */
313 if (buf[u] == '\n') {
314 curr->subject[i] = '\0';
317 if (buf[u] == '\0') {
318 curr->subject[i] = '\0';
321 if (i >= subject_width) {
322 curr->subject[i] = '\0';
325 while (strchr(buf, '\n') == NULL && !feof(fp)) {
326 fgets(buf, text_buffer_size, fp);
331 /* nothing special so just set it */
332 curr->subject[i++] = buf[u++];
343 struct ring_list *tmp;
344 if (curr->from[0] != '\0') {
345 if (i != print_mails) {
346 snprintf(buf, text_buffer_size, "\nF: %-*s S: %-*s", from_width,
347 curr->from, subject_width, curr->subject);
348 } else { /* first time - no \n in front */
349 snprintf(buf, text_buffer_size, "F: %-*s S: %-*s", from_width,
350 curr->from, subject_width, curr->subject);
353 snprintf(buf, text_buffer_size, "\n");
355 strncat(output, buf, max_len - strlen(output));
358 curr = curr->previous;