]> Pileus Git - ~andy/rhawk/blob - email.awk
fix up commands
[~andy/rhawk] / email.awk
1 @include "json.awk"
2
3 # Save email addresses
4 BEGIN { json_load("var/mail.txt", mail_enable) }
5 END   { json_save("var/mail.txt", mail_enable) }
6
7 # Email notifications
8 BEGIN {
9         mail_hist  = 60 # Send 60 seconds of backlog
10         mail_delay = 60 # Wait 60 seconds after last mention before mailing
11
12         mail_from  = NICK "<andy753421@gmail.com>"
13         mail_err   = "If you received this message in error,\n" \
14                      "someone in #rhnoise is being a jerk"
15
16         for (_user in mail_enable) 
17                 debug("watching " mail_enable[_user] " for " _user)
18 }
19
20 function mail_send(addr, subj, body,
21                    sendmail, errmsg)
22 {
23         gsub(/[^a-zA-Z0-9_+@.-]/, "", addr)
24         sendmail = "/usr/sbin/sendmail " addr
25         print "To: " addr        | sendmail
26         print "From: " mail_from | sendmail
27         print "Subject: " subj   | sendmail
28         print ""                 | sendmail
29         print body               | sendmail
30         print mail_err           | sendmail
31         close(sendmail)
32 }
33
34 function mail_prep(user, chan,
35                    addr, line, body,
36                    sec, si, sn, ss,
37                    msg, mi, mn)
38 {
39         addr = mail_enable[user]
40         sn   = asorti(mail_log[chan], ss)
41         body = ""
42         for (si = 1; si <= sn; si++) {
43                 sec = ss[si]
44                 mn = length(mail_log[chan][sec])
45                 for (mi = 0; mi < mn; mi++) { 
46                         msg = mail_log[chan][sec][mi]
47                         if (sec > mail_ready[user][chan] - mail_hist) {
48                                 if (msg ~ user)
49                                         line = "* " msg
50                                 else
51                                         line = "  " msg
52                                 body = body line "\n"
53                         }
54                 }
55         }
56         privmsg(chan, "notifying " user " at " addr)
57         mail_send(addr, "Message for " user " in " chan, body)
58         delete mail_ready[user][chan]
59 }
60
61 function mail_run(  user, chan, ready, time)
62 {
63         for (user in mail_ready)
64         for (chan in mail_ready[user]) {
65                 ready = mail_ready[user][chan]
66                 delay = systime() - ready
67                 if (ready && delay > mail_delay)
68                         mail_prep(user, chan)
69         }
70 }
71
72 FROM ~ OWNER &&
73 TO == NICK &&
74 /^e?mail .* .*/ {
75         reply("notifying " $2 " for " $3)
76         mail_enable[$3] = $2
77 }
78
79 TO == NICK &&
80 /^e?mail  *[^ ]*$/ {
81         _user = FROM
82         _addr = $2
83         gsub(/[^a-zA-Z0-9_+@.-]/, "", _user)
84         gsub(/[^a-zA-Z0-9_+@.-]/, "", _addr)
85         reply("notifying " _addr " for " _user)
86         mail_enable[_user] = _addr
87 }
88
89 FROM ~ OWNER &&
90 TO == NICK &&
91 /^stfu .*/ {
92         reply("well fine then")
93         delete mail_enable[$2]
94         delete mail_ready[$2]
95 }
96
97 TO == NICK &&
98 /^stfu$/ {
99         reply("well fine then")
100         delete mail_enable[FROM]
101         delete mail_ready[FROM]
102 }
103
104 TO == NICK &&
105 /^who/ {
106         for (_user in mail_enable)
107                 reply("\"" _user "\" <" mail_enable[_user] ">")
108 }
109
110 DST ~ /^#.*/ {
111         for (_user in mail_enable)
112                 if ($0 ~ "\\<"_user"\\>") {
113                         mail_ready[_user][DST] = systime()
114                         debug("queueing messages to " DST " for " _user)
115                 }
116 }
117
118 FROM in mail_enable {
119         delete mail_ready[FROM]
120         debug("clearing message for " FROM)
121 }
122
123 DST ~ /^#.*/ {
124         _t = systime()
125         _i = length(mail_log[DST][_t])
126         if (_i==0) delete mail_log[DST][_t]
127         mail_log[DST][_t][_i] = DST " " strftime("(%H:%M:%S) ") FROM ": " $0
128         #debug("log["DST"]["_t"]["_i"] = "mail_log[DST][_t][_i])
129 }
130
131 // {
132         mail_run()
133 }