5 * author: mac@calmar.ws Marco Candrian
7 * Modified for use in Conky by Brenden Matthews
10 * scanning from top to bottom on a mbox
11 * The output as follows:
12 * F: FROM_LENGHT S: SUBJECT_LENGHT
14 * Usage: ${mboxscan [-n <number of messages to print>]
15 * [-fw <from width>] [-sw <subject width>]
16 * [-t <minumum delay in sec> "mbox" }
29 #define SUBJECT_WIDTH 22
36 struct ring_list *previous;
37 struct ring_list *next;
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;
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;
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
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.
68 static char mbox_mail_spool[TEXT_BUFFER_SIZE];
70 void mbox_scan(char *args, char *output, size_t max_len)
74 char buf[text_buffer_size];
77 /* output was set to 1 after malloc'ing in conky.c */
78 /* -> beeing able to test it here for catching SIGUSR1 */
84 if (!args_ok || force_rescan) {
86 char *substr = strstr(args, "-n");
88 if (sscanf(substr, "-n %i", &print_mails) != 1) {
89 print_mails = PRINT_MAILS;
92 print_mails = PRINT_MAILS;
97 substr = strstr(args, "-t");
99 if (sscanf(substr, "-t %i", &time_delay) != 1) {
100 time_delay = TIME_DELAY;
103 time_delay = TIME_DELAY;
106 substr = strstr(args, "-fw");
108 if (sscanf(substr, "-fw %i", &from_width) != 1) {
109 from_width = FROM_WIDTH;
112 from_width = FROM_WIDTH;
115 substr = strstr(args, "-sw");
117 if (sscanf(substr, "-sw %i", &subject_width) != 1) {
118 subject_width = SUBJECT_WIDTH;
121 subject_width = SUBJECT_WIDTH;
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);
130 char *copy_args = strdup(args);
131 char *tmp = strtok(copy_args, " ");
134 tmp = strtok(NULL, " ");
139 strncpy(mbox_mail_spool, start, TEXT_BUFFER_SIZE);
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}");
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)
161 last_update = current_update_time;
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 */
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) {
175 last_ctime = statbuf.st_ctime;
176 last_mtime = statbuf.st_mtime;
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;
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';
191 curr->previous = prev;
197 /* connect end to start for an endless loop-ring */
198 startlist->previous = curr;
199 curr->next = startlist;
204 fp = fopen(mbox_mail_spool, "r");
209 flag = 1; /* first find a "From " to set it to 0 for header-sarchings */
211 if (fgets(buf, text_buffer_size, fp) == NULL)
214 if (strncmp(buf, "From ", 5) == 0) {
218 while (strchr(buf, '\n') == NULL && !feof(fp))
219 fgets(buf, text_buffer_size, fp);
221 flag = 0; /* in the headers now */
225 if (flag == 1) { /* in the body, so skip */
229 if (buf[0] == '\n') {
230 /* beyond the headers now (empty line), skip until \n */
231 /* then search for new mail ("From ") */
233 while (strchr(buf, '\n') == NULL && !feof(fp))
234 fgets(buf, text_buffer_size, fp);
235 flag = 1; /* in the body now */
239 if ((strncmp(buf, "X-Status: ", 10) == 0)
240 || (strncmp(buf, "Status: R", 9) == 0)) {
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) */
248 while (strchr(buf, '\n') == NULL && !feof(fp))
249 fgets(buf, text_buffer_size, fp);
253 /* that covers ^From: and ^from: ^From:<tab> */
254 if (strncmp(buf + 1, "rom:", 4) == 0) {
257 u = 6; /* no "From: " string needed, so skip */
260 if (buf[u] == '"') { /* no quotes around names */
265 if (buf[u] == '<' && i > 1) { /* some are: From: * <foo@bar.com> */
267 curr->from[i] = '\0';
269 while (strchr(buf, '\n') == NULL && !feof(fp))
270 fgets(buf, text_buffer_size, fp);
274 if (buf[u] == '\n') {
275 curr->from[i] = '\0';
279 if (buf[u] == '\0') {
280 curr->from[i] = '\0';
284 if (i >= from_width) {
285 curr->from[i] = '\0';
287 while (strchr(buf, '\n') == NULL && !feof(fp))
288 fgets(buf, text_buffer_size, fp);
292 /* nothing special so just set it */
293 curr->from[i++] = buf[u++];
297 /* that covers ^Subject: and ^subject: and ^Subjec:<tab> */
298 if (strncmp(buf + 1, "ubject:", 7) == 0) {
301 u = 9; /* no "Subject: " string needed, so skip */
304 if (buf[u] == '\n') {
305 curr->subject[i] = '\0';
308 if (buf[u] == '\0') {
309 curr->subject[i] = '\0';
312 if (i >= subject_width) {
313 curr->subject[i] = '\0';
316 while (strchr(buf, '\n') == NULL && !feof(fp))
317 fgets(buf, text_buffer_size, fp);
321 /* nothing special so just set it */
322 curr->subject[i++] = buf[u++];
331 struct ring_list *tmp;
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);
342 snprintf(buf, text_buffer_size, "\n");
344 strncat(output, buf, max_len - strlen(output));
347 curr = curr->previous;