当前位置: 首页>>代码示例>>Python>>正文


Python Commands.do方法代码示例

本文整理汇总了Python中commands.Commands.do方法的典型用法代码示例。如果您正苦于以下问题:Python Commands.do方法的具体用法?Python Commands.do怎么用?Python Commands.do使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在commands.Commands的用法示例。


在下文中一共展示了Commands.do方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from commands import Commands [as 别名]
# 或者: from commands.Commands import do [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
开发者ID:BackupTheBerlios,项目名称:xemele-svn,代码行数:104,代码来源:xemele_bot.py


注:本文中的commands.Commands.do方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。