/usr/share/pyshared/d_rats/sessions/chat.py is in d-rats 0.3.3-3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | import random
import time
import gobject
from d_rats import signals, platform, gps, utils, station_status
from d_rats.version import DRATS_VERSION
from d_rats.sessions import base, stateless
from d_rats.ddt2 import DDT2EncodedFrame, DDT2RawData
class ChatSession(stateless.StatelessSession, gobject.GObject):
__gsignals__ = {
"incoming-chat-message" : signals.INCOMING_CHAT_MESSAGE,
"outgoing-chat-message" : signals.OUTGOING_CHAT_MESSAGE,
"ping-request" : signals.PING_REQUEST,
"ping-response" : signals.PING_RESPONSE,
"incoming-gps-fix" : signals.INCOMING_GPS_FIX,
"station-status" : signals.STATION_STATUS,
"get-current-status" : signals.GET_CURRENT_STATUS,
}
_signals = __gsignals__
__cb = None
__cb_data = None
type = base.T_STATELESS
T_DEF = 0
T_PNG_REQ = 1
T_PNG_RSP = 2
T_PNG_ERQ = 3
T_PNG_ERS = 4
T_STATUS = 5
compress = False
def __init__(self, *args, **kwargs):
stateless.StatelessSession.__init__(self, *args, **kwargs)
gobject.GObject.__init__(self)
self.set_ping_function()
self.handler = self.incoming_data
self.__ping_handlers = {}
def set_ping_function(self, func=None):
if func is not None:
self.pingfn = func
else:
self.pingfn = self.ping_data
def ping_data(self):
p = platform.get_platform()
return _("Running") + " D-RATS %s (%s)" % (DRATS_VERSION,
p.os_version_string())
def _emit(self, signal, *args):
gobject.idle_add(self.emit, signal, *args)
def _incoming_chat(self, frame):
self._emit("incoming-chat-message",
frame.s_station,
frame.d_station,
unicode(frame.data, "utf-8"))
def _incoming_gps(self, fix):
self._emit("incoming-gps-fix", fix)
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()
self._emit("ping-response",
frame.s_station,
frame.d_station,
unicode(frame.data, "utf-8"))
elif frame.type == self.T_PNG_RSP:
print "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
print "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 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
print "Sending raw: %s" % data
self._sm.outgoing(self, f)
def write(self, data, dest="CQCQCQ"):
self._emit("outgoing-chat-message", self._sm.station, self._st, data)
stateless.StatelessSession.write(self, data, dest)
def ping_station(self, station):
f = DDT2EncodedFrame()
f.d_station = station
f.type = self.T_PNG_REQ
f.data = "Ping Request"
f.set_compress(False)
self._sm.outgoing(self, f)
self._emit("ping-request", f.s_station, f.d_station, "Request")
def ping_echo_station(self, station, data, cb=None, *cbdata):
if cb:
self.__ping_handlers[station] = (cb, cbdata)
f = DDT2EncodedFrame()
f.d_station = station
f.type = self.T_PNG_ERQ
f.data = data
f.set_compress(False)
self._sm.outgoing(self, f)
self._emit("ping-request", f.s_station, f.d_station,
"%s %i %s" % (_("Echo of"),
len(data),
_("bytes")))
def advertise_status(self, stat, msg):
if stat > station_status.STATUS_MAX or stat < station_status.STATUS_MIN:
raise Exception("Status integer %i out of range" % stat)
f = DDT2EncodedFrame()
f.d_station = "CQCQCQ"
f.type = self.T_STATUS
f.data = "%i%s" % (stat, msg)
self._sm.outgoing(self, f)
|