mirror of
http://aero2k.de/t/repos/urlbot-native.git
synced 2017-09-06 15:25:38 +02:00
add bot detection, stfu if the bot is here. also fix teatimer, events shall use the room of the source.
This commit is contained in:
43
urlbot.py
43
urlbot.py
@@ -5,6 +5,8 @@ The URLBot - ready to strive for desaster in YOUR jabber MUC
|
|||||||
"""
|
"""
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
from common import (
|
from common import (
|
||||||
conf_load, conf_save,
|
conf_load, conf_save,
|
||||||
rate_limit_classes,
|
rate_limit_classes,
|
||||||
@@ -136,8 +138,34 @@ class UrlBot(IdleBot):
|
|||||||
if str is not type(message):
|
if str is not type(message):
|
||||||
message = '\n'.join(message)
|
message = '\n'.join(message)
|
||||||
|
|
||||||
# check other bots, add nospoiler with urls
|
def cached(function, ttl=60):
|
||||||
|
cache = {}
|
||||||
|
ttl = 60
|
||||||
|
now = time.time()
|
||||||
|
|
||||||
|
def wrapper(*args):
|
||||||
|
hash_ = hash(args)
|
||||||
|
if hash_ in cache and cache[args]['time'] < now - ttl:
|
||||||
|
return cache[hash_]['result']
|
||||||
|
else:
|
||||||
|
result = function(*args)
|
||||||
|
cache[hash_] = {}
|
||||||
|
cache[hash_]['time'] = now
|
||||||
|
cache[hash_]['result'] = result
|
||||||
|
return result
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
@cached
|
||||||
|
def get_bots_present(room):
|
||||||
|
print("test!")
|
||||||
|
other_bots = conf_load().get("other_bots", ())
|
||||||
|
users = self.plugin['xep_0045'].getRoster(room)
|
||||||
|
return set(users).intersection(set(other_bots))
|
||||||
|
|
||||||
|
|
||||||
def _prevent_panic(message, room):
|
def _prevent_panic(message, room):
|
||||||
|
"""check other bots, add nospoiler with urls"""
|
||||||
if 'http' in message:
|
if 'http' in message:
|
||||||
other_bots = conf_load().get("other_bots", ())
|
other_bots = conf_load().get("other_bots", ())
|
||||||
users = self.plugin['xep_0045'].getRoster(room)
|
users = self.plugin['xep_0045'].getRoster(room)
|
||||||
@@ -149,15 +177,20 @@ class UrlBot(IdleBot):
|
|||||||
print(message)
|
print(message)
|
||||||
else:
|
else:
|
||||||
if msg_obj:
|
if msg_obj:
|
||||||
message = _prevent_panic(message, msg_obj['from'].bare)
|
# TODO: bot modes off/on/auto... this should be active for "on".
|
||||||
|
# message = _prevent_panic(message, msg_obj['from'].bare)
|
||||||
|
if get_bots_present(msg_obj['from'].bare):
|
||||||
|
return
|
||||||
self.send_message(
|
self.send_message(
|
||||||
mto=msg_obj['from'].bare,
|
mto=msg_obj['from'].bare,
|
||||||
mbody=message,
|
mbody=message,
|
||||||
mtype='groupchat'
|
mtype='groupchat'
|
||||||
)
|
)
|
||||||
else: # unset msg_obj == broadcast
|
else:
|
||||||
for room in self.rooms:
|
for room in self.rooms:
|
||||||
message = _prevent_panic(message, room)
|
# message = _prevent_panic(message, room)
|
||||||
|
if get_bots_present(room):
|
||||||
|
continue
|
||||||
self.send_message(
|
self.send_message(
|
||||||
mto=room,
|
mto=room,
|
||||||
mbody=message,
|
mbody=message,
|
||||||
@@ -266,7 +299,7 @@ class UrlBot(IdleBot):
|
|||||||
if 'event' in action:
|
if 'event' in action:
|
||||||
event = action["event"]
|
event = action["event"]
|
||||||
if 'msg' in event:
|
if 'msg' in event:
|
||||||
register_event(event["time"], self.send_reply, [event['msg']])
|
register_event(event["time"], self.send_reply, [event['msg'], msg_obj])
|
||||||
elif 'command' in event:
|
elif 'command' in event:
|
||||||
command = event["command"]
|
command = event["command"]
|
||||||
if rate_limit(RATE_EVENT):
|
if rate_limit(RATE_EVENT):
|
||||||
|
|||||||
Reference in New Issue
Block a user