Log email subject line support
authorStefan Hajnoczi <stefanha@gmail.com>
Sun, 21 Jun 2009 16:28:34 +0000 (17:28 +0100)
committerStefan Hajnoczi <stefanha@gmail.com>
Sun, 21 Jun 2009 16:28:34 +0000 (17:28 +0100)
cmds.py
utils.py

diff --git a/cmds.py b/cmds.py
index ac85da5..6d98664 100644 (file)
--- a/cmds.py
+++ b/cmds.py
@@ -102,37 +102,50 @@ def log_command(who, target, replyto, words):
     '''Control channel logging (log start, log stop, log list)'''
     if len(words) < 2:
         return
     '''Control channel logging (log start, log stop, log list)'''
     if len(words) < 2:
         return
-    channel = target
-    if len(words) >= 3:
-        channel = words[2].startswith('#') and words[2] or target
-    if words[1] == 'start':
+
+    # Parse arguments
+    words.pop(0)
+    cmd     = words.pop(0)
+    channel = words and words[0].startswith('#') and words.pop(0) or target
+    emails  = words and words.pop(0) or ''
+    subject = ' '.join(words)
+
+    if cmd == 'start':
         if channel in logs:
             return 'PRIVMSG', (replyto, 'Logging %s already started' % channel)
         if channel in logs:
             return 'PRIVMSG', (replyto, 'Logging %s already started' % channel)
-        cmd = []
+        output = []
         if channel.startswith('#') and channel != target:
         if channel.startswith('#') and channel != target:
-            cmd.append(('CMD', ('JOIN %s' % channel,)))
-        name = utils.make_log(channel)
-        f = open(name, 'w')
-        logs[channel] = (name, f)
-        cmd.append(('PRIVMSG', (replyto, 'Start logging %s...' % channel)))
-        return cmd
-
-    elif words[1] == 'stop':
+            output.append(('CMD', ('JOIN %s' % channel,)))
+        filename = utils.make_log(channel)
+        logs[channel] = {
+            'filename': filename,
+            'fileobj': open(filename, 'w'),
+            'emails': emails,
+            'subject': subject,
+        }
+        output.append(('PRIVMSG', (replyto, 'Start logging %s...' % channel)))
+        return output
+
+    elif cmd == 'stop':
         if not channel in logs:
             return 'PRIVMSG', (replyto, 'Logging %s not started yet' % channel)
         if not channel in logs:
             return 'PRIVMSG', (replyto, 'Logging %s not started yet' % channel)
-        (name, f) = logs.pop(channel)
-        f.close()
-
-        to_address = None
-        if len(words) >= 3 and words[-1] != channel:
-            to_address = words[-1]
-        if to_address:
-            unrecognized = utils.email_log(to_address, channel, name, aliases)
-            cmd = [('PRIVMSG', (replyto, 'Unrecognized user %s' % user)) for user in unrecognized]
-        return [('PRIVMSG', (replyto, 'Stop logging %s.  Saved log file (%s)' % (channel, name)))] + cmd
-
-    elif words[1] == 'list':
-        return 'PRIVMSG', (replyto, 'Logging: %s' % ','.join(logs.keys()))
+        log = logs.pop(channel)
+        log['fileobj'].close()
+
+        if not emails:
+            emails = log['emails']
+        if not subject:
+            subject = log['subject']
+        if not subject:
+            subject = 'IRC logs for ' + channel
+
+        unrecognized = utils.email_log(emails, subject, log['filename'], aliases)
+        output = [('PRIVMSG', (replyto, 'Unrecognized alias %s' % user)) for user in unrecognized]
+        output.append(('PRIVMSG', (replyto, 'Stop logging %s.  Saved log file (%s)' % (channel, log['filename']))))
+        return output
+
+    elif cmd == 'list':
+        return [('PRIVMSG', (replyto, channel)) for channel in logs.keys()]
 op_commands['log'] = log_command
 
 def join_command(who, target, replyto, words):
 op_commands['log'] = log_command
 
 def join_command(who, target, replyto, words):
index f25663b..4d8e733 100644 (file)
--- a/utils.py
+++ b/utils.py
@@ -9,7 +9,7 @@ def dbg(msg):
     if config.DEBUG:
         sys.stderr.write(msg + '\n')
 
     if config.DEBUG:
         sys.stderr.write(msg + '\n')
 
-def email_log(to_address, channel, logfile, aliases):
+def email_log(to_address, subject, logfile, aliases):
     email_addresses = []
     unrecognized = []
     for a in to_address.split(','):
     email_addresses = []
     unrecognized = []
     for a in to_address.split(','):
@@ -22,12 +22,13 @@ def email_log(to_address, channel, logfile, aliases):
     if email_addresses:
         header = '''From: %(from_address)s
 To: %(to_address)s
     if email_addresses:
         header = '''From: %(from_address)s
 To: %(to_address)s
-Subject: irc %(channel)s log
+Subject: %(subject)s
 
 
-''' % {'from_address': config.FROM_ADDRESS,
-            'to_address': ','.join(email_addresses),
-            'channel': channel,
-            }
+''' % {
+    'from_address': config.FROM_ADDRESS,
+    'to_address': ','.join(email_addresses),
+    'subject': subject,
+}
         msg = header + open(logfile).read()
         s = smtplib.SMTP(config.MTA)
         s.sendmail(config.FROM_ADDRESS, email_addresses, msg)
         msg = header + open(logfile).read()
         s = smtplib.SMTP(config.MTA)
         s.sendmail(config.FROM_ADDRESS, email_addresses, msg)
@@ -45,5 +46,5 @@ def make_log(channel):
 
 def do_log(logs, target, nick, msg):
     if target in logs:
 
 def do_log(logs, target, nick, msg):
     if target in logs:
-        (name, f) = logs[target]
-        f.write('%s <%s> %s\n' % (get_time(), nick, msg))
+        log = logs[target]
+        log['fileobj'].write('%s <%s> %s\n' % (get_time(), nick, msg))