def _update_message_info(self, iter, force=False): fn, = self.store.get(iter, ML_COL_FILE) subj = self.current_info.get_msg_subject(fn) read = self.current_info.get_msg_read(fn) if subj == _("Unknown") or force: # Not registered, so update the registry form = formgui.FormFile(fn) self.current_info.set_msg_type(fn, form.id) self.current_info.set_msg_read(fn, read) self.current_info.set_msg_subject(fn, form.get_subject_string()) self.current_info.set_msg_sender(fn, form.get_sender_string()) self.current_info.set_msg_recip(fn, form.get_recipient_string()) ts = os.stat(fn).st_ctime read = self.current_info.get_msg_read(fn) if read: icon = self.message_pixbuf else: icon = self.unread_pixbuf self.store.set(iter, ML_COL_ICON, icon, ML_COL_SEND, self.current_info.get_msg_sender(fn), ML_COL_RECP, self.current_info.get_msg_recip(fn), ML_COL_SUBJ, self.current_info.get_msg_subject(fn), ML_COL_TYPE, self.current_info.get_msg_type(fn), ML_COL_DATE, ts, ML_COL_READ, read)
def __create_form(self, msg): mail = email.message_from_string(msg.get_content()) sender = mail.get("From", "Unknown") if ":" in sender: method, sender = sender.split(":", 1) sender = "WL2K:" + sender if self._callsign == self._config.get("user", "callsign"): box = "Inbox" else: box = "Outbox" template = os.path.join(self._config.form_source_dir(), "email.xml") formfn = os.path.join(self._config.form_store_dir(), box, "%s.xml" % msg.get_id()) form = formgui.FormFile(template) form.set_field_value("_auto_sender", sender) form.set_field_value("recipient", self._callsign) form.set_field_value("subject", mail.get("Subject", "Unknown")) form.set_field_value("message", mail.get_payload()) form.set_path_src(sender.strip()) form.set_path_dst(self._callsign) form.set_path_mid(msg.get_id()) form.add_path_element("@WL2K") form.add_path_element(self._config.get("user", "callsign")) form.save_to(formfn) return formfn
def create_form(self, config, callsign): mail = email.message_from_string(self.__content) sender = mail.get("From", "Unknown") if ":" in sender: method, sender = sender.split(":", 1) sender = "WL2K:" + sender body = mail.get("Body", "0") try: body_length = int(body) except ValueError: raise Exception("Error parsing Body header length `%s'" % value) body_start = self.__content.index("\r\n\r\n") + 4 rest = self.__content[body_start + body_length:] message = self.__content[body_start:body_start + body_length] if callsign == config.get("user", "callsign"): box = "Inbox" else: box = "Outbox" template = os.path.join(config.form_source_dir(), "email.xml") formfn = os.path.join(config.form_store_dir(), box, "%s.xml" % self.get_id()) form = formgui.FormFile(template) form.set_field_value("_auto_sender", sender) form.set_field_value("recipient", callsign) form.set_field_value("subject", mail.get("Subject", "Unknown")) form.set_field_value("message", message) files = mail.get_all("File") if files: for att in files: length, name = att.split(" ", 1) filedata = rest[2:int(length) + 2] # Length includes leading CRLF printlog( "WL2k", " : File %s %i (%i)" % (name, len(filedata), int(length))) rest = rest[int(length) + 2:] form.add_attachment(name, filedata) form.set_path_src(sender.strip()) form.set_path_dst(callsign) form.set_path_mid(self.get_id()) form.add_path_element("@WL2K") form.add_path_element(config.get("user", "callsign")) form.save_to(formfn) return formfn
def get_shared_messages(self, for_station): """Return a list of (title, stamp, filename) forms destined for station @for_station""" shared = _("Inbox") path = os.path.join(self._config.platform.config_dir(), "messages") if not os.path.isdir(path): os.makedirs(path) info = MessageFolderInfo(os.path.join(path, shared)) ret = [] for fn in info.files(): stamp = os.stat(fn).st_mtime ffn = "%s/%s" % (shared, os.path.basename(fn)) form = formgui.FormFile(fn) ret.append((form.get_subject_string(), stamp, ffn)) return ret
def _new_msg(self, button, msgtype=None): types = glob(os.path.join(self._config.form_source_dir(), "*.xml")) forms = {} for fn in types: forms[os.path.basename(fn).replace(".xml", "")] = fn if msgtype is None: parent = self._wtree.get_widget("mainwindow") d = inputdialog.ChoiceDialog(list(forms.keys()), title=_("Choose a form"), parent=parent) r = d.run() msgtype = d.choice.get_active_text() d.destroy() if r != Gtk.RESPONSE_OK: return current = self._messages.current_info.name() self._folders.select_folder(_("Drafts")) tstamp = time.strftime("form_%m%d%Y_%H%M%S.xml") newfn = self._messages.current_info.create_msg(tstamp) form = formgui.FormFile(forms[msgtype]) call = self._config.get("user", "callsign") form.add_path_element(call) form.set_path_src(call) form.set_path_mid(mkmsgid(call)) form.save_to(newfn) def close_msg_cb(response, info): if response == int(Gtk.RESPONSE_CLOSE): info.delete(newfn) if self._messages.current_info == info: self._messages.refresh() self._folders.select_folder(current) self._messages.open_msg(newfn, True, close_msg_cb, self._messages.current_info)
def __form_received(self, object, id, fn, port=None): if port: id = "%s_%s" % (id, port) print(f"[NEWFORM {id}]: {fn}") f = formgui.FormFile(fn) msg = f'{_("Message")} "{f.get_subject_string()}" {_("received from")} {f.get_sender_string()}' myc = self.config.get("user", "callsign") dst = f.get_path_dst() src = f.get_path_src() pth = f.get_path() fwd_on = self.config.getboolean("settings", "msg_forward") is_dst = msgrouting.is_sendable_dest(myc, dst) nextst = msgrouting.gratuitous_next_hop(dst, pth) or dst bounce = "@" in src and "@" in dst isseen = myc in f.get_path()[:-1] print( f"Decision: fwd:{fwd_on} sendable:{is_dst} next:{nextst} bounce:{bounce} seen:{isseen}" ) if fwd_on and is_dst and not bounce and not isseen: msg += " (%s %s)" % (_("forwarding to"), nextst) msgrouting.move_to_outgoing(self.config, fn) refresh_folder = "Outbox" else: refresh_folder = "Inbox" msgrouting.msg_unlock(fn) self.mainwindow.tabs["messages"].refresh_if_folder(refresh_folder) event = main_events.FormEvent(id, msg) event.set_as_final() self.mainwindow.tabs["event"].event(event)
def _rpl_msg(self, button, fn=None): def subj_reply(subj): if "RE:" in subj.upper(): return subj else: return "RE: %s" % subj def msg_reply(msg): if self._config.getboolean("prefs", "msg_include_reply"): return "--- Original Message ---\r\n\r\n" + msg else: return "" save_fields = [ ("_auto_number", "_auto_number", lambda x: str(int(x) + 1)), ("_auto_subject", "_auto_subject", subj_reply), ("subject", "subject", lambda x: "RE: %s" % x), ("message", "message", msg_reply), ("_auto_sender", "_auto_recip", None), ] if not fn: try: sel = self._messages.get_selected_messages() except TypeError: return if len(sel) > 1: print("FIXME: Warn about multiple reply") return fn = sel[0] current = self._messages.current_info.name() self._folders.select_folder(_("Drafts")) oform = formgui.FormFile(fn) tmpl = os.path.join(self._config.form_source_dir(), "%s.xml" % oform.id) nform = formgui.FormFile(tmpl) nform.add_path_element(self._config.get("user", "callsign")) try: for sf, df, xf in save_fields: oldval = oform.get_field_value(sf) if not oldval: continue if xf: nform.set_field_value(df, xf(oldval)) else: nform.set_field_value(df, oldval) except Exception as e: log_exception() print(("Failed to do reply: %s" % e)) return if ";" in oform.get_path_dst(): rpath = ";".join(reversed(oform.get_path()[:-1])) print(("rpath: %s (%s)" % (rpath, oform.get_path()))) nform.set_path_dst(rpath) else: nform.set_path_dst(oform.get_path_src()) call = self._config.get("user", "callsign") nform.set_path_src(call) nform.set_path_mid(mkmsgid(call)) tstamp = time.strftime("form_%m%d%Y_%H%M%S.xml") newfn = self._messages.current_info.create_msg(tstamp) nform.save_to(newfn) def close_msg_cb(response, info): if self._messages.current_info == info: print( ("Respone was %i (%i)" % (response, Gtk.RESPONSE_CANCEL))) if response in [Gtk.RESPONSE_CANCEL, Gtk.RESPONSE_CLOSE]: info.delete(newfn) self._folders.select_folder(current) else: self._messages.refresh(newfn) self._messages.open_msg(newfn, True, close_msg_cb, self._messages.current_info)