Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import msvcrt
- import Queue
- import socket
- import string
- import sys
- import time
- ch = "#acars"
- server = "irc.efnet.pl"
- port = "6667"
- irc = socket.socket()
- event = ''
- nickname = "acarsdbot"
- def send(arg):
- irc.send(arg+"\r\n")
- time.sleep(0.55)
- def get_line_content(inp):
- try:
- if string.find(inp, ':', 2) > 0:
- return inp[string.find(inp, ':', 2)+1:]
- else:
- return ''
- except:
- return "--- DEBUG Error in getLineContent"
- def check_buffer(inp):
- try:
- return (True if inp[-1] in "\r\n" else False)
- except:
- return False
- def strip_color(s):
- k = string.split(s, '\x03')
- o = ''
- for x in k:
- q = list(x)
- l = 2
- t = 0
- while t < l:
- if len(q) > 0:
- if q[0] == ',':
- l = 5
- if q[0] in "0123456789," and t != 3:
- q.pop(0)
- t += 1
- o += string.join(q, sep='')
- for x in '\x02\x03\x1d\x1f\x0f':
- o = string.replace(o, x, '')
- return o
- def split_ctcp(inp):
- msgEnd = [0]
- inp = string.rstrip(inp) + ' '
- msgStart = string.find(inp, '\x01')
- msgEnd[0] = string.find(inp, ' ', msgStart)
- while msgEnd[len(msgEnd)-1] > 2:
- msgEnd.append(string.find(inp, ' ', msgEnd[len(msgEnd)-1]+1))
- msgEnd.pop()
- msgEnd[len(msgEnd)-1] = (string.find(inp, '\x01') \
- if string.find(inp, '\x01') > msgStart + 1 \
- else msgEnd[len(msgEnd)-1]-1)
- out = []
- msgEnd.insert(0, msgStart)
- i = 0
- while msgEnd[i] > 2 and i < len(msgEnd)-1:
- out.append(inp[msgEnd[i]+1:msgEnd[i+1]])
- i += 1
- #find sender
- sender = inp[1:string.find(inp, '!', 2)]
- out.insert(1, sender)
- out[0] = string.upper(out[0])
- for i in range(len(out)-1, 0, -1):
- if out[i] == '':
- out.pop(i)
- return out
- def reply_ctcp(target, req):
- send("NOTICE "+target+" :\x01"+req+"\x01")
- def send_ctcp(target, req):
- send("PRIVMSG "+target+" :\x01"+req+"\x01")
- def generate_lines(inp, lc):
- doReset = False
- inter = string.split(inp, "\r")
- out = inter[lc]
- lc += 1
- if lc >= len(inter):
- lc = 0
- doReset = True
- return [out, lc, doReset]
- def privmsg(target, message):
- send("PRIVMSG "+target+" :"+message)
- def get_command(inp, command):
- """boolean repr. if inp contains command"""
- try:
- return string.lower((getLine(inp)[2]+' '*(len(command)+3))\
- [:len(command)]) == command
- except:
- return False
- # [channel, sender, text]
- def get_line(inp):
- try:
- out = [0,0,0,0]
- out[1] = (string.split(inp, ' ')[0])[1:]
- if "!" in out[1]:
- out[1] = (string.split(out[1], '!'))[0]
- out[0] = (string.split(inp, ' ')[2])
- out[2] = inp[string.find(inp,':', 2)+1:]
- out[3] = string.upper(inp[string.find(inp," ")+1:\
- string.find(inp," ",string.find(inp," ")+1)])
- except:
- out = ['','error','','']
- return out
- def format_mode(inp):
- try:
- out = [0,0,0,0]
- out[1] = (string.split(inp, ' ')[0])[1:]
- if "!" in out[1]:
- out[1] = (string.split(out[1], '!'))[0]
- out[0] = (string.split(inp, ' ')[2])
- out[2] = inp[string.find(inp,out[0])+len(out[0])+1:]
- out[3] = string.upper(inp[string.find(inp," ")+1:\
- string.find(inp," ",string.find(inp," ")+1)])
- except:
- return ['','','','']
- return out
- def hostmask(inp):
- out = ['','--- DEBUG Error in hostmask()','']
- if '!' not in inp or '@' not in inp:
- return ['','--- DEBUG Error in hostmask(), missing trigger character',
- '']
- out[0] = (string.split(inp,"!"))[0]
- out[1] = inp[string.find(inp, '!')+1:string.find(inp, '@')]
- out[2] = (inp[string.find(inp,'@')+1:]).split(' ')[0]
- return out
- def ping(inp):
- inter = string.split(inp, ' ')
- if string.lower(inter[0]) == "ping":
- send(string.replace(inp, "PING", "PONG", 1))\
- def connect(nick = "Python0", realname = "Real name", server = "localhost",
- port = 6667):
- if type(port) == str:
- port = int(port)
- tries = 0
- while tries < 3:
- try:
- global irc
- irc = socket.socket()
- irc.setblocking(0)
- irc.settimeout(0.5)
- irc.connect((server, port))
- time.sleep(0.5)
- set_nick(nick)
- send("USER "+nick+" localhost "+server+" :"+realname)
- return True
- except:
- tries += 1
- time.sleep(4)
- if tries == 4:
- return False
- def disconnect(partmsg = "Connection closed"):
- global irc
- #print "Closing..."
- send("QUIT :"+partmsg)
- irc.close()
- msvcrt.getch()
- ### general IRC features
- def join(channel):
- send("JOIN :"+channel)
- def part(channel, message = ""):
- send("PART :"+channel+" :"+message)
- def set_nick(nick):
- send("NICK "+nick)
- def receive():
- try:
- return irc.recv(2048)
- except:
- return ''
- ## END StickybotLayer
- def handle_ctcp(inp):
- if (string.find(inp, '\x01') > 2) and (string.find(inp, "PRIVMSG") > 2):
- ctcp = splitCTCP(inp)
- if "PING" in string.upper(ctcp[0]):
- reply_ctcp(ctcp[1], "PING "+ctcp[2])
- if "VERSION" in string.upper(ctcp[0]):
- reply_ctcp(ctcp[1], "VERSION acarsd Reporter Bot")
- def check_error():
- if (buff+' '*19)[0:19] == "ERROR :Closing Link: "[0:19]:
- doExit = True
- def check_nick():
- if string.find(buff, ':', 2) > 11:
- i = string.find(buff, ':', 2)+1
- if (buff+' '*16)[i:i+15] == "Nickname is alr":
- nick = "acarsdcl"+['`','_','^'][random.randrange(3)]
- set_nick(nick)
- def check_verified_line():
- global doExit
- global buff
- global event
- out = hostmask(buff)
- if "PRIVMSG %s :&version" in buff:
- event = "&event version"
- if ("CVSoft-P2" in out[1] or "CVSoft" in out[1] or "notipa" in out[1]\
- or "kj6psg" in out[1]):## and (out[2] == "cpe-107-185.52.246.socal.res.rr.com" or out[2] == "withg.org"):
- if "PRIVMSG %s :&killbot" % ch in buff:
- doExit = True
- out = get_line(buff)
- privmsg(ch, "killbot initiated by authenticated user")
- event = "&event kill"
- if "PRIVMSG %s :&event" % ch in buff:
- out = get_line(buff)
- event = out[2]
- #here's an example:
- ##if get_command(buff, "&quit"):
- ## doExit = True
- ## out = get_line(buff)
- ## privmsg(out[1], "Exiting IRC...")
- def dt(inp):
- return int(time.time()-inp)
- def handle_motd(inp):
- """this handles the MOTD, setting flag wasMOTD to true if it has passed.
- input is the line to check if it is the end of a MOTD"""
- global wasMOTD
- if "End of /MOTD" in inp:
- wasMOTD = True
- join(ch)
- def build_line_buffer():
- """this code translates buffull into a line"""
- global buff, buffull, lineCounter, doReset
- bufftemp = '\x02'
- while not (check_buffer(buffull)):
- bufftemp = receive()
- if bufftemp == '':
- break
- buffull = buffull+bufftemp
- temp = generate_lines(buffull, lineCounter)
- # generateLines returns [a line, lineCounter, doReset]
- buff = string.strip(temp[0], "\r\n")
- lineCounter = temp[1]
- doReset = temp[2]
- return (buff, doReset)
- # :irc.colosolutions.net 376 CVPython :End of /MOTD command.
- def handle_buffer():
- try:
- commands = ["killbot", "&nickchange"]
- global doExit
- r = irc_queue.get(False)
- irc_queue.task_done()
- if len(r) > 0 and r not in commands and "&nickchange" not in r:
- privmsg(ch, r)
- elif r == "killbot":
- doExit = True
- elif "&nickchange " in r and len(r) > 13:
- set_nick(r.split(' ')[1])
- except Queue.Empty:
- return ''
- except:
- return "ERROR "+sys.exc_info()[1]
- raise
- def handler(buff, doReset):
- global buffull
- handle_motd(buff)
- ping(buff)
- check_nick()
- handle_ctcp(buff)
- check_verified_line()
- time.sleep(0.01)
- if doReset:
- buffull = ''
- return False
- return doReset
- # connect to IRC
- def main():
- connect(nickname, "acarsdbot", server, port)
- global buff, buffull, ch, doExit, irc_queue, lineCounter, wasMOTD
- buff, buffull = '', ''
- doExit, doReset, wasMOTD = False, False, False
- lineCounter = 0
- while not (doExit):
- buff, doReset = build_line_buffer()
- #code goes from here to doReset's redefinition
- if wasMOTD and len(buff)>0:
- pass #print buff
- if wasMOTD:
- handle_buffer()
- doReset = handler(buff, doReset)
- disconnect()
- irc_queue = Queue.Queue(50)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement