Last active
August 29, 2015 13:57
-
-
Save jamiehannaford/9529954 to your computer and use it in GitHub Desktop.
Monitors an IRC channel for keywords and e-mails the respective teams with the message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import socket | |
import string | |
import re | |
import os | |
import smtplib | |
from email.mime.text import MIMEText | |
# IRC settings | |
CHANNEL = '#raxdrg' | |
HOST = "irc.freenode.net' | |
PORT = 6667 | |
NICK = 'test-listener' | |
IDENT = NICK | |
REALNAME = NICK | |
# Email settings | |
SMTP_HOST = 'smtp.mailgun.org' | |
SMTP_USER = os.environ.get('SMTP_USER') | |
SMTP_PASS = os.environ.get('SMTP_PASS') | |
TO = 'jamie.hannaford@rackspace.com' | |
FROM = 'irc-listener@rs13226.mailgun.org' | |
if SMTP_USER is None or SMTP_PASS is None: | |
raise Exception('Both SMTP_USER and SMTP_PASS must be set as env vars') | |
# Other settings | |
KEYWORDS = [ | |
# General | |
'sdk', | |
# Languages | |
#'php', 'java', 'python', 'ruby', 'javascript', | |
# SDK names | |
'opencloud', 'pyrax', 'jclouds', 'fog', 'pkgcloud', 'gophercloud', 'knife-rackspace', 'vagrant-rackspace', 'rumm', 'openstack.net', | |
] | |
readbuffer = '' | |
s = socket.socket() | |
s.settimeout(None) | |
s.connect((HOST, PORT)) | |
# Set nickname | |
s.send("NICK %s\r\n" % NICK) | |
# Specify username, hostname + real name. Follows RFC1459, rather than RFC2812. | |
s.send("USER %s %s foo :%s\r\n" % (IDENT, HOST, REALNAME)) | |
# Join channel | |
s.send("JOIN %s\r\n" % CHANNEL) | |
pattern = ":(.+)\!.+\sPRIVMSG\s%s\s:(.*(%s).*)" % (CHANNEL, '|'.join(KEYWORDS)) | |
regex = re.compile(pattern, re.IGNORECASE) | |
while True: | |
readbuffer = readbuffer + s.recv(1024) | |
temp = string.split(readbuffer, "\n") | |
readbuffer = temp.pop() | |
for line in temp: | |
result = regex.match(line) | |
if (result): | |
text = "%s has posted something on IRC that might require attention:\n\n%s\n\n" % (result.group(1), result.group(2)) | |
message = MIMEText(text) | |
message['Subject'] = '[IRC-HELP] Message notification' | |
message['From'] = FROM | |
message['To'] = TO | |
smtp = smtplib.SMTP(SMTP_HOST) | |
smtp.login(SMTP_USER, SMTP_PASS) | |
smtp.sendmail(FROM, TO, message.as_string()) | |
smtp.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Jamie, I have made your script a bit more pythonic and object oriented.