mirror of
http://aero2k.de/t/repos/urlbot-native.git
synced 2017-09-06 15:25:38 +02:00
handle multiple rooms; rename chat_write->send_reply, msg->msg_obj
This commit is contained in:
@@ -17,7 +17,7 @@ if '__main__' == __name__:
|
|||||||
config = {
|
config = {
|
||||||
'jid': 'FIXME',
|
'jid': 'FIXME',
|
||||||
'password': 'FIXME',
|
'password': 'FIXME',
|
||||||
'room': 'FIXME',
|
'rooms': ['FIXME'],
|
||||||
|
|
||||||
'src-url': 'FIXME',
|
'src-url': 'FIXME',
|
||||||
|
|
||||||
|
|||||||
36
plugins.py
36
plugins.py
@@ -105,9 +105,9 @@ def parse_skynet(args):
|
|||||||
'msg': '''I'm an independent bot and have nothing to do with other artificial intelligence systems!'''
|
'msg': '''I'm an independent bot and have nothing to do with other artificial intelligence systems!'''
|
||||||
}
|
}
|
||||||
|
|
||||||
def data_parse_other(msg):
|
def data_parse_other(msg_obj):
|
||||||
data = msg['body']
|
data = msg_obj['body']
|
||||||
reply_user = msg['mucnick']
|
reply_user = msg_obj['mucnick']
|
||||||
|
|
||||||
for p in plugins['parse']:
|
for p in plugins['parse']:
|
||||||
if ratelimit_exceeded(p['ratelimit_class']):
|
if ratelimit_exceeded(p['ratelimit_class']):
|
||||||
@@ -132,7 +132,7 @@ def data_parse_other(msg):
|
|||||||
if None != ret:
|
if None != ret:
|
||||||
if 'msg' in list(ret.keys()):
|
if 'msg' in list(ret.keys()):
|
||||||
ratelimit_touch(RATE_CHAT)
|
ratelimit_touch(RATE_CHAT)
|
||||||
chat_write(ret['msg'])
|
send_reply(ret['msg'], msg_obj)
|
||||||
|
|
||||||
def command_command(args):
|
def command_command(args):
|
||||||
if 'register' == args:
|
if 'register' == args:
|
||||||
@@ -353,7 +353,7 @@ def command_teatimer(args):
|
|||||||
return {
|
return {
|
||||||
'name': 'teatimer',
|
'name': 'teatimer',
|
||||||
'desc': 'sets a tea timer to $1 or currently %d seconds' % conf('tea_steep_time'),
|
'desc': 'sets a tea timer to $1 or currently %d seconds' % conf('tea_steep_time'),
|
||||||
'args': ('reply_user', 'argv0', 'argv1'),
|
'args': ('reply_user', 'msg_obj', 'argv0', 'argv1'),
|
||||||
'ratelimit_class': RATE_GLOBAL
|
'ratelimit_class': RATE_GLOBAL
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -379,7 +379,7 @@ def command_teatimer(args):
|
|||||||
'msg': args['reply_user'] + ': time format error: ' + str(e)
|
'msg': args['reply_user'] + ': time format error: ' + str(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
register_event(ready, chat_write, args['reply_user'] + ': Your tea is ready!')
|
register_event(ready, send_reply, (args['reply_user'] + ': Your tea is ready!', args['msg_obj']))
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'msg': args['reply_user'] + ': Tea timer set to %s' % time.strftime(
|
'msg': args['reply_user'] + ': Tea timer set to %s' % time.strftime(
|
||||||
@@ -464,8 +464,8 @@ def command_else(args):
|
|||||||
'msg': args['reply_user'] + ''': I'm a bot (highlight me with 'info' for more information).'''
|
'msg': args['reply_user'] + ''': I'm a bot (highlight me with 'info' for more information).'''
|
||||||
}
|
}
|
||||||
|
|
||||||
def data_parse_commands(msg):
|
def data_parse_commands(msg_obj):
|
||||||
data = msg['body']
|
data = msg_obj['body']
|
||||||
words = data.split(' ')
|
words = data.split(' ')
|
||||||
|
|
||||||
if 2 > len(words): # need at least two words
|
if 2 > len(words): # need at least two words
|
||||||
@@ -480,7 +480,7 @@ def data_parse_commands(msg):
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
reply_user = msg['mucnick']
|
reply_user = msg_obj['mucnick']
|
||||||
(argv0, argv1) = (None, None)
|
(argv0, argv1) = (None, None)
|
||||||
if 1 < len(words):
|
if 1 < len(words):
|
||||||
argv0 = words[1]
|
argv0 = words[1]
|
||||||
@@ -506,6 +506,8 @@ def data_parse_commands(msg):
|
|||||||
args['cmd_list'] = cmds
|
args['cmd_list'] = cmds
|
||||||
elif 'reply_user' == a:
|
elif 'reply_user' == a:
|
||||||
args['reply_user'] = reply_user
|
args['reply_user'] = reply_user
|
||||||
|
elif 'msg_obj' == a:
|
||||||
|
args['msg_obj'] = msg_obj
|
||||||
elif 'argv0' == a:
|
elif 'argv0' == a:
|
||||||
args['argv0'] = argv0
|
args['argv0'] = argv0
|
||||||
elif 'argv1' == a:
|
elif 'argv1' == a:
|
||||||
@@ -522,14 +524,14 @@ def data_parse_commands(msg):
|
|||||||
if ratelimit_exceeded(RATE_CHAT):
|
if ratelimit_exceeded(RATE_CHAT):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
chat_write(ret['msg'])
|
send_reply(ret['msg'], msg_obj)
|
||||||
else:
|
else:
|
||||||
for line in ret['msg']:
|
for line in ret['msg']:
|
||||||
ratelimit_touch(RATE_CHAT)
|
ratelimit_touch(RATE_CHAT)
|
||||||
if ratelimit_exceeded(RATE_CHAT):
|
if ratelimit_exceeded(RATE_CHAT):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
chat_write(line)
|
send_reply(line, msg_obj)
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -541,9 +543,9 @@ def data_parse_commands(msg):
|
|||||||
if 'msg' in list(ret.keys()):
|
if 'msg' in list(ret.keys()):
|
||||||
if list is type(ret['msg']):
|
if list is type(ret['msg']):
|
||||||
for m in ret['msg']:
|
for m in ret['msg']:
|
||||||
chat_write(m)
|
send_reply(m, msg_obj)
|
||||||
else:
|
else:
|
||||||
chat_write(ret['msg'])
|
send_reply(ret['msg'], msg_obj)
|
||||||
|
|
||||||
funcs = {}
|
funcs = {}
|
||||||
funcs['parse'] = (parse_mental_ill, parse_skynet, parse_debbug, parse_cve)
|
funcs['parse'] = (parse_mental_ill, parse_skynet, parse_debbug, parse_cve)
|
||||||
@@ -556,8 +558,8 @@ funcs['command'] = (
|
|||||||
_dir = dir()
|
_dir = dir()
|
||||||
|
|
||||||
if debug_enabled():
|
if debug_enabled():
|
||||||
def _chat_write(a):
|
def _send_reply(a, msg_obj):
|
||||||
logger('chat_write', a)
|
logger('send_reply[%s]' % msg_obj, a)
|
||||||
|
|
||||||
def _conf(a):
|
def _conf(a):
|
||||||
return 'bot'
|
return 'bot'
|
||||||
@@ -569,9 +571,9 @@ if debug_enabled():
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
chat_write
|
send_reply
|
||||||
except NameError:
|
except NameError:
|
||||||
chat_write = _chat_write
|
send_reply = _send_reply
|
||||||
|
|
||||||
try:
|
try:
|
||||||
conf
|
conf
|
||||||
|
|||||||
52
urlbot.py
52
urlbot.py
@@ -102,21 +102,21 @@ def extract_title(url):
|
|||||||
|
|
||||||
return (-1, 'error')
|
return (-1, 'error')
|
||||||
|
|
||||||
def chat_write(message):
|
def send_reply(message, msg_obj):
|
||||||
set_conf('request_counter', conf('request_counter') + 1)
|
set_conf('request_counter', conf('request_counter') + 1)
|
||||||
|
|
||||||
for m in message:
|
for m in message:
|
||||||
if 0x20 > ord(m):
|
if 0x20 > ord(m):
|
||||||
logger('warn', 'strange char 0x%02x in chat_write(message), skipping' % ord(m))
|
logger('warn', 'strange char 0x%02x in send_reply(message), skipping' % ord(m))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if debug_enabled():
|
if debug_enabled():
|
||||||
print(message)
|
print(message)
|
||||||
else:
|
else:
|
||||||
xmpp.send_message(
|
xmpp.send_message(
|
||||||
mto=conf('room'),
|
mto=msg_obj['from'].bare,
|
||||||
mbody=message,
|
mbody=message,
|
||||||
mtype='groupchat'
|
mtype=msg_obj['type']
|
||||||
)
|
)
|
||||||
|
|
||||||
def ratelimit_touch(ignored=None): # FIXME: separate counters
|
def ratelimit_touch(ignored=None): # FIXME: separate counters
|
||||||
@@ -134,7 +134,8 @@ def ratelimit_exceeded(ignored=None): # FIXME: separate counters
|
|||||||
if (time.time() - first) < conf('hist_max_time'):
|
if (time.time() - first) < conf('hist_max_time'):
|
||||||
if hist_flag:
|
if hist_flag:
|
||||||
hist_flag = False
|
hist_flag = False
|
||||||
chat_write('(rate limited to %d messages in %d seconds, try again at %s)' %(conf('hist_max_count'), conf('hist_max_time'), time.strftime('%T %Z', time.localtime(hist_ts[0] + conf('hist_max_time')))))
|
# FIXME: this is very likely broken now
|
||||||
|
send_reply('(rate limited to %d messages in %d seconds, try again at %s)' %(conf('hist_max_count'), conf('hist_max_time'), time.strftime('%T %Z', time.localtime(hist_ts[0] + conf('hist_max_time')))))
|
||||||
|
|
||||||
logger('warn', 'rate limiting exceeded: ' + pickle.dumps(hist_ts))
|
logger('warn', 'rate limiting exceeded: ' + pickle.dumps(hist_ts))
|
||||||
return True
|
return True
|
||||||
@@ -142,13 +143,13 @@ def ratelimit_exceeded(ignored=None): # FIXME: separate counters
|
|||||||
hist_flag = True
|
hist_flag = True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def extract_url(data):
|
def extract_url(data, msg_obj):
|
||||||
ret = None
|
ret = None
|
||||||
result = re.findall("(https?://[^\s>]+)", data)
|
result = re.findall("(https?://[^\s>]+)", data)
|
||||||
if result:
|
if result:
|
||||||
for url in result:
|
for url in result:
|
||||||
ratelimit_touch()
|
ratelimit_touch()
|
||||||
if ratelimit_exceeded():
|
if ratelimit_exceeded(msg_obj):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
flag = False
|
flag = False
|
||||||
@@ -203,7 +204,7 @@ def extract_url(data):
|
|||||||
message = message.replace('\n', '\\n')
|
message = message.replace('\n', '\\n')
|
||||||
|
|
||||||
logger('info', 'printing ' + message)
|
logger('info', 'printing ' + message)
|
||||||
chat_write(message)
|
send_reply(message, msg_obj)
|
||||||
ret = True
|
ret = True
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
@@ -215,8 +216,8 @@ def parse_pn(data):
|
|||||||
logger('warn', 'received PN: ' + data)
|
logger('warn', 'received PN: ' + data)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def handle_msg(msg):
|
def handle_msg(msg_obj):
|
||||||
content = msg['body']
|
content = msg_obj['body']
|
||||||
|
|
||||||
# FIXME: still needed?
|
# FIXME: still needed?
|
||||||
if 'has set the subject to:' in content:
|
if 'has set the subject to:' in content:
|
||||||
@@ -230,16 +231,16 @@ def handle_msg(msg):
|
|||||||
logger('info', 'silenced, this is my own log')
|
logger('info', 'silenced, this is my own log')
|
||||||
return
|
return
|
||||||
|
|
||||||
if True != extract_url(content):
|
if True != extract_url(content, msg_obj):
|
||||||
plugins.data_parse_commands(msg)
|
plugins.data_parse_commands(msg_obj)
|
||||||
plugins.data_parse_other(msg)
|
plugins.data_parse_other(msg_obj)
|
||||||
return
|
return
|
||||||
|
|
||||||
class bot(ClientXMPP):
|
class bot(ClientXMPP):
|
||||||
def __init__(self, jid, password, room, nick):
|
def __init__(self, jid, password, rooms, nick):
|
||||||
ClientXMPP.__init__(self, jid, password)
|
ClientXMPP.__init__(self, jid, password)
|
||||||
|
|
||||||
self.room = room
|
self.rooms = rooms
|
||||||
self.nick = nick
|
self.nick = nick
|
||||||
|
|
||||||
self.add_event_handler('session_start', self.session_start)
|
self.add_event_handler('session_start', self.session_start)
|
||||||
@@ -249,23 +250,24 @@ class bot(ClientXMPP):
|
|||||||
self.get_roster()
|
self.get_roster()
|
||||||
self.send_presence()
|
self.send_presence()
|
||||||
|
|
||||||
self.plugin['xep_0045'].joinMUC(
|
for room in self.rooms:
|
||||||
self.room,
|
self.plugin['xep_0045'].joinMUC(
|
||||||
self.nick,
|
room,
|
||||||
wait=True
|
self.nick,
|
||||||
)
|
wait=True
|
||||||
|
)
|
||||||
|
|
||||||
def muc_message(self, msg):
|
def muc_message(self, msg_obj):
|
||||||
# don't talk to yourself
|
# don't talk to yourself
|
||||||
if msg['mucnick'] == self.nick:
|
if msg_obj['mucnick'] == self.nick:
|
||||||
return
|
return
|
||||||
|
|
||||||
return handle_msg(msg)
|
return handle_msg(msg_obj)
|
||||||
|
|
||||||
if '__main__' == __name__:
|
if '__main__' == __name__:
|
||||||
import plugins
|
import plugins
|
||||||
|
|
||||||
plugins.chat_write = chat_write
|
plugins.send_reply = send_reply
|
||||||
plugins.ratelimit_exceeded = ratelimit_exceeded
|
plugins.ratelimit_exceeded = ratelimit_exceeded
|
||||||
plugins.ratelimit_touch = ratelimit_touch
|
plugins.ratelimit_touch = ratelimit_touch
|
||||||
|
|
||||||
@@ -281,7 +283,7 @@ if '__main__' == __name__:
|
|||||||
xmpp = bot(
|
xmpp = bot(
|
||||||
jid=conf('jid'),
|
jid=conf('jid'),
|
||||||
password=conf('password'),
|
password=conf('password'),
|
||||||
room=conf('room'),
|
rooms=conf('rooms'),
|
||||||
nick=conf('bot_user')
|
nick=conf('bot_user')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user