2295f068699d9a2405bb6bbd60704a046eea6828
[dockapps.git] / wmbiff / wmbiff / wmbiff.c
blob2295f068699d9a2405bb6bbd60704a046eea6828
1 /* $Id: wmbiff.c,v 1.70 2005/10/07 03:07:58 bluehal Exp $ */
3 // typedef int gcry_error_t;
5 #ifdef HAVE_CONFIG_H
6 #include <config.h>
7 #endif
9 #include <time.h>
10 #include <ctype.h>
12 #ifdef HAVE_POLL
13 #include <poll.h>
14 #else
15 #include <sys/time.h>
16 #endif
18 #include <sys/wait.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <signal.h>
23 #include <X11/Xlib.h>
24 #include <X11/xpm.h>
25 #include <X11/cursorfont.h>
26 #include <X11/XKBlib.h>
28 #include <errno.h>
29 #include <string.h>
31 #include "../wmgeneral/wmgeneral.h"
32 #include "../wmgeneral/misc.h"
34 #include "Client.h"
35 #include "charutil.h"
36 #include "MessageList.h"
38 #ifdef USE_DMALLOC
39 #include <dmalloc.h>
40 #endif
43 #include "wmbiff-master-led.xpm"
44 static char wmbiff_mask_bits[64 * 64];
45 static const int wmbiff_mask_width = 64;
46 // const int wmbiff_mask_height = 64;
48 #define CHAR_WIDTH 5
49 #define CHAR_HEIGHT 7
51 #define BLINK_TIMES 8
52 #define DEFAULT_SLEEP_INTERVAL 20000
53 #define BLINK_SLEEP_INTERVAL 200
54 #define DEFAULT_LOOP 5
56 #define MAX_NUM_MAILBOXES 40
57 static mbox_t mbox[MAX_NUM_MAILBOXES];
59 /* this is the normal pixmap. */
60 static const char *skin_filename = "wmbiff-master-led.xpm";
61 /* global notify action taken (if globalnotify option is set) */
62 static const char *globalnotify = NULL;
64 /* path to search for pixmaps */
65 /* /usr/share/wmbiff should have the default pixmap. */
66 /* /usr/local/share/wmbiff if compiled locally. */
67 /* / is there in case a user wants to specify a complete path */
68 /* . is there for development. */
69 static const char *skin_search_path = DEFAULT_SKIN_PATH;
70 /* for gnutls */
71 const char *certificate_filename = NULL;
73 /* it could be argued that a better default exists. */
74 #define DEFAULT_FONT "-*-fixed-*-r-*-*-10-*-*-*-*-*-*-*"
75 static const char *font = NULL;
77 int debug_default = DEBUG_ERROR;
79 /* color from wmbiff's xpm, down to 24 bits. */
80 const char *foreground = "white"; /* foreground white */
81 const char *background = "#505075"; /* background blue */
82 static const char *highlight = "red";
83 int SkipCertificateCheck = 0;
84 int Relax = 0; /* be not paranoid */
85 static int notWithdrawn = 0;
87 static unsigned int num_mailboxes = 1;
88 static const int x_origin = 5;
89 static const int y_origin = 5;
90 static int forever = 1; /* keep running. */
92 extern Window win;
93 extern Window iconwin;
95 Cursor busy_cursor, ready_cursor;
97 static __inline /*@out@ */ void *
98 malloc_ordie(size_t len)
100 void *ret = malloc(len);
101 if (ret == NULL) {
102 fprintf(stderr, "unable to allocate %d bytes\n", (int) len);
103 abort();
105 return (ret);
108 /* where vertically the mailbox sits for blitting characters. */
109 static int mbox_y(unsigned int mboxnum)
111 return ((11 * mboxnum) + y_origin);
114 /* Read a line from a file to obtain a pair setting=value
115 skips # and leading spaces
116 NOTE: if setting finish with 0, 1, 2, 3 or 4 last char are deleted and
117 index takes its value... if not index will get -1
118 Returns -1 if no setting=value
120 static int ReadLine(FILE * fp, /*@out@ */ char *setting,
121 /*@out@ */ char *value, /*@out@ */ int *mbox_index)
123 char buf[BUF_SIZE];
124 char *p, *q;
125 int len;
127 *setting = '\0';
128 *value = '\0';
129 *mbox_index = -1;
131 if (!fp || feof(fp))
132 return -1;
134 if (!fgets(buf, BUF_SIZE - 1, fp))
135 return -1;
137 len = strlen(buf);
139 if (buf[len - 1] == '\n') {
140 buf[len - 1] = '\0'; /* strip linefeed */
143 StripComment(buf);
145 if (!(p = strtok(buf, "=")))
146 return -1;
147 if (!(q = strtok(NULL, "\n")))
148 return -1;
150 /* Chg - Mark Hurley
151 * Date: May 8, 2001
152 * Removed for loop (which removed leading spaces)
153 * Leading & Trailing spaces need to be removed
154 * to Fix Debian bug #95849
156 FullTrim(p);
157 FullTrim(q);
159 /* strcpy(setting, p); nspring replaced with sscanf dec 2002 */
160 strcpy(value, q);
162 if (sscanf(p, "%[_a-z.]%d", setting, mbox_index) == 2) {
163 /* mailbox-specific configuration, ends in a digit */
164 if (*mbox_index < 0 || *mbox_index >= MAX_NUM_MAILBOXES) {
165 DMA(DEBUG_ERROR, "invalid mailbox number %d\n", *mbox_index);
166 exit(EXIT_FAILURE);
168 } else if (sscanf(p, "%[a-z]", setting) == 1) {
169 /* global configuration, all text. */
170 *mbox_index = -1;
171 } else {
172 /* we found an uncommented line that has an equals,
173 but is non-alphabetic. */
174 DMA(DEBUG_INFO, "unparsed setting %s\n", p);
175 return -1;
178 DMA(DEBUG_INFO, "@%s.%d=%s@\n", setting, *mbox_index, value);
179 return 1;
182 struct path_demultiplexer {
183 const char *id; /* followed by a colon */
184 int (*creator) ( /*@notnull@ */ Pop3 pc, const char *path);
187 static struct path_demultiplexer paths[] = {
188 {"pop3:", pop3Create},
189 {"pop3s:", pop3Create},
190 {"shell:", shellCreate},
191 {"imap:", imap4Create},
192 {"imaps:", imap4Create},
193 {"sslimap:", imap4Create},
194 {"maildir:", maildirCreate},
195 {"mbox:", mboxCreate},
196 {NULL, NULL}
200 static void parse_mbox_path(unsigned int item)
202 int i;
203 /* find the creator */
204 for (i = 0;
205 paths[i].id != NULL
206 && strncasecmp(mbox[item].path, paths[i].id, strlen(paths[i].id));
207 i++);
208 /* if found, execute */
209 if (paths[i].id != NULL) {
210 if (paths[i].creator((&mbox[item]), mbox[item].path) != 0) {
211 DMA(DEBUG_ERROR, "creator for mailbox %u returned failure\n",
212 item);
214 } else {
215 /* default are mbox */
216 mboxCreate((&mbox[item]), mbox[item].path);
220 static int Read_Config_File(char *filename, int *loopinterval)
222 FILE *fp;
223 char setting[BUF_SMALL], value[BUF_SIZE];
224 int mbox_index;
225 unsigned int i;
227 if (!(fp = fopen(filename, "r"))) {
228 DMA(DEBUG_ERROR, "Unable to open %s, no settings read: %s\n",
229 filename, strerror(errno));
230 return 0;
232 while (!feof(fp)) {
233 /* skanky: -1 can represent an unparsed line
234 or an error */
235 if (ReadLine(fp, setting, value, &mbox_index) == -1)
236 continue;
238 /* settings that can be global go here. */
239 if (!strcmp(setting, "interval")) {
240 *loopinterval = atoi(value);
241 continue;
242 } else if (!strcmp(setting, "askpass")) {
243 const char *askpass = strdup_ordie(value);
244 if (mbox_index == -1) {
245 DMA(DEBUG_INFO, "setting all to askpass %s\n", askpass);
246 for (i = 0; i < MAX_NUM_MAILBOXES; i++)
247 mbox[i].askpass = askpass;
248 } else {
249 mbox[mbox_index].askpass = askpass;
251 continue;
252 } else if (!strcmp(setting, "skinfile")) {
253 skin_filename = strdup_ordie(value);
254 continue;
255 } else if (!strcmp(setting, "certfile")) { /* not yet supported */
256 certificate_filename = strdup_ordie(value);
257 continue;
258 } else if (!strcmp(setting, "globalnotify")) {
259 globalnotify = strdup_ordie(value);
260 continue;
261 } else if (mbox_index == -1) {
262 DMA(DEBUG_INFO, "Unknown global setting '%s'\n", setting);
263 continue; /* Didn't read any setting.[0-5] value */
266 if (mbox_index >= MAX_NUM_MAILBOXES) {
267 DMA(DEBUG_ERROR, "Don't have %d mailboxes.\n", mbox_index);
268 continue;
271 if (1U + mbox_index > num_mailboxes
272 && mbox_index + 1 <= MAX_NUM_MAILBOXES) {
273 num_mailboxes = 1U + mbox_index;
276 /* now only local settings */
277 if (!strcmp(setting, "label.")) {
278 if (strlen(value) + 1 > BUF_SMALL) {
279 DMA(DEBUG_ERROR,
280 "Mailbox %i label string '%s' is too long.\n",
281 mbox_index, value);
282 continue;
283 } else {
284 strncpy(mbox[mbox_index].label, value, BUF_SMALL - 1);
286 } else if (!strcmp(setting, "path.")) {
287 if (strlen(value) + 1 > BUF_BIG) {
288 DMA(DEBUG_ERROR,
289 "Mailbox %i path string '%s' is too long.\n",
290 mbox_index, value);
291 continue;
292 } else {
293 strncpy(mbox[mbox_index].path, value, BUF_BIG - 1);
295 } else if (!strcmp(setting, "notify.")) {
296 if (strlen(value) + 1 > BUF_BIG) {
297 DMA(DEBUG_ERROR,
298 "Mailbox %i notify string '%s' is too long.\n",
299 mbox_index, value);
300 continue;
301 } else {
302 strncpy(mbox[mbox_index].notify, value, BUF_BIG - 1);
304 } else if (!strcmp(setting, "action.")) {
305 if (strlen(value) + 1 > BUF_BIG) {
306 DMA(DEBUG_ERROR,
307 "Mailbox %i action string '%s' is too long.\n",
308 mbox_index, value);
309 continue;
310 } else {
311 strncpy(mbox[mbox_index].action, value, BUF_BIG - 1);
313 } else if (!strcmp(setting, "action_disconnected.")) {
314 if (strlen(value) + 1 > BUF_BIG) {
315 DMA(DEBUG_ERROR,
316 "Mailbox %i action_disconnected string '%s' is too long.\n",
317 mbox_index, value);
318 continue;
319 } else {
320 strncpy(mbox[mbox_index].actiondc, value, BUF_BIG - 1);
322 } else if (!strcmp(setting, "action_new_mail.")) {
323 if (strlen(value) + 1 > BUF_BIG) {
324 DMA(DEBUG_ERROR,
325 "Mailbox %i action_new_mail string '%s' is too long.\n",
326 mbox_index, value);
327 continue;
328 } else {
329 strncpy(mbox[mbox_index].actionnew, value, BUF_BIG - 1);
331 } else if (!strcmp(setting, "action_no_new_mail.")) {
332 if (strlen(value) + 1 > BUF_BIG) {
333 DMA(DEBUG_ERROR,
334 "Mailbox %i action_no_new_mail string '%s' is too long.\n",
335 mbox_index, value);
336 continue;
337 } else {
338 strncpy(mbox[mbox_index].actionnonew, value, BUF_BIG - 1);
340 } else if (!strcmp(setting, "interval.")) {
341 mbox[mbox_index].loopinterval = atoi(value);
342 } else if (!strcmp(setting, "buttontwo.")) {
343 if (strlen(value) + 1 > BUF_BIG) {
344 DMA(DEBUG_ERROR,
345 "Mailbox %i buttontwo string '%s' is too long.\n",
346 mbox_index, value);
347 continue;
348 } else {
349 strncpy(mbox[mbox_index].button2, value, BUF_BIG - 1);
351 } else if (!strcmp(setting, "fetchcmd.")) {
352 if (strlen(value) + 1 > BUF_BIG) {
353 DMA(DEBUG_ERROR,
354 "Mailbox %i fetchcmd string '%s' is too long.\n",
355 mbox_index, value);
356 continue;
357 } else {
358 strncpy(mbox[mbox_index].fetchcmd, value, BUF_BIG - 1);
360 } else if (!strcmp(setting, "fetchinterval.")) {
361 mbox[mbox_index].fetchinterval = atoi(value);
362 } else if (!strcmp(setting, "debug.")) {
363 int debug_value = debug_default;
364 if (strcasecmp(value, "all") == 0) {
365 debug_value = DEBUG_ALL;
367 /* could disable debugging, but I want the command
368 line argument to provide all information
369 possible. */
370 mbox[mbox_index].debug = debug_value;
371 } else {
372 DMA(DEBUG_INFO, "Unknown setting '%s'\n", setting);
375 (void) fclose(fp);
376 for (i = 0; i < num_mailboxes; i++)
377 if (mbox[i].label[0] != '\0')
378 parse_mbox_path(i);
379 return 1;
383 static void init_biff(char *config_file)
385 #ifdef HAVE_GCRYPT_H
386 gcry_error_t rc;
387 #endif
388 int loopinterval = DEFAULT_LOOP;
389 unsigned int i;
391 for (i = 0; i < MAX_NUM_MAILBOXES; i++) {
392 memset(mbox[i].label, 0, BUF_SMALL);
393 memset(mbox[i].path, 0, BUF_BIG);
394 memset(mbox[i].notify, 0, BUF_BIG);
395 memset(mbox[i].action, 0, BUF_BIG);
396 memset(mbox[i].actiondc, 0, BUF_BIG);
397 memset(mbox[i].actionnew, 0, BUF_BIG);
398 memset(mbox[i].actionnonew, 0, BUF_BIG);
399 memset(mbox[i].button2, 0, BUF_BIG);
400 memset(mbox[i].fetchcmd, 0, BUF_BIG);
401 mbox[i].loopinterval = 0;
402 mbox[i].getHeaders = NULL;
403 mbox[i].releaseHeaders = NULL;
404 mbox[i].debug = debug_default;
405 mbox[i].askpass = DEFAULT_ASKPASS;
408 #ifdef HAVE_GCRYPT_H
409 /* gcrypt is a little strange, in that it doesn't
410 * seem to initialize its memory pool by itself.
411 * I believe we *expect* "Warning: using insecure memory!"
413 /* gcryctl_disable_secmem gets called before check_version -- in a message on
414 gcrypt-devel august 17 2004. */
415 if ((rc = gcry_control(GCRYCTL_DISABLE_SECMEM, 0)) != 0) {
416 DMA(DEBUG_ERROR,
417 "Error: tried to disable gcrypt warning and failed: %d\n"
418 " Message: %s\n" " libgcrypt version: %s\n", rc,
419 gcry_strerror(rc), gcry_check_version(NULL));
422 /* recently made a requirement, section 2.4 of gcrypt manual */
423 if (gcry_check_version("1.1.42") == NULL) {
424 DMA(DEBUG_ERROR, "Error: incompatible gcrypt version.\n");
426 #endif
428 DMA(DEBUG_INFO, "config_file = %s.\n", config_file);
429 if (!Read_Config_File(config_file, &loopinterval)) {
430 char *m;
431 /* setup defaults if there's no config */
432 if ((m = getenv("MAIL")) != NULL) {
433 /* we are using MAIL environment var. type mbox */
434 if (strlen(m) + 1 > BUF_BIG) {
435 DMA(DEBUG_ERROR,
436 "MAIL environment var '%s' is too long.\n", m);
437 } else {
438 DMA(DEBUG_INFO, "Using MAIL environment var '%s'.\n", m);
439 strncpy(mbox[0].path, m, BUF_BIG - 1);
441 } else if ((m = getenv("USER")) != NULL) {
442 /* we will use the USER env var to find an mbox name */
443 if (strlen(m) + 10 + 1 > BUF_BIG) {
444 DMA(DEBUG_ERROR,
445 "USER environment var '%s' is too long.\n", m);
446 } else {
447 DMA(DEBUG_INFO, "Using /var/mail/%s.\n", m);
448 strcpy(mbox[0].path, "/var/mail/");
449 strncat(mbox[0].path, m, BUF_BIG - 1 - 10);
450 if (mbox[0].path[9] != '/') {
451 DMA(DEBUG_ERROR,
452 "Unexpected failure to construct /var/mail/username, please "
453 "report this with your operating system info and the version of wmbiff.");
456 } else {
457 DMA(DEBUG_ERROR, "Cannot open config file '%s' nor use the "
458 "MAIL environment var.\n", config_file);
459 exit(EXIT_FAILURE);
461 if (!exists(mbox[0].path)) {
462 DMA(DEBUG_ERROR, "Cannot open config file '%s', and the "
463 "default %s doesn't exist.\n", config_file, mbox[0].path);
464 exit(EXIT_FAILURE);
466 strcpy(mbox[0].label, "Spool");
467 mboxCreate((&mbox[0]), mbox[0].path);
470 /* Make labels look right */
471 for (i = 0; i < num_mailboxes; i++) {
472 if (mbox[i].label[0] != '\0') {
473 /* append a colon, but skip if we're using fonts. */
474 if (font == NULL) {
475 int j = strlen(mbox[i].label);
476 if (j < 5) {
477 memset(mbox[i].label + j, ' ', 5 - j);
480 /* but always end after 5 characters */
481 mbox[i].label[6] = '\0';
482 /* set global loopinterval to boxes with 0 loop */
483 if (!mbox[i].loopinterval) {
484 mbox[i].loopinterval = loopinterval;
490 static char **LoadXPM(const char *pixmap_filename)
492 char **xpm;
493 int success;
494 success = XpmReadFileToData((char *) pixmap_filename, &xpm);
495 switch (success) {
496 case XpmOpenFailed:
497 DMA(DEBUG_ERROR, "Unable to open %s\n", pixmap_filename);
498 break;
499 case XpmFileInvalid:
500 DMA(DEBUG_ERROR, "%s is not a valid pixmap\n", pixmap_filename);
501 break;
502 case XpmNoMemory:
503 DMA(DEBUG_ERROR, "Insufficient memory to read %s\n",
504 pixmap_filename);
505 break;
506 default:
507 break;
509 return (xpm);
512 /* tests as "test -f" would */
513 int exists(const char *filename)
515 struct stat st_buf;
516 DMA(DEBUG_INFO, "looking for %s\n", filename);
517 if (stat(filename, &st_buf) == 0 && S_ISREG(st_buf.st_mode)) {
518 DMA(DEBUG_INFO, "found %s\n", filename);
519 return (1);
521 return (0);
524 /* acts like execvp, with code inspired by it */
525 /* mustfree */
526 static char *search_path(const char *path, const char *find_me)
528 char *buf;
529 const char *p;
530 int len, pathlen;
531 if (strchr(find_me, '/') != NULL) {
532 return (strdup_ordie(find_me));
534 pathlen = strlen(path);
535 len = strlen(find_me) + 1;
536 buf = malloc_ordie(pathlen + len + 1);
537 memcpy(buf + pathlen + 1, find_me, len);
538 buf[pathlen] = '/';
540 for (p = path; p != NULL; path = p, path++) {
541 char *startp;
542 p = strchr(path, ':');
543 if (p == NULL) {
544 /* not found; p should point to the null char at the end */
545 startp =
546 memcpy(buf + pathlen - strlen(path), path, strlen(path));
547 } else if (p == path) {
548 /* double colon in a path apparently means try here */
549 startp = &buf[pathlen + 1];
550 } else {
551 /* copy the part between the colons to the buffer */
552 startp = memcpy(buf + pathlen - (p - path), path, p - path);
554 if (exists(startp) != 0) {
555 char *ret = strdup_ordie(startp);
556 free(buf);
557 return (ret);
560 free(buf);
561 return (NULL);
564 /* verifies that .wmbiffrc, is a file, is owned by the user,
565 is not world writeable, and is not world readable. This
566 is just to help keep passwords secure */
567 static int wmbiffrc_permissions_check(const char *wmbiffrc_fname)
569 struct stat st;
570 if (stat(wmbiffrc_fname, &st) != 0) {
571 DMA(DEBUG_ERROR, "Can't stat wmbiffrc: '%s'\n", wmbiffrc_fname);
572 return (1); /* well, it's not a bad permission
573 problem: if you can't find it,
574 neither can the bad guys.. */
576 if ((st.st_mode & S_IFDIR) != 0) {
577 DMA(DEBUG_ERROR, ".wmbiffrc '%s' is a directory!\n"
578 "exiting. don't do that.", wmbiffrc_fname);
579 exit(EXIT_FAILURE);
581 if (st.st_uid != getuid()) {
582 char *user = getenv("USER");
583 DMA(DEBUG_ERROR,
584 ".wmbiffrc '%s' isn't owned by you.\n"
585 "Verify its contents, then 'chown %s %s'\n",
586 wmbiffrc_fname, ((user != NULL) ? user : "(your username)"),
587 wmbiffrc_fname);
588 return (0);
590 if ((st.st_mode & S_IWOTH) != 0) {
591 DMA(DEBUG_ERROR, ".wmbiffrc '%s' is world writable.\n"
592 "Verify its contents, then 'chmod 0600 %s'\n",
593 wmbiffrc_fname, wmbiffrc_fname);
594 return (0);
596 if ((st.st_mode & S_IROTH) != 0) {
597 DMA(DEBUG_ERROR, ".wmbiffrc '%s' is world readable.\n"
598 "Please run 'chmod 0600 %s' and consider changing your passwords.\n",
599 wmbiffrc_fname, wmbiffrc_fname);
600 return (0);
602 return (1);
605 static void ClearDigits(unsigned int i)
607 if (font) {
608 eraseRect(39, mbox_y(i), 58, mbox_y(i + 1) - 1, background);
609 } else {
610 /* overwrite the colon */
611 copyXPMArea((10 * (CHAR_WIDTH + 1)), 64, (CHAR_WIDTH + 1),
612 (CHAR_HEIGHT + 1), 35, mbox_y(i));
613 /* blank out the number fields. */
614 copyXPMArea(39, 84, (3 * (CHAR_WIDTH + 1)), (CHAR_HEIGHT + 1), 39,
615 mbox_y(i));
619 /* Blits a string at given co-ordinates. If a ``new''
620 parameter is nonzero, all digits will be yellow */
621 static void BlitString(const char *name, int x, int y, int new)
623 if (font != NULL) {
624 /* an alternate behavior - draw the string using a font
625 instead of the pixmap. should allow pretty colors */
626 drawString(x, y + CHAR_HEIGHT + 1, name,
627 new ? highlight : foreground, background, 0);
628 } else {
629 /* normal, LED-like behavior. */
630 int i, c, k = x;
631 for (i = 0; name[i] != '\0'; i++) {
632 c = toupper(name[i]);
633 if (c >= 'A' && c <= 'Z') { /* it's a letter */
634 c -= 'A';
635 copyXPMArea(c * (CHAR_WIDTH + 1), (new ? 95 : 74),
636 (CHAR_WIDTH + 1), (CHAR_HEIGHT + 1), k, y);
637 k += (CHAR_WIDTH + 1);
638 } else { /* it's a number or symbol */
639 c -= '0';
640 if (new) {
641 copyXPMArea((c * (CHAR_WIDTH + 1)) + 65, 0,
642 (CHAR_WIDTH + 1), (CHAR_HEIGHT + 1), k, y);
643 } else {
644 copyXPMArea((c * (CHAR_WIDTH + 1)), 64,
645 (CHAR_WIDTH + 1), (CHAR_HEIGHT + 1), k, y);
647 k += (CHAR_WIDTH + 1);
654 /* Blits number to give coordinates.. two 0's, right justified */
655 static void BlitNum(int num, int x, int y, int new)
657 char buf[32];
659 sprintf(buf, "%02i", num);
661 if (font != NULL) {
662 const char *color = (new) ? highlight : foreground;
663 drawString(x + (CHAR_WIDTH * 2 + 4), y + CHAR_HEIGHT + 1, buf,
664 color, background, 1);
665 } else {
666 int newx = x;
668 if (num > 99)
669 newx -= (CHAR_WIDTH + 1);
670 if (num > 999)
671 newx -= (CHAR_WIDTH + 1);
673 BlitString(buf, newx, y, new);
677 /* helper function for displayMsgCounters, which has outgrown its name */
678 static void blitMsgCounters(unsigned int i)
680 int y_row = mbox_y(i); /* constant for each mailbox */
681 ClearDigits(i); /* Clear digits */
682 if ((mbox[i].blink_stat & 0x01) == 0) {
683 int newmail = (mbox[i].UnreadMsgs > 0) ? 1 : 0;
684 if (mbox[i].TextStatus[0] != '\0') {
685 BlitString(mbox[i].TextStatus, 39, y_row, newmail);
686 } else {
687 int mailcount =
688 (newmail) ? mbox[i].UnreadMsgs : 0;
689 BlitNum(mailcount, 45, y_row, newmail);
695 * void execnotify(1) : runs notify command, if given (not null)
697 static void execnotify( /*@null@ */ const char *notifycmd)
699 if (notifycmd != NULL) { /* need to call notify() ? */
700 if (!strcasecmp(notifycmd, "true")) {
701 /* Yes, nothing */
702 } else {
703 /* Else call external notifyer, ignoring the pid */
704 (void) execCommand(notifycmd);
710 static void
711 displayMsgCounters(unsigned int i, int mail_stat, int *Blink_Mode)
713 switch (mail_stat) {
714 case 2: /* New mail has arrived */
715 /* Enter blink-mode for digits */
716 mbox[i].blink_stat = BLINK_TIMES * 2;
717 *Blink_Mode |= (1 << i); /* Global blink flag set for this mailbox */
718 blitMsgCounters(i);
719 execnotify(mbox[i].notify);
720 break;
721 case 1: /* mailbox has been rescanned/changed */
722 blitMsgCounters(i);
723 break;
724 case 0:
725 break;
726 case -1: /* Error was detected */
727 ClearDigits(i); /* Clear digits */
728 BlitString("XX", 45, mbox_y(i), 0);
729 break;
733 /** counts mail in spool-file
734 Returned value:
735 -1 : Error was encountered
736 0 : mailbox status wasn't changed
737 1 : mailbox was changed (NO new mail)
738 2 : mailbox was changed AND new mail has arrived
740 static int count_mail(unsigned int item)
742 int rc = 0;
744 if (!mbox[item].checkMail) {
745 return -1;
748 if (mbox[item].checkMail(&(mbox[item])) < 0) {
749 /* we failed to obtain any numbers therefore set
750 * them to -1's ensuring the next pass (even if
751 * zero) will be captured correctly
753 mbox[item].TotalMsgs = -1;
754 mbox[item].UnreadMsgs = -1;
755 mbox[item].OldMsgs = -1;
756 mbox[item].OldUnreadMsgs = -1;
757 return -1;
760 if (mbox[item].UnreadMsgs > mbox[item].OldUnreadMsgs &&
761 mbox[item].UnreadMsgs > 0) {
762 rc = 2; /* New mail detected */
763 } else if (mbox[item].UnreadMsgs < mbox[item].OldUnreadMsgs ||
764 mbox[item].TotalMsgs != mbox[item].OldMsgs) {
765 rc = 1; /* mailbox was changed - NO new mail */
766 } else {
767 rc = 0; /* mailbox wasn't changed */
769 mbox[item].OldMsgs = mbox[item].TotalMsgs;
770 mbox[item].OldUnreadMsgs = mbox[item].UnreadMsgs;
771 return rc;
774 static int periodic_mail_check(void)
776 int NeedRedraw = 0;
777 static int Blink_Mode = 0; /* Bit mask, digits are in blinking
778 mode or not. Each bit for separate
779 mailbox */
780 int Sleep_Interval; /* either DEFAULT_SLEEP_INTERVAL or
781 BLINK_SLEEP_INTERVAL */
782 int NewMail = 0; /* flag for global notify */
783 unsigned int i;
784 time_t curtime = time(0);
785 for (i = 0; i < num_mailboxes; i++) {
786 if (mbox[i].label[0] != '\0') {
787 if (curtime >= mbox[i].prevtime + mbox[i].loopinterval) {
788 int mailstat = 0;
789 NeedRedraw = 1;
790 DM(&mbox[i], DEBUG_INFO,
791 "working on [%u].label=>%s< [%u].path=>%s<\n", i,
792 mbox[i].label, i, mbox[i].path);
793 DM(&mbox[i], DEBUG_INFO,
794 "curtime=%d, prevtime=%d, interval=%d\n",
795 (int) curtime, (int) mbox[i].prevtime,
796 mbox[i].loopinterval);
797 mbox[i].prevtime = curtime;
799 XDefineCursor(display, iconwin, busy_cursor);
800 RedrawWindow();
802 mailstat = count_mail(i);
804 XUndefineCursor(display, iconwin);
806 /* Global notify */
807 if (mailstat == 2)
808 NewMail = 1;
810 displayMsgCounters(i, mailstat, &Blink_Mode);
811 /* update our idea of current time, as it
812 may have changed as we check. */
813 curtime = time(0);
815 if (mbox[i].blink_stat > 0) {
816 if (--mbox[i].blink_stat <= 0) {
817 Blink_Mode &= ~(1 << i);
818 mbox[i].blink_stat = 0;
820 displayMsgCounters(i, 1, &Blink_Mode);
821 NeedRedraw = 1;
823 if (mbox[i].fetchinterval > 0 && mbox[i].fetchcmd[0] != '\0'
824 && curtime >=
825 mbox[i].prevfetch_time + mbox[i].fetchinterval) {
827 XDefineCursor(display, iconwin, busy_cursor);
828 RedrawWindow();
830 (void) execCommand(mbox[i].fetchcmd);
832 XUndefineCursor(display, iconwin);
834 mbox[i].prevfetch_time = curtime;
839 /* exec globalnotify if there was any new mail */
840 if (NewMail == 1)
841 execnotify(globalnotify);
843 if (Blink_Mode == 0) {
844 for (i = 0; i < num_mailboxes; i++) {
845 mbox[i].blink_stat = 0;
847 Sleep_Interval = DEFAULT_SLEEP_INTERVAL;
848 } else {
849 Sleep_Interval = BLINK_SLEEP_INTERVAL;
852 if (NeedRedraw) {
853 NeedRedraw = 0;
854 RedrawWindow();
857 return Sleep_Interval;
860 static int findTopOfMasterXPM(const char **skin_xpm)
862 int i;
863 for (i = 0; skin_xpm[i] != NULL; i++) {
864 if (strstr(skin_xpm[i], "++++++++") != NULL)
865 return i;
867 DMA(DEBUG_ERROR,
868 "couldn't find the top of the xpm file using the simple method\n");
869 exit(EXIT_FAILURE);
872 static char **CreateBackingXPM(int width, int height,
873 const char **skin_xpm)
875 char **ret = malloc_ordie(sizeof(char *) * (height + 6)
876 + sizeof(void *) /* trailing null space */ );
877 const int colors = 5;
878 const int base = colors + 1;
879 const int margin = 4;
880 int i;
881 int top = findTopOfMasterXPM(skin_xpm);
882 ret[0] = malloc_ordie(30);
883 sprintf(ret[0], "%d %d %d %d", width, height, colors, 1);
884 ret[1] = (char *) " \tc #0000FF"; /* no color */
885 ret[2] = (char *) ".\tc #505075"; /* background gray */
886 ret[2] = malloc_ordie(30);
887 sprintf(ret[2], ".\tc %s", background);
888 ret[3] = (char *) "+\tc #000000"; /* shadowed */
889 ret[4] = (char *) "@\tc #C7C3C7"; /* highlight */
890 ret[5] = (char *) ":\tc #004941"; /* led off */
891 for (i = base; i < base + height; i++) {
892 ret[i] = malloc_ordie(width);
894 for (i = base; i < base + margin; i++) {
895 memset(ret[i], ' ', width);
897 for (i = base + margin; i < height + base - margin; i++) {
898 memset(ret[i], ' ', margin);
900 if (i == base + margin) {
901 memset(ret[i] + margin, '+', width - margin - margin);
902 } else if (i == base + height - margin - 1) {
903 memset(ret[i] + margin, '@', width - margin - margin);
904 } else {
905 // " +..:::...:::...:::...:::...:::.......:::...:::...:::...@ "
906 // " +.:...:.:...:.:...:.:...:.:...:..:..:...:.:...:.:...:..@ " ",
907 ret[i][margin] = '+';
908 memset(ret[i] + margin + 1, '.', width - margin - margin - 1);
909 ret[i][width - margin - 1] = '@';
910 memcpy(ret[i],
911 skin_xpm[((i - (base + margin) - 1) % 11) + top + 1],
912 width);
915 memset(ret[i] + width - margin, ' ', margin);
917 for (i = base + height - margin; i < height + base; i++) {
918 memset(ret[i], ' ', width);
920 ret[height + base] = NULL; /* not sure if this is necessary, it just
921 seemed like a good idea */
922 return (ret);
926 * NOTE: this function assumes that the ConnectionNumber() macro
927 * will return the file descriptor of the Display struct
928 * (it does under XFree86 and solaris' openwin X)
930 static void XSleep(int millisec)
932 #ifdef HAVE_POLL
933 struct pollfd timeout;
935 timeout.fd = ConnectionNumber(display);
936 timeout.events = POLLIN;
938 poll(&timeout, 1, millisec);
939 #else
940 struct timeval to;
941 struct timeval *timeout = NULL;
942 fd_set readfds;
943 int max_fd;
945 if (millisec >= 0) {
946 timeout = &to;
947 to.tv_sec = millisec / 1000;
948 to.tv_usec = (millisec % 1000) * 1000;
950 FD_ZERO(&readfds);
951 FD_SET(ConnectionNumber(display), &readfds);
952 max_fd = ConnectionNumber(display);
954 select(max_fd + 1, &readfds, NULL, NULL, timeout);
955 #endif
958 const char **restart_args;
960 static void restart_wmbiff(int sig
961 #ifdef HAVE___ATTRIBUTE__
962 __attribute__ ((unused))
963 #endif
966 if (restart_args) {
967 DMA(DEBUG_ERROR, "exec()'ing %s\n", restart_args[0]);
968 sleep(1);
969 execvp(restart_args[0], (char *const *) restart_args);
970 DMA(DEBUG_ERROR, "exec of %s failed: %s\n",
971 restart_args[0], strerror(errno));
972 exit(EXIT_FAILURE);
974 else
975 fprintf(stderr, "Unable to restart wmbiff: missing restart arguments (NULL)!\n");
978 extern int x_socket(void)
980 return ConnectionNumber(display);
982 extern void ProcessPendingEvents(void)
984 static int but_pressed_region = -1; /* static so click can be determined */
985 int but_released_region = -1;
986 /* X Events */
987 while (XPending(display)) {
988 XEvent Event;
989 const char *press_action;
991 XNextEvent(display, &Event);
993 switch (Event.type) {
994 case Expose:
995 if (Event.xany.window != win && Event.xany.window != iconwin) {
996 msglst_redraw();
997 } else {
998 RedrawWindow();
1000 break;
1001 case DestroyNotify:
1002 XCloseDisplay(display);
1003 exit(EXIT_SUCCESS);
1004 break;
1005 case ButtonPress:
1006 but_pressed_region =
1007 CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
1008 switch (Event.xbutton.button) {
1009 case 1:
1010 press_action = mbox[but_pressed_region].action;
1011 break;
1012 case 2:
1013 press_action = mbox[but_pressed_region].button2;
1014 break;
1015 case 3:
1016 press_action = mbox[but_pressed_region].fetchcmd;
1017 break;
1018 default:
1019 press_action = NULL;
1020 break;
1023 if (press_action && strcmp(press_action, "msglst") == 0) {
1024 msglst_show(&mbox[but_pressed_region],
1025 Event.xbutton.x_root, Event.xbutton.y_root);
1027 break;
1028 case ButtonRelease:
1029 but_released_region =
1030 CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
1031 if (but_released_region == but_pressed_region
1032 && but_released_region >= 0) {
1033 const char *click_action, *extra_click_action = NULL;
1035 switch (Event.xbutton.button) {
1036 case 1: /* Left mouse-click */
1037 /* C-S-left will restart wmbiff. */
1038 if ((Event.xbutton.state & ControlMask) &&
1039 (Event.xbutton.state & ShiftMask)) {
1040 restart_wmbiff(0);
1042 /* do we need to run an extra action? */
1043 if (mbox[but_released_region].UnreadMsgs == -1) {
1044 extra_click_action =
1045 mbox[but_released_region].actiondc;
1046 } else if (mbox[but_released_region].UnreadMsgs > 0) {
1047 extra_click_action =
1048 mbox[but_released_region].actionnew;
1049 } else {
1050 extra_click_action =
1051 mbox[but_released_region].actionnonew;
1053 click_action = mbox[but_released_region].action;
1054 break;
1055 case 2: /* Middle mouse-click */
1056 click_action = mbox[but_released_region].button2;
1057 break;
1058 case 3: /* Right mouse-click */
1059 click_action = mbox[but_released_region].fetchcmd;
1060 break;
1061 default:
1062 click_action = NULL;
1063 break;
1065 if (extra_click_action != NULL
1066 && extra_click_action[0] != 0
1067 && strcmp(extra_click_action, "msglst")) {
1068 DM(&mbox[but_released_region], DEBUG_INFO,
1069 "runing: %s", extra_click_action);
1070 (void) execCommand(extra_click_action);
1072 if (click_action != NULL
1073 && click_action[0] != '\0'
1074 && strcmp(click_action, "msglst")) {
1075 DM(&mbox[but_released_region], DEBUG_INFO,
1076 "running: %s", click_action);
1077 (void) execCommand(click_action);
1081 /* a button was released, hide the message list if open */
1082 msglst_hide();
1084 but_pressed_region = -1;
1085 /* RedrawWindow(); */
1086 break;
1087 case MotionNotify:
1088 break;
1089 case KeyPress:{
1090 XKeyPressedEvent *xkpe = (XKeyPressedEvent *) & Event;
1091 KeySym ks = XkbKeycodeToKeysym(display, xkpe->keycode, 0, 0);
1092 if (ks > XK_0 && ks < XK_0 + min(9U, num_mailboxes)) {
1093 const char *click_action = mbox[ks - XK_1].action;
1094 if (click_action != NULL
1095 && click_action[0] != '\0'
1096 && strcmp(click_action, "msglst")) {
1097 DM(&mbox[but_released_region], DEBUG_INFO,
1098 "running: %s", click_action);
1099 (void) execCommand(click_action);
1104 break;
1105 default:
1106 break;
1111 static void do_biff(int argc, const char **argv)
1113 unsigned int i;
1114 int Sleep_Interval;
1115 const char **skin_xpm = NULL;
1116 const char **bkg_xpm = NULL;
1117 char *skin_file_path = search_path(skin_search_path, skin_filename);
1118 int wmbiff_mask_height = mbox_y(num_mailboxes) + 4;
1120 DMA(DEBUG_INFO, "running %u mailboxes w %d h %d\n", num_mailboxes,
1121 wmbiff_mask_width, wmbiff_mask_height);
1123 if (skin_file_path != NULL) {
1124 skin_xpm = (const char **) LoadXPM(skin_file_path);
1125 free(skin_file_path);
1127 if (skin_xpm == NULL) {
1128 DMA(DEBUG_ERROR, "using built-in xpm; %s wasn't found in %s\n",
1129 skin_filename, skin_search_path);
1130 skin_xpm = wmbiff_master_xpm;
1133 bkg_xpm = (const char **) CreateBackingXPM(wmbiff_mask_width, wmbiff_mask_height, skin_xpm);
1135 createXBMfromXPM(wmbiff_mask_bits, bkg_xpm,
1136 wmbiff_mask_width, wmbiff_mask_height);
1138 openXwindow(argc, argv, bkg_xpm, skin_xpm, wmbiff_mask_bits,
1139 wmbiff_mask_width, wmbiff_mask_height, notWithdrawn);
1141 /* now that display is set, we can create the cursors
1142 (mouse pointer shapes) */
1143 busy_cursor = XCreateFontCursor(display, XC_watch);
1144 ready_cursor = XCreateFontCursor(display, XC_left_ptr);
1146 if (font != NULL) {
1147 if (loadFont(font) < 0) {
1148 DMA(DEBUG_ERROR, "unable to load font. exiting.\n");
1149 exit(EXIT_FAILURE);
1153 /* First time setup of button regions and labels */
1154 for (i = 0; i < num_mailboxes; i++) {
1155 /* make it easy to recover the mbox index from a mouse click */
1156 AddMouseRegion(i, x_origin, mbox_y(i), 58, mbox_y(i + 1) - 1);
1157 if (mbox[i].label[0] != '\0') {
1158 mbox[i].prevtime = mbox[i].prevfetch_time = 0;
1159 BlitString(mbox[i].label, x_origin, mbox_y(i), 0);
1163 do {
1165 Sleep_Interval = periodic_mail_check();
1166 ProcessPendingEvents();
1167 XSleep(Sleep_Interval);
1169 while (forever); /* forever is usually true,
1170 but not when debugging with -exit */
1171 if (skin_xpm != NULL && skin_xpm != wmbiff_master_xpm) {
1172 free(skin_xpm); // added 3 jul 02, appeasing valgrind
1174 if (bkg_xpm != NULL) {
1175 // Allocated in CreateBackingXPM()
1176 free((void *)bkg_xpm[0]);
1177 free((void *)bkg_xpm[2]);
1178 int mem_block;
1179 for (mem_block = 6; mem_block < 6 + wmbiff_mask_height; mem_block++)
1180 free((void *)bkg_xpm[mem_block]);
1181 free(bkg_xpm);
1185 static void sigchld_handler(int sig
1186 #ifdef HAVE___ATTRIBUTE__
1187 __attribute__ ((unused))
1188 #endif
1191 while (waitpid(0, NULL, WNOHANG) > 0);
1192 signal(SIGCHLD, sigchld_handler);
1195 static void usage(void)
1197 printf("\nwmBiff v%s"
1198 " - incoming mail checker\n"
1199 "Gennady Belyakov and others (see the README file)\n"
1200 "Please report bugs to %s\n"
1201 "\n"
1202 "usage:\n"
1203 " -bg <color> background color\n"
1204 " -c <filename> use specified config file\n"
1205 " -debug enable debugging\n"
1206 " -display <display name> use specified X display\n"
1207 " -fg <color> foreground color\n"
1208 " -font <font> font instead of LED\n"
1209 " -geometry +XPOS+YPOS initial window position\n"
1210 " -h this help screen\n"
1211 " -hi <color> highlight color for new mail\n"
1212 #ifdef USE_GNUTLS
1213 " -skip-certificate-check using TLS, don't validate the\n"
1214 " server's certificate\n"
1215 #endif
1216 " -relax assume the configuration is \n"
1217 " correct, parse it without paranoia, \n"
1218 " and assume hostnames are okay.\n"
1219 " -v print the version number\n"
1220 " +w not withdrawn: run as a window\n"
1221 "\n", PACKAGE_VERSION, PACKAGE_BUGREPORT);
1224 static void printversion(void)
1226 printf("wmbiff v%s\n", PACKAGE_VERSION);
1230 static void parse_cmd(int argc, const char **argv, char *config_file)
1232 int i;
1234 config_file[0] = '\0';
1236 /* Parse Command Line */
1238 for (i = 1; i < argc; i++) {
1239 const char *arg = argv[i];
1241 if (*arg == '-') {
1242 switch (arg[1]) {
1243 case 'b':
1244 if (strcmp(arg + 1, "bg") == 0) {
1245 if (argc > (i + 1)) {
1246 background = strdup_ordie(argv[i + 1]);
1247 DMA(DEBUG_INFO, "new background: %s", foreground);
1248 i++;
1249 if (font == NULL)
1250 font = DEFAULT_FONT;
1253 break;
1254 case 'd':
1255 if (strcmp(arg + 1, "debug") == 0) {
1256 debug_default = DEBUG_ALL;
1257 } else if (strcmp(arg + 1, "display") == 0) {
1258 /* passed to X's command line parser */
1259 } else {
1260 usage();
1261 exit(EXIT_FAILURE);
1263 break;
1264 case 'f':
1265 if (strcmp(arg + 1, "fg") == 0) {
1266 if (argc > (i + 1)) {
1267 foreground = strdup_ordie(argv[i + 1]);
1268 DMA(DEBUG_INFO, "new foreground: %s", foreground);
1269 i++;
1270 if (font == NULL)
1271 font = DEFAULT_FONT;
1273 } else if (strcmp(arg + 1, "font") == 0) {
1274 if (argc > (i + 1)) {
1275 if (strcmp(argv[i + 1], "default") == 0) {
1276 font = DEFAULT_FONT;
1277 } else {
1278 font = strdup_ordie(argv[i + 1]);
1280 DMA(DEBUG_INFO, "new font: %s", font);
1281 i++;
1283 } else {
1284 usage();
1285 exit(EXIT_FAILURE);
1287 break;
1288 case 'g':
1289 if (strcmp(arg + 1, "geometry") != 0) {
1290 usage();
1291 exit(EXIT_FAILURE);
1292 } else {
1293 i++; /* gobble the argument */
1294 if (i >= argc) { /* fail if there's nothing to gobble */
1295 usage();
1296 exit(EXIT_FAILURE);
1299 break;
1300 case 'h':
1301 if (strcmp(arg + 1, "hi") == 0) {
1302 if (argc > (i + 1)) {
1303 highlight = strdup_ordie(argv[i + 1]);
1304 DMA(DEBUG_INFO, "new highlight: %s", highlight);
1305 i++;
1306 if (font == NULL)
1307 font = DEFAULT_FONT;
1309 } else if (strcmp(arg + 1, "h") == 0) {
1310 usage();
1311 exit(EXIT_SUCCESS);
1313 break;
1314 case 'v':
1315 printversion();
1316 exit(EXIT_SUCCESS);
1317 break;
1318 case 's':
1319 if (strcmp(arg + 1, "skip-certificate-check") == 0) {
1320 SkipCertificateCheck = 1;
1321 } else {
1322 usage();
1323 exit(EXIT_SUCCESS);
1326 break;
1327 case 'r':
1328 if (strcmp(arg + 1, "relax") == 0) {
1329 Relax = 1;
1330 } else {
1331 usage();
1332 exit(EXIT_SUCCESS);
1335 break;
1336 case 'c':
1337 if (argc > (i + 1)) {
1338 strncpy(config_file, argv[i + 1], 255);
1339 i++;
1341 break;
1342 case 'e': /* undocumented for debugging */
1343 if (strcmp(arg + 1, "exit") == 0) {
1344 forever = 0;
1346 break;
1347 default:
1348 usage();
1349 exit(EXIT_SUCCESS);
1350 break;
1352 } else if (*arg == '+') {
1353 switch (arg[1]) {
1354 case 'w':
1355 notWithdrawn = 1;
1356 break;
1357 default:
1358 usage();
1359 exit(EXIT_SUCCESS);
1360 break;
1366 int main(int argc, const char *argv[])
1368 char uconfig_file[256];
1370 /* hold on to the arguments we were started with; we
1371 will need them if we have to restart on sigusr1 */
1372 restart_args =
1373 (const char **) malloc((argc + 1) * sizeof(const char *));
1374 if (restart_args) {
1375 memcpy(restart_args, argv, (argc) * sizeof(const char *));
1376 restart_args[argc] = NULL;
1379 parse_cmd(argc, argv, uconfig_file);
1381 /* decide what the config file is */
1382 if (uconfig_file[0] != '\0') { /* user-specified config file */
1383 DMA(DEBUG_INFO, "Using user-specified config file '%s'.\n",
1384 uconfig_file);
1385 } else {
1386 const char *home = getenv("HOME");
1387 if (home == NULL) {
1388 DMA(DEBUG_ERROR,
1389 "$HOME undefined. Use the -c option to specify a wmbiffrc\n");
1390 exit(EXIT_FAILURE);
1392 sprintf(uconfig_file, "%s/.wmbiffrc", home);
1395 if (wmbiffrc_permissions_check(uconfig_file) == 0) {
1396 DMA(DEBUG_ERROR,
1397 "WARNING: In future versions of WMBiff, .wmbiffrc MUST be\n"
1398 "owned by the user, and not readable or writable by others.\n\n");
1400 init_biff(uconfig_file);
1401 signal(SIGCHLD, sigchld_handler);
1402 signal(SIGUSR1, restart_wmbiff);
1403 signal(SIGPIPE, SIG_IGN); /* write() may fail */
1405 do_biff(argc, argv);
1407 // free resources
1408 if (restart_args)
1409 free(restart_args);
1411 return 0;