mirror of
http://aero2k.de/t/repos/urlbot-native.git
synced 2017-09-06 15:25:38 +02:00
port urlbot: remove mcabber specific input stuff
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
.*swp
|
.*swp
|
||||||
*.pyc
|
*.pyc
|
||||||
local_config.py
|
local_config.py
|
||||||
|
urlbot.persistent
|
||||||
|
|||||||
49
bot-xmppy.py
49
bot-xmppy.py
@@ -1,49 +0,0 @@
|
|||||||
#!/usr/bin/python
|
|
||||||
|
|
||||||
import xmpp
|
|
||||||
from local_config import conf
|
|
||||||
|
|
||||||
import time
|
|
||||||
t = -time.time()
|
|
||||||
|
|
||||||
def message_handler(connect_object, message_node):
|
|
||||||
# hopefully the backlog is processed in this time
|
|
||||||
# FIXME: find a better way.
|
|
||||||
if (t + time.time() < 1):
|
|
||||||
return None
|
|
||||||
|
|
||||||
msg_from = message_node.getFrom().getResource()
|
|
||||||
msg_body = message_node.getBody()
|
|
||||||
|
|
||||||
if not type(msg_body) in [str, unicode]:
|
|
||||||
return None
|
|
||||||
|
|
||||||
if msg_body.startswith(conf('bot_user')):
|
|
||||||
connect_object.send(
|
|
||||||
xmpp.protocol.Message(
|
|
||||||
to=conf('room'),
|
|
||||||
body='hello %s!' % msg_from,
|
|
||||||
typ='groupchat'
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
|
||||||
print '%20s: %s' %(msg_from, msg_body)
|
|
||||||
except Exception as e:
|
|
||||||
print e
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
jid = xmpp.protocol.JID(conf('jid'))
|
|
||||||
|
|
||||||
client = xmpp.Client(jid.getDomain(), debug=[])
|
|
||||||
client.connect()
|
|
||||||
client.auth(jid.getNode(), conf('password'))
|
|
||||||
client.RegisterHandler('message', message_handler)
|
|
||||||
|
|
||||||
client.send(xmpp.Presence(to=(conf('room') + '/' + conf('bot_user'))))
|
|
||||||
|
|
||||||
while (t + time.time()) < 30:
|
|
||||||
client.Process(1)
|
|
||||||
|
|
||||||
client.disconnect()
|
|
||||||
@@ -20,7 +20,6 @@ delay = 0.100 # seconds
|
|||||||
basedir = '.'
|
basedir = '.'
|
||||||
if 2 == len(sys.argv): basedir = sys.argv[1]
|
if 2 == len(sys.argv): basedir = sys.argv[1]
|
||||||
|
|
||||||
event_files_dir = os.path.join(basedir, conf('path_event_files'))
|
|
||||||
fifo_path = os.path.join(basedir, conf('path_cmdfifo'))
|
fifo_path = os.path.join(basedir, conf('path_cmdfifo'))
|
||||||
|
|
||||||
def debug_enabled():
|
def debug_enabled():
|
||||||
6
urlbot-mcabber/.gitignore
vendored
6
urlbot-mcabber/.gitignore
vendored
@@ -1,6 +0,0 @@
|
|||||||
.*swp
|
|
||||||
*.pyc
|
|
||||||
cmdfifo
|
|
||||||
logs/
|
|
||||||
event_files/
|
|
||||||
urlbot.persistent
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
#!/usr/bin/python3
|
|
||||||
|
|
||||||
import time, sys
|
|
||||||
|
|
||||||
def _logger(a, b): sys.stderr.write('logger: %s::%s\n' %(a, b))
|
|
||||||
try: logger
|
|
||||||
except NameError: logger = _logger
|
|
||||||
|
|
||||||
if '__main__' == __name__:
|
|
||||||
print('''this is a config file, which is not meant to be executed''')
|
|
||||||
exit(-1)
|
|
||||||
|
|
||||||
config = {
|
|
||||||
'src-url': 'FIXME',
|
|
||||||
|
|
||||||
'bot_user': 'urlbot',
|
|
||||||
'bot_owner': 'FIXME',
|
|
||||||
|
|
||||||
'hist_max_count': 5,
|
|
||||||
'hist_max_time': 10 * 60,
|
|
||||||
|
|
||||||
'uptime': -time.time(),
|
|
||||||
'request_counter': 0,
|
|
||||||
|
|
||||||
'path_event_files': 'event_files',
|
|
||||||
'path_cmdfifo': 'cmdfifo',
|
|
||||||
'persistent_storage': 'urlbot.persistent',
|
|
||||||
|
|
||||||
'url_blacklist': [
|
|
||||||
r'^.*heise\.de/[^/]+/meldung/.*$',
|
|
||||||
r'^.*wikipedia\.org/wiki/.*$'
|
|
||||||
],
|
|
||||||
|
|
||||||
# the "dice" feature will use more efficient random data (0) for given users
|
|
||||||
'enhanced-random-user': ( 'FIXME', 'FIXME' ),
|
|
||||||
|
|
||||||
'tea_steep_time': (3*60 + 40),
|
|
||||||
|
|
||||||
'image_preview': True
|
|
||||||
}
|
|
||||||
|
|
||||||
def conf(val):
|
|
||||||
if val in list(config.keys()):
|
|
||||||
return config[val]
|
|
||||||
logger('warn', 'conf(): unknown key ' + str(val))
|
|
||||||
return None
|
|
||||||
|
|
||||||
def set_conf(key, val):
|
|
||||||
config[key] = val
|
|
||||||
return None
|
|
||||||
@@ -3,10 +3,30 @@
|
|||||||
|
|
||||||
import sys, os, stat, re, time, pickle, random
|
import sys, os, stat, re, time, pickle, random
|
||||||
import urllib.request, urllib.parse, urllib.error, html.parser
|
import urllib.request, urllib.parse, urllib.error, html.parser
|
||||||
from local_config import conf, set_conf
|
|
||||||
from common import *
|
from common import *
|
||||||
from strsim import str_sim
|
from strsim import str_sim
|
||||||
|
|
||||||
|
try:
|
||||||
|
from local_config import conf, set_conf
|
||||||
|
except ImportError:
|
||||||
|
import sys
|
||||||
|
sys.stderr.write('''
|
||||||
|
%s: E: local_config.py isn't tracked because of included secrets and
|
||||||
|
%s site specific configurations. Rename local_config.py.skel and
|
||||||
|
%s adjust to you needs.
|
||||||
|
'''[1:] % (
|
||||||
|
sys.argv[0],
|
||||||
|
' ' * len(sys.argv[0]),
|
||||||
|
' ' * len(sys.argv[0])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
sys.exit(-1)
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from sleekxmpp import ClientXMPP
|
||||||
|
|
||||||
# rate limiting to 5 messages per 10 minutes
|
# rate limiting to 5 messages per 10 minutes
|
||||||
hist_ts = []
|
hist_ts = []
|
||||||
hist_flag = True
|
hist_flag = True
|
||||||
@@ -215,29 +235,15 @@ def parse_pn(data):
|
|||||||
logger('warn', 'received PN: ' + data)
|
logger('warn', 'received PN: ' + data)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def parse_delete(filepath):
|
def handle_msg(msg):
|
||||||
try:
|
content = msg['body']
|
||||||
fd = open(filepath, 'r')
|
|
||||||
except IOError as e:
|
|
||||||
logger('err', 'file has vanished: %s: %s' % (filepath, e))
|
|
||||||
return False
|
|
||||||
|
|
||||||
content = fd.read(BUFSIZ) # ignore more than BUFSIZ
|
|
||||||
fd.close()
|
|
||||||
os.remove(filepath) # probably better crash here
|
|
||||||
|
|
||||||
if content[1:1+len(conf('bot_user'))] == conf('bot_user'):
|
|
||||||
return
|
|
||||||
|
|
||||||
|
# FIXME: still needed?
|
||||||
if 'has set the subject to:' in content:
|
if 'has set the subject to:' in content:
|
||||||
return
|
return
|
||||||
|
|
||||||
if content.startswith('PRIV#'):
|
|
||||||
parse_pn(content)
|
|
||||||
return
|
|
||||||
|
|
||||||
if 'nospoiler' in content:
|
if 'nospoiler' in content:
|
||||||
# logger('info', "no spoiler for: " + content)
|
logger('info', "no spoiler for: " + content)
|
||||||
return
|
return
|
||||||
|
|
||||||
if sys.argv[0] in content:
|
if sys.argv[0] in content:
|
||||||
@@ -249,6 +255,42 @@ def parse_delete(filepath):
|
|||||||
plugins.data_parse_other(content)
|
plugins.data_parse_other(content)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
class bot(ClientXMPP):
|
||||||
|
def __init__(self, jid, password, room, nick):
|
||||||
|
ClientXMPP.__init__(self, jid, password)
|
||||||
|
|
||||||
|
self.room = room
|
||||||
|
self.nick = nick
|
||||||
|
|
||||||
|
self.add_event_handler('session_start', self.session_start)
|
||||||
|
self.add_event_handler('groupchat_message', self.muc_message)
|
||||||
|
|
||||||
|
def session_start(self, event):
|
||||||
|
self.get_roster()
|
||||||
|
self.send_presence()
|
||||||
|
|
||||||
|
self.plugin['xep_0045'].joinMUC(
|
||||||
|
self.room,
|
||||||
|
self.nick,
|
||||||
|
wait=True
|
||||||
|
)
|
||||||
|
|
||||||
|
def muc_message(self, msg):
|
||||||
|
print(msg['mucnick'])
|
||||||
|
print(msg['body'])
|
||||||
|
|
||||||
|
# don't talk to yourself
|
||||||
|
if msg['mucnick'] == self.nick:
|
||||||
|
return
|
||||||
|
|
||||||
|
# self.send_message(
|
||||||
|
# mto=msg['from'].bare,
|
||||||
|
# mbody='got[%s]' % msg['body'],
|
||||||
|
# mtype='groupchat'
|
||||||
|
# )
|
||||||
|
|
||||||
|
return handle_msg(msg)
|
||||||
|
|
||||||
if '__main__' == __name__:
|
if '__main__' == __name__:
|
||||||
import plugins
|
import plugins
|
||||||
|
|
||||||
@@ -260,6 +302,23 @@ if '__main__' == __name__:
|
|||||||
|
|
||||||
print(sys.argv[0] + ' ' + VERSION)
|
print(sys.argv[0] + ' ' + VERSION)
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.DEBUG,
|
||||||
|
format='%(levelname)-8s %(message)s'
|
||||||
|
)
|
||||||
|
|
||||||
|
xmpp = bot(
|
||||||
|
jid=conf('jid'),
|
||||||
|
password=conf('password'),
|
||||||
|
room=conf('room'),
|
||||||
|
nick=conf('bot_user')
|
||||||
|
)
|
||||||
|
|
||||||
|
xmpp.connect()
|
||||||
|
xmpp.register_plugin('xep_0045')
|
||||||
|
xmpp.process(threaded=False)
|
||||||
|
|
||||||
|
|
||||||
if not os.path.exists(fifo_path):
|
if not os.path.exists(fifo_path):
|
||||||
logger('error', 'fifo_path "%s" does not exist, exiting' % fifo_path)
|
logger('error', 'fifo_path "%s" does not exist, exiting' % fifo_path)
|
||||||
exit(1)
|
exit(1)
|
||||||
@@ -270,10 +329,6 @@ if '__main__' == __name__:
|
|||||||
|
|
||||||
while 1:
|
while 1:
|
||||||
try:
|
try:
|
||||||
for f in os.listdir(event_files_dir):
|
|
||||||
if 'mcabber-' == f[:8]:
|
|
||||||
parse_delete(os.path.join(event_files_dir, f))
|
|
||||||
|
|
||||||
plugins.event_trigger()
|
plugins.event_trigger()
|
||||||
|
|
||||||
time.sleep(delay)
|
time.sleep(delay)
|
||||||
Reference in New Issue
Block a user