def create_form_from_mail(config, mail, tmpfn): subject = mail.get("Subject", "[no subject]") sender = mail.get("From", "Unknown <*****@*****.**>") xml = None body = "" if mail.is_multipart(): html = None for part in mail.walk(): if part.get_content_maintype() == "multipart": continue elif part.get_content_type() == "d-rats/form_xml": xml = str(part.get_payload()) break # A form payload trumps all elif part.get_content_type() == "text/plain": body += part.get_payload(decode=True) elif part.get_content_type() == "text/html": html = part.get_payload(decode=True) if not body: body = html else: body = mail.get_payload(decode=True) if not body and not xml: raise Exception("Unable to find a usable part") messageid = mail.get("Message-ID", time.strftime("%m%d%Y%H%M%S")) if not msgrouting.msg_lock(tmpfn): print "AIEE: Unable to lock incoming email message file!" if xml: f = file(tmpfn, "w") f.write(xml) f.close() form = formgui.FormFile(tmpfn) recip = form.get_recipient_string() if "%" in recip: recip, addr = recip.split("%", 1) recip = recip.upper() else: print "Email from %s: %s" % (sender, subject) recip, addr = rfc822.parseaddr(mail.get("To", "UNKNOWN")) efn = os.path.join(config.form_source_dir(), "email.xml") form = formgui.FormFile(efn) form.set_field_value("_auto_sender", sender) form.set_field_value("recipient", recip) form.set_field_value("subject", "EMAIL: %s" % subject) form.set_field_value("message", utils.filter_to_ascii(body)) form.set_path_src(sender.strip()) form.set_path_dst(recip.strip()) form.set_path_mid(messageid) form.save_to(tmpfn) return form
def _route_message(self, msg, slist, routes): form = formgui.FormFile(msg) path = form.get_path() emok = path[-2:] != ["EMAIL", self.__config.get("user", "callsign")] src = form.get_path_src() dst = form.get_path_dst() del form routed = False route = self._route_msg(src, dst, path, slist, routes) if not route: pass elif route.upper().startswith("WL2K:"): routed = self._route_via_wl2k(src, route, msg) elif "@" in src and "@" in dst: # Don't route a message from email to email pass elif "@" in route: if emok: routed = self._route_via_email(dst, msg) else: routed = self._route_via_station(dst, route, slist, msg) return routed
def form_to_email(config, msgfn, replyto=None): form = formgui.FormFile(msgfn) form.configure(config) if not replyto: replyto = "*****@*****.**" sender = form.get_path_src() for i in "<>\"'": if i in sender: sender = sender.replace(i, "") root = MIMEMultipart("related") root["Subject"] = form.get_subject_string() root["From"] = '"%s" <%s>' % (sender, replyto) root["To"] = form.get_path_dst() root["Reply-To"] = root["From"] root["Message-id"] = form.get_path_mid() root.preamble = "This is a multi-part message in MIME format" altp = MIMEMultipart("alternative") root.attach(altp) warn = "This message is a D-RATS rich form and is only viewable " +\ "in D_RATS itself or in an HTML-capable email client" if form.id == "email": altp.attach(MIMEText(form.get_field_value("message"), "plain")) else: altp.attach(MIMEText(warn, "plain")) altp.attach(MIMEText(form.export_to_string(), "html")) payload = MIMEBase("d-rats", "form_xml") payload.set_payload(form.get_xml()) payload.add_header("Content-Disposition", "attachment", filename="do_not_open_me") root.attach(payload) del form return root
def _form_to_wl2k_em(self, dst, msgfn): form = formgui.FormFile(msgfn) if form.id != "email": raise Exception("WL2K support requires email type message") payload = form.get_field_value("message") call = self.__config.get("user", "callsign") call = "".join([x for x in call if x.isalpha()]) attachments = [] for name, length in form.get_attachments(): data = form.get_attachment(name) attachments.append(wl2k.WinLinkAttachment(name, data)) msg = wl2k.WinLinkMessage() msg.encode_message(form.get_path_src(), [dst], form.get_subject_string(), payload, attachments) return msg
def worker(self, path): md = os.path.join(self.coord.config.form_store_dir(), _("Inbox")) newfn = time.strftime(os.path.join(md, "form_%m%d%Y_%H%M%S.xml")) if not msgrouting.msg_lock(newfn): print "AIEE! Unable to lock incoming new message file!" fn = self.session.recv_file(newfn) name = "%s %s %s" % (self.session.name, _("from"), self.session.get_station()) if fn == newfn: form = formgui.FormFile(fn) form.add_path_element(self.coord.config.get("user", "callsign")) form.save_to(fn) self.completed("form") self.coord.session_newform(self.session, fn) else: self.failed() print "<--- Form transfer failed -->"
def _form_to_wl2k_em(self, dst, msgfn): form = formgui.FormFile(msgfn) if form.id != "email": raise Exception("WL2K support requires email type message") payload = form.get_field_value("message") call = self.__config.get("user", "callsign") call = "".join([x for x in call if x.isalpha()]) mid = time.strftime("D%H%M%S") + call[:12] msg = "Mid: %s\r\n" % mid + \ "Subject: %s\r\n" % form.get_subject_string() + \ "From: %s\r\n" % form.get_path_src() + \ "To: %s\r\n" % dst + \ "Body: %i\r\n" % len(payload) + \ "Date: %s\r\n" % time.strftime("%Y/%m/%d %H:%M", time.gmtime()) + \ "\r\n" + \ payload + "\r\n" + \ "\r\n" return msg
def _get_queue(self): queue = {} qd = os.path.join(self.__config.form_store_dir(), "Outbox") fl = glob(os.path.join(qd, "*.xml")) for f in fl: if not msg_lock(f): print "Message %s is locked, skipping" % f continue form = formgui.FormFile(f) call = form.get_path_dst() del form if not call: msg_unlock(f) continue elif not queue.has_key(call): queue[call] = [f] else: queue[call].append(f) return queue
def _update_path(self, fn, call): form = formgui.FormFile(fn) form.add_path_element(call) form.save_to(fn)