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