f7214703a16bc0980a56f114080cd4e3ccec8732
[monky] / src / mboxscan.c
1 /*
2  * Conky, a system monitor, based on torsmo
3  *
4  * Any original torsmo code is licensed under the BSD license
5  *
6  * All code written since the fork of torsmo is licensed under the GPL
7  *
8  * Please see COPYING for details
9  *
10  * Copyright (c) 2006 Marco Candrian <mac@calmar.ws>
11  * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al. (see AUTHORS)
12  * All rights reserved.
13  *
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.
18  *
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/>. 
25  *
26  *  $Id$
27  */
28
29 #include "conky.h"
30 #include <sys/stat.h>
31 #include <sys/time.h>
32 #include <errno.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "mboxscan.h"
37
38 #define FROM_WIDTH 10
39 #define SUBJECT_WIDTH 22
40 #define PRINT_MAILS 5
41 #define TIME_DELAY 5
42
43 struct ring_list {
44         char *from;
45         char *subject;
46         struct ring_list *previous;
47         struct ring_list *next;
48 };
49
50 static time_t last_ctime;       /* needed for mutt at least */
51 static time_t last_mtime;       /* not sure what to test: testing both now */
52 static double last_update;
53
54 static int args_ok = 0;
55 static int from_width;
56 static int subject_width;
57 static int print_mails;
58 static int time_delay;
59
60 /*
61  * I don't know what to use: TEXT_BUFFER_SIZE or text_buffer_size
62  * text_buffer_size is the maximum output in chars
63  * TEXT_BUFFER_SIZE actually something like a allowed size
64  * for things in the config, below 'TEXT'. Or what is more probably
65  * max_user_text. Anyway, I used TEXT_BUFFER_SIZE for not 'output' things here
66  * -- calmar
67  *
68  *  To clarify, TEXT_BUFFER_SIZE is used for buffers of fixed size, and 
69  *  text_buffer_size is used for buffers which can change in size.
70  *  text_buffer_size is just defined as TEXT_BUFFER_SIZE to start,
71  *  so its okay for most things, however if something is allocated
72  *  with text_buffer_size and then text_buffer_size changes but
73  *  the array doesn't, you might have some issues if you are using
74  *  text_buffer_size to determine the size of the array.
75  *  -- brenden
76  */
77
78 static char mbox_mail_spool[TEXT_BUFFER_SIZE];
79
80 void mbox_scan(char *args, char *output, size_t max_len)
81 {
82         int i, u, flag;
83         int force_rescan = 0;
84         char buf[text_buffer_size];
85         struct stat statbuf;
86
87         /* output was set to 1 after malloc'ing in conky.c */
88         /* -> beeing able to test it here for catching SIGUSR1 */
89         if (output[0] == 1) {
90                 force_rescan = 1;
91                 output[0] = '\0';
92         }
93
94         if (!args_ok || force_rescan) {
95
96                 char *substr = strstr(args, "-n");
97                 if (substr) {
98                         if (sscanf(substr, "-n %i", &print_mails) != 1) {
99                                 print_mails = PRINT_MAILS;
100                         }
101                 } else {
102                         print_mails = PRINT_MAILS;
103                 }
104                 if (print_mails < 1)
105                         print_mails = 1;
106
107                 substr = strstr(args, "-t");
108                 if (substr) {
109                         if (sscanf(substr, "-t %i", &time_delay) != 1) {
110                                 time_delay = TIME_DELAY;
111                         }
112                 } else {
113                         time_delay = TIME_DELAY;
114                 }
115
116                 substr = strstr(args, "-fw");
117                 if (substr) {
118                         if (sscanf(substr, "-fw %i", &from_width) != 1) {
119                                 from_width = FROM_WIDTH;
120                         }
121                 } else {
122                         from_width = FROM_WIDTH;
123                 }
124
125                 substr = strstr(args, "-sw");
126                 if (substr) {
127                         if (sscanf(substr, "-sw %i", &subject_width) != 1) {
128                                 subject_width = SUBJECT_WIDTH;
129                         }
130                 } else {
131                         subject_width = SUBJECT_WIDTH;
132                 }
133                 /* encapsulated with "'s find first occurrence of " */
134                 if (args[strlen(args) - 1] == '"') {
135                         strncpy(mbox_mail_spool, args, TEXT_BUFFER_SIZE);
136                         char *start = strchr(mbox_mail_spool, '"') + 1;
137                         start[(long)(strrchr(mbox_mail_spool, '"') - start)] = '\0';
138                         strncpy(mbox_mail_spool, start, TEXT_BUFFER_SIZE);
139                 } else {
140                         char *copy_args = strdup(args);
141                         char *tmp = strtok(copy_args, " ");
142                         char *start = tmp;
143                         while (tmp) {
144                                 tmp = strtok(NULL, " ");
145                                 if (tmp) {
146                                         start = tmp;
147                                 }
148                         }
149                         strncpy(mbox_mail_spool, start, TEXT_BUFFER_SIZE);
150                         free(copy_args);
151                 }
152                 if (strlen(mbox_mail_spool) < 1) {
153                         CRIT_ERR("Usage: ${mboxscan [-n <number of messages to print>] [-fw <from width>] [-sw <subject width>] [-t <delay in sec> mbox]}");
154                 }
155
156                 /* allowing $MAIL in the config */
157                 if (!strcmp(mbox_mail_spool, "$MAIL")) {
158                         strcpy(mbox_mail_spool, current_mail_spool);
159                 }
160
161                 if (stat(mbox_mail_spool, &statbuf)) {
162                         CRIT_ERR("can't stat %s: %s", mbox_mail_spool, strerror(errno));
163                 }
164                 args_ok = 1;    /* args-computing necessary only once */
165         }
166
167         /* if time_delay not yet reached, then return */
168         if (current_update_time - last_update < time_delay && !force_rescan)
169                 return;
170
171         last_update = current_update_time;
172
173         /* mbox still exists? and get stat-infos */
174         if (stat(mbox_mail_spool, &statbuf)) {
175                 ERR("can't stat %s: %s", mbox_mail_spool, strerror(errno));
176                 output[0] = '\0';       /* delete any output */
177                 return;
178         }
179
180         /* modification time has not changed, so skip scanning the box */
181         if (statbuf.st_ctime == last_ctime && statbuf.st_mtime == last_mtime && !force_rescan) {
182                 return;
183         }
184
185         last_ctime = statbuf.st_ctime;
186         last_mtime = statbuf.st_mtime;
187
188         /* build up double-linked ring-list to hold data, while scanning down * the mbox */
189         struct ring_list *curr = 0, *prev = 0, *startlist = 0;
190
191         for (i = 0; i < print_mails; i++) {
192                 curr = (struct ring_list *)malloc(sizeof(struct ring_list));
193                 curr->from = (char *)malloc(sizeof(char[from_width + 1]));
194                 curr->subject = (char *)malloc(sizeof(char[subject_width + 1]));
195                 curr->from[0] = '\0';
196                 curr->subject[0] = '\0';
197
198                 if (i == 0)
199                         startlist = curr;
200                 if (i > 0) {
201                         curr->previous = prev;
202                         prev->next = curr;
203                 }
204                 prev = curr;
205         }
206
207         /* connect end to start for an endless loop-ring */
208         startlist->previous = curr;
209         curr->next = startlist;
210
211         /* mbox */
212         FILE *fp;
213
214         fp = fopen(mbox_mail_spool, "r");
215         if (!fp) {
216                 return;
217         }
218
219         flag = 1;               /* first find a "From " to set it to 0 for header-sarchings */
220         while (!feof(fp)) {
221                 if (fgets(buf, text_buffer_size, fp) == NULL)
222                         break;
223
224                 if (strncmp(buf, "From ", 5) == 0) {
225                         curr = curr->next;
226
227                         /* skip until \n */
228                         while (strchr(buf, '\n') == NULL && !feof(fp))
229                                 fgets(buf, text_buffer_size, fp);
230
231                         flag = 0;       /* in the headers now */
232                         continue;
233                 }
234
235                 if (flag == 1) {        /* in the body, so skip */
236                         continue;
237                 }
238
239                 if (buf[0] == '\n') {
240                         /* beyond the headers now (empty line), skip until \n */
241                         /* then search for new mail ("From ") */
242
243                         while (strchr(buf, '\n') == NULL && !feof(fp))
244                                 fgets(buf, text_buffer_size, fp);
245                         flag = 1;       /* in the body now */
246                         continue;
247                 }
248
249                 if ((strncmp(buf, "X-Status: ", 10) == 0)
250                     || (strncmp(buf, "Status: R", 9) == 0)) {
251
252                         /* Mail was read or something, so skip that message */
253                         flag = 1;       /* search for next From */
254                         curr->subject[0] = '\0';
255                         curr->from[0] = '\0';
256                         curr = curr->previous;  /* (will get current again on new * 'From ' finding) */
257                         /* Skip until \n */
258                         while (strchr(buf, '\n') == NULL && !feof(fp))
259                                 fgets(buf, text_buffer_size, fp);
260                         continue;
261                 }
262
263                 /* that covers ^From: and ^from: ^From:<tab> */
264                 if (strncmp(buf + 1, "rom:", 4) == 0) {
265
266                         i = 0;
267                         u = 6;  /* no "From: " string needed, so skip */
268                         while (1) {
269
270                                 if (buf[u] == '"') {    /* no quotes around names */
271                                         u++;
272                                         continue;
273                                 }
274
275                                 if (buf[u] == '<' && i > 1) {   /* some are: From: * <foo@bar.com> */
276
277                                         curr->from[i] = '\0';
278                                         /* skip until \n */
279                                         while (strchr(buf, '\n') == NULL && !feof(fp))
280                                                 fgets(buf, text_buffer_size, fp);
281                                         break;
282                                 }
283
284                                 if (buf[u] == '\n') {
285                                         curr->from[i] = '\0';
286                                         break;
287                                 }
288
289                                 if (buf[u] == '\0') {
290                                         curr->from[i] = '\0';
291                                         break;
292                                 }
293
294                                 if (i >= from_width) {
295                                         curr->from[i] = '\0';
296                                         /* skip until \n */
297                                         while (strchr(buf, '\n') == NULL && !feof(fp))
298                                                 fgets(buf, text_buffer_size, fp);
299                                         break;
300                                 }
301
302                                 /* nothing special so just set it */
303                                 curr->from[i++] = buf[u++];
304                         }
305                 }
306
307                 /* that covers ^Subject: and ^subject: and ^Subjec:<tab> */
308                 if (strncmp(buf + 1, "ubject:", 7) == 0) {
309
310                         i = 0;
311                         u = 9;  /* no "Subject: " string needed, so skip */
312                         while (1) {
313
314                                 if (buf[u] == '\n') {
315                                         curr->subject[i] = '\0';
316                                         break;
317                                 }
318                                 if (buf[u] == '\0') {
319                                         curr->subject[i] = '\0';
320                                         break;
321                                 }
322                                 if (i >= subject_width) {
323                                         curr->subject[i] = '\0';
324
325                                         /* skip until \n */
326                                         while (strchr(buf, '\n') == NULL && !feof(fp))
327                                                 fgets(buf, text_buffer_size, fp);
328                                         break;
329                                 }
330
331                                 /* nothing special so just set it */
332                                 curr->subject[i++] = buf[u++];
333                         }
334                 }
335
336         }
337
338         fclose(fp);
339
340         output[0] = '\0';
341         struct ring_list *tmp;
342         i = print_mails;
343         while (i) {
344                 if (curr->from[0] != '\0') {
345                         if (i != print_mails) {
346                                 snprintf(buf, text_buffer_size, "\nF: %-*s S: %-*s", from_width, curr->from, subject_width, curr->subject);
347                         } else {        /* first time - no \n in front */
348                                 snprintf(buf, text_buffer_size, "F: %-*s S: %-*s", from_width, curr->from, subject_width, curr->subject);
349                         }
350
351                 } else {
352                         snprintf(buf, text_buffer_size, "\n");
353                 }
354                 strncat(output, buf, max_len - strlen(output));
355
356                 tmp = curr;
357                 curr = curr->previous;
358                 free(tmp->from);
359                 free(tmp->subject);
360                 free(tmp);
361
362                 i--;
363         }
364 }