Skip to content

Commit b65f8d4

Browse files
committed
use curve for bot api
1 parent 06bcba5 commit b65f8d4

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
@@ -649,7 +649,7 @@ def ban_user(mod_user, room, ban_user, also_delete_posts=False):
649649
).fetchone()[0]
650650
)
651651

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

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

665665
conn.execute(

0 commit comments

Comments
 (0)