yuuki/libs/tools.py

354 lines
12 KiB
Python
Raw Normal View History

2020-02-09 17:09:13 +08:00
# -*- coding: utf-8 -*-
"""
Yuuki_Libs
(c) 2020 Star Inc.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
import os
import sys
import json
import ntpath
import random
import requests
import traceback
from yuuki_core.ttypes import OpType, MIDType, ContentType, Group, Message
2020-02-10 14:36:32 +08:00
2020-02-09 17:09:13 +08:00
class Yuuki_StaticTools:
@staticmethod
2020-02-12 15:01:03 +08:00
def sybGetGroupCreator(groupInfo):
"""
Get the LINE Group Creator
:param groupInfo: LINE Group
:return: LINE Contact
"""
if groupInfo.creator is None:
contact = groupInfo.members[0]
2020-02-09 17:09:13 +08:00
else:
2020-02-12 15:01:03 +08:00
contact = groupInfo.creator
2020-02-09 17:09:13 +08:00
return contact
@staticmethod
def readCommandLine(msgs):
2020-02-12 15:01:03 +08:00
"""
Read string list as a command line
:param msgs: List of strings
:return: string
"""
2020-02-12 16:01:07 +08:00
reply_msg = " ".join(msgs).lstrip()
2020-02-12 15:01:03 +08:00
return reply_msg
2020-02-09 17:09:13 +08:00
@staticmethod
def errorReport():
2020-02-12 15:01:03 +08:00
"""
Report errors as tuple
:return: tuple
"""
2020-02-09 17:09:13 +08:00
err1, err2, err3 = sys.exc_info()
traceback.print_tb(err3)
tb_info = traceback.extract_tb(err3)
filename, line, func, text = tb_info[-1]
2020-02-12 16:01:07 +08:00
ErrorInfo = "occurred in\n{}\n\non line {}\nin statement {}".format(filename, line, text)
2020-02-09 17:09:13 +08:00
return err1, err2, err3, ErrorInfo
@staticmethod
def securityForWhere(ncMessage):
2020-02-12 15:01:03 +08:00
"""
Return arguments for security tasks
:param ncMessage: Operation
:return: tuple
"""
2020-02-12 20:26:47 +08:00
if ncMessage.type == OpType.NOTIFIED_UPDATE_GROUP or\
ncMessage.type == OpType.NOTIFIED_INVITE_INTO_GROUP or\
ncMessage.type == OpType.NOTIFIED_ACCEPT_GROUP_INVITATION or\
ncMessage.type == OpType.NOTIFIED_KICKOUT_FROM_GROUP:
return {
"GroupID": ncMessage.param1,
"Action": ncMessage.param2,
"Another": ncMessage.param3,
}
2020-02-09 17:09:13 +08:00
@staticmethod
def dictShuffle(dict_object, requirement=None):
2020-02-12 15:01:03 +08:00
"""
Shuffle dicts
:param dict_object: dict
:param requirement: list
:return: dict
"""
2020-02-09 17:09:13 +08:00
dict_key = [key for key in dict_object]
random.shuffle(dict_key)
result = {}
for key in dict_key:
if requirement is None:
result[key] = dict_object[key]
else:
if key in requirement:
result[key] = dict_object[key]
return result
@staticmethod
def sendToWho(ncMessage):
2020-02-12 15:01:03 +08:00
"""
Get who to send with Operation
:param ncMessage: Operation
:return: string
"""
2020-02-09 17:09:13 +08:00
if ncMessage.message.toType == MIDType.USER:
return ncMessage.message.from_
elif ncMessage.message.toType == MIDType.ROOM:
return ncMessage.message.to
elif ncMessage.message.toType == MIDType.GROUP:
return ncMessage.message.to
class Yuuki_DynamicTools:
def __init__(self, Yuuki):
self.Yuuki = Yuuki
2020-02-10 14:36:32 +08:00
self.Yuuki_StaticTools = Yuuki_StaticTools()
2020-02-09 17:09:13 +08:00
def getClient(self, userId):
2020-02-12 15:01:03 +08:00
"""
Get client by account userId
:param userId: string
:return: TalkServiceClient
"""
2020-02-09 17:09:13 +08:00
if self.Yuuki.Threading:
if userId == self.Yuuki.MyMID:
(client, _) = self.Yuuki.Connect.connect()
return client
else:
return self.Yuuki.Connect.helperThreadConnect(userId)
else:
Accounts = [self.Yuuki.client] + self.Yuuki.Connect.helper
for count, AccountUserId in enumerate(self.Yuuki.AllAccountIds):
if AccountUserId == userId:
return Accounts[count]
def checkInInvitationList(self, ncMessage, userId=None):
2020-02-12 15:01:03 +08:00
"""
Check If userId in Invitation List
:param ncMessage: Operation
:param userId: string
:return: boolean
"""
2020-02-09 17:09:13 +08:00
if userId is None:
userId = self.Yuuki.MyMID
if ncMessage.param3 == userId:
inList = True
elif "\x1e" in ncMessage.param3:
if userId in ncMessage.param3.split("\x1e"):
inList = True
else:
inList = False
else:
inList = False
return inList
2020-02-12 15:01:03 +08:00
def changeGroupUrlStatus(self, groupInfo, status, userId=None):
"""
Change LINE Group URL Status
:param groupInfo: Line Group
:param status: boolean
:param userId: string
:return: None
"""
2020-02-09 17:09:13 +08:00
result = Group()
2020-02-12 15:01:03 +08:00
for key in groupInfo.__dict__:
2020-02-09 17:09:13 +08:00
if key != "members" or key != "invitee":
2020-02-12 15:01:03 +08:00
result.__dict__[key] = groupInfo.__dict__[key]
result.preventJoinByTicket = not status
2020-02-09 17:09:13 +08:00
if userId is not None:
2020-02-10 14:36:32 +08:00
self.getClient(userId).updateGroup(self.Yuuki.Seq, result)
2020-02-09 17:09:13 +08:00
else:
2020-02-12 15:01:03 +08:00
self.getClient(self.Yuuki.MyMID).updateGroup(self.Yuuki.Seq, result)
2020-02-09 17:09:13 +08:00
def configSecurityStatus(self, groupId, status):
2020-02-12 15:01:03 +08:00
"""
Configure LINE Group Security Status for Yuuki
:param groupId: string
:param status: boolean
:return: None
"""
2020-02-09 17:09:13 +08:00
group_status = self.Yuuki.data.SEGrouptype
if 0 in status:
group_status[OpType.NOTIFIED_UPDATE_GROUP] = True
if 1 in status:
group_status[OpType.NOTIFIED_INVITE_INTO_GROUP] = True
if 2 in status:
group_status[OpType.NOTIFIED_ACCEPT_GROUP_INVITATION] = True
if 3 in status:
group_status[OpType.NOTIFIED_KICKOUT_FROM_GROUP] = True
self.Yuuki.data.updateData(["Group", groupId, "SEGroup"], group_status)
def cleanMyGroupInvitations(self):
2020-02-12 15:01:03 +08:00
"""
Clean personal group invitations for LINE account
:return: None
"""
2020-02-10 14:36:32 +08:00
for client in [self.getClient(self.Yuuki.MyMID)] + self.Yuuki.Connect.helper:
2020-02-09 17:09:13 +08:00
for cleanInvitations in client.getGroupIdsInvited():
client.acceptGroupInvitation(self.Yuuki.Seq, cleanInvitations)
client.leaveGroup(self.Yuuki.Seq, cleanInvitations)
def getContact(self, userId):
2020-02-12 15:01:03 +08:00
"""
Get LINE Contact information with userId
:param userId: string
:return: LINE Contact
"""
2020-02-09 17:09:13 +08:00
if len(userId) == len(self.Yuuki.MyMID) and userId[0] == "u":
2020-02-12 15:01:03 +08:00
# noinspection PyBroadException
2020-02-09 17:09:13 +08:00
try:
2020-02-12 15:01:03 +08:00
contactInfo = self.getClient(self.Yuuki.MyMID).getContact(userId)
2020-02-09 17:09:13 +08:00
except:
contactInfo = False
else:
contactInfo = False
return contactInfo
2020-02-12 15:01:03 +08:00
def getGroupTicket(self, groupId, userId, renew=False):
"""
Get LINE Group Ticket with groupId and userId
:param groupId: string
:param userId: string
:param renew: boolean
:return: string
"""
2020-02-09 17:09:13 +08:00
GroupTicket = ""
2020-02-12 15:01:03 +08:00
GroupData = self.Yuuki.data.getGroup(groupId)
2020-02-09 17:09:13 +08:00
if "GroupTicket" in GroupData:
if GroupData["GroupTicket"].get(userId) is not None:
GroupTicket = GroupData["GroupTicket"].get(userId)
else:
assert "Error JSON data type - GroupTicket"
if GroupTicket == "" or renew:
2020-02-12 15:01:03 +08:00
GroupTicket = self.getClient(userId).reissueGroupTicket(groupId)
2020-02-10 14:36:32 +08:00
self.Yuuki.data.updateData(
2020-02-12 15:01:03 +08:00
["Group", groupId, "GroupTicket", userId], GroupTicket)
2020-02-09 17:09:13 +08:00
return GroupTicket
def limitReset(self):
2020-02-12 15:01:03 +08:00
"""
Reset Yuuki modify LINE Group Member List limits
:return: None
"""
2020-02-09 17:09:13 +08:00
for userId in self.Yuuki.AllAccountIds:
2020-02-10 14:36:32 +08:00
self.Yuuki.data.updateData(
["LimitInfo", "KickLimit", userId], self.Yuuki.KickLimit)
self.Yuuki.data.updateData(
["LimitInfo", "CancelLimit", userId], self.Yuuki.CancelLimit)
2020-02-09 17:09:13 +08:00
2020-02-12 15:01:03 +08:00
def modifyGroupMemberList(self, action, groupInfo, userId, exceptUserId=None):
"""
Modify LINE Group Member List
2020-02-12 15:14:09 +08:00
:param action: integer (1->kick 2->cancel)
2020-02-12 15:01:03 +08:00
:param groupInfo: LINE Group
:param userId: string
:param exceptUserId: List of userId
:return: string
"""
2020-02-12 15:14:09 +08:00
actions = {1: "KickLimit", 2: "CancelLimit"}
assert action in actions, "Invalid action code"
if len(self.Yuuki.Connect.helper) >= 1:
members = [member.mid for member in groupInfo.members if member.mid in self.Yuuki.AllAccountIds]
accounts = self.Yuuki_StaticTools.dictShuffle(
self.Yuuki.data.getData(["LimitInfo", actions[action]]), members)
if len(accounts) == 0:
return "None"
if exceptUserId:
accounts[exceptUserId] = -1
helper = max(accounts, key=accounts.get)
else:
if exceptUserId == self.Yuuki.MyMID:
return "None"
helper = self.Yuuki.MyMID
actions_func = {
1: self.getClient(helper).cancelGroupInvitation,
2: self.getClient(helper).cancelGroupInvitation
}
Limit = self.Yuuki.data.getData(["LimitInfo", actions[action], helper])
if Limit > 0:
actions_func[action](self.Yuuki.Seq, groupInfo.id, [userId])
self.Yuuki.data.limitDecrease(actions[action], helper)
else:
self.sendText(groupInfo.id, self.Yuuki.get_text("Cancel Limit."))
return helper
2020-02-09 17:09:13 +08:00
2020-02-12 15:01:03 +08:00
def sendText(self, send_to, msg):
"""
Send text to LINE Chat
:param send_to: string
:param msg: string
:return: None
"""
message = Message(to=send_to, text=msg)
2020-02-10 14:36:32 +08:00
self.getClient(self.Yuuki.MyMID).sendMessage(self.Yuuki.Seq, message)
2020-02-09 17:09:13 +08:00
2020-02-12 15:01:03 +08:00
def sendUser(self, send_to, userId):
"""
Send LINE contact to LINE Chat
:param send_to: string
:param userId: string
:return: None
"""
2020-02-09 17:09:13 +08:00
message = Message(
contentType=ContentType.CONTACT,
text='',
contentMetadata={
'mid': userId,
'displayName': 'LINE User',
},
2020-02-12 15:01:03 +08:00
to=send_to
2020-02-09 17:09:13 +08:00
)
2020-02-10 14:36:32 +08:00
self.getClient(self.Yuuki.MyMID).sendMessage(self.Yuuki.Seq, message)
2020-02-09 17:09:13 +08:00
def sendMedia(self, send_to, send_type, path):
2020-02-12 15:01:03 +08:00
"""
Send media file to LINE Chat
:param send_to: string
:param send_type: string
:param path: string
:return: None
"""
2020-02-09 17:09:13 +08:00
if os.path.exists(path):
file_name = ntpath.basename(path)
file_size = len(open(path, 'rb').read())
message = Message(to=send_to, text=None)
message.contentType = send_type
message.contentPreview = None
message.contentMetadata = {
'FILE_NAME': str(file_name),
'FILE_SIZE': str(file_size),
}
if send_type == ContentType.FILE:
media_name = file_name
else:
media_name = 'media'
2020-02-10 14:36:32 +08:00
message_id = self.getClient(self.Yuuki.MyMID).sendMessage(
self.Yuuki.Seq, message).id
2020-02-09 17:09:13 +08:00
files = {
'file': open(path, 'rb'),
}
params = {
'name': media_name,
'oid': message_id,
'size': file_size,
'type': ContentType._VALUES_TO_NAMES[send_type].lower(),
'ver': '1.0',
}
data = {
'params': json.dumps(params)
}
url = self.Yuuki.LINE_Media_server + '/talk/m/upload.nhn'
2020-02-10 14:36:32 +08:00
r = requests.post(
url, headers=self.Yuuki.connectHeader, data=data, files=files)
2020-02-09 17:09:13 +08:00
if r.status_code != 201:
2020-02-10 14:36:32 +08:00
self.sendText(send_to, "Error!")