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() |
Jamie, I have made your script a bit more pythonic and object oriented.
import sys
import socket
import string
import re
import os
import smtplib
from email.mime.text import MIMEText
class IRCReporter(object):
# 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 = "such-and-such" #os.environ.get('SMTP_USER')
SMTP_PASS = "hey hey" #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',
]
def start(self):
readbuffer = ''
s = socket.socket()
s.settimeout(None)
s.connect((self.HOST, self.PORT))
# Set nickname
s.send("NICK %s\r\n" % self.NICK)
# Specify username, hostname + real name. Follows RFC1459, rather than RFC2812.
s.send("USER %s %s foo :%s\r\n" % (self.IDENT, self.HOST, self.REALNAME))
# Join channel
s.send("JOIN %s\r\n" % self.CHANNEL)
pattern = ":(.+)\!.+\sPRIVMSG\s%s\s:(.*(%s).*)" % (self.CHANNEL, '|'.join(self.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'] = self.FROM
message['To'] = self.TO
smtp = smtplib.SMTP(self.SMTP_HOST)
smtp.login(self.SMTP_USER, self.SMTP_PASS)
smtp.sendmail(self.FROM, self.TO, message.as_string())
smtp.quit()
if __name__ == '__main__':
rep = IRCReporter()
rep.start()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Jamie, would it help to use python
irclib
to abstract away some of the low level irc stuff?