class DeleteFriendsController(Controller): def __init__(self): self.friendshipservice = KahlaFriendShipApiService() self.checkstatusservice = KahlaSignInStatusCheckService() # 定义参数 def get_options(self): return [ Option('-u', '--username', dest='username'), ] # 处理输入参数, 检查合法性 def run(self, username): # 这条必须编写, 并且带上传入的参数 self.compute(username) # 处理业务逻辑 def main(self, username): if self.checkstatusservice.check() == True: friends = self.friendshipservice.Friends() friendsdata = json.loads(friends.text)["items"] for x in friendsdata: if x["displayName"] == username: r = self.friendshipservice.DeleteFriend(x["userId"]) r = json.loads(r.text) if r["code"] == 0: return "" else: return r["message"] return "The user name you entered is incorrect!" else: return "You are not logged in!"
class GroupsController(Controller): def __init__(self): self.friendshipservice = KahlaFriendShipApiService() self.checkstatusservice = KahlaSignInStatusCheckService() # 定义参数 def get_options(self): return [] # 处理输入参数, 检查合法性 def run(self): # 这条必须编写, 并且带上传入的参数 self.compute() # 处理业务逻辑 def main(self): if self.checkstatusservice.check() == True: friends = self.friendshipservice.Friends() friendsdata = json.loads(friends.text)["items"] datas = [] for x in friendsdata: if x["discriminator"] != "PrivateConversation": datas.append(x["displayName"]) return datas else: return ["You are not logged in!"]
class SearchGroupsController(Controller): def __init__(self): self.friendshipservice = KahlaFriendShipApiService() self.checkstatusservice = KahlaSignInStatusCheckService() # 定义参数 def get_options(self): return [ Option('-si', '--searchinput', dest='searchinput'), ] # 处理输入参数, 检查合法性 def run(self, searchinput): # 这条必须编写, 并且带上传入的参数 self.compute(searchinput) # 处理业务逻辑 def main(self, searchinput): if self.checkstatusservice.check() == True: friends = self.friendshipservice.Friends() friendsdata = json.loads(friends.text)["items"] datas = [] for x in friendsdata: if x["displayName"].lower().find(searchinput.lower()) >= 0: if x["discriminator"] != "PrivateConversation": pingdata = "{0}".format(x["displayName"]) datas.append(pingdata) return datas else: return ["You are not logged in!"]
class ListenController(Controller): def __init__(self): self.apiaddress = KahlaApiAddressService() self.authapiservice = KahlaAuthApiService() self.checkstatusservice = KahlaSignInStatusCheckService() # 定义参数 def get_options(self): return [] # 处理输入参数, 检查合法性 def run(self): # 这条必须编写, 并且带上传入的参数 self.compute() # 处理业务逻辑 def main(self): if self.checkstatusservice.check() == True: r = json.loads(self.authapiservice.InitPusher().text) self.listenerkahla = KahlaWebsocketListener(r["serverPath"]) self.listenerkahla.connect() try: self.listenerkahla.run_forever() return "" except KeyboardInterrupt: self.listenerkahla.close() return "" else: return "You are not logged in!"
class LeaveGroupsController(Controller): def __init__(self): self.friendshipservice = KahlaFriendShipApiService() self.checkstatusservice = KahlaSignInStatusCheckService() # 定义参数 def get_options(self): return [ Option('-g', '--group', dest='group'), ] # 处理输入参数, 检查合法性 def run(self, group): # 这条必须编写, 并且带上传入的参数 self.compute(group) # 处理业务逻辑 def main(self, group): if self.checkstatusservice.check() == True: friends = self.friendshipservice.Friends() friendsdata = json.loads(friends.text)["items"] for x in friendsdata: if x["displayName"] == group: if x["discriminator"] == "GroupConversation": self.friendshipservice.LeaveGroup(x["displayName"]) return "" return "The user name you entered is incorrect!" else: return "You are not logged in!"
class GetMessagesController(Controller): def __init__(self): self.friendshipservice = KahlaFriendShipApiService() self.checkstatusservice = KahlaSignInStatusCheckService() self.conversionservice = KahlaConversationApiService() self.processmessage = ProcessMessage() # 定义参数 def get_options(self): return [ Option('-u', '--username', dest='username'), Option('-t', '--take', dest='take') ] # 处理输入参数, 检查合法性 def run(self, username, take): # 这条必须编写, 并且带上传入的参数 self.compute(username, take) # 处理业务逻辑 def main(self, username, take): if self.checkstatusservice.check() == True: friends = self.friendshipservice.Friends() friendsdata = json.loads(friends.text)["items"] datas = [] for x in friendsdata: if x['displayName'] == username: me = json.loads(self.friendshipservice.Me().text)["value"] r = self.conversionservice.GetMesssages(x['conversationId'], take) resultdata = json.loads(r.text) if resultdata["code"] == 0: for xx in resultdata["items"]: if x["discriminator"] == "PrivateConversation": if xx["senderId"] != me["id"]: result = str(decrypt(bytes(xx["content"], "UTF-8"), bytes(x["aesKey"], "UTF-8")), "UTF-8") result = self.processmessage.processMessage(result) datas.append("{0} | {1}".format(x["displayName"], result)) else: result = str(decrypt(bytes(xx["content"], "UTF-8"), bytes(x["aesKey"], "UTF-8")), "UTF-8") result = self.processmessage.processMessage(result) datas.append("{0} | {1}".format(me["nickName"], result)) else: data = json.loads(self.friendshipservice.UserDetail(xx['senderId']).text)["user"] result = str(decrypt(bytes(xx["content"], "UTF-8"), bytes(x["aesKey"], "UTF-8")), "UTF-8") result = self.processmessage.processMessage(result) datas.append("{0} | {1} | {2}".format(x["displayName"], data["nickName"], result)) return datas else: return ["unknown error!"] return ["Your user name is incorrect!"] else: return ["You are not logged in!"]
class LogoutController(Controller): def __init__(self): self.checksignstatus = KahlaSignInStatusCheckService() # 定义参数 def get_options(self): return [] # 处理输入参数, 检查合法性 def run(self): # 这条必须编写, 并且带上传入的参数 self.compute() # 处理业务逻辑 def main(self): if self.checksignstatus.check() == True: try: os.remove("./user.cookie.bin") return "" except: return "You are not logged in!" else: return "You are not logged in!"
class SendController(Controller): def __init__(self): self.friendshipservice = KahlaFriendShipApiService() self.checkstatusservice = KahlaSignInStatusCheckService() self.conversionservice = KahlaConversationApiService() # 定义参数 def get_options(self): return [ Option('-u', '--username', dest='username'), Option('-m', '--message', dest='message') ] # 处理输入参数, 检查合法性 def run(self, username, message): # 这条必须编写, 并且带上传入的参数 self.compute(username, message) # 处理业务逻辑 def main(self, username, message): if self.checkstatusservice.check() == True: friends = self.friendshipservice.Friends() friendsdata = json.loads(friends.text)["items"] for x in friendsdata: if x['displayName'] == username: message = encrypt(bytes(message, "utf-8"), bytes(x["aesKey"], "utf-8")) r = self.conversionservice.SendMessage(x['conversationId'], message) resultdata = json.loads(r.text) if resultdata["code"] == 0: return "" else: return "The message could not be sent successfully!" return "Your user name is incorrect!" else: return "You are not logged in!"