]> Pileus Git - ~andy/rhawk/commitdiff
email
authorAndy Spencer <andy753421@gmail.com>
Tue, 5 Jun 2012 07:25:19 +0000 (07:25 +0000)
committerAndy Spencer <andy753421@gmail.com>
Tue, 5 Jun 2012 07:25:19 +0000 (07:25 +0000)
email.awk [new file with mode: 0644]
rhawk

diff --git a/email.awk b/email.awk
new file mode 100644 (file)
index 0000000..2e0fd4d
--- /dev/null
+++ b/email.awk
@@ -0,0 +1,99 @@
+# Email notifications
+BEGIN {
+       mail_hist  = 60 # Send 60 seconds of backlog
+       mail_delay = 60 # Wait 60 seconds after last mention before mailing
+
+       mail_from  = NICK "<andy753421@gmail.com>"
+       mail_err   = "If you recieved this message in error,\n" \
+                    "someone in #rhnoise is begin a jerk"
+}
+
+function mail_send(addr, subj, body,
+                  sendmail, errmsg)
+{
+       gsub(/[^a-zA-Z0-9_+@.-]/, "", addr)
+       sendmail = "/usr/sbin/sendmail " addr
+       print "To: " addr        | sendmail
+       print "From: " mail_from | sendmail
+       print "Subject: " subj   | sendmail
+       print ""                 | sendmail
+       print body               | sendmail
+       print mail_err           | sendmail
+       close(sendmail)
+}
+
+function mail_prep(user, chan,
+                  addr, line, body,
+                  sec, si, sn, ss,
+                  msg, mi, mn)
+{
+       addr = mail_enable[user]
+       sn   = asorti(mail_log[chan], ss)
+       body = ""
+       for (si = 1; si <= sn; si++) {
+               sec = ss[si]
+               mn = length(mail_log[chan][sec])
+               for (mi = 0; mi < mn; mi++) { 
+                       msg = mail_log[chan][sec][mi]
+                       if (sec > mail_ready[user][chan] - mail_hist) {
+                               if (msg ~ user)
+                                       line = "* " msg
+                               else
+                                       line = "  " msg
+                               body = body line "\n"
+                       }
+               }
+       }
+       say("notifying " user " at " addr)
+       mail_send(addr, "Message for " user " in " chan, body)
+       delete mail_ready[user][chan]
+}
+
+function mail_run(  user, chan, ready, time)
+{
+       for (user in mail_ready)
+       for (chan in mail_ready[user]) {
+               ready = mail_ready[user][chan]
+               delay = systime() - ready
+               if (ready && delay > mail_delay)
+                       mail_prep(user, chan)
+       }
+}
+
+FROM ~ OWNER &&
+/^e?mail .* .*/ {
+       reply("notifying " $2 " for " $3)
+       mail_enable[$3] = $2
+}
+
+/^e?mail  *[^ ]*$/ {
+       reply("notifying " $2)
+       mail_enable[FROM] = $2
+}
+
+/^stfu$/ {
+       reply("well fine then")
+       delete mail_enable[FROM]
+}
+
+TO in mail_enable {
+       mail_ready[TO][DST] = systime()
+       debug("queing messages to " DST " for " TO)
+}
+
+FROM in mail_enable {
+       delete mail_ready[FROM]
+       debug("clearing message for " FROM)
+}
+
+DST ~ /^#.*/ {
+       _t = systime()
+       _i = length(mail_log[DST][_t])
+       if (_i==0) delete mail_log[DST][_t]
+       mail_log[DST][_t][_i] = DST " " strftime("(%H:%M:%S) ") FROM ": " $0
+       debug("log["DST"]["_t"]["_i"] = "mail_log[DST][_t][_i])
+}
+
+// {
+       mail_run()
+}
diff --git a/rhawk b/rhawk
index 5549aa83a624917a23976f41612a0c53fcb008b1..db1b8f49b8136815b55f944fceb46ef86e8e4714 100755 (executable)
--- a/rhawk
+++ b/rhawk
@@ -2,6 +2,7 @@
 
 @include "irc.awk"
 @include "spades.awk"
+@include "email.awk"
 
 # Initialization
 BEGIN {