Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import socket
- def authdbtoarray():
- f=open('authdb.txt','r')
- niz2=f.read()
- f.close()
- niz2 = niz2.split('\n')
- cols = 3
- rows = len(niz2)
- niz = [[0 for i in range(cols)] for j in range(rows)]
- for i in range(0,len(niz2)):
- niz[i]=niz2[i].split('\t')
- return niz
- def authdbadd(auth,access):
- f=open('authdb.txt','a')
- f.write ( '\n*\t' + access + '\t' + auth + '\t-' )
- f.close()
- def arrayadd(auth,access,authdb):
- newauthdb = [[0 for i in range(4)] for j in range(len(authdb)+1)]
- for i in range (0,len(newauthdb)):
- if (i < len(newauthdb)-1):
- newauthdb[i] = authdb[i]
- if (i == len(newauthdb)-1):
- newauthdb[i] = ['jea','jea','jea','jea']
- newauthdb[-1][0]= '%d' % (len(newauthdb))
- newauthdb[-1][1]= access
- newauthdb[-1][2]= auth
- newauthdb[-1][3]= '-'
- return newauthdb
- def numbertopic(topic,nick,operator):
- topic2= ''
- fqm = 0
- for i in range(0,len(topic.split())):
- if operator == '+' and topic.split()[i] == '?' and fqm == 0:
- fqm = 1
- topic2 = topic2 + nick + ' '
- elif operator == '-' and topic.split()[i] == nick and fqm == 0 :
- fqm = 1
- topic2 = topic2 + '?' + ' '
- elif i == 4:
- if operator == '+':
- broj = int(topic.split()[4][1])+1
- broj = str(broj)
- if operator == '-':
- broj = int(topic.split()[4][1])-1
- broj = str(broj)
- topic2 = topic2 + '(' + broj + '):4' + ' '
- else:
- topic2 = topic2 + topic.split()[i] + ' '
- topic = topic2[:len(topic2)-1]
- if broj == '10':
- send (s, 'PRIVMSG #shitnub :' + 'GAME READY')
- topic = 'Modes: (0) // Players (0):4 ? ? ? ? ? ? ? ? ? ? ? // Obs: ? ? //'
- send (s, 'TOPIC #shitnub :' + topic)
- return topic
- #TODO2 : access level
- #data for todo:('event_handler handling:', ':wineasy2.se.quakenet.org 330 StockBOT StockTME2 StockTME :is authed as')
- def nick( data ): #pharses :nick!@blahlbah.blah to nick
- nick = ''
- for i in range(1,len(data)):
- if (data[i] == '!'): break
- nick = nick + data[i]
- return nick
- def send( s, data ):
- s.send( data + '\r\n' )
- print ( 'sent: ', data )
- def parse_data( data, buffer ):
- data = buffer + data
- events = data.split( '\n' ) # split events by \n as EFNet doesn't include \r
- buffer = events.pop()
- return( events, buffer )
- def event_handler( s, data , authdb,topic): # create list of events
- channel = '#shitnub'
- data = data.rstrip( '\r' ) # remove \r at end (for EFNet compability)
- print ( 'event_handler handling:', data )
- if data.split()[0] == 'PING': send( s, 'PONG ' + data.split()[1] )
- #perform after identifying on server
- if data.split()[1] == '221':
- send( s, 'PRIVMSG q@cserve.quakenet.org :auth StockTME kamikaza')
- #('event_handler handling:', ':Q!TheQBot@CServe.quakenet.org NOTICE StockBOT :You are now logged in as StockTME.')
- if data.split()[1]== 'NOTICE':
- if data == ':Q!TheQBot@CServe.quakenet.org NOTICE StockBOT :You are now logged in as StockTME.':
- send(s,'JOIN #shitnub')
- if data.split()[1] == 'MODE' and data.split()[2] == '#shitnub' and data.split()[3] == '+o' and data.split()[4] == 'StockBOT':
- send(s,'TOPIC #shitnub :' + topic)
- #when whois store in table nick to auth
- #data for todo:('event_handler handling:', ':wineasy2.se.quakenet.org 330 StockBOT StockTME2 StockTME :is authed as')
- if data.split()[1] == '330':
- indatabase = 0
- for i in range(0,len(authdb)):
- # debuger: print ('%d : %s == %s ') % (len(authdb), authdb[i][2], data.split()[3])
- if (authdb[i][2] == data.split()[4]):
- authdb[i][3] = data.split()[3]
- indatabase=1
- if (indatabase == 0):
- send( s, 'ban #shitnub ' + data.split()[3])
- send( s, 'kick #shitnub ' + data.split()[3])
- #todo1: EVENTS WHEN REGARDING AUTH : NICKCHANGE,JOIN,PART,QUIT SERVER,UNAUTHED USER?
- #tod2: Change players in topic on events NICKCHANGE . And remove them if PART QUIT SERVER
- #tododoremaining:
- #data for todo - ('event_handler handling:', ':StockTME2!i@StockTME.users.quakenet.org NICK :StockTME') - nickchange
- if (data.split()[1] == 'NICK'):
- print(data.split()[2][2:len(data.split()[2])])
- topic = topic.replace(nick(data.split()[0]),data.split()[2][1:])
- send(s,'TOPIC #shitnub :' + topic)
- send (s, 'WHOIS ' + data.split()[2][1:])
- #data for todo - ('event_handler handling:', ':StockTME!i@StockTME.users.quakenet.org PART #stocktest') - leave
- if ((data.split()[1] == 'PART') and (data.split()[2] == channel)):
- topic = topic.replace(nick(data.split()[0]),'?')
- topic = numbertopic(topic,nick(data.split()[0]),'-')
- for i in range(0,len(authdb)):
- if (authdb[i][3] == nick(data.split()[0])):
- authdb[i][3] = '-'
- #data for todo - ('event_handler handling:', ':StockTME4!i@StockTME.users.quakenet.org QUIT :Signed off')
- if (data.split()[1] == 'QUIT'):
- topic = topic.replace(nick(data.split()[0]),'?')
- topic = numbertopic(topic,nick(data.split()[0]),'-')
- for i in range(0,len(authdb)):
- if (authdb[i][3] == nick(data.split()[0])):
- authdb[i][3] = '-'
- #data for todo - ('event_handler handling:', ':StockTME!i@StockTME.users.quakenet.org JOIN #stocktest') - join
- if (data.split()[1] == 'JOIN') and (data.split()[2] == channel):
- send (s, 'WHOIS ' + nick(data.split()[0]))
- #data for todo - ('event_handler handling:', ':wineasy2.se.quakenet.org 353 StockBOT = #shitnub :StockBOT @StockBAus @Q')
- if (data.split()[1] == '353') and (data.split()[2] == 'StockBOT') and (data.split()[3] == '=') and (data.split()[4] == channel):
- for i in range(6,len(data.split())):
- if data.split()[i][0] == '@' and data.split()[i] != '@Q' and data.split()[i] != '@S':
- send (s, 'WHOIS ' + data.split()[i][1:len(data.split()[i])])
- else:
- send (s, 'WHOIS ' + data.split()[i])
- #data for todo - ('event_handler handling:', ':StockTME!i@StockTME.users.quakenet.org PRIVMSG #stocktest :yea') -privmsg
- if ((data.split()[1] == 'PRIVMSG') and (data.split()[2] == channel)) :
- #todo3: EVENTS ADDING IN TABLE AUTH WHEN : PRIVMSG ; if accesslvl(nick) = not exists , rest is in else
- exists = 0
- for i in range(0,len(authdb)):
- if (authdb[i][3] == nick(data.split()[0])):
- exists = 1
- authlevel=authdb[i][1]
- if (exists == 0):
- send (s, 'WHOIS ' + nick(data.split()[0]))
- #commands
- if (exists == 1):
- #owner commands
- if (authlevel == 'owner'):
- #!authdb --- shows authdb variable
- if (data.split()[3] == ':!authdb'):
- print(authdb)
- #!send --- makes bot send any raw?? text
- if (data.split()[3] == ':!send'):
- sending = ''
- for i in range(4,len(data.split())): sending= sending + data.split()[i] + ' '
- send ( s, sending[:len(sending)-1])
- #!authlevel auth level
- if (data.split()[3] == ':!authlevel'):
- authdbadd(data.split()[4],data.split()[5])
- authdb = arrayadd(data.split()[4],data.split()[5],authdb)
- print (authdb)
- send (s, 'NOTICE ' + nick(data.split()[0]) + ' :added')
- #mamber
- if ((authlevel == 'owner') or (authlevel == 'mamber')) :
- #!commands --- this command shows commands
- if (data.split()[3] == ':!commands'):
- send (s, 'NOTICE ' + nick(data.split()[0]) + ' :COMMANDS are as fallows: ')
- send (s, 'NOTICE ' + nick(data.split()[0]) + ' :!commands --- this command shows commands')
- send (s, 'NOTICE ' + nick(data.split()[0]) + ' :!i --- this command shows some informations about you')
- send (s, 'NOTICE ' + nick(data.split()[0]) + ' :!add --- this command adds you to game')
- send (s, 'NOTICE ' + nick(data.split()[0]) + ' :!remove --- this command removes you from game')
- if (authlevel == 'owner'):
- send (s, 'NOTICE ' + nick(data.split()[0]) + ' :!authdb --- shows authdb variable')
- send (s, 'NOTICE ' + nick(data.split()[0]) + ' :!send --- makes bot send any raw?? text')
- send (s, 'NOTICE ' + nick(data.split()[0]) + ' :!authlevel auth access --- adds user')
- if (data.split()[3] == ':!i'):
- send (s, 'NOTICE ' + nick(data.split()[0]) + ' :User level = ' + authlevel)
- if (data.split()[3] == ':!add'): #Modes: (0) // Players (0): ? ? ? ? ? ? ? ? ? ? ? // Obs: ? ? // i topic = 'Modes: (0) // Players (0): ? ? ? ? ? ? ? ? ? ? ? // Obs: ? ? //'
- added = 0
- if added == 0:
- for i in range(0,len(topic.split())):
- if topic.split()[i] == nick(data.split()[0]):
- added = 1
- if added == 0:
- topic = numbertopic(topic,nick(data.split()[0]),'+')
- if (data.split()[3] == ':!remove'):
- added = 0
- if added == 0:
- for i in range(0,len(topic.split())):
- if topic.split()[i] == nick(data.split()[0]):
- added = 1
- if added == 1:
- topic = numbertopic(topic,nick(data.split()[0]),'-')
- return authdb,topic
- s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
- authdb = authdbtoarray() # from local db txt file to 2d array [Row][Column]
- topic = 'Modes: (0) // Players (0):4 ? ? ? ? ? ? ? ? ? ? ? // Obs: ? ? //'
- print('Attempting to connect...')
- try:
- s.connect( ('wineasy.se.quakenet.org',6667) )
- print('Connected!')
- connected = True
- registered = False
- buffer = ''
- except:
- print('Failed to connect.', sys.exc_info() )
- connected = False
- while connected:
- data = s.recv(4096)
- if data == '': connected = False
- else:
- if not registered: # register at first server response
- print ( 'Registering:' )
- send( s, 'NICK StockBOT' )
- send( s, 'USER StockBOT StockBOT StockBOT :Stock IRC Client' )
- registered = True
- events, buffer = parse_data( data, buffer )
- for e in events: authdb,topic = event_handler( s, e ,authdb,topic)
- print('Exiting.')
- s.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement