refactor urlbot plugin structure and code style
This commit is contained in:
63
idlebot.py
63
idlebot.py
@@ -1,7 +1,11 @@
|
||||
#!/usr/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
import logging
|
||||
import time
|
||||
|
||||
from common import *
|
||||
import sys
|
||||
|
||||
from common import VERSION, EVENTLOOP_DELAY
|
||||
|
||||
try:
|
||||
from local_config import conf, set_conf
|
||||
@@ -16,12 +20,14 @@ except ImportError:
|
||||
' ' * len(sys.argv[0])
|
||||
)
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
from sleekxmpp import ClientXMPP
|
||||
|
||||
got_hangup = False
|
||||
|
||||
class bot(ClientXMPP):
|
||||
|
||||
class IdleBot(ClientXMPP):
|
||||
def __init__(self, jid, password, rooms, nick):
|
||||
ClientXMPP.__init__(self, jid, password)
|
||||
|
||||
@@ -31,22 +37,27 @@ class bot(ClientXMPP):
|
||||
self.add_event_handler('session_start', self.session_start)
|
||||
self.add_event_handler('groupchat_message', self.muc_message)
|
||||
|
||||
def session_start(self, event):
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
def session_start(self, _):
|
||||
self.get_roster()
|
||||
self.send_presence()
|
||||
|
||||
for room in self.rooms:
|
||||
log.info('%s: joining' % room)
|
||||
self.logger.info('%s: joining' % room)
|
||||
ret = self.plugin['xep_0045'].joinMUC(
|
||||
room,
|
||||
self.nick,
|
||||
wait=True
|
||||
)
|
||||
log.info('%s: joined with code %s' % (room, ret))
|
||||
self.logger.info('%s: joined with code %s' % (room, ret))
|
||||
|
||||
def muc_message(self, msg_obj):
|
||||
global got_hangup
|
||||
|
||||
"""
|
||||
Handle muc messages, return if irrelevant content or die by hangup.
|
||||
:param msg_obj:
|
||||
:return:
|
||||
"""
|
||||
# don't talk to yourself
|
||||
if msg_obj['mucnick'] == self.nick:
|
||||
return
|
||||
@@ -55,35 +66,51 @@ class bot(ClientXMPP):
|
||||
return
|
||||
|
||||
if msg_obj['body'].startswith(conf('bot_user')) and 'hangup' in msg_obj['body']:
|
||||
log.warn("got 'hangup' from '%s': '%s'" % (
|
||||
self.logger.warn("got 'hangup' from '%s': '%s'" % (
|
||||
msg_obj['mucnick'], msg_obj['body']
|
||||
))
|
||||
global got_hangup
|
||||
got_hangup = True
|
||||
sys.exit(1)
|
||||
return
|
||||
|
||||
|
||||
if '__main__' == __name__:
|
||||
log.info(VERSION)
|
||||
def start(botclass, active=False):
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format=sys.argv[0] + ' %(asctime)s %(levelname).1s %(funcName)-15s %(message)s'
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.info(VERSION)
|
||||
|
||||
xmpp = bot(
|
||||
bot = botclass(
|
||||
jid=conf('jid'),
|
||||
password=conf('password'),
|
||||
rooms=conf('rooms'),
|
||||
nick=conf('bot_user')
|
||||
)
|
||||
import plugins
|
||||
|
||||
xmpp.connect()
|
||||
xmpp.register_plugin('xep_0045')
|
||||
xmpp.process()
|
||||
if active:
|
||||
plugins.register_all()
|
||||
if plugins.plugin_enabled_get(plugins.command_dsa_watcher):
|
||||
# first result is lost.
|
||||
plugins.command_dsa_watcher(['dsa-watcher', 'crawl'])
|
||||
|
||||
bot.connect()
|
||||
bot.register_plugin('xep_0045')
|
||||
bot.process()
|
||||
|
||||
while 1:
|
||||
try:
|
||||
# do nothing here, just idle
|
||||
if got_hangup:
|
||||
xmpp.disconnect()
|
||||
if not plugins.event_trigger():
|
||||
bot.disconnect()
|
||||
sys.exit(1)
|
||||
|
||||
time.sleep(EVENTLOOP_DELAY)
|
||||
except KeyboardInterrupt:
|
||||
print('')
|
||||
exit(130)
|
||||
|
||||
|
||||
if '__main__' == __name__:
|
||||
start(IdleBot)
|
||||
|
||||
Reference in New Issue
Block a user