c029f1c248528efb38f11a0e64486f8a25c0706b
1 /* $Id: wmbiff.c,v 1.70 2005/10/07 03:07:58 bluehal Exp $ */
3 // typedef int gcry_error_t;
19 #include <sys/types.h>
25 #include <X11/cursorfont.h>
26 #include <X11/keysym.h>
31 #include "../wmgeneral/wmgeneral.h"
32 #include "../wmgeneral/misc.h"
36 #include "MessageList.h"
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;
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
;
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. */
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
);
102 fprintf(stderr
, "unable to allocate %d bytes\n", (int) len
);
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
)
134 if (!fgets(buf
, BUF_SIZE
- 1, fp
))
139 if (buf
[len
- 1] == '\n') {
140 buf
[len
- 1] = '\0'; /* strip linefeed */
145 if (!(p
= strtok(buf
, "=")))
147 if (!(q
= strtok(NULL
, "\n")))
152 * Removed for loop (which removed leading spaces)
153 * Leading & Trailing spaces need to be removed
154 * to Fix Debian bug #95849
159 /* strcpy(setting, p); nspring replaced with sscanf dec 2002 */
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
);
168 } else if (sscanf(p
, "%[a-z]", setting
) == 1) {
169 /* global configuration, all text. */
172 /* we found an uncommented line that has an equals,
173 but is non-alphabetic. */
174 DMA(DEBUG_INFO
, "unparsed setting %s\n", p
);
178 DMA(DEBUG_INFO
, "@%s.%d=%s@\n", setting
, *mbox_index
, value
);
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
},
200 static void parse_mbox_path(unsigned int item
)
203 /* find the creator */
206 && strncasecmp(mbox
[item
].path
, paths
[i
].id
, strlen(paths
[i
].id
));
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",
215 /* default are mbox */
216 mboxCreate((&mbox
[item
]), mbox
[item
].path
);
220 static int Read_Config_File(char *filename
, int *loopinterval
)
223 char setting
[BUF_SMALL
], value
[BUF_SIZE
];
227 if (!(fp
= fopen(filename
, "r"))) {
228 DMA(DEBUG_ERROR
, "Unable to open %s, no settings read: %s\n",
229 filename
, strerror(errno
));
233 /* skanky: -1 can represent an unparsed line
235 if (ReadLine(fp
, setting
, value
, &mbox_index
) == -1)
238 /* settings that can be global go here. */
239 if (!strcmp(setting
, "interval")) {
240 *loopinterval
= atoi(value
);
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
;
249 mbox
[mbox_index
].askpass
= askpass
;
252 } else if (!strcmp(setting
, "skinfile")) {
253 skin_filename
= strdup_ordie(value
);
255 } else if (!strcmp(setting
, "certfile")) { /* not yet supported */
256 certificate_filename
= strdup_ordie(value
);
258 } else if (!strcmp(setting
, "globalnotify")) {
259 globalnotify
= strdup_ordie(value
);
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
);
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
) {
280 "Mailbox %i label string '%s' is too long.\n",
284 strncpy(mbox
[mbox_index
].label
, value
, BUF_SMALL
- 1);
286 } else if (!strcmp(setting
, "path.")) {
287 if (strlen(value
) + 1 > BUF_BIG
) {
289 "Mailbox %i path string '%s' is too long.\n",
293 strncpy(mbox
[mbox_index
].path
, value
, BUF_BIG
- 1);
295 } else if (!strcmp(setting
, "notify.")) {
296 if (strlen(value
) + 1 > BUF_BIG
) {
298 "Mailbox %i notify string '%s' is too long.\n",
302 strncpy(mbox
[mbox_index
].notify
, value
, BUF_BIG
- 1);
304 } else if (!strcmp(setting
, "action.")) {
305 if (strlen(value
) + 1 > BUF_BIG
) {
307 "Mailbox %i action string '%s' is too long.\n",
311 strncpy(mbox
[mbox_index
].action
, value
, BUF_BIG
- 1);
313 } else if (!strcmp(setting
, "action_disconnected.")) {
314 if (strlen(value
) + 1 > BUF_BIG
) {
316 "Mailbox %i action_disconnected string '%s' is too long.\n",
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
) {
325 "Mailbox %i action_new_mail string '%s' is too long.\n",
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
) {
334 "Mailbox %i action_no_new_mail string '%s' is too long.\n",
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
) {
345 "Mailbox %i buttontwo string '%s' is too long.\n",
349 strncpy(mbox
[mbox_index
].button2
, value
, BUF_BIG
- 1);
351 } else if (!strcmp(setting
, "fetchcmd.")) {
352 if (strlen(value
) + 1 > BUF_BIG
) {
354 "Mailbox %i fetchcmd string '%s' is too long.\n",
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
370 mbox
[mbox_index
].debug
= debug_value
;
372 DMA(DEBUG_INFO
, "Unknown setting '%s'\n", setting
);
376 for (i
= 0; i
< num_mailboxes
; i
++)
377 if (mbox
[i
].label
[0] != '\0')
383 static void init_biff(char *config_file
)
388 int loopinterval
= DEFAULT_LOOP
;
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
;
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) {
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");
428 DMA(DEBUG_INFO
, "config_file = %s.\n", config_file
);
429 if (!Read_Config_File(config_file
, &loopinterval
)) {
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
) {
436 "MAIL environment var '%s' is too long.\n", m
);
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
) {
445 "USER environment var '%s' is too long.\n", m
);
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] != '/') {
452 "Unexpected failure to construct /var/mail/username, please "
453 "report this with your operating system info and the version of wmbiff.");
457 DMA(DEBUG_ERROR
, "Cannot open config file '%s' nor use the "
458 "MAIL environment var.\n", config_file
);
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
);
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. */
475 int j
= strlen(mbox
[i
].label
);
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
)
494 success
= XpmReadFileToData((char *) pixmap_filename
, &xpm
);
497 DMA(DEBUG_ERROR
, "Unable to open %s\n", pixmap_filename
);
500 DMA(DEBUG_ERROR
, "%s is not a valid pixmap\n", pixmap_filename
);
503 DMA(DEBUG_ERROR
, "Insufficient memory to read %s\n",
512 /* tests as "test -f" would */
513 int exists(const char *filename
)
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
);
524 /* acts like execvp, with code inspired by it */
526 static char *search_path(const char *path
, const char *find_me
)
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
);
540 for (p
= path
; p
!= NULL
; path
= p
, path
++) {
542 p
= strchr(path
, ':');
544 /* not found; p should point to the null char at the end */
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];
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
);
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
)
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
);
581 if (st
.st_uid
!= getuid()) {
582 char *user
= getenv("USER");
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)"),
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
);
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
);
605 static void ClearDigits(unsigned int i
)
608 eraseRect(39, mbox_y(i
), 58, mbox_y(i
+ 1) - 1, background
);
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,
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)
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);
629 /* normal, LED-like behavior. */
631 for (i
= 0; name
[i
] != '\0'; i
++) {
632 c
= toupper(name
[i
]);
633 if (c
>= 'A' && c
<= 'Z') { /* it's a letter */
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 */
641 copyXPMArea((c
* (CHAR_WIDTH
+ 1)) + 65, 0,
642 (CHAR_WIDTH
+ 1), (CHAR_HEIGHT
+ 1), k
, y
);
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)
659 sprintf(buf
, "%02i", num
);
662 const char *color
= (new) ? highlight
: foreground
;
663 drawString(x
+ (CHAR_WIDTH
* 2 + 4), y
+ CHAR_HEIGHT
+ 1, buf
,
664 color
, background
, 1);
669 newx
-= (CHAR_WIDTH
+ 1);
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
);
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")) {
703 /* Else call external notifyer, ignoring the pid */
704 (void) execCommand(notifycmd
);
711 displayMsgCounters(unsigned int i
, int mail_stat
, int *Blink_Mode
)
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 */
719 execnotify(mbox
[i
].notify
);
721 case 1: /* mailbox has been rescanned/changed */
726 case -1: /* Error was detected */
727 ClearDigits(i
); /* Clear digits */
728 BlitString("XX", 45, mbox_y(i
), 0);
733 /** counts mail in spool-file
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
)
744 if (!mbox
[item
].checkMail
) {
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;
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 */
767 rc
= 0; /* mailbox wasn't changed */
769 mbox
[item
].OldMsgs
= mbox
[item
].TotalMsgs
;
770 mbox
[item
].OldUnreadMsgs
= mbox
[item
].UnreadMsgs
;
774 static int periodic_mail_check(void)
777 static int Blink_Mode
= 0; /* Bit mask, digits are in blinking
778 mode or not. Each bit for separate
780 int Sleep_Interval
; /* either DEFAULT_SLEEP_INTERVAL or
781 BLINK_SLEEP_INTERVAL */
782 int NewMail
= 0; /* flag for global notify */
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
) {
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
);
802 mailstat
= count_mail(i
);
804 XUndefineCursor(display
, iconwin
);
810 displayMsgCounters(i
, mailstat
, &Blink_Mode
);
811 /* update our idea of current time, as it
812 may have changed as we check. */
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
);
823 if (mbox
[i
].fetchinterval
> 0 && mbox
[i
].fetchcmd
[0] != '\0'
825 mbox
[i
].prevfetch_time
+ mbox
[i
].fetchinterval
) {
827 XDefineCursor(display
, iconwin
, busy_cursor
);
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 */
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
;
849 Sleep_Interval
= BLINK_SLEEP_INTERVAL
;
857 return Sleep_Interval
;
860 static int findTopOfMasterXPM(const char **skin_xpm
)
863 for (i
= 0; skin_xpm
[i
] != NULL
; i
++) {
864 if (strstr(skin_xpm
[i
], "++++++++") != NULL
)
868 "couldn't find the top of the xpm file using the simple method\n");
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;
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
);
905 // " +..:::...:::...:::...:::...:::.......:::...:::...:::...@ "
906 // " +.:...:.:...:.:...:.:...:.:...:..:..:...:.:...:.:...:..@ " ",
907 ret
[i
][margin
] = '+';
908 memset(ret
[i
] + margin
+ 1, '.', width
- margin
- margin
- 1);
909 ret
[i
][width
- margin
- 1] = '@';
911 skin_xpm
[((i
- (base
+ margin
) - 1) % 11) + top
+ 1],
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 */
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
)
933 struct pollfd timeout
;
935 timeout
.fd
= ConnectionNumber(display
);
936 timeout
.events
= POLLIN
;
938 poll(&timeout
, 1, millisec
);
941 struct timeval
*timeout
= NULL
;
947 to
.tv_sec
= millisec
/ 1000;
948 to
.tv_usec
= (millisec
% 1000) * 1000;
951 FD_SET(ConnectionNumber(display
), &readfds
);
952 max_fd
= ConnectionNumber(display
);
954 select(max_fd
+ 1, &readfds
, NULL
, NULL
, timeout
);
958 const char **restart_args
;
960 static void restart_wmbiff(int sig
961 #ifdef HAVE___ATTRIBUTE__
962 __attribute__ ((unused
))
966 DMA(DEBUG_ERROR
, "exec()'ing %s\n", restart_args
[0]);
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
));
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;
983 while (XPending(display
)) {
985 const char *press_action
;
987 XNextEvent(display
, &Event
);
989 switch (Event
.type
) {
991 if (Event
.xany
.window
!= win
&& Event
.xany
.window
!= iconwin
) {
998 XCloseDisplay(display
);
1002 but_pressed_region
=
1003 CheckMouseRegion(Event
.xbutton
.x
, Event
.xbutton
.y
);
1004 switch (Event
.xbutton
.button
) {
1006 press_action
= mbox
[but_pressed_region
].action
;
1009 press_action
= mbox
[but_pressed_region
].button2
;
1012 press_action
= mbox
[but_pressed_region
].fetchcmd
;
1015 press_action
= NULL
;
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
);
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
)) {
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
;
1046 extra_click_action
=
1047 mbox
[but_released_region
].actionnonew
;
1049 click_action
= mbox
[but_released_region
].action
;
1051 case 2: /* Middle mouse-click */
1052 click_action
= mbox
[but_released_region
].button2
;
1054 case 3: /* Right mouse-click */
1055 click_action
= mbox
[but_released_region
].fetchcmd
;
1058 click_action
= NULL
;
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 */
1080 but_pressed_region
= -1;
1081 /* RedrawWindow(); */
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
);
1107 static void do_biff(int argc
, const char **argv
)
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
);
1143 if (loadFont(font
) < 0) {
1144 DMA(DEBUG_ERROR
, "unable to load font. exiting.\n");
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);
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
) {
1175 static void sigchld_handler(int sig
1176 #ifdef HAVE___ATTRIBUTE__
1177 __attribute__ ((unused
))
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"
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"
1203 " -skip-certificate-check using TLS, don't validate the\n"
1204 " server's certificate\n"
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
)
1224 config_file
[0] = '\0';
1226 /* Parse Command Line */
1228 for (i
= 1; i
< argc
; i
++) {
1229 const char *arg
= argv
[i
];
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
);
1240 font
= DEFAULT_FONT
;
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 */
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
);
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
;
1268 font
= strdup_ordie(argv
[i
+ 1]);
1270 DMA(DEBUG_INFO
, "new font: %s", font
);
1279 if (strcmp(arg
+ 1, "geometry") != 0) {
1283 i
++; /* gobble the argument */
1284 if (i
>= argc
) { /* fail if there's nothing to gobble */
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
);
1297 font
= DEFAULT_FONT
;
1299 } else if (strcmp(arg
+ 1, "h") == 0) {
1309 if (strcmp(arg
+ 1, "skip-certificate-check") == 0) {
1310 SkipCertificateCheck
= 1;
1318 if (strcmp(arg
+ 1, "relax") == 0) {
1327 if (argc
> (i
+ 1)) {
1328 strncpy(config_file
, argv
[i
+ 1], 255);
1332 case 'e': /* undocumented for debugging */
1333 if (strcmp(arg
+ 1, "exit") == 0) {
1342 } else if (*arg
== '+') {
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 */
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",
1374 const char *home
= getenv("HOME");
1377 "$HOME undefined. Use the -c option to specify a wmbiffrc\n");
1380 sprintf(uconfig_file
, "%s/.wmbiffrc", home
);
1383 if (wmbiffrc_permissions_check(uconfig_file
) == 0) {
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
);