def doit(self, msg, flink): """Twitter the message with file link. The file link will be URL-shortened msg: The message flink: File link """ # Shorten URL, when existing if flink is None: postfix = "" # Nothing to attach else: postfix = " " + self._urlshorten(flink) pinfo('Tweet: "' + msg + '"' + postfix) # For Debug: Manually disable if Opts.has_key('testmode') and Opts['testmode']: pwarn(" Tweet not posted in testmode") else: self._api.PostUpdate(msg + postfix)
def main(): # *** Command line parsing cmd_usage="usage: %prog [options] args" cmd_desc ="""tweetfile - Don't call yourself, add it into your .forward file""" cmd_version="%prog " + __version__ cmd_parser = optparse.OptionParser(usage=cmd_usage, description=cmd_desc, version=cmd_version) cmd_parser.add_option('-V', action='version', help=optparse.SUPPRESS_HELP) cmd_parser.add_option('--quiet', '-q', dest='quiet', action='store_true', default=False, help='Quiet Output') # more options to add (cmd_opts, cmd_args) = cmd_parser.parse_args() # Setup logging: Show only from warnings when being QUIET logging.basicConfig(level=logging.WARNING if cmd_opts.quiet else logging.INFO, format="%(message)s") # Chroot logging only here, because otherwise it will confuse the logger pinfo("Went into chroot with directory " + Opts['chroot_dir']) # Abort when different than no argument if len(cmd_args) != 0: perror("No command line arguments are expected!") cmd_parser.print_usage() return 1 # *** First read stdin to extract the information for parsing try: uid, text, msg, getfile = extract_mail_content(sys.stdin) except ExtractMailError as detail: # Error encountered, most likely not allowed user pwarn(detail) return 1 # Parsing of .forward file should continue except: raise # Other errors are just raised further # Debug pinfo("uid " + str(uid) + " --> " + UserList[uid]['rcpt']) pinfo("text " + text) if getfile is None: pinfo("No attachment found") else: pinfo("Attachment found with name " + getfile.get_filename()) # Store the attachment in file system if getfile is not None: try: # Add the urlbase to the filename file_link = UserList[uid]['fileurlbase'] + "/" + store_file(uid, getfile) except StoreFileError as detail: # Error encountered, we just raise it perror(detail) raise except: raise # Other errors are just raised further else: # We had no file link file_link = None # Then tweet the stuff try: twit = twittermsg(UserList[uid]['twitter']) # Create new object twit.doit(text, file_link) pinfo("Twitter update done") twit.finish() # Cleanup except TwitterMsgError as detail: # Error encountered perr(detail) return 1 # failure except: # All other errors - propagate raise # Create new message with the tweet content mailbody = "Forwarded by tweetfile" if file_link: mailbody = "Link: " + file_link + "\n\n" + mailbody fmsg = email.mime.text.MIMEText(mailbody, "plain", "utf-8") fmsg["From"] = msg["From"] fmsg["To"] = "*****@*****.**" fmsg["Subject"] = email.header.Header(text, "utf-8") # Finally forward the mail, when it is requested if Opts.has_key('testmode') and Opts['testmode']: pwarn(" No mail forwarding in testmode") elif UserList[uid]['forwardto'] is not None: try: # Open SMTPConnection; Catch exceptions smtp = smtplib.SMTP(Opts['smtp_server_host'], Opts['smtp_server_port']) except (smtplib.SMTPException, socket.error) as detail: perror("Establishing SMTP connection to " + \ Opts['smtp_server_host'] + ":" + Opts['smtp_server_port'] + \ " failed: " + str(detail)) except: raise # Rais all other errors else: # No error opening, continue for rcpts in UserList[uid]['forwardto']: # Iterate over all reciptiens try: # Send data; Catch exceptions # Update fmsg rcpt fmsg.replace_header('To', rcpts) smtp.sendmail(fmsg['From'], rcpts, fmsg.as_string()) except smtplib.SMTPException as detail: perror("SMTP Mail sending failed: " + str(detail)) break else: pinfo(" Successfully forwarded message to " + rcpts + "\n") # Over and out smtp.quit() # All done, over and out return 0
# Then tweet the stuff try: twit = twittermsg(UserList[uid]['twitter']) # Create new object twit.doit(text, file_link) pinfo("Twitter update done") twit.finish() # Cleanup except TwitterMsgError, detail: # Error encountered perr(detail) return 1 # failure except: # All other errors - propagate raise # Finally forward the mail, when it is requested if Opts.has_key('testmode') and Opts['testmode']: pwarn(" No mail forwarding in testmode") elif UserList[uid]['forwardto'] is not None: msg_to_orginal = msg['To'] # Backup orginal value try: # Open SMTPConnection; Catch exceptions smtp = smtplib.SMTP(Opts['smtp_server_host'], Opts['smtp_server_port']) except (smtplib.SMTPException, socket.error), detail: perror("Establishing SMTP connection to " + \ Opts['smtp_server_host'] + ":" + Opts['smtp_server_port'] + \ " failed: " + str(detail)) except: raise # Rais all other errors else: # No error opening, continue for rcpts in UserList[uid]['forwardto']: # Iterate over all reciptiens try: # Send data; Catch exceptions # Update msg rcpt