· 9 years ago · Sep 17, 2017, 12:56 AM
1import sys
2import socket
3
4def authdbtoarray():
5 f=open('authdb.txt','r')
6 niz2=f.read()
7 f.close()
8 niz2 = niz2.split('\n')
9 cols = 3
10 rows = len(niz2)
11 niz = [[0 for i in range(cols)] for j in range(rows)]
12 for i in range(0,len(niz2)):
13 niz[i]=niz2[i].split('\t')
14 return niz
15
16def authdbadd(auth,access):
17 f=open('authdb.txt','a')
18 f.write ( '\n*\t' + access + '\t' + auth + '\t-' )
19 f.close()
20
21def arrayadd(auth,access,authdb):
22 newauthdb = [[0 for i in range(4)] for j in range(len(authdb)+1)]
23 for i in range (0,len(newauthdb)):
24 if (i < len(newauthdb)-1):
25 newauthdb[i] = authdb[i]
26 if (i == len(newauthdb)-1):
27 newauthdb[i] = ['jea','jea','jea','jea']
28 newauthdb[-1][0]= '%d' % (len(newauthdb))
29 newauthdb[-1][1]= access
30 newauthdb[-1][2]= auth
31 newauthdb[-1][3]= '-'
32 return newauthdb
33def numbertopic(topic,nick,operator):
34 topic2= ''
35 fqm = 0
36 for i in range(0,len(topic.split())):
37 if operator == '+' and topic.split()[i] == '?' and fqm == 0:
38 fqm = 1
39 topic2 = topic2 + nick + ' '
40 elif operator == '-' and topic.split()[i] == nick and fqm == 0 :
41 fqm = 1
42 topic2 = topic2 + '?' + ' '
43 elif i == 4:
44 if operator == '+':
45 broj = int(topic.split()[4][1])+1
46 broj = str(broj)
47 if operator == '-':
48 broj = int(topic.split()[4][1])-1
49 broj = str(broj)
50 topic2 = topic2 + '(' + broj + '):4' + ' '
51 else:
52 topic2 = topic2 + topic.split()[i] + ' '
53 topic = topic2[:len(topic2)-1]
54 if broj == '10':
55 send (s, 'PRIVMSG #shitnub :' + 'GAME READY')
56 topic = 'Modes: (0) // Players (0):4 ? ? ? ? ? ? ? ? ? ? ? // Obs: ? ? //'
57 send (s, 'TOPIC #shitnub :' + topic)
58 return topic
59
60#TODO2 : access level
61#data for todo:('event_handler handling:', ':wineasy2.se.quakenet.org 330 StockBOT StockTME2 StockTME :is authed as')
62
63def nick( data ): #pharses :nick!@blahlbah.blah to nick
64 nick = ''
65 for i in range(1,len(data)):
66 if (data[i] == '!'): break
67 nick = nick + data[i]
68 return nick
69
70def send( s, data ):
71 s.send( data + '\r\n' )
72 print ( 'sent: ', data )
73
74def parse_data( data, buffer ):
75 data = buffer + data
76 events = data.split( '\n' ) # split events by \n as EFNet doesn't include \r
77 buffer = events.pop()
78 return( events, buffer )
79
80def event_handler( s, data , authdb,topic): # create list of events
81 channel = '#shitnub'
82 data = data.rstrip( '\r' ) # remove \r at end (for EFNet compability)
83 print ( 'event_handler handling:', data )
84 if data.split()[0] == 'PING': send( s, 'PONG ' + data.split()[1] )
85
86 #perform after identifying on server
87 if data.split()[1] == '221':
88 send( s, 'PRIVMSG q@cserve.quakenet.org :auth StockTME kamikaza')
89 #('event_handler handling:', ':Q!TheQBot@CServe.quakenet.org NOTICE StockBOT :You are now logged in as StockTME.')
90 if data.split()[1]== 'NOTICE':
91 if data == ':Q!TheQBot@CServe.quakenet.org NOTICE StockBOT :You are now logged in as StockTME.':
92 send(s,'JOIN #shitnub')
93 if data.split()[1] == 'MODE' and data.split()[2] == '#shitnub' and data.split()[3] == '+o' and data.split()[4] == 'StockBOT':
94 send(s,'TOPIC #shitnub :' + topic)
95
96
97
98 #when whois store in table nick to auth
99 #data for todo:('event_handler handling:', ':wineasy2.se.quakenet.org 330 StockBOT StockTME2 StockTME :is authed as')
100 if data.split()[1] == '330':
101 indatabase = 0
102 for i in range(0,len(authdb)):
103 # debuger: print ('%d : %s == %s ') % (len(authdb), authdb[i][2], data.split()[3])
104 if (authdb[i][2] == data.split()[4]):
105 authdb[i][3] = data.split()[3]
106 indatabase=1
107 if (indatabase == 0):
108 send( s, 'ban #shitnub ' + data.split()[3])
109 send( s, 'kick #shitnub ' + data.split()[3])
110
111
112 #todo1: EVENTS WHEN REGARDING AUTH : NICKCHANGE,JOIN,PART,QUIT SERVER,UNAUTHED USER?
113 #tod2: Change players in topic on events NICKCHANGE . And remove them if PART QUIT SERVER
114 #tododoremaining:
115 #data for todo - ('event_handler handling:', ':StockTME2!i@StockTME.users.quakenet.org NICK :StockTME') - nickchange
116 if (data.split()[1] == 'NICK'):
117 print(data.split()[2][2:len(data.split()[2])])
118 topic = topic.replace(nick(data.split()[0]),data.split()[2][1:])
119 send(s,'TOPIC #shitnub :' + topic)
120 send (s, 'WHOIS ' + data.split()[2][1:])
121 #data for todo - ('event_handler handling:', ':StockTME!i@StockTME.users.quakenet.org PART #stocktest') - leave
122 if ((data.split()[1] == 'PART') and (data.split()[2] == channel)):
123 topic = topic.replace(nick(data.split()[0]),'?')
124 topic = numbertopic(topic,nick(data.split()[0]),'-')
125 for i in range(0,len(authdb)):
126 if (authdb[i][3] == nick(data.split()[0])):
127 authdb[i][3] = '-'
128 #data for todo - ('event_handler handling:', ':StockTME4!i@StockTME.users.quakenet.org QUIT :Signed off')
129 if (data.split()[1] == 'QUIT'):
130 topic = topic.replace(nick(data.split()[0]),'?')
131 topic = numbertopic(topic,nick(data.split()[0]),'-')
132 for i in range(0,len(authdb)):
133 if (authdb[i][3] == nick(data.split()[0])):
134 authdb[i][3] = '-'
135 #data for todo - ('event_handler handling:', ':StockTME!i@StockTME.users.quakenet.org JOIN #stocktest') - join
136 if (data.split()[1] == 'JOIN') and (data.split()[2] == channel):
137 send (s, 'WHOIS ' + nick(data.split()[0]))
138 #data for todo - ('event_handler handling:', ':wineasy2.se.quakenet.org 353 StockBOT = #shitnub :StockBOT @StockBAus @Q')
139 if (data.split()[1] == '353') and (data.split()[2] == 'StockBOT') and (data.split()[3] == '=') and (data.split()[4] == channel):
140 for i in range(6,len(data.split())):
141 if data.split()[i][0] == '@' and data.split()[i] != '@Q' and data.split()[i] != '@S':
142 send (s, 'WHOIS ' + data.split()[i][1:len(data.split()[i])])
143 else:
144 send (s, 'WHOIS ' + data.split()[i])
145 #data for todo - ('event_handler handling:', ':StockTME!i@StockTME.users.quakenet.org PRIVMSG #stocktest :yea') -privmsg
146 if ((data.split()[1] == 'PRIVMSG') and (data.split()[2] == channel)) :
147 #todo3: EVENTS ADDING IN TABLE AUTH WHEN : PRIVMSG ; if accesslvl(nick) = not exists , rest is in else
148 exists = 0
149 for i in range(0,len(authdb)):
150 if (authdb[i][3] == nick(data.split()[0])):
151 exists = 1
152 authlevel=authdb[i][1]
153 if (exists == 0):
154 send (s, 'WHOIS ' + nick(data.split()[0]))
155 #commands
156 if (exists == 1):
157 #owner commands
158 if (authlevel == 'owner'):
159 #!authdb --- shows authdb variable
160 if (data.split()[3] == ':!authdb'):
161 print(authdb)
162 #!send --- makes bot send any raw?? text
163 if (data.split()[3] == ':!send'):
164 sending = ''
165 for i in range(4,len(data.split())): sending= sending + data.split()[i] + ' '
166 send ( s, sending[:len(sending)-1])
167 #!authlevel auth level
168 if (data.split()[3] == ':!authlevel'):
169 authdbadd(data.split()[4],data.split()[5])
170 authdb = arrayadd(data.split()[4],data.split()[5],authdb)
171 print (authdb)
172 send (s, 'NOTICE ' + nick(data.split()[0]) + ' :added')
173
174 #mamber
175 if ((authlevel == 'owner') or (authlevel == 'mamber')) :
176 #!commands --- this command shows commands
177 if (data.split()[3] == ':!commands'):
178 send (s, 'NOTICE ' + nick(data.split()[0]) + ' :COMMANDS are as fallows: ')
179 send (s, 'NOTICE ' + nick(data.split()[0]) + ' :!commands --- this command shows commands')
180 send (s, 'NOTICE ' + nick(data.split()[0]) + ' :!i --- this command shows some informations about you')
181 send (s, 'NOTICE ' + nick(data.split()[0]) + ' :!add --- this command adds you to game')
182 send (s, 'NOTICE ' + nick(data.split()[0]) + ' :!remove --- this command removes you from game')
183 if (authlevel == 'owner'):
184 send (s, 'NOTICE ' + nick(data.split()[0]) + ' :!authdb --- shows authdb variable')
185 send (s, 'NOTICE ' + nick(data.split()[0]) + ' :!send --- makes bot send any raw?? text')
186 send (s, 'NOTICE ' + nick(data.split()[0]) + ' :!authlevel auth access --- adds user')
187 if (data.split()[3] == ':!i'):
188 send (s, 'NOTICE ' + nick(data.split()[0]) + ' :User level = ' + authlevel)
189 if (data.split()[3] == ':!add'): #Modes: (0) // Players (0): ? ? ? ? ? ? ? ? ? ? ? // Obs: ? ? // i topic = 'Modes: (0) // Players (0): ? ? ? ? ? ? ? ? ? ? ? // Obs: ? ? //'
190 added = 0
191 if added == 0:
192 for i in range(0,len(topic.split())):
193 if topic.split()[i] == nick(data.split()[0]):
194 added = 1
195 if added == 0:
196 topic = numbertopic(topic,nick(data.split()[0]),'+')
197
198 if (data.split()[3] == ':!remove'):
199 added = 0
200 if added == 0:
201 for i in range(0,len(topic.split())):
202 if topic.split()[i] == nick(data.split()[0]):
203 added = 1
204 if added == 1:
205 topic = numbertopic(topic,nick(data.split()[0]),'-')
206
207
208 return authdb,topic
209
210s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
211authdb = authdbtoarray() # from local db txt file to 2d array [Row][Column]
212topic = 'Modes: (0) // Players (0):4 ? ? ? ? ? ? ? ? ? ? ? // Obs: ? ? //'
213print('Attempting to connect...')
214try:
215 s.connect( ('wineasy.se.quakenet.org',6667) )
216 print('Connected!')
217 connected = True
218 registered = False
219 buffer = ''
220except:
221 print('Failed to connect.', sys.exc_info() )
222 connected = False
223while connected:
224 data = s.recv(4096)
225 if data == '': connected = False
226 else:
227 if not registered: # register at first server response
228 print ( 'Registering:' )
229 send( s, 'NICK StockBOT' )
230 send( s, 'USER StockBOT StockBOT StockBOT :Stock IRC Client' )
231 registered = True
232 events, buffer = parse_data( data, buffer )
233 for e in events: authdb,topic = event_handler( s, e ,authdb,topic)
234
235print('Exiting.')
236s.close()