def ctl_new(self, frame): try: (id, ) = struct.unpack("B", frame.data[:1]) name = frame.data[1:] except Exception as e: print("Session request had invalid ID: %s" % e) return print("New session %i from remote" % id) exist = self._sm.get_session(rid=id, rst=frame.s_station) if exist: print("Re-acking existing session %s:%i:%i" % (frame.s_station, id, exist._id)) self.ack_req(frame.s_station, struct.pack("BB", id, exist._id)) return print("ACK'ing session request for %i" % id) try: c = self.stypes[frame.type] print("Got type: %s" % c) s = c(name) s._rs = id s.set_state(base.ST_OPEN) except Exception as e: log_exception() print("Can't start session type `%s': %s" % (frame.type, e)) return num = self._sm._register_session(s, frame.s_station, "new,in") data = struct.pack("BB", id, num) self.ack_req(frame.s_station, data)
def write(self, buf): try: self._serial.write(buf) except Exception as er: print(f"Serial write exception: {err}") utils.log_exception() raise DataPathIOError("Failed to write to serial port")
def RPC_pos_report(self, job): result = {} mycall = self.__config.get("user", "callsign") rqcall = job.get_station() if rqcall == mycall or rqcall == ".": rqcall = None try: fix = self.emit("get-current-position", rqcall) result["rc"] = "OK" except Exception as e: print("Exception while getting position of %s: " % rqcall) log_exception() fix = None result["rc"] = "No data for station '%s'" % job.get_station() if fix: self.emit("user-send-chat", "CQCQCQ", self.__port, fix.to_NMEA_GGA(), True) print("[RPC] Position request for `%s'" % job.get_station()) return result
def read(self, count): try: data = self._serial.read(count) except Exception as err: print(f"Serial read exception: {err}") utils.log_exception() raise DataPathIOError("Failed to read from serial port") return data
def _handle_dele(self, args): try: index = int(args) except Exception: utils.log_exception() raise POP3Exception("Invalid message number") self.del_message(index - 1) self._say("Deleted")
def _handle_retr(self, args): try: index = int(args) except Exception: utils.log_exception() raise POP3Exception("Invalid message number") m = self.get_message(index - 1) mstr = str(m) self._say("OK %i octets" % len(mstr)) self.wfile.write(mstr + "\r\n.\r\n")
def RPC_pos_report(self, job): result = {} mycall = self.__config.get("user", "callsign") rqcall = job.get_station() if rqcall == mycall or rqcall == ".": rqcall = None try: fix = self.emit("get-current-position", rqcall) result["rc"] = "OK" except Exception, e: print "Exception while getting position of %s: " % rqcall log_exception() fix = None result["rc"] = "No data for station '%s'" % job.get_station()
def connect(self): if ":" in self.port: self.port, tncport = self.port.split(":", 1) tncport = int(tncport) else: tncport = 0 try: self._serial = TNCSerial(port=self.port, tncport=tncport, baudrate=self.baud, timeout=self.timeout, writeTimeout=self.timeout * 10, xonxoff=0) except Exception as err: print(f"TNC exception on connect: {err}") utils.log_exception() raise DataPathNotConnectedError("Unable to open serial port")
def handle(self): self.state = "" self._say("D-RATS waiting") while True: try: self._handle() except POP3Exit as e: self._say(e) break except POP3Exception as e: self._say(e, True) break except Exception as e: utils.log_exception() self._say("Internal error", True) break
def RPC_pos_report(self, job): result = {} mycall = self.__config.get("user", "callsign") rqcall = job.get_station() printlog("RPC", " : Position request for `%s'" % rqcall) printlog("RPC", " : Self=%s" % format(self)) if rqcall == mycall or rqcall == ".": rqcall = None try: #obtaining current position from config or local gps fix = self.emit("get-current-position", rqcall) #result = fix.to_NMEA_GGA() result["rc"] = "True" result["msg"] = fix.to_APRS( symtab=self.__config.get("settings", "aprssymtab"), symbol=self.__config.get("settings", "aprssymbol")) except Exception as e: printlog( "RPC", " : Case KO : Exception while getting position of %s: " % rqcall) log_exception() fix = None result["rc"] = "False" result["msg"] = " No data for station '%s'" % job.get_station() if fix: #sending the position to transport // but this is broken!! printlog("RPC", " : port is : %s" % self.__port) printlog("RPC", " : fix is : %s" % fix) printlog("RPC", " : fix in NMEA GGA is: %s" % fix.to_NMEA_GGA()) printlog("RPC", " : fix in APRS is: %s" % result) #self.emit("user-send-chat","CQCQCQ", self.__port, fix.to_NMEA_GGA(), True) self.emit("user-send-chat", "CQCQCQ", self.__port, result["msg"], True) return None
def incoming_data(self, frame): print "Got chat frame: %s" % frame if frame.type == self.T_DEF: fix = gps.parse_GPS(frame.data) if fix and fix.valid: self._incoming_gps(fix) else: self._incoming_chat(frame) elif frame.type == self.T_PNG_REQ: self._emit("ping-request", frame.s_station, frame.d_station, "Request") if frame.d_station == "CQCQCQ": delay = random.randint(0,50) / 10.0 print "Broadcast ping, waiting %.1f sec" % delay time.sleep(delay) elif frame.d_station != self._sm.station: return # Not for us frame.d_station = frame.s_station frame.type = self.T_PNG_RSP try: frame.data = self.pingfn() except Exception, e: print "Ping function failed: %s" % e return self._sm.outgoing(self, frame) try: s, m = self.emit("get-current-status") self.advertise_status(s, m) except Exception, e: print "Exception while getting status for ping reply:" utils.log_exception()
class POP3Handler(SocketServer.StreamRequestHandler): def _say(self, what, error=False): if error: c = "-ERR" else: c = "+OK" self.wfile.write(c + " %s\r\n" % what) print "[POP3] %s %s" % (c, what) def _handle_user(self, args): self._say("username %s accepted" % args) self._user = args return True def _handle_pass(self, args): self._say("password accepted") return True def get_messages(self, username): return [] def _handle_list(self, args): self._handle_stat(args) msgs = self.get_messages(self._user) index = 1 for msg in msgs: print "%i %i" % (index, len(str(msg))) self.wfile.write("%i %i \r\n" % (index, len(str(msg)))) index += 1 self.wfile.write(".\r\n") return True def _handle_stat(self, args): msgs = self.get_messages(self._user) size = 0 for i in msgs: size += len(str(i)) self._say("%i %i" % (len(msgs), size)) return False def _handle_retr(self, args): try: index = int(args) except Exception: utils.log_exception() raise POP3Exception("Invalid message number") m = self.get_message(index - 1) mstr = str(m) self._say("OK %i octets" % len(mstr)) self.wfile.write(mstr + "\r\n.\r\n") def _handle_top(self, args): msgno, lines = args.split(" ", 1) msgno = int(msgno) - 1 lines = int(lines) msg = self.get_message(msgno) self._say("top of message follows") self.wfile.write("\r\n".join(str(msg).split("\r\n")[:lines])) self.wfile.write("\r\n.\r\n") def _handle_dele(self, args): try: index = int(args) except Exception: utils.log_exception() raise POP3Exception("Invalid message number") self.del_message(index - 1) self._say("Deleted") def _handle(self): dispatch = { "USER": (("", ), self._handle_user), "PASS": (("USER", ), self._handle_pass), "LIST": (("PASS", "LIST", "STAT"), self._handle_list), "STAT": (("PASS", "LIST", "STAT"), self._handle_stat), "RETR": (("LIST", "STAT"), self._handle_retr), "TOP": (("LIST", "STAT"), self._handle_top), "DELE": (("LIST", "STAT"), self._handle_dele), } data = self.rfile.readline().strip() if not data: raise POP3Exception("Conversation error") try: cmd, args = data.split(" ", 1) except: cmd = data args = "" cmd = cmd.upper() print "[POP3] %s %s" % (cmd, args) if cmd == "QUIT": raise POP3Exit("Goodbye") if cmd not in dispatch.keys(): self._say("Unsupported command `%s'" % cmd, True) return states, handler = dispatch[cmd] if self.state not in states: raise POP3Exception("Can't get there from here") if handler(args): self.state = cmd def handle(self): self.state = "" self._say("D-RATS waiting") while True: try: self._handle() except POP3Exit, e: self._say(e) break except POP3Exception, e: self._say(e, True) break except Exception, e: utils.log_exception() self._say("Internal error", True) break
print "Re-acking existing session %s:%i:%i" % (frame.s_station, id, exist._id) self.ack_req(frame.s_station, struct.pack("BB", id, exist._id)) return print "ACK'ing session request for %i" % id try: c = self.stypes[frame.type] print "Got type: %s" % c s = c(name) s._rs = id s.set_state(base.ST_OPEN) except Exception, e: log_exception() print "Can't start session type `%s': %s" % (frame.type, e) return num = self._sm._register_session(s, frame.s_station, "new,in") data = struct.pack("BB", id, num) self.ack_req(frame.s_station, data) def ctl(self, frame): if frame.d_station != self._sm.station: print "Control ignoring frame for station %s" % frame.d_station return if frame.type == T_ACK: self.ctl_ack(frame)
self._emit("ping-response", frame.s_station, frame.d_station, "%s %i %s" % (_("Echo of"), len(frame.data), _("bytes"))) elif frame.type == self.T_PNG_ERS: self._emit("ping-response", frame.s_station, frame.d_station, "%s %i %s" % (_("Echo of"), len(frame.data), _("bytes"))) if self.__ping_handlers.has_key(frame.s_station): cb, data = self.__ping_handlers[frame.s_station] try: cb(*data) except Exception: print "Exception while running ping callback" utils.log_exception() elif frame.type == self.T_STATUS: try: s = int(frame.data[0]) except Exception: print "Unable to parse station status: %s" % {frame.s_station : frame.data} s = 0 self._emit("station-status", frame.s_station, s, frame.data[1:]) def write_raw(self, data): f = DDT2RawData() f.data = data f.type = self.T_DEF
if exist: print "Re-acking existing session %s:%i:%i" % (frame.s_station, id, exist._id) self.ack_req(frame.s_station, struct.pack("BB", id, exist._id)) return print "ACK'ing session request for %i" % id try: c = self.stypes[frame.type] print "Got type: %s" % c s = c(name) s._rs = id s.set_state(base.ST_OPEN) except Exception, e: log_exception() print "Can't start session type `%s': %s" % (frame.type, e) return num = self._sm._register_session(s, frame.s_station, "new,in") data = struct.pack("BB", id, num) self.ack_req(frame.s_station, data) def ctl(self, frame): if frame.d_station != self._sm.station: print "Control ignoring frame for station %s" % frame.d_station return if frame.type == T_ACK: self.ctl_ack(frame)
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)
def incoming_data(self, frame): printlog("Chat"," : Got chat frame: %s" % frame) if frame.type == self.T_DEF: fix = gps.parse_GPS(frame.data) if fix and fix.valid: self._incoming_gps(fix) else: self._incoming_chat(frame) elif frame.type == self.T_PNG_REQ: self._emit("ping-request", frame.s_station, frame.d_station, "Request") if frame.d_station == "CQCQCQ": delay = random.randint(0,50) / 10.0 printlog("Chat"," : Broadcast ping, waiting %.1f sec" % delay) time.sleep(delay) elif frame.d_station != self._sm.station: return # Not for us frame.d_station = frame.s_station frame.type = self.T_PNG_RSP try: frame.data = self.pingfn() except Exception as e: printlog("Chat"," : Ping function failed: %s" % e) return self._sm.outgoing(self, frame) try: s, m = self.emit("get-current-status") self.advertise_status(s, m) except Exception as e: printlog("Chat"," : Exception while getting status for ping reply:") utils.log_exception() self._emit("ping-response", frame.s_station, frame.d_station, six.text_type(frame.data, "utf-8")) elif frame.type == self.T_PNG_RSP: printlog("Chat"," : PING OUT") self._emit("ping-response", frame.s_station, frame.d_station, frame.data) elif frame.type == self.T_PNG_ERQ: self._emit("ping-request", frame.s_station, frame.d_station, "%s %i %s" % (_("Echo request of"), len(frame.data), _("bytes"))) if frame.d_station == "CQCQCQ": delay = random.randint(0, 100) / 10.0 printlog("Chat"," : Broadcast ping echo, waiting %.1f sec" % delay) time.sleep(delay) elif frame.d_station != self._sm.station: return # Not for us frame.d_station = frame.s_station frame.type = self.T_PNG_ERS self._sm.outgoing(self, frame) self._emit("ping-response", frame.s_station, frame.d_station, "%s %i %s" % (_("Echo of"), len(frame.data), _("bytes"))) elif frame.type == self.T_PNG_ERS: self._emit("ping-response", frame.s_station, frame.d_station, "%s %i %s" % (_("Echo of"), len(frame.data), _("bytes"))) if frame.s_station in self.__ping_handlers: cb, data = self.__ping_handlers[frame.s_station] try: cb(*data) except Exception: printlog("Chat"," : Exception while running ping callback") utils.log_exception() elif frame.type == self.T_STATUS: try: s = int(frame.data[0]) except Exception: printlog("Chat"," : Unable to parse station status: %s" % {frame.s_station : frame.data}) s = 0 self._emit("station-status", frame.s_station, s, frame.data[1:])