]> Pileus Git - ~andy/rhawk/blobdiff - email.awk
Save game after flipping the table
[~andy/rhawk] / email.awk
index 7f007b5e277942c830f171521bda2ca03c99da2d..57b56d40c0c5fd1ecbf00959766ad3a10be2eb98 100644 (file)
--- a/email.awk
+++ b/email.awk
@@ -1,10 +1,6 @@
 @include "json.awk"
 
 # Save email addresses
-END {
-       json_save("var/mail.json", mail_enable)
-}
-
 BEGIN {
        json_load("var/mail.json", mail_enable)
        for (_user in mail_enable)
@@ -22,23 +18,24 @@ BEGIN {
        mail_hist   = 5*60 # If the users has not spoken withn mail_before before
        mail_before = 5*60 # someone mentions their name and does not reply within
        mail_after  = 5*60 # mail_after seconds, email them hist seconds of the backlog
-
-       mail_from   = NICK "<andy753421@gmail.com>"
-       mail_err    = "If you received this message in error,\n" \
-                     "someone in #rhnoise is being a jerk"
 }
 
 function mail_send(addr, subj, body,
-                  sendmail, errmsg)
+                  from, error, sendmail, errmsg)
 {
+       from  = NICK "<" NICK "@pileus.org>"
+       error = "If you received this message in error,\n" \
+               "someone in #rhnoise is being a jerk"
+
        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
+       print "To: " addr      | sendmail
+       print "From: " from    | sendmail
+       print "Subject: " subj | sendmail
+       print ""               | sendmail
+       print body             | sendmail
+       print ""               | sendmail
+       print error            | sendmail
        close(sendmail)
 }
 
@@ -80,46 +77,70 @@ function mail_run(  user, chan, ready, time)
        }
 }
 
+function mail_save(file)
+{
+       json_save(file, mail_enable)
+}
+
+# Help
+/^\.help$/ {
+       say(".help mail -- manage email subscriptions")
+}
+
+/^\.help e?mail$/ {
+       say("Mail -- email users when they are mentioned")
+       say(NICK ": subscribe [addr] -- set your mailing address to [addr]")
+       say(NICK ": unsubscribe [addr] -- remove your subscription")
+       say(NICK ": addresses -- show your subscription address")
+       next
+}
+
+# Commands
 AUTH == OWNER &&
 TO == NICK &&
-/^e?mail .* .*/ {
+/^subscribe .* .*/ {
        reply("notifying " $2 " for " $3)
        mail_enable[$3] = $2
+       mail_save("var/mail.json")
 }
 
 TO == NICK &&
-/^e?mail  *[^ ]*$/ {
+/^subscribe  *[^ ]*$/ {
        _user = FROM
        _addr = $2
        gsub(/[^a-zA-Z0-9_+@.-]/, "", _user)
        gsub(/[^a-zA-Z0-9_+@.-]/, "", _addr)
        reply("notifying " _addr " for " _user)
        mail_enable[_user] = _addr
+       mail_save("var/mail.json")
 }
 
 AUTH == OWNER &&
 TO == NICK &&
-/^stfu .*/ {
+/^unsubscribe .*/ {
        reply("well fine then")
        delete mail_enable[$2]
        delete mail_ready[$2]
        delete mail_seen[$2]
+       mail_save("var/mail.json")
 }
 
 TO == NICK &&
-/^stfu$/ {
+/^unsubscribe$/ {
        reply("well fine then")
        delete mail_enable[FROM]
        delete mail_ready[FROM]
        delete mail_seen[FROM]
+       mail_save("var/mail.json")
 }
 
 TO == NICK &&
-/^who/ {
+/^addresses/ {
        for (_user in mail_enable)
                reply("\"" _user "\" <" mail_enable[_user] ">")
 }
 
+# Message logging and monitoring
 DST ~ /^#.*/ {
        for (_user in mail_enable) {
                _idle = systime() - mail_seen[_user]