Got the IRC bot working with the new mechanism.

This commit is contained in:
Griatch 2014-02-26 22:25:05 +01:00
parent 6c45d76b56
commit ab6dcc605d
6 changed files with 39 additions and 35 deletions

View file

@ -173,7 +173,7 @@ class Channel(TypeClass):
# by a custom player.msg() to treat channel-receives differently. # by a custom player.msg() to treat channel-receives differently.
player.msg(msg.message, from_obj=msg.senders, from_channel=self.id) player.msg(msg.message, from_obj=msg.senders, from_channel=self.id)
except AttributeError, e: except AttributeError, e:
logger.log_trace("%s\nCannot send msg to connection '%s'" % (e, player)) logger.log_trace("%s\nCannot send msg to player '%s'." % (e, player))
def msg(self, msgobj, header=None, senders=None, sender_strings=None, def msg(self, msgobj, header=None, senders=None, sender_strings=None,
persistent=False, online=False, emit=False, external=False): persistent=False, online=False, emit=False, external=False):

View file

@ -8,10 +8,11 @@ from src.players.player import Player
from src.scripts.scripts import Script from src.scripts.scripts import Script
from src.commands.command import Command from src.commands.command import Command
from src.commands.cmdset import CmdSet from src.commands.cmdset import CmdSet
from src.commands.cmdhandler import CMD_NOMATCH from src.commands.cmdhandler import CMD_NOMATCH, CMD_LOGINSTART
from src.utils import search from src.utils import search
_SESSIONS = None _SESSIONS = None
_CHANNELDB = None
# Bot helper utilities # Bot helper utilities
@ -32,12 +33,12 @@ class BotStarter(Script):
"Kick bot into gear" "Kick bot into gear"
if not self.db.started: if not self.db.started:
self.player.start() self.player.start()
self.db.started = False self.db.started = True
def at_server_reload(self): def at_server_reload(self):
""" """
If server reloads we don't need to start the bot again, If server reloads we don't need to reconnect the protocol
the Portal resync will do that for us. again, this is handled by the portal reconnect mechanism.
""" """
self.db.started = True self.db.started = True
@ -53,12 +54,9 @@ class CmdBotListen(Command):
session and pipes it into its execute_cmd session and pipes it into its execute_cmd
method. method.
""" """
key = CMD_NOMATCH key = "bot_data_in"
def func(self): def func(self):
text = self.cmdstring + self.args self.obj.typeclass.execute_cmd(self.args.strip(), sessid=self.sessid)
self.obj.execute_cmd(text, sessid=self.sessid)
class BotCmdSet(CmdSet): class BotCmdSet(CmdSet):
"Holds the BotListen command" "Holds the BotListen command"
@ -153,9 +151,6 @@ class IRCBot(Bot):
if irc_port: if irc_port:
self.db.irc_port = irc_port self.db.irc_port = irc_port
# cache channel
self.ndb.ev_channel = self.db.ev_channel
# instruct the server and portal to create a new session with # instruct the server and portal to create a new session with
# the stored configuration # the stored configuration
configdict = {"uid":self.dbid, configdict = {"uid":self.dbid,
@ -169,17 +164,21 @@ class IRCBot(Bot):
""" """
Takes text from connected channel (only) Takes text from connected channel (only)
""" """
if "from_channel" in kwargs and text: if not self.ndb.ev_channel and self.db.ev_channel:
# a channel receive. This is the only one we deal with # cache channel lookup
channel = kwargs.pop("from_channel") self.ndb.ev_channel = self.db.ev_channel
ckey = channel.key if "from_channel" in kwargs and text and self.ndb.ev_channel.dbid == kwargs["from_channel"]:
text = "[%s] %s" % (ckey, text) if "from_obj" not in kwargs or kwargs["from_obj"] != [self.dbobj.id]:
self.dbobj.msg(text=text) text = "bot_data_out %s" % text
self.dbobj.msg(text=text)
def execute_cmd(self, text=None, sessid=None): def execute_cmd(self, text=None, sessid=None):
""" """
Take incoming data and send it to connected channel. This is triggered Take incoming data and send it to connected channel. This is triggered
by the CmdListen command in the BotCmdSet. by the CmdListen command in the BotCmdSet.
""" """
if self.ndb.channel: if not self.ndb.ev_channel and self.db.ev_channel:
self.ndb.channel.msg(text) # cache channel lookup
self.ndb.ev_channel = self.db.ev_channel
if self.ndb.ev_channel:
self.ndb.ev_channel.msg(text, senders=self.dbobj.id)

View file

@ -7,6 +7,7 @@ from src.typeclasses.managers import TypedObjectManager
from src.typeclasses.managers import returns_typeclass_list from src.typeclasses.managers import returns_typeclass_list
from src.utils.utils import make_iter from src.utils.utils import make_iter
__all__ = ("ScriptManager",) __all__ = ("ScriptManager",)
_GA = object.__getattribute__
VALIDATE_ITERATION = 0 VALIDATE_ITERATION = 0
@ -44,23 +45,22 @@ class ScriptManager(TypedObjectManager):
if not obj: if not obj:
return [] return []
obj = obj.dbobj obj = obj.dbobj
player = obj.__class__.__name__ == "PlayerDB" player = _GA(_GA(obj, "__class__"), "__name__") == "PlayerDB"
print "get_all_scripts_on_obj:", obj, player
if key: if key:
dbref = self.dbref(key) dbref = self.dbref(key)
if dbref or dbref == 0: if dbref or dbref == 0:
if player: if player:
script = self.filter(db_player=obj, id=dbref) return self.filter(db_player=obj, id=dbref)
else: else:
script = self.filter(db_obj=obj, id=dbref) return self.filter(db_obj=obj, id=dbref)
if script:
return script
elif player: elif player:
return self.filter(db_player=obj, db_key=key) return self.filter(db_player=obj, db_key=key)
return self.filter(db_obj=obj.dbobj, db_key=key) else:
if player: return self.filter(db_obj=obj, db_key=key)
self.filter(db_player=obj) elif player:
return self.filter(db_obj=obj) return self.filter(db_player=obj)
else:
return self.filter(db_obj=obj)
@returns_typeclass_list @returns_typeclass_list
def get_all_scripts(self, key=None): def get_all_scripts(self, key=None):
@ -168,6 +168,7 @@ class ScriptManager(TypedObjectManager):
if dbref and self.dbref(dbref, reqhash=False): if dbref and self.dbref(dbref, reqhash=False):
scripts = self.get_id(dbref) scripts = self.get_id(dbref)
elif obj: elif obj:
#print "calling get_all_scripts_on_obj", obj, key, VALIDATE_ITERATION
scripts = self.get_all_scripts_on_obj(obj, key=key) scripts = self.get_all_scripts_on_obj(obj, key=key)
else: else:
scripts = self.get_all_scripts(key=key) #self.model.get_all_cached_instances() scripts = self.get_all_scripts(key=key) #self.model.get_all_cached_instances()

View file

@ -225,7 +225,7 @@ class ScriptBase(TypeClass):
""" """
#print "Script %s (%s) start (active:%s, force:%s) ..." % (self.key, id(self.dbobj), #print "Script %s (%s) start (active:%s, force:%s) ..." % (self.key, id(self.dbobj),
# self.is_active, force_restart) # self.is_active, force_restart)
if self.dbobj.is_active and not force_restart: if self.dbobj.is_active and not force_restart:
# script already runs and should not be restarted. # script already runs and should not be restarted.

View file

@ -40,17 +40,19 @@ class IRCBot(irc.IRCClient, Session):
self.uid = self.factory.uid self.uid = self.factory.uid
self.logged_in = True self.logged_in = True
self.factory.sessionhandler.connect(self) self.factory.sessionhandler.connect(self)
logger.log_infomsg("IRC bot connected")
def privmsg(self, user, channel, msg): def privmsg(self, user, channel, msg):
"A message was sent to channel" "A message was sent to channel"
if not msg.startswith('***'): if not msg.startswith('***'):
user = user.split('!', 1)[0] user = user.split('!', 1)[0]
self.data_in self.data_in("bot_data_in %s@%s: %s" % (user, channel, msg))
def action(self, user, channel, msg): def action(self, user, channel, msg):
"An action was done in channel" "An action was done in channel"
if not msg.startswith('**'): if not msg.startswith('**'):
user = user.split('!', 1)[0] user = user.split('!', 1)[0]
self.data_in("bot_data_in %s@%s %s" % (user, channel, msg))
def data_in(self, text=None, **kwargs): def data_in(self, text=None, **kwargs):
"Data IRC -> Server" "Data IRC -> Server"
@ -58,7 +60,9 @@ class IRCBot(irc.IRCClient, Session):
def data_out(self, text=None, **kwargs): def data_out(self, text=None, **kwargs):
"Data from server-> IRC" "Data from server-> IRC"
self.say(self.channel, text) if text.startswith("bot_data_out"):
text = text.split(" ", 1)[1]
self.say(self.channel, text)
class IRCBotFactory(protocol.ReconnectingClientFactory): class IRCBotFactory(protocol.ReconnectingClientFactory):

View file

@ -201,7 +201,7 @@ class ServerSessionHandler(SessionHandler):
# protocols like SSH # protocols like SSH
sess.player = _PlayerDB.objects.get_player_from_uid(sess.uid) sess.player = _PlayerDB.objects.get_player_from_uid(sess.uid)
sess.at_sync() sess.at_sync()
# validate all script # validate all scripts
_ScriptDB.objects.validate() _ScriptDB.objects.validate()
self.sessions[sess.sessid] = sess self.sessions[sess.sessid] = sess
sess.data_in(CMD_LOGINSTART) sess.data_in(CMD_LOGINSTART)