本文整理汇总了Python中commands.Commands.supports方法的典型用法代码示例。如果您正苦于以下问题:Python Commands.supports方法的具体用法?Python Commands.supports怎么用?Python Commands.supports使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类commands.Commands
的用法示例。
在下文中一共展示了Commands.supports方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from commands import Commands [as 别名]
# 或者: from commands.Commands import supports [as 别名]
#.........这里部分代码省略.........
self.debug("DONE!")
#cursor = conn.cursor ()
#cursor.execute ("SELECT VERSION()")
#row = cursor.fetchone ()
#print "server version:", row[0]
#cursor.close ()
#conn.close ()
def start_bot(self):
self.debug("- Initializing Jabber connection...")
jid = xmpp.JID(self.config['bot_jid'])
user = jid.getNode()
server = jid.getDomain()
self.con = xmpp.Client(server, debug=[])
conres = self.con.connect()
if not conres:
raise ("Unable to connect to server %s!" % server)
sys.exit(1)
if conres<>'tls':
raise "Warning: unable to estabilish secure connection - TLS failed!"
self.debug("Connected!")
self.debug("- Authenticating...")
authres = self.con.auth(user, self.config['bot_password'], self.config['bot_resource'])
if not authres:
raise "Authentication failure on %s -- check bot's login/password." % server
sys.exit(1)
if authres<>'sasl':
raise ("Warning: unable to perform SASL auth os %s. Old " +
"authentication method used!" % server)
self.debug("DONE!")
self.con.RegisterHandler('message',self.messageHandler)
self.con.RegisterHandler('iq',self.rosterHandler, ns="jabber:iq:roster")
self.con.sendInitPresence() # requestRoster=1
self.debug("- Entering main loop...")
while self.loop(): pass
def getBuddyList(self):
return self.buddylist
def getDB(self):
return self.dbcon
def connection(self):
return self.con
def loop(self):
try:
self.con.Process(1)
except KeyboardInterrupt: return False
return True
def rosterHandler(self, con, mess):
"""load the Bot's buddylist"""
items = mess.getChildren()[0].getChildren()
for item in items:
if item.getAttr('subscription') == 'both':
self.addBuddy(item.getAttr('jid'))
else:
if item.getAttr('subscription') == 'remove':
self.removeBuddy(item.getAttr('jid'))
def addBuddy(self, jid):
if not jid in self.buddylist:# .has_kay(jid):
self.buddylist[jid] = xmpp.JID(jid)
def removeBuddy(self, jid):
del self.buddylist[jid]
def sendTo(self, to, msg):
self.con.send(xmpp.Message(to, msg))
def messageHandler(self, con, mess):
msg = mess.getBody().strip()
sender = mess.getFrom()
if ' ' in msg:
command, args = msg.split(' ',1)
else:
command, args = msg, ''
cmd = command.lower()
if self.commands.supports(cmd):
reply = self.commands.do(cmd, sender, args)
else:
reply = 'command not found: "%s". try "help"' % ( cmd, )
if reply:
self.sendTo(sender, reply)
def debug(self, msg):
print msg