# # Mail notifier plugin for fubar. Creates a sunken button which changes # color when there is new mail and executes $CONFIGDIR/mail when pressed # namespace eval mail { set image [image create bitmap -background white -foreground black \ -data {#define mailfg_width 24 #define mailfg_height 12 static unsigned char mailfg_bits[] = { 0xff, 0xff, 0xff, 0x0d, 0x00, 0xb0, 0x71, 0x00, 0x8e, 0x81, 0x83, 0x81, 0x01, 0x7c, 0x80, 0x01, 0x7e, 0x80, 0x81, 0x81, 0x83, 0x71, 0x00, 0x8c, 0x0d, 0x00, 0xf0, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; } -maskdata {#define mailbgd_width 24 #define mailbgd_height 12 static unsigned char mailbgd_bits[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; }] button .mail -relief sunken -bd 1 -image $image -width 32\ -command { exec $CONFIGDIR/mail &} set mailDefBg [.mail cget -bg] # # procedure to be invoked each minute # proc ::mail::checkMbox {} { variable mailpath return [expr {[file atime $mailpath]<[file mtime $mailpath]}] } proc ::mail::checkMailDir {} { variable mailpath return [llength [glob -nocomplain $mailpath/new/*]] } proc checkImap {} { variable imap variable imapnewmail variable imapstate set f [open "|$::RunCmd($imap(host)) $imap(command) 2>/dev/null" r+] fconfigure $f -buffering line -blocking no fileevent $f readable "::mail::imapRead $f" set imapstate 1 vwait ::mail::imapnewmail return $imapnewmail } proc imapRead {f} { variable imapstate variable imapnewmail if {[eof $f]} { catch {close $f} return } set line [gets $f] if {$imapstate == 1 && [regexp -nocase ready $line]} { puts $f "1 EXAMINE INBOX" set imapstate 2 } elseif {$imapstate ==2} { if {[string match "1 *" $line]} { puts $f "2 LOGOUT" set imapstate 3 } elseif {[regexp {^\*[[:space:]]+([[:digit:]]+)[[:space:]]+RECENT} $line match new]} { set imapnewmail $new } } } proc ::mail::check {args} { variable mailDefBg variable checkcmd if {[$checkcmd]} { .mail configure -bg red } else { .mail configure -bg $mailDefBg } } variable mailpath variable checkcmd # # Find out mailbox type # if {[file exists $::CONFIGDIR/imapmail]} { set f [open $::CONFIGDIR/imapmail] catch {array set imap [read $f]} if {[info exists imap(host)]&&[info exists imap(command)]&& [info exists ::RunCmd($imap(host))]} { set checkcmd checkImap } else { tk_messageBox -title error -type ok -message "Incorrect syntax of imapmail file" } } else { set tryboxes [list /var/mail/$::env(LOGNAME) /var/spool/mail/$::env(LOGNAME) ~/Maildir] if {[info exists ::env(MAIL)]} { set tryboxes [concat [list $::env(MAIL)] $tryboxes] } foreach box $tryboxes { if {[file exists $box]} { set mailpath $box if {[file isdirectory $box]&&[file isdirectory $box/new]} { set checkcmd checkMailDir } else { set checkcmd checkMbox } break } } } if {[info exists checkcmd]} { pack .mail -side right -padx 10 notifier ::mail::check } }