diff options
Diffstat (limited to 'slime-0.11/slime_send.py')
-rw-r--r-- | slime-0.11/slime_send.py | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/slime-0.11/slime_send.py b/slime-0.11/slime_send.py new file mode 100644 index 0000000..3e6c3b8 --- /dev/null +++ b/slime-0.11/slime_send.py @@ -0,0 +1,171 @@ +import smtplib, string, regex, regsub, pwd, os, socket, rfc822, time +import helpers, StringIO +from ui_config import config + +draft_txt = """From: %s +Reply-to: %s +To: +Cc: +Subject: + +%s""" + +reply_txt = """From: %s +Reply-to: %s +To: %s +Cc: %s +Subject: %s%s +In-Reply-To: %s +References: %s + +%s: +%s + +%s""" + +_user_address = None + +def deduce_user_address(): + global _user_address + if config["from-address"]: + return config["from-address"] + if _user_address is None: + userinfo = pwd.getpwuid(os.getuid()) + host = socket.gethostname() + ip = socket.gethostbyname(host) + fqdn = socket.gethostbyaddr(ip)[0] + _user_address = "%s@%s" % (userinfo[0], fqdn) + if userinfo[4]: + x = regsub.sub(",*$", "", userinfo[4]) + _user_address = "%s <%s>" % (x, _user_address) + return _user_address + +def get_signature_text(): + try: + pathname = os.path.expanduser(config["signature-file"]) + sig = "-- \n" + helpers.read_text_from_named_file(pathname) + except IOError: + sig = "" + return sig + +def make_new_message_text(): + return draft_txt % (deduce_user_address(), config["reply-to"], + get_signature_text()) + +def remove_duplicate_addrs(recipients, used_addrs): + result = [] + for recipient in string.split(recipients, ","): + name, addr = rfc822.parseaddr(recipient) + if not addr in used_addrs: + result.append(recipient) + used_addrs.append(addr) + return string.join(result, ","), used_addrs + +def make_reply_message_text(msg): + user_addr = deduce_user_address() + reply_to = config["reply-to"] + to_addrs = msg["to"] + if to_addrs: + to_addrs = msg["from"] + ", " + to_addrs + else: + to_addrs = msg["from"] + cc_addrs = msg["cc"] + + user_addr_name, user_addr_addr = rfc822.parseaddr(user_addr) + reply_to_name, reply_to_addr = rfc822.parseaddr(reply_to) + + if user_addr_addr and reply_to_addr: + used_addrs = [user_addr_addr, reply_to_addr] + elif user_addr_addr: + used_addrs = [user_addr_addr] + elif reply_to_addr: + used_addrs = [reply_to_addr] + else: + used_addrs = [] + to_addrs, used_addrs = remove_duplicate_addrs(to_addrs, used_addrs) + cc_addrs, used_addrs = remove_duplicate_addrs(cc_addrs, used_addrs) + + subject = msg["subject"] + if string.lower(subject[:3]) == "re:": + re = "" + else: + re = "Re: " + + msgid = msg["message-id"] + refs = msg["references"] + " " + msgid + while len(refs) > config["max-references-length"]: + new_refs = regsub.sub("[^ ]* ", "", refs) + if len(new_refs) == refs: + break + refs = new_refs + + quoted_author = msg["from"] + quoted = msg.getbodytext() + quoted = regsub.gsub("^", "> ", quoted) + if quoted and quoted[-1] != "\n": + quoted = quoted + "\n" + + return reply_txt % (user_addr, reply_to, to_addrs, + cc_addrs, re, subject, msgid, refs, + quoted_author, quoted, get_signature_text()) + +empty_trans = string.maketrans("", "") +sevenbit_chars = None + +def deduce_charset(str): + """Deduce the character set used in a string.""" + global sevenbit_chars + if not sevenbit_chars: + list = [] + for code in range(128): + list.append(chr(code)) + sevenbit_chars = string.join(list, "") + if string.translate(str, empty_trans, sevenbit_chars): + return "ISO-8859-1" + return "us-ascii" + +empty_header = regex.compile("^[^:][^:]*:[ \t]*$") +date_header = regex.compile("^Date:", regex.casefold) + +def prepare_for_send(msg): + nonempty = [] + hit = 0 + for line in string.split(msg.getheadertext(), "\n"): + if line and not line[0] in string.whitespace: + if date_header.match(line) == -1: + hit = (empty_header.match(line) == -1) + else: + hit = 0 + if line and hit: + nonempty.append(line) + nonempty.append("Date: %s" % helpers.rfc822_date()) + nonempty.append("") + headers = string.join(nonempty, "\n") + + msg.change_text(StringIO.StringIO(headers + "\n" + msg.getbodytext(0))) + +def send_msg(msg): + name, sender = msg.getaddr("from") + receivers = [] + for header in ["to", "cc"]: + if msg[header]: + for name, addr in msg.getaddrlist(header): + receivers.append(addr) + + smtp = smtplib.SMTP(config["smtp-server"]) + smtp.helo() + smtp.mail_from(sender) + for receiver in receivers: + smtp.rcpt_to(receiver) + text = string.split(msg.getfulltext(), "\n") + smtp.data(text) + smtp.quit() + +def main(): + print deduce_user_address() + print rfc822_date() + print deduce_charset("this is us-ascii") + print deduce_charset("tämä on latin1:tä") + +if __name__ == "__main__": + main() |