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.
player.msg(msg.message, from_obj=msg.senders, from_channel=self.id)
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,
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.commands.command import Command
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
_SESSIONS = None
_CHANNELDB = None
# Bot helper utilities
@ -32,12 +33,12 @@ class BotStarter(Script):
"Kick bot into gear"
if not self.db.started:
self.player.start()
self.db.started = False
self.db.started = True
def at_server_reload(self):
"""
If server reloads we don't need to start the bot again,
the Portal resync will do that for us.
If server reloads we don't need to reconnect the protocol
again, this is handled by the portal reconnect mechanism.
"""
self.db.started = True
@ -53,12 +54,9 @@ class CmdBotListen(Command):
session and pipes it into its execute_cmd
method.
"""
key = CMD_NOMATCH
key = "bot_data_in"
def func(self):
text = self.cmdstring + self.args
self.obj.execute_cmd(text, sessid=self.sessid)
self.obj.typeclass.execute_cmd(self.args.strip(), sessid=self.sessid)
class BotCmdSet(CmdSet):
"Holds the BotListen command"
@ -153,9 +151,6 @@ class IRCBot(Bot):
if 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
# the stored configuration
configdict = {"uid":self.dbid,
@ -169,17 +164,21 @@ class IRCBot(Bot):
"""
Takes text from connected channel (only)
"""
if "from_channel" in kwargs and text:
# a channel receive. This is the only one we deal with
channel = kwargs.pop("from_channel")
ckey = channel.key
text = "[%s] %s" % (ckey, text)
self.dbobj.msg(text=text)
if not self.ndb.ev_channel and self.db.ev_channel:
# cache channel lookup
self.ndb.ev_channel = self.db.ev_channel
if "from_channel" in kwargs and text and self.ndb.ev_channel.dbid == kwargs["from_channel"]:
if "from_obj" not in kwargs or kwargs["from_obj"] != [self.dbobj.id]:
text = "bot_data_out %s" % text
self.dbobj.msg(text=text)
def execute_cmd(self, text=None, sessid=None):
"""
Take incoming data and send it to connected channel. This is triggered
by the CmdListen command in the BotCmdSet.
"""
if self.ndb.channel:
self.ndb.channel.msg(text)
if not self.ndb.ev_channel and self.db.ev_channel:
# 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.utils.utils import make_iter
__all__ = ("ScriptManager",)
_GA = object.__getattribute__
VALIDATE_ITERATION = 0
@ -44,23 +45,22 @@ class ScriptManager(TypedObjectManager):
if not obj:
return []
obj = obj.dbobj
player = obj.__class__.__name__ == "PlayerDB"
print "get_all_scripts_on_obj:", obj, player
player = _GA(_GA(obj, "__class__"), "__name__") == "PlayerDB"
if key:
dbref = self.dbref(key)
if dbref or dbref == 0:
if player:
script = self.filter(db_player=obj, id=dbref)
return self.filter(db_player=obj, id=dbref)
else:
script = self.filter(db_obj=obj, id=dbref)
if script:
return script
return self.filter(db_obj=obj, id=dbref)
elif player:
return self.filter(db_player=obj, db_key=key)
return self.filter(db_obj=obj.dbobj, db_key=key)
if player:
self.filter(db_player=obj)
return self.filter(db_obj=obj)
else:
return self.filter(db_obj=obj, db_key=key)
elif player:
return self.filter(db_player=obj)
else:
return self.filter(db_obj=obj)
@returns_typeclass_list
def get_all_scripts(self, key=None):
@ -168,6 +168,7 @@ class ScriptManager(TypedObjectManager):
if dbref and self.dbref(dbref, reqhash=False):
scripts = self.get_id(dbref)
elif obj:
#print "calling get_all_scripts_on_obj", obj, key, VALIDATE_ITERATION
scripts = self.get_all_scripts_on_obj(obj, key=key)
else:
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),
# self.is_active, force_restart)
# self.is_active, force_restart)
if self.dbobj.is_active and not force_restart:
# 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.logged_in = True
self.factory.sessionhandler.connect(self)
logger.log_infomsg("IRC bot connected")
def privmsg(self, user, channel, msg):
"A message was sent to channel"
if not msg.startswith('***'):
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):
"An action was done in channel"
if not msg.startswith('**'):
user = user.split('!', 1)[0]
self.data_in("bot_data_in %s@%s %s" % (user, channel, msg))
def data_in(self, text=None, **kwargs):
"Data IRC -> Server"
@ -58,7 +60,9 @@ class IRCBot(irc.IRCClient, Session):
def data_out(self, text=None, **kwargs):
"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):

View file

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