wmbiff: Remove unused variable.
[dockapps.git] / wmbiff / wmbiff / wmbiff.c
blobc029f1c248528efb38f11a0e64486f8a25c0706b
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/keysym.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 DMA(DEBUG_ERROR, "exec()'ing %s\n", restart_args[0]);
967 sleep(1);
968 execvp(restart_args[0], (char *const *) restart_args);
969 DMA(DEBUG_ERROR, "exec of %s failed: %s\n",
970 restart_args[0], strerror(errno));
971 exit(EXIT_FAILURE);
974 extern int x_socket(void)
976 return ConnectionNumber(display);
978 extern void ProcessPendingEvents(void)
980 static int but_pressed_region = -1; /* static so click can be determined */
981 int but_released_region = -1;
982 /* X Events */
983 while (XPending(display)) {
984 XEvent Event;
985 const char *press_action;
987 XNextEvent(display, &Event);
989 switch (Event.type) {
990 case Expose:
991 if (Event.xany.window != win && Event.xany.window != iconwin) {
992 msglst_redraw();
993 } else {
994 RedrawWindow();
996 break;
997 case DestroyNotify:
998 XCloseDisplay(display);
999 exit(EXIT_SUCCESS);
1000 break;
1001 case ButtonPress:
1002 but_pressed_region =
1003 CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
1004 switch (Event.xbutton.button) {
1005 case 1:
1006 press_action = mbox[but_pressed_region].action;
1007 break;
1008 case 2:
1009 press_action = mbox[but_pressed_region].button2;
1010 break;
1011 case 3:
1012 press_action = mbox[but_pressed_region].fetchcmd;
1013 break;
1014 default:
1015 press_action = NULL;
1016 break;
1019 if (press_action && strcmp(press_action, "msglst") == 0) {
1020 msglst_show(&mbox[but_pressed_region],
1021 Event.xbutton.x_root, Event.xbutton.y_root);
1023 break;
1024 case ButtonRelease:
1025 but_released_region =
1026 CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
1027 if (but_released_region == but_pressed_region
1028 && but_released_region >= 0) {
1029 const char *click_action, *extra_click_action = NULL;
1031 switch (Event.xbutton.button) {
1032 case 1: /* Left mouse-click */
1033 /* C-S-left will restart wmbiff. */
1034 if ((Event.xbutton.state & ControlMask) &&
1035 (Event.xbutton.state & ShiftMask)) {
1036 restart_wmbiff(0);
1038 /* do we need to run an extra action? */
1039 if (mbox[but_released_region].UnreadMsgs == -1) {
1040 extra_click_action =
1041 mbox[but_released_region].actiondc;
1042 } else if (mbox[but_released_region].UnreadMsgs > 0) {
1043 extra_click_action =
1044 mbox[but_released_region].actionnew;
1045 } else {
1046 extra_click_action =
1047 mbox[but_released_region].actionnonew;
1049 click_action = mbox[but_released_region].action;
1050 break;
1051 case 2: /* Middle mouse-click */
1052 click_action = mbox[but_released_region].button2;
1053 break;
1054 case 3: /* Right mouse-click */
1055 click_action = mbox[but_released_region].fetchcmd;
1056 break;
1057 default:
1058 click_action = NULL;
1059 break;
1061 if (extra_click_action != NULL
1062 && extra_click_action[0] != 0
1063 && strcmp(extra_click_action, "msglst")) {
1064 DM(&mbox[but_released_region], DEBUG_INFO,
1065 "runing: %s", extra_click_action);
1066 (void) execCommand(extra_click_action);
1068 if (click_action != NULL
1069 && click_action[0] != '\0'
1070 && strcmp(click_action, "msglst")) {
1071 DM(&mbox[but_released_region], DEBUG_INFO,
1072 "running: %s", click_action);
1073 (void) execCommand(click_action);
1077 /* a button was released, hide the message list if open */
1078 msglst_hide();
1080 but_pressed_region = -1;
1081 /* RedrawWindow(); */
1082 break;
1083 case MotionNotify:
1084 break;
1085 case KeyPress:{
1086 XKeyPressedEvent *xkpe = (XKeyPressedEvent *) & Event;
1087 KeySym ks = XKeycodeToKeysym(display, xkpe->keycode, 0);
1088 if (ks > XK_0 && ks < XK_0 + min(9U, num_mailboxes)) {
1089 const char *click_action = mbox[ks - XK_1].action;
1090 if (click_action != NULL
1091 && click_action[0] != '\0'
1092 && strcmp(click_action, "msglst")) {
1093 DM(&mbox[but_released_region], DEBUG_INFO,
1094 "running: %s", click_action);
1095 (void) execCommand(click_action);
1100 break;
1101 default:
1102 break;
1107 static void do_biff(int argc, const char **argv)
1109 unsigned int i;
1110 int Sleep_Interval;
1111 const char **skin_xpm = NULL;
1112 const char **bkg_xpm = NULL;
1113 char *skin_file_path = search_path(skin_search_path, skin_filename);
1114 int wmbiff_mask_height = mbox_y(num_mailboxes) + 4;
1116 DMA(DEBUG_INFO, "running %u mailboxes w %d h %d\n", num_mailboxes,
1117 wmbiff_mask_width, wmbiff_mask_height);
1119 if (skin_file_path != NULL) {
1120 skin_xpm = (const char **) LoadXPM(skin_file_path);
1121 free(skin_file_path);
1123 if (skin_xpm == NULL) {
1124 DMA(DEBUG_ERROR, "using built-in xpm; %s wasn't found in %s\n",
1125 skin_filename, skin_search_path);
1126 skin_xpm = wmbiff_master_xpm;
1129 bkg_xpm = (const char **) CreateBackingXPM(wmbiff_mask_width, wmbiff_mask_height, skin_xpm);
1131 createXBMfromXPM(wmbiff_mask_bits, bkg_xpm,
1132 wmbiff_mask_width, wmbiff_mask_height);
1134 openXwindow(argc, argv, bkg_xpm, skin_xpm, wmbiff_mask_bits,
1135 wmbiff_mask_width, wmbiff_mask_height, notWithdrawn);
1137 /* now that display is set, we can create the cursors
1138 (mouse pointer shapes) */
1139 busy_cursor = XCreateFontCursor(display, XC_watch);
1140 ready_cursor = XCreateFontCursor(display, XC_left_ptr);
1142 if (font != NULL) {
1143 if (loadFont(font) < 0) {
1144 DMA(DEBUG_ERROR, "unable to load font. exiting.\n");
1145 exit(EXIT_FAILURE);
1149 /* First time setup of button regions and labels */
1150 for (i = 0; i < num_mailboxes; i++) {
1151 /* make it easy to recover the mbox index from a mouse click */
1152 AddMouseRegion(i, x_origin, mbox_y(i), 58, mbox_y(i + 1) - 1);
1153 if (mbox[i].label[0] != '\0') {
1154 mbox[i].prevtime = mbox[i].prevfetch_time = 0;
1155 BlitString(mbox[i].label, x_origin, mbox_y(i), 0);
1159 do {
1161 Sleep_Interval = periodic_mail_check();
1162 ProcessPendingEvents();
1163 XSleep(Sleep_Interval);
1165 while (forever); /* forever is usually true,
1166 but not when debugging with -exit */
1167 if (skin_xpm != NULL && skin_xpm != wmbiff_master_xpm) {
1168 free(skin_xpm); // added 3 jul 02, appeasing valgrind
1170 if (bkg_xpm != NULL) {
1171 free(bkg_xpm);
1175 static void sigchld_handler(int sig
1176 #ifdef HAVE___ATTRIBUTE__
1177 __attribute__ ((unused))
1178 #endif
1181 while (waitpid(0, NULL, WNOHANG) > 0);
1182 signal(SIGCHLD, sigchld_handler);
1185 static void usage(void)
1187 printf("\nwmBiff v%s"
1188 " - incoming mail checker\n"
1189 "Gennady Belyakov and others (see the README file)\n"
1190 "Please report bugs to %s\n"
1191 "\n"
1192 "usage:\n"
1193 " -bg <color> background color\n"
1194 " -c <filename> use specified config file\n"
1195 " -debug enable debugging\n"
1196 " -display <display name> use specified X display\n"
1197 " -fg <color> foreground color\n"
1198 " -font <font> font instead of LED\n"
1199 " -geometry +XPOS+YPOS initial window position\n"
1200 " -h this help screen\n"
1201 " -hi <color> highlight color for new mail\n"
1202 #ifdef USE_GNUTLS
1203 " -skip-certificate-check using TLS, don't validate the\n"
1204 " server's certificate\n"
1205 #endif
1206 " -relax assume the configuration is \n"
1207 " correct, parse it without paranoia, \n"
1208 " and assume hostnames are okay.\n"
1209 " -v print the version number\n"
1210 " +w not withdrawn: run as a window\n"
1211 "\n", PACKAGE_VERSION, PACKAGE_BUGREPORT);
1214 static void printversion(void)
1216 printf("wmbiff v%s\n", PACKAGE_VERSION);
1220 static void parse_cmd(int argc, const char **argv, char *config_file)
1222 int i;
1224 config_file[0] = '\0';
1226 /* Parse Command Line */
1228 for (i = 1; i < argc; i++) {
1229 const char *arg = argv[i];
1231 if (*arg == '-') {
1232 switch (arg[1]) {
1233 case 'b':
1234 if (strcmp(arg + 1, "bg") == 0) {
1235 if (argc > (i + 1)) {
1236 background = strdup_ordie(argv[i + 1]);
1237 DMA(DEBUG_INFO, "new background: %s", foreground);
1238 i++;
1239 if (font == NULL)
1240 font = DEFAULT_FONT;
1243 break;
1244 case 'd':
1245 if (strcmp(arg + 1, "debug") == 0) {
1246 debug_default = DEBUG_ALL;
1247 } else if (strcmp(arg + 1, "display") == 0) {
1248 /* passed to X's command line parser */
1249 } else {
1250 usage();
1251 exit(EXIT_FAILURE);
1253 break;
1254 case 'f':
1255 if (strcmp(arg + 1, "fg") == 0) {
1256 if (argc > (i + 1)) {
1257 foreground = strdup_ordie(argv[i + 1]);
1258 DMA(DEBUG_INFO, "new foreground: %s", foreground);
1259 i++;
1260 if (font == NULL)
1261 font = DEFAULT_FONT;
1263 } else if (strcmp(arg + 1, "font") == 0) {
1264 if (argc > (i + 1)) {
1265 if (strcmp(argv[i + 1], "default") == 0) {
1266 font = DEFAULT_FONT;
1267 } else {
1268 font = strdup_ordie(argv[i + 1]);
1270 DMA(DEBUG_INFO, "new font: %s", font);
1271 i++;
1273 } else {
1274 usage();
1275 exit(EXIT_FAILURE);
1277 break;
1278 case 'g':
1279 if (strcmp(arg + 1, "geometry") != 0) {
1280 usage();
1281 exit(EXIT_FAILURE);
1282 } else {
1283 i++; /* gobble the argument */
1284 if (i >= argc) { /* fail if there's nothing to gobble */
1285 usage();
1286 exit(EXIT_FAILURE);
1289 break;
1290 case 'h':
1291 if (strcmp(arg + 1, "hi") == 0) {
1292 if (argc > (i + 1)) {
1293 highlight = strdup_ordie(argv[i + 1]);
1294 DMA(DEBUG_INFO, "new highlight: %s", highlight);
1295 i++;
1296 if (font == NULL)
1297 font = DEFAULT_FONT;
1299 } else if (strcmp(arg + 1, "h") == 0) {
1300 usage();
1301 exit(EXIT_SUCCESS);
1303 break;
1304 case 'v':
1305 printversion();
1306 exit(EXIT_SUCCESS);
1307 break;
1308 case 's':
1309 if (strcmp(arg + 1, "skip-certificate-check") == 0) {
1310 SkipCertificateCheck = 1;
1311 } else {
1312 usage();
1313 exit(EXIT_SUCCESS);
1316 break;
1317 case 'r':
1318 if (strcmp(arg + 1, "relax") == 0) {
1319 Relax = 1;
1320 } else {
1321 usage();
1322 exit(EXIT_SUCCESS);
1325 break;
1326 case 'c':
1327 if (argc > (i + 1)) {
1328 strncpy(config_file, argv[i + 1], 255);
1329 i++;
1331 break;
1332 case 'e': /* undocumented for debugging */
1333 if (strcmp(arg + 1, "exit") == 0) {
1334 forever = 0;
1336 break;
1337 default:
1338 usage();
1339 exit(EXIT_SUCCESS);
1340 break;
1342 } else if (*arg == '+') {
1343 switch (arg[1]) {
1344 case 'w':
1345 notWithdrawn = 1;
1346 break;
1347 default:
1348 usage();
1349 exit(EXIT_SUCCESS);
1350 break;
1356 int main(int argc, const char *argv[])
1358 char uconfig_file[256];
1360 /* hold on to the arguments we were started with; we
1361 will need them if we have to restart on sigusr1 */
1362 restart_args =
1363 (const char **) malloc((argc + 1) * sizeof(const char *));
1364 memcpy(restart_args, argv, (argc) * sizeof(const char *));
1365 restart_args[argc] = NULL;
1367 parse_cmd(argc, argv, uconfig_file);
1369 /* decide what the config file is */
1370 if (uconfig_file[0] != '\0') { /* user-specified config file */
1371 DMA(DEBUG_INFO, "Using user-specified config file '%s'.\n",
1372 uconfig_file);
1373 } else {
1374 const char *home = getenv("HOME");
1375 if (home == NULL) {
1376 DMA(DEBUG_ERROR,
1377 "$HOME undefined. Use the -c option to specify a wmbiffrc\n");
1378 exit(EXIT_FAILURE);
1380 sprintf(uconfig_file, "%s/.wmbiffrc", home);
1383 if (wmbiffrc_permissions_check(uconfig_file) == 0) {
1384 DMA(DEBUG_ERROR,
1385 "WARNING: In future versions of WMBiff, .wmbiffrc MUST be\n"
1386 "owned by the user, and not readable or writable by others.\n\n");
1388 init_biff(uconfig_file);
1389 signal(SIGCHLD, sigchld_handler);
1390 signal(SIGUSR1, restart_wmbiff);
1391 signal(SIGPIPE, SIG_IGN); /* write() may fail */
1393 do_biff(argc, argv);
1394 return 0;