class email(threading.Thread): def __init__(self, matchedtext, disp_path, nicesize): self.matchedtext = matchedtext self.disp_path = disp_path self.nicesize = nicesize threading.Thread.__init__(self) #Send tiem nao def run(self): #create thread-local data to further prevent var overwrites under high load thread_data = threading.local() #connect to the server try: thread_data.smtpconn = smtplib.SMTP(option["smtp_server"]) #Uncomment the line below to be dazzled with all the crazy server chatter. Very spammy. #thread_data.smtpconn.set_debuglevel(1) thread_data.smtpconn.ehlo() thread_data.is_connected = True #If theres an error while connecting, verbose/log it except: thread_data.verbtext="\007"+color["bpurple"]+"SCCwatcher encountered an error while connecting to SMTP server, no email was sent" if option["verbose"] == 'on': verbose(thread_data.verbtext) if option["logenabled"] == 'on': thread_data.verbtext = xchat.strip(thread_data.verbtext) logging(xchat.strip(thread_data.verbtext), "SMTP_FAIL") thread_data.is_connected = False #If we've gotten this far, then we should have some type of connection to the server. Now we can send our message #Still using try incase something else fails if thread_data.is_connected == True: #Should we start a tls session? if option["smtp_tls"] == "on": thread_data.smtpconn.starttls() thread_data.smtpconn.ehlo() #If the user gave a username/password, log in with it. if len(option["smtp_username"]) > 0: try: thread_data.smtpconn.login(option["smtp_username"], option["smtp_password"]) thread_data.is_auth = True except: thread_data.verbtext="\007"+color["bpurple"]+"SCCwatcher encountered an error while authenticating with the SMTP server, no email was sent" if option["verbose"] == 'on': verbose(thread_data.verbtext) if option["logenabled"] == 'on': thread_data.verbtext = xchat.strip(thread_data.verbtext) logging(xchat.strip(thread_data.verbtext), "SMTP_FAIL") thread_data.is_auth = False #Otherwise just continue on without authenticating else: thread_data.is_auth = True if thread_data.is_auth == True: try: #The actual message we will be sending needs to be created with the function message_builder() thread_data.smtpconn.sendmail(option["smtp_from"], option["smtp_to"], self.message_builder()) thread_data.smtpconn.close() thread_data.verbtext="\007"+color["bpurple"]+"SCCwatcher successfully emailed " + color["dgrey"] + option["smtp_to"] if option["verbose"] == 'on': verbose(thread_data.verbtext) if option["logenabled"] == 'on': thread_data.verbtext = xchat.strip(thread_data.verbtext) logging(xchat.strip(thread_data.verbtext), "SMTP_SUCCESS") except: thread_data.verbtext="\007"+color["bpurple"]+"SCCwatcher encountered an error while talking to the SMTP server, no email was sent" if option["verbose"] == 'on': verbose(thread_data.verbtext) if option["logenabled"] == 'on': thread_data.verbtext = xchat.strip(thread_data.verbtext) logging(xchat.strip(thread_data.verbtext), "SMTP_FAIL") if option["use_external_command"] == "on": do_cmd(self.matchedtext, self.disp_path, self.nicesize).start() #Here we build our email message def message_builder(self): thread_data = threading.local() thread_data.current_time = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime()) #Here we replace all the special strings with actual data # Acceptable special strings are: # %torrent% %category% %size% %time% %dlpath% %ulpath% %utserver% # To see what they mean, just see below. thread_data.ftpdetails = re.match("ftp:\/\/(.*):(.*)@(.*):([^\/]*.)/(.*)", option["ftpdetails"]) if thread_data.ftpdetails is not None: thread_data.ftpstring = "ftp://" + thread_data.ftpdetails.group(3) + ":" + thread_data.ftpdetails.group(4) + "/" + thread_data.ftpdetails.group(5) else: thread_data.ftpstring = "BAD_FTP_DETAILS" thread_data.utstring = option["utorrent_hostname"] + ":" + option["utorrent_port"] thread_data.email_body = option["smtp_message"].replace('%torrent%', self.matchedtext.group(3)) thread_data.email_body = thread_data.email_body.replace('%category%', self.matchedtext.group(2)) thread_data.email_body = thread_data.email_body.replace('%size%', self.nicesize) thread_data.email_body = thread_data.email_body.replace('%time%', thread_data.current_time) thread_data.email_body = thread_data.email_body.replace('%dlpath%', self.disp_path) thread_data.email_body = thread_data.email_body.replace('%ulpath%', thread_data.ftpstring) thread_data.email_body = thread_data.email_body.replace('%utserver%', thread_data.utstring) thread_data.email_subject = option["smtp_subject"].replace('%torrent%', self.matchedtext.group(3)) thread_data.email_subject = thread_data.email_subject.replace('%category%', self.matchedtext.group(2)) thread_data.email_subject = thread_data.email_subject.replace('%size%', self.nicesize) thread_data.email_subject = thread_data.email_subject.replace('%time%', thread_data.current_time) thread_data.email_subject = thread_data.email_subject.replace('%dlpath%', self.disp_path) thread_data.email_subject = thread_data.email_subject.replace('%ulpath%', thread_data.ftpstring) thread_data.email_subject = thread_data.email_subject.replace('%utserver%', thread_data.utstring) thread_data.message = """ Subject: %s Content-Type: text/html; charset=ISO-8859-1 %s """ % (thread_data.email_subject, thread_data.email_body) thread_data.message = thread_data.message.strip() return thread_data.message