All pastes #2103034 Raw Edit

Miscellany

public text v1 · immutable
#2103034 ·published 2012-01-14 16:38 UTC
rendered paste body


#
# THE MAIN FILE:
#



#!/usr/bin/python

import time, thread
import bb.irc, bb.util
import bb.ircfn


def fn_received(irc, data, mm):
  try:
    # I don't understand why this doesn't work
    if (len(mm)>3) and (mm[3]==':!reload'):
      ud=bb.ircfn.udata(mm[0])
      if bb.ircfn.checkAuth(ud,'reload'):
        print "RELOADING"
        reload(bb.ircfn)
        print "RELOADED"
    else:
      # This is working well
      ifn = bb.ircfn.IRCFunction()
      ifn.handleInput(irc, mm, data)
      ifn = None # this is a desperate attempt to get reload to work
  except:
    try:
      print "EXCEPT fn_received: %s\r\n%s\r\n\r\n" % (repr(bb.util.exinfo()),data)
    except:
      print "EXCEPT fn_received: IN EXCEPT!"


def fn_sent(irc, data):
  print ">>> SENDING: ", data.strip()
  return data

 
def irc_thread(irc,nn):
  irc.start()

if __name__ == '__main__':
  
  irc = bb.irc.stream(fn_received,fn_sent)
  thread.start_new_thread(irc_thread,(irc,None))
  
  # loop here
  try:
    while True:
      time.sleep(1)
  except:
    pass









VERSION = '1'

class IRCEvent (object):
  def __init__(self, mm):
    self.target = ''
    self.prefix = ''
    self.host = ''
    self.user = ''
    self.nick = ''
    
    ML = len(mm)
    if mm[0] and mm[0][0]==':':
      mm[0]=mm[0][1:]
    if ML>3 and mm[3] and mm[3][0]==':':
      mm[3]=mm[3][1:]
    
    if ML == 1:
      self.cmd = mm[0]
      self.arg = ''
    elif ML == 2:
      self.cmd = mm[0]
      self.arg = mm[1]
    else:
      self.prefix=mm[0]
      x = mm[0].split('!',1)
      if len(x)==1:
        self.nick=x[0]
        self.user=x[0]
        self.host=x[0]
      else:
        self.nick=x[0]
        x = x[1].split('@',1)
        self.user=x[0]
        if len(x)>1:
          self.host=x[1]
      self.cmd = mm[1]
      self.arg = mm[3]
      self.target = mm[2]

Class = IRCEvent






#
# THE bb.ircfn FILE
#

class IRCFunction(object):

  def handleInput (self, irc, mm, data):
    e = IRCEvent(mm)
    
    print data
    print " %s %s %s %s %s %s" % (e.nick, e.user, e.host, e.cmd, e.target, e.arg)
    print " "
    
    if (e.cmd == 'PRIVMSG'):
      line = e.arg.split(' ')
      if not len(line) or not line[0][0:1]=='!':
        return
      op = line[0][1:]
      if not self.checkAuth(e, op):
        return
      
      if op=='join' and line[1][0:1]=='#':
        ss = "JOIN %s\r\n" % line[1]
        irc.send(ss)
        print op, ':', line, "...", ss
      
      elif op=='say' and len(line)>1:
        self.__say(irc, e, line)
      
      elif op=='part':
        if len(line)>1 and line[1][0:1]=='#':
          irc.send("PART %s\r\n" % line[1])
        elif e.target and e.target[0]=='#':
          irc.send("PART %s\r\n" % e.target)
      
      elif op=='quit':
        irc.send("QUIT %s\r\n" % ' '.join(line[1:]))
      
      elif op=='version':
        self.__say(irc, e, ["version", "%s" % VERSION])

  
  def __say(self, irc, e, line):
    if line[0][1]=='#':
      irc.send("PRIVMSG %s %s\r\n" % (line[1], ' '.join(line[2:])))
    elif e.target and e.target[0]=='#':
      irc.send("PRIVMSG %s %s\r\n" % (e.target, ' '.join(line[1:])))
    else:
      irc.send("PRIVMSG %s %s\r\n" % (e.nick, ' '.join(line[1:])))
    

  def checkAuth(self, e, op):
    return True

Class = IRCFunction