Merge branch 'master' into 'master'

Add sender support to TextCDC

See merge request !2
This commit is contained in:
dave 2015-09-04 14:19:00 -07:00
commit b9466cd223
1 changed files with 104 additions and 102 deletions

View File

@ -1,102 +1,104 @@
#!/usr/bin/env python #!/usr/bin/env python
""" """
.. module::TextCDC .. module::TextCDC
:synopsis: Text Chrisdotcode, right now. :synopsis: Text Chrisdotcode, right now.
.. moduleauthor::Nick Krichevsky <nick@ollien.com> .. moduleauthor::Nick Krichevsky <nick@ollien.com>
""" """
import smtplib import smtplib
import imaplib import imaplib
from threading import Timer from threading import Timer
from pyircbot.modulebase import ModuleBase, ModuleHook from pyircbot.modulebase import ModuleBase, ModuleHook
class TextCDC(ModuleBase): class TextCDC(ModuleBase):
def __init__(self, bot, moduleName): def __init__(self, bot, moduleName):
ModuleBase.__init__(self, bot, moduleName) ModuleBase.__init__(self, bot, moduleName)
self.hooks.append(ModuleHook("PRIVMSG",self.handleMessage)) self.hooks.append(ModuleHook("PRIVMSG",self.handleMessage))
self.loadConfig() self.loadConfig()
self.timer = None self.bot = bot
self.setupTimer() self.timer = None
self.setupTimer()
def ondisable(self):
if self.timer != None: def ondisable(self):
self.timer.cancel() if self.timer != None:
self.timer.cancel()
def handleMessage(self, args, prefix, trailing):
channel = args[0] def handleMessage(self, args, prefix, trailing):
if self.bot.messageHasCommand(".textstatus", trailing): channel = args[0]
#self.bot.act_PRIVMSG(channel, "POP: %s" % "Good" if setupPop() != None else "Failed.") prefix = self.bot.decodePrefix(prefix)
self.bot.act_PRIVMSG(channel, "SMTP: %s" % "Good" if setupSMTP() != None else "Failed.") if self.bot.messageHasCommand(".textstatus", trailing):
if self.bot.messageHasCommand(".text-cdc", trailing): #self.bot.act_PRIVMSG(channel, "POP: %s" % "Good" if setupPop() != None else "Failed.")
message = ' '.join(trailing.split(" ")[1:]) self.bot.act_PRIVMSG(channel, "SMTP: %s" % "Good" if setupSMTP() != None else "Failed.")
smtp = self.setupSMTP() if self.bot.messageHasCommand(".text-cdc", trailing):
try: message = ' '.join(trailing.split(" ")[1:])
smtp.sendmail(self.config["account"]["auth"]["username"], self.config["email-addr"], "Subject:\n\n%s" % message) smtp = self.setupSMTP()
smtp.quit() try:
self.bot.act_PRIVMSG(channel, "Message sent.") smtp.sendmail(self.config["account"]["auth"]["username"], self.config["email-addr"], "Subject:\n\n%s -%s" % (message, prefix.nick))
except Exception as e: smtp.quit()
self.bot.log.error(str(e)) self.bot.act_PRIVMSG(channel, "Message sent.")
self.bot.act_PRIVMSG(channel, "An SMTP Error has Occured") except Exception as e:
self.bot.log.error(str(e))
def setupIMAP(self): self.bot.act_PRIVMSG(channel, "An SMTP Error has Occured")
imapObj = None
if self.config["account"]["imap"]["ssl"]: def setupIMAP(self):
imapObj = imaplib.IMAP4_SSL(self.config["account"]["imap"]["host"], self.config["account"]["imap"]["port"]) imapObj = None
else: if self.config["account"]["imap"]["ssl"]:
imapObj = imaplib.IMAP4(self.config["account"]["imap"]["host"], self.config["account"]["imap"]["port"]) imapObj = imaplib.IMAP4_SSL(self.config["account"]["imap"]["host"], self.config["account"]["imap"]["port"])
imapObj.login(self.config["account"]["auth"]["username"], self.config["account"]["auth"]["password"]) else:
resp = imapObj.select("INBOX") imapObj = imaplib.IMAP4(self.config["account"]["imap"]["host"], self.config["account"]["imap"]["port"])
if resp[0] == "OK": imapObj.login(self.config["account"]["auth"]["username"], self.config["account"]["auth"]["password"])
return imapObj resp = imapObj.select("INBOX")
else: if resp[0] == "OK":
return None return imapObj
else:
def setupSMTP(self): return None
smtpObj = None
if self.config["account"]["smtp"]["ssl"]: def setupSMTP(self):
smtpObj = smtplib.SMTP_SSL(self.config["account"]["smtp"]["host"], self.config["account"]["smtp"]["port"]) smtpObj = None
else: if self.config["account"]["smtp"]["ssl"]:
smtpObj = smtplib.SMTP_SSL(self.config["account"]["smtp"]["host"], self.config["account"]["smtp"]["port"]) smtpObj = smtplib.SMTP_SSL(self.config["account"]["smtp"]["host"], self.config["account"]["smtp"]["port"])
if self.config["account"]["smtp"]["authentication"]: else:
resp = smtpObj.login(self.config["account"]["auth"]["username"], self.config["account"]["auth"]["password"]) smtpObj = smtplib.SMTP_SSL(self.config["account"]["smtp"]["host"], self.config["account"]["smtp"]["port"])
if resp[0] == 235: if self.config["account"]["smtp"]["authentication"]:
return smtpObj resp = smtpObj.login(self.config["account"]["auth"]["username"], self.config["account"]["auth"]["password"])
else: if resp[0] == 235:
return None return smtpObj
else: else:
resp = smtpObj.connect() return None
if resp[0] == 220: else:
return smtpObj resp = smtpObj.connect()
else: if resp[0] == 220:
return None return smtpObj
else:
def setupTimer(self): return None
self.timer = Timer(self.config["interval"], self.checkMail, [self.bot, self.config["email-addr"], self.config["output-channels"]],{})
self.timer.start() def setupTimer(self):
self.timer = Timer(self.config["interval"], self.checkMail, [self.bot, self.config["email-addr"], self.config["output-channels"]],{})
def checkMail(self, bot, emailAddr, channels, imapObj = None): self.timer.start()
try:
if imapObj == None: def checkMail(self, bot, emailAddr, channels, imapObj = None):
imapObj = self.setupIMAP() try:
result = imapObj.search(None, "(FROM \"%s\")" % emailAddr) if imapObj == None:
if (result[0] == "OK"): imapObj = self.setupIMAP()
messageIds = result[1][0].decode("utf-8") result = imapObj.search(None, "(FROM \"%s\")" % emailAddr)
if len(messageIds) > 0: if (result[0] == "OK"):
messageIds = messageIds.split(" ") messageIds = result[1][0].decode("utf-8")
for messageId in messageIds: if len(messageIds) > 0:
message = imapObj.fetch(messageId, "BODY[TEXT]") messageIds = messageIds.split(" ")
if (message[0] == "OK"): for messageId in messageIds:
messageText = message[1][0][1].decode("utf-8").split("-----Original Message-----")[0].rstrip() message = imapObj.fetch(messageId, "BODY[TEXT]")
for channel in channels: if (message[0] == "OK"):
bot.act_PRIVMSG(channel, "Message from CDC: %s" % messageText) messageText = message[1][0][1].decode("utf-8").split("-----Original Message-----")[0].rstrip()
imapObj.store(messageId, "+FLAGS", "\\Deleted") for channel in channels:
else: bot.act_PRIVMSG(channel, "Message from CDC: %s" % messageText)
raise Exception("SMTP Error. Status was %s, expected OK" % message[0]) imapObj.store(messageId, "+FLAGS", "\\Deleted")
imapObj.logout() else:
self.setupTimer() raise Exception("SMTP Error. Status was %s, expected OK" % message[0])
except Exception as e: imapObj.logout()
if imapObj != None: self.setupTimer()
imapObj.logout() except Exception as e:
self.setupTimer() if imapObj != None:
raise e imapObj.logout()
self.setupTimer()
raise e