def command_otr(self, args): """ A command to start or end OTR encryption """ args = args.split() if not args: return self.core.command_help("otr") if isinstance(self.core.current_tab(), ConversationTab): jid = JID(self.core.current_tab().get_name()) command = args[0] if command == 'start': otr_state = self.get_otr(self.core.current_tab()) self.otr_say(self.core.current_tab(), otr_state.start().decode()) elif command == 'end': otr_state = self.get_otr(self.core.current_tab()) msg = otr_state.end() if msg is not None: self.otr_say(self.core.current_tab(), msg.decode()) elif command == 'fpr': otr_state = self.get_otr(self.core.current_tab()) our = otr_state.our_fpr if our: our = hex(int.from_bytes(our, 'big'))[2:].ljust(40).upper() their = otr_state.their_fpr if their: their = hex(int.from_bytes(their, 'big'))[2:].ljust(40).upper() self.core.current_tab().add_message('Your: %s Their: %s' % (our, their)) self.core.refresh_window()
def presence_event_handler(self, presence): try: presence_sender = presence["from"] presence_receiver_jid = JID(presence["to"]) presence_receiver = str(presence_receiver_jid.user) + "@" \ + str(presence_receiver_jid.domain) status = presence["status"] if(presence_receiver == self.boundjid.bare and presence_sender != self.boundjid.full): if (status != "" and "#" in status): pstatus, peer_id = status.split("#") if (pstatus == "ident"): self.presence_publisher.post_update( dict(PeerId=peer_id, OverlayId=self.overlay_id)) self._log("Resolved Peer@Overlay {0}@{1} - {2}" .format(peer_id[:7], self.overlay_id, presence_sender)) self.jid_cache.add_entry(node_id=peer_id, jid=presence_sender) elif (pstatus == "uid?"): if (self.node_id == peer_id): payload = self.boundjid.full + "#" + self.node_id self.send_msg(presence_sender, "uid!", payload) else: self._log("Unrecognized PSTATUS: {0}".format(pstatus)) except Exception as err: self._log("XmppTransport:Exception:{0} presence:{1}".format(err, presence), severity="LOG_ERROR")
def handle_presence(self, presence): #try: presence_sender = presence['from'] presence_receiver_jid = JID(presence['to']) presence_receiver = str(presence_receiver_jid.user)+"@"+str(presence_receiver_jid.domain) status = presence['status'] for interface, xmpp_details in self.ipop_xmpp_details.items(): # Check whether the Receiver JID belongs to the XMPP Object of the particular virtual network, # If YES update JID-UID table if presence_receiver == xmpp_details["username"] and presence_sender != xmpp_details["XMPPObj"].boundjid\ .full: if (status != "" and "#" in status): p_type, uid = status.split('#') if (p_type=="uid_is"): self.presence_publisher.PostUpdate(dict(uid_notification=uid, interface_name=interface)) self.log("UID {0} received from {1}".format(uid,presence_sender), severity=log_level) elif (p_type == "uid?"): if (xmpp_details["uid"]==uid): xmpp_details["XMPPObj"].send_presence(pstatus="jid_uid#" + xmpp_details["uid"]+"#"+ xmpp_details["XMPPObj"].boundjid.full) setup_load = "UID_MATCH" + "#" + "None" + "#" + str(presence_sender) msg_load = xmpp_details["XMPPObj"].boundjid.full+"#"+xmpp_details["uid"] self.sendxmppmsg(presence_sender, xmpp_details["XMPPObj"], setup_load, msg_load) self.log("UID {0} matched, replied to {1}".format(uid, presence_sender), "debug") else: self.log("UID {0} request received from {1}".format(uid, str(presence_sender)), "debug")
def updateroster(self, message): try: presence_receiver_jid = JID(message['to']) presence_receiver = str(presence_receiver_jid.user) + "@" + str( presence_receiver_jid.domain) interface_name, xmppobj = "", None # Iterate across the XMPPClient module table to find the TapInterface for the XMPP server message for tapName, xmpp_details in list(self.ipop_xmpp_details.items()): if presence_receiver == xmpp_details["username"]: xmppobj = xmpp_details interface_name = tapName break # check whether Server Message has a matching key in the XMPP Table if not stop processing if xmppobj is None: return # iterate across the roster details to find unsubscribe JIDs for nodejid, data in message["roster"]["items"].items(): if data["subscription"] == "remove": for ele in list(xmppobj["jid_uid"].keys()): tempjid = JID(ele) jid = str(tempjid.user) + "@" + str(tempjid.domain) # Check whether the JID of the unsubscribed user exists in the JID-UID Table, # If Yes remove it from JID-UID, UID-JID Table and sent a trigger message to Topology Manager # to remove it from its discovered node list if jid.find(str(nodejid)) != -1: node_uid = xmppobj[ele][0] del xmppobj["jid_uid"][ele] # Check whether UID exists in the UID-JID Table, If YES remove it if node_uid in xmppobj["uid_jid"].keys(): del xmppobj["uid_jid"][node_uid] self.registerCBT( "Logger", "info", "{0} has been deleted from the roster.". format(node_uid)) msg = { "uid": node_uid, "type": "offline_peer", "interface_name": interface_name } self.registerCBT("BaseTopologyManager", "XMPP_MSG", msg) # Remove the peer from XMPP Online Peerlist if it exists if node_uid in xmppobj["online_xmpp_peers"]: xmppobj["online_xmpp_peers"].remove(node_uid) except Exception as err: self.log("Exception in deletepeerjid method.{0}".format(err), severity="error")
def handle_presence(self, presence): try: presence_sender = presence['from'] presence_receiver_jid = JID(presence['to']) presence_receiver = str(presence_receiver_jid.user) + "@" + str( presence_receiver_jid.domain) for xmpp_details in self.ipop_xmpp_details.values(): # Check whether the Receiver JID belongs to the XMPP Object of the particular virtual network, # If YES update JID-UID table if presence_receiver == xmpp_details["username"] and presence_sender != xmpp_details["XMPPObj"].boundjid\ .full: xmpp_details["jid_uid"][presence_sender][1] = time.time() self.log( "Presence received from {0}".format(presence_sender), severity=log_level) except Exception as err: self.log( "Exception caught in XMPPClient handle_presence method : {0}". format(err), severity="error")
def offline_xmpp_peers(self, message): try: peerjid = message["from"] self.log("Peer JID {0} offline".format(peerjid), severity="info") presence_receiver_jid = JID(message['to']) presence_receiver = str(presence_receiver_jid.user) + "@" + str( presence_receiver_jid.domain) interface_name, xmppobj = "", None # Iterate across the XMPPClient module table to find the TapInterface for the XMPP server message for tapName, xmpp_details in list(self.ipop_xmpp_details.items()): if presence_receiver == xmpp_details["username"]: xmppobj = xmpp_details interface_name = tapName break # check whether Server Message has a matching key in the XMPP Table if not stop processing if xmppobj is None: return # Check whether the JID of the offline peer exists in the JID-UID Table, If YES remove it from the # JID-UID, UID-JID and sent a trigger message to Topology Manager # to remove it from its discovered node list if peerjid in xmppobj["jid_uid"].keys(): uid = xmppobj["jid_uid"][peerjid][0] del xmppobj["jid_uid"][peerjid] # Check whether UID exists in the UID-JID Table, If YES remove it if uid in xmppobj["uid_jid"].keys(): del xmppobj["uid_jid"][uid] self.log( "Removed Peer JID: {0} UID: {1} from the JID-UID and UID-JID Table" .format(peerjid, uid), severity="info") msg = { "uid": uid, "type": "offline_peer", "interface_name": interface_name } self.registerCBT("BaseTopologyManager", "XMPP_MSG", msg) except Exception as err: self.log( "Exception in remove peerjid method. Error::{0}".format(err), severity="error")
def presence_event_handler(self, presence): """ Handles peer presence event messages """ try: presence_sender = presence["from"] presence_receiver_jid = JID(presence["to"]) presence_receiver = str(presence_receiver_jid.user) + "@" \ + str(presence_receiver_jid.domain) status = presence["status"] if (presence_receiver == self.boundjid.bare and presence_sender != self.boundjid.full): if (status != "" and "#" in status): pstatus, peer_id = status.split("#") if pstatus == "ident": # a notification of a peers node id to jid mapping pts = self.jid_cache.add_entry(node_id=peer_id, jid=presence_sender) self.presence_publisher.post_update( dict(PeerId=peer_id, OverlayId=self.overlay_id, PresenceTimestamp=pts)) self._sig.sig_log( "Presence has resolved Peer@Overlay {0}@{1} -> {2}" .format(peer_id[:7], self.overlay_id, presence_sender)) elif pstatus == "uid?": # a request for our node id if self.node_id == peer_id: payload = self.boundjid.full + "#" + self.node_id self.send_msg(presence_sender, "uid!", payload) else: self._sig.sig_log( "Unrecognized PSTATUS: {0}".format(pstatus), "LOG_WARNING") except Exception as err: self._sig.sig_log( "XmppTransport:Exception:{0} presence:{1}".format( err, presence), "LOG_ERROR")
def xmppmessagelistener(self, msg): presence_receiver_jid = JID(msg['to']) sender_jid = msg['from'] presence_receiver = str(presence_receiver_jid.user) + "@" + str( presence_receiver_jid.domain) interface_name, xmppobj = "", None # Iterate across the XMPPClient module table to find the TapInterface for the XMPP server message for tapName, xmpp_details in list(self.ipop_xmpp_details.items()): if presence_receiver == xmpp_details["username"]: xmppobj = xmpp_details interface_name = tapName break # check whether Server Message has a matching key in the XMPP Table if not stop processing if xmppobj is None: return # Check whether Node UID obtained from CFX if xmppobj["uid"] == "": self.log("UID not received from Tincan. Please check Tincan logs.", severity="warning") return # Check whether the message was initiated by the node itself if YES discard it if sender_jid == xmppobj["XMPPObj"].boundjid.full: return # extract setup and content setup = str(msg['Ipop']['setup']) payload = str(msg['Ipop']['payload']) msg_type, target_uid, target_jid = setup.split("#") if msg_type == "regular_msg": self.log("Received regular mesage from {0}".format(msg['from']), severity=log_level) self.log("Msg is {0}".format(payload), severity="debug") elif msg_type == "xmpp_advertisement": # peer_uid - uid of the node that sent the advt # target_uid - what it percieves as my uid try: peer_uid, target_uid = payload.split("#") # Check that the source of xmpp message is not the current node, if NOT, update the JID-UID Table's # current time, UID and message count attributes if peer_uid != xmppobj["uid"]: xmppobj["uid_jid"][peer_uid] = sender_jid xmppobj["jid_uid"][sender_jid][0] = peer_uid xmppobj["jid_uid"][sender_jid][2] += 1 xmppobj["jid_uid"][sender_jid][1] = time.time() self.log( "XMPP Message: Received advertisement from peer {0}". format(peer_uid), severity=log_level) except Exception as error: self.log( "Exception caught while processing advt_payload: {0}. Error: {1}" .format(payload, str(error)), severity="error") # compare uid's here , if target uid does not match with mine do nothing. # have to avoid loop messages. if target_uid == xmppobj["uid"]: sender_uid, recvd_data = payload.split("#") # If I recvd XMPP msg from this peer, I should record his UID-JID & JID-UID if sender_uid not in xmppobj["uid_jid"].keys(): self.ipop_xmpp_details[interface_name][ "update_xmpppeerlist_flag"] = True self.ipop_xmpp_details[interface_name][ "online_xmpp_peers"].append(sender_uid) self.ipop_xmpp_details[interface_name]["online_xmpp_peers"] = \ list(set(self.ipop_xmpp_details[interface_name]["online_xmpp_peers"])) self.sendxmpppeerlist(interface_name) xmppobj["uid_jid"][sender_uid] = sender_jid msg = {} if msg_type == "get_casdetails": msg["uid"] = sender_uid msg["data"] = recvd_data msg["interface_name"] = interface_name # send this CBT to BaseTopology Manager self.registerCBT('LinkManager', 'RETRIEVE_CAS_FROM_TINCAN', msg) self.log( "XMPP Message: Received CAS request from peer {0}".format( msg["uid"]), severity=log_level) elif msg_type == "sent_casdetails": msg["uid"] = sender_uid msg["data"] = recvd_data msg["interface_name"] = interface_name self.registerCBT('LinkManager', 'CREATE_P2PLINK', msg) self.log("Received CAS details from peer {0}".format( msg["uid"]), severity=log_level)
def xmppmessagelistener(self, msg): receiver_jid = JID(msg['to']) sender_jid = msg['from'] receiver = str(receiver_jid.user) + "@" + str(receiver_jid.domain) interface_name, xmppobj = "", None self.log("RECEIVED MESSAGE MSG {} ".format(msg), "debug") # Iterate across the XMPPClient module table to find the TapInterface for the XMPP server message for tapName, xmpp_details in list(self.ipop_xmpp_details.items()): if receiver == xmpp_details["username"]: xmppobj = xmpp_details interface_name = tapName break # check whether Server Message has a matching key in the XMPP Table if not stop processing if xmppobj is None: return # Check whether Node UID obtained from CFX if xmppobj["uid"] == "": self.log("UID not received from Tincan. Please check Tincan logs.", severity="warning") return # Check whether the message was initiated by the node itself if YES discard it if sender_jid == xmppobj["XMPPObj"].boundjid.full: return # extract setup and content setup = msg['Ipop']['setup'] payload = msg['Ipop']['payload'] msg_type, target_uid, target_jid = setup.split("#") self.log("RECEIVED MESSAGE setup {} payload {}".format(setup,payload),"debug") if msg_type == "regular_msg": self.log("Received regular mesage from {0}".format(msg['from']), severity=log_level) self.log("Msg is {0}".format(payload), severity="debug") elif msg_type == "UID_MATCH": # This type does not contains target uid match_jid,matched_uid = payload.split("#") self.log("UID match received from JID {} for UID {}".format(match_jid,matched_uid),"debug") # complete all pending CBTs #CBTQ = self.pending_CBTQ[matched_uid] CBTQ = self.ipop_xmpp_details[interface_name]["pending_CBTQ"][matched_uid] self.log("Type CBTQ {} ".format(type(CBTQ)), "debug") self.log("CBTQ {} ".format(CBTQ),"debug") while not CBTQ.empty(): cbt = CBTQ.get() self.log("cbt {}".format(cbt),"debug") setup_load = "FORWARDED_CBT" + "#" + "None" + "#" + match_jid msg_payload = json.dumps(cbt) self.sendxmppmsg(match_jid,xmppobj["XMPPObj"],setup_load,msg_payload) self.log("sent out pending cbt for UID {}".format(matched_uid), "debug") return elif msg_type == "FORWARDED_CBT": self.log("____________________HERE-6__________________", "debug") # does not contains target uid payload = json.loads(payload) self.log("payload {}".format(payload), "debug") self.log("____________________HERE-7__________________", "debug") dest_module = payload["dest_module"] self.log("____________________HERE-8__________________", "debug") src_uid = payload["sender_uid"] action = payload["action"] cbtdata = dict(uid=src_uid,data=payload['core_data'],interface_name = interface_name) self.log("cbtdata {}".format(cbtdata),"debug") self.registerCBT(dest_module, action, cbtdata, _tag=src_uid) self.log("Received forwarded CBT from {}".format(sender_jid),"debug") return