Skip to content

Commit d1ccb04

Browse files
committed
use curve for bot api
1 parent c207b19 commit d1ccb04

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

sogs/events.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ def event_name_valid(eventname):
2525
return eventname.lower() in EVENTS
2626

2727

28+
def _user_from_conn(conn, automod=True):
29+
""" make a model.User from a connection using it's curve pubkey as the session id, if automod is True we auto promote the bot to a moderator if it isn't already"""
30+
user = User(session_id='05{}'.format(conn.pubkey.encode('hex')))
31+
if automod and not user.global_moderator:
32+
user.set_moderator()
33+
return user
34+
35+
2836
def _sub_conn_to_event(name, conn):
2937
""" subscribe a connection to an event type """
3038
if not event_name_valid(name):
@@ -100,8 +108,9 @@ def _handle_mod_ban(msg):
100108
room_id = int(parts[1].decode('utf-8'))
101109
user_id = int(parts[0].decode('utf-8'))
102110
room = model.Room(id=room_id)
103-
user = model.User(id=user_id)
104-
if not model.ban_user(None, room, user):
111+
ban_user = model.User(id=user_id)
112+
bot = _user_from_conn(msg.conn)
113+
if not model.ban_user(bot, room, ban_user):
105114
raise Exception("user not banned")
106115

107116

@@ -140,8 +149,7 @@ def start():
140149
_bot_category.add_request_handler('ban', lambda msg: _handle_request(_handle_mod_ban, msg))
141150

142151
for addr in config.API_ADDRS:
143-
# TODO: implement curve?
144-
_mq.listen(addr, False)
152+
_mq.listen(addr, True)
145153
_mq.start()
146154

147155

sogs/model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ def ban_user(mod_user, room, ban_user, also_delete_posts=False):
648648
).fetchone()[0]
649649
)
650650

651-
if mod_user and is_mod and not check_permission(mod_user, room, admin=True):
651+
if is_mod and not check_permission(mod_user, room, admin=True):
652652
app.logger.warn(
653653
"Cannot ban {} from {}: the ban target is a room moderator, "
654654
"but the ban initiator ({}) is not an admin".format(
@@ -657,8 +657,8 @@ def ban_user(mod_user, room, ban_user, also_delete_posts=False):
657657
)
658658
return False
659659

660-
if mod_user is None and is_mod:
661-
app.logger.warn("bots cannot ban moderators")
660+
if mod_user is None or not mod_user.global_moderator:
661+
# sanity check just in case
662662
return False
663663

664664
conn.execute(

0 commit comments

Comments
 (0)