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