]> www.wagner.pp.ru Git - oss/fubar.git/blob - plugins/mail
Reimport after CVS crash
[oss/fubar.git] / plugins / mail
1 #
2 # Mail notifier plugin for fubar. Creates a sunken button which changes
3 # color when there is new mail and executes $CONFIGDIR/mail when pressed
4 #
5 namespace eval mail {
6         set image [image create bitmap  -background white -foreground black \
7                         -data {#define mailfg_width 24
8         #define mailfg_height 12
9         static unsigned char mailfg_bits[] = {
10         0xff, 0xff, 0xff, 0x0d, 0x00, 0xb0, 0x71, 0x00, 0x8e, 0x81, 0x83, 0x81,
11         0x01, 0x7c, 0x80, 0x01, 0x7e, 0x80, 0x81, 0x81, 0x83, 0x71, 0x00, 0x8c,
12         0x0d, 0x00, 0xf0, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
13         } -maskdata {#define mailbgd_width 24
14         #define mailbgd_height 12
15         static unsigned char mailbgd_bits[] = {
16         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
17         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
18         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
19         }]
20         button .mail -relief sunken -bd 1 -image $image -width 32\
21         -command { exec $CONFIGDIR/mail &}
22         set mailDefBg [.mail cget -bg]
23
24         #
25         # procedure to be invoked each minute 
26         #
27         proc ::mail::checkMbox {} {
28                 variable mailpath       
29                 return [expr {[file atime $mailpath]<[file mtime $mailpath]}]
30         }   
31
32         proc ::mail::checkMailDir {} {
33                 variable mailpath 
34                 return [llength [glob -nocomplain $mailpath/new/*]]
35         }       
36
37         proc checkImap {} {
38                 variable imap
39                 variable imapnewmail
40                 variable imapstate
41                 set f [open "|$::RunCmd($imap(host)) $imap(command) 2>/dev/null" r+]
42                 fconfigure $f -buffering line -blocking no
43                 fileevent $f readable "::mail::imapRead $f"
44                 set imapstate 1
45                 vwait ::mail::imapnewmail
46                 return $imapnewmail
47         }
48         proc imapRead {f} {
49                 variable imapstate
50                 variable imapnewmail
51                 if {[eof $f]} {
52                         catch {close $f}
53                         return
54                 }
55                 set line [gets $f]
56                 if {$imapstate == 1 && [regexp -nocase ready $line]} {
57                         puts $f "1 EXAMINE INBOX"
58                         set imapstate 2
59                 } elseif {$imapstate ==2} {
60                         if {[string match "1 *" $line]} {
61                                 puts $f "2 LOGOUT"
62                                 set imapstate 3
63                         } elseif {[regexp {^\*[[:space:]]+([[:digit:]]+)[[:space:]]+RECENT} $line match new]} {
64                                 set imapnewmail $new
65                         }       
66                 }
67         }
68
69         proc ::mail::check {args} {
70                 variable mailDefBg 
71                 variable checkcmd
72                 if {[$checkcmd]} {
73                         .mail configure -bg red
74                 } else {
75                         .mail configure -bg $mailDefBg
76                 }
77         }       
78
79         variable mailpath
80         variable checkcmd
81
82         #
83         # Find out mailbox type
84         # 
85         if {[file exists $::CONFIGDIR/imapmail]} {
86                 set f [open $::CONFIGDIR/imapmail]
87                 catch {array set imap [read $f]}
88                 if {[info exists imap(host)]&&[info exists imap(command)]&&
89                         [info exists ::RunCmd($imap(host))]} {
90                         set checkcmd checkImap
91             } else {
92                         tk_messageBox -title error -type ok -message "Incorrect syntax of imapmail file" 
93                 }       
94         } else {        
95                 set tryboxes [list /var/mail/$::env(LOGNAME) /var/spool/mail/$::env(LOGNAME) ~/Maildir]
96
97                 if {[info exists ::env(MAIL)]} {
98                         set tryboxes [concat [list $::env(MAIL)] $tryboxes]
99                 }
100
101                 foreach box $tryboxes {
102                         if {[file exists $box]} {
103                                 set mailpath $box
104                                 if {[file isdirectory $box]&&[file isdirectory $box/new]} {
105                                         set checkcmd checkMailDir
106                                 } else {
107                                         set checkcmd checkMbox
108                                 }
109                                 break
110                         }
111                 }       
112         }
113         if {[info exists checkcmd]} {
114                 pack .mail -side right -padx 10
115                 notifier ::mail::check
116         }
117 }