]> Pileus Git - ~andy/rhawk/blob - email.awk
Update and re-enable email
[~andy/rhawk] / email.awk
1 @include "json.awk"
2
3 # Save email addresses
4 BEGIN {
5         json_load("var/mail.json", mail_enable)
6         for (_user in mail_enable)
7                 debug("watching " mail_enable[_user] " for " _user)
8 }
9
10 TO == NICK && /^sync/ {
11         json_load("var/mail.json", mail_enable)
12         for (_user in mail_enable)
13                 debug("watching " mail_enable[_user] " for " _user)
14 }
15
16 # Email notifications
17 BEGIN {
18         mail_hist   = 5*60 # If the users has not spoken withn mail_before before
19         mail_before = 5*60 # someone mentions their name and does not reply within
20         mail_after  = 5*60 # mail_after seconds, email them hist seconds of the backlog
21
22         mail_from   = NICK "<" NICK "@pileus.org>"
23         mail_err    = "If you received this message in error,\n" \
24                       "someone in #rhnoise is being a jerk"
25 }
26
27 function mail_send(addr, subj, body,
28                    sendmail, errmsg)
29 {
30         gsub(/[^a-zA-Z0-9_+@.-]/, "", addr)
31         sendmail = "/usr/sbin/sendmail " addr
32         print "To: " addr        | sendmail
33         print "From: " mail_from | sendmail
34         print "Subject: " subj   | sendmail
35         print ""                 | sendmail
36         print body               | sendmail
37         print mail_err           | sendmail
38         close(sendmail)
39 }
40
41 function mail_prep(user, chan,
42                    addr, line, body,
43                    sec, si, sn, ss,
44                    msg, mi, mn)
45 {
46         addr = mail_enable[user]
47         sn   = asorti(mail_log[chan], ss)
48         body = ""
49         for (si = 1; si <= sn; si++) {
50                 sec = ss[si]
51                 mn = length(mail_log[chan][sec])
52                 for (mi = 0; mi < mn; mi++) {
53                         msg = mail_log[chan][sec][mi]
54                         if (sec > mail_ready[user][chan] - mail_hist) {
55                                 if (msg ~ user)
56                                         line = "* " msg
57                                 else
58                                         line = "  " msg
59                                 body = body line "\n"
60                         }
61                 }
62         }
63         say(chan, "notifying " user " at " addr)
64         mail_send(addr, "Message for " user " in " chan, body)
65         delete mail_ready[user][chan]
66 }
67
68 function mail_run(  user, chan, ready, time)
69 {
70         for (user in mail_ready)
71         for (chan in mail_ready[user]) {
72                 ready = mail_ready[user][chan]
73                 delay = systime() - ready
74                 if (ready && delay > mail_after)
75                         mail_prep(user, chan)
76         }
77 }
78
79 function mail_save(file)
80 {
81         json_save(file, mail_enable)
82 }
83
84 # Help
85 /^\.help$/ {
86         say(".help mail -- manage email subscriptions")
87 }
88
89 /^\.help e?mail$/ {
90         say("Mail -- email users when they are mentioned")
91         say(".subscribe [addr] -- set your mailing address to [addr]")
92         say(".unsubscribe [addr] -- remove your subscription")
93         say(".addresses -- show your subscription address")
94         next
95 }
96
97 # Commands
98 AUTH == OWNER &&
99 TO == NICK &&
100 /^subscribe .* .*/ {
101         reply("notifying " $2 " for " $3)
102         mail_enable[$3] = $2
103         mail_save("var/mail.json")
104 }
105
106 TO == NICK &&
107 /^subscribe  *[^ ]*$/ {
108         _user = FROM
109         _addr = $2
110         gsub(/[^a-zA-Z0-9_+@.-]/, "", _user)
111         gsub(/[^a-zA-Z0-9_+@.-]/, "", _addr)
112         reply("notifying " _addr " for " _user)
113         mail_enable[_user] = _addr
114         mail_save("var/mail.json")
115 }
116
117 AUTH == OWNER &&
118 TO == NICK &&
119 /^unsubscribe .*/ {
120         reply("well fine then")
121         delete mail_enable[$2]
122         delete mail_ready[$2]
123         delete mail_seen[$2]
124         mail_save("var/mail.json")
125 }
126
127 TO == NICK &&
128 /^unsubscribe$/ {
129         reply("well fine then")
130         delete mail_enable[FROM]
131         delete mail_ready[FROM]
132         delete mail_seen[FROM]
133         mail_save("var/mail.json")
134 }
135
136 TO == NICK &&
137 /^addresses/ {
138         for (_user in mail_enable)
139                 reply("\"" _user "\" <" mail_enable[_user] ">")
140 }
141
142 # Message logging and monitoring
143 DST ~ /^#.*/ {
144         for (_user in mail_enable) {
145                 _idle = systime() - mail_seen[_user]
146                 if ($0 ~ "\\<"_user"\\>" && _idle > mail_before) {
147                         mail_ready[_user][DST] = systime()
148                         debug("queueing messages to " DST " for " _user)
149                 }
150         }
151 }
152
153 FROM in mail_enable {
154         delete mail_ready[FROM]
155         mail_seen[FROM] = systime()
156         debug("clearing message for " FROM)
157 }
158
159 DST ~ /^#.*/ {
160         _t = systime()
161         _i = length(mail_log[DST][_t])
162         if (_i==0) delete mail_log[DST][_t]
163         mail_log[DST][_t][_i] = DST " " strftime("(%H:%M:%S) ") FROM ": " $0
164         #debug("log["DST"]["_t"]["_i"] = "mail_log[DST][_t][_i])
165 }
166
167 // {
168         mail_run()
169 }