-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
87 lines (62 loc) · 2.92 KB
/
utils.py
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
import logging
import time
user_states = {}
def update_user_state(user_id, state):
user_states[user_id] = state
def get_user_state(user_id):
return user_states.get(user_id, None)
def send_message(message, destination, interface):
max_payload_size = 200
for i in range(0, len(message), max_payload_size):
chunk = message[i:i + max_payload_size]
try:
d = interface.sendText(
text=chunk,
destinationId=destination,
wantAck=True,
wantResponse=False
)
destid = get_node_id_from_num(destination, interface)
chunk = chunk.replace('\n', '\\n')
logging.info(f"Sending message to user '{get_node_short_name(destid, interface)}' ({destid}) with sendID {d.id}: \"{chunk}\"")
except Exception as e:
logging.info(f"REPLY SEND ERROR {e.message}")
time.sleep(2)
def get_node_info(interface, short_name):
nodes = [{'num': node_id, 'shortName': node['user']['shortName'], 'longName': node['user']['longName']}
for node_id, node in interface.nodes.items()
if node['user']['shortName'].lower() == short_name]
return nodes
def get_node_id_from_num(node_num, interface):
for node_id, node in interface.nodes.items():
if node['num'] == node_num:
return node_id
return None
def get_node_short_name(node_id, interface):
node_info = interface.nodes.get(node_id)
if node_info:
return node_info['user']['shortName']
return None
def send_bulletin_to_bbs_nodes(board, sender_short_name, subject, content, unique_id, bbs_nodes, interface):
message = f"BULLETIN|{board}|{sender_short_name}|{subject}|{content}|{unique_id}"
for node_id in bbs_nodes:
send_message(message, node_id, interface)
def send_mail_to_bbs_nodes(sender_id, sender_short_name, recipient_id, subject, content, unique_id, bbs_nodes,
interface):
message = f"MAIL|{sender_id}|{sender_short_name}|{recipient_id}|{subject}|{content}|{unique_id}"
logging.info(f"SERVER SYNC: Syncing new mail message {subject} sent from {sender_short_name} to other BBS systems.")
for node_id in bbs_nodes:
send_message(message, node_id, interface)
def send_delete_bulletin_to_bbs_nodes(bulletin_id, bbs_nodes, interface):
message = f"DELETE_BULLETIN|{bulletin_id}"
for node_id in bbs_nodes:
send_message(message, node_id, interface)
def send_delete_mail_to_bbs_nodes(unique_id, bbs_nodes, interface):
message = f"DELETE_MAIL|{unique_id}"
logging.info(f"SERVER SYNC: Sending delete mail sync message with unique_id: {unique_id}")
for node_id in bbs_nodes:
send_message(message, node_id, interface)
def send_channel_to_bbs_nodes(name, url, bbs_nodes, interface):
message = f"CHANNEL|{name}|{url}"
for node_id in bbs_nodes:
send_message(message, node_id, interface)