-
Notifications
You must be signed in to change notification settings - Fork 0
/
vuorobotti.py
228 lines (162 loc) · 5.83 KB
/
vuorobotti.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# -*- coding: utf-8 -*-
import socket
import botcommands
import threading
from threading import Timer
import sys
import sched, time
import settings
import filereader
import dom4status
#onkelmia: ctrl-c ei lopeta threadeja, eikä tajua reconnectata jos yhteys
#katkeaa. lisäksi ei tajua vaihtaa nikkiä jos nikki rekisteröity
class Ircbot:
def __init__( self):
# välttämättömiä tietoja
self.server = settings.server
self.port = settings.port
self.username = settings.name
self.realname = settings.name
self.nick = settings.nick
self.msgcount = 0
# luodaan socketit
self.socket = socket.socket()
self.listensocket = socket.socket()
# haetaan botille komennot
self.commands = botcommands.command_dict
# päälooppia toistettan kunnes done = 1
self.done = 0
# kanava jolle botti halutaan
self.channel = settings.channel
self.tmr = Timer(60, self.clearCounter, ())
self.tmr.start()
#nollataan spämminestolaskuri
def clearCounter(self):
self.msgcount = 0
self.tmr = Timer(60, self.clearCounter, ())
self.tmr.start()
def send( self, string ):
self.socket.send( (string + '\r\n'))
#lähettää viestin, jos lähetetty 6 viestiä minuutissa, ei tee mitään
def sendmsg( self, string):
if self.msgcount < 10:
self.msgcount += 1
self.send(('PRIVMSG %s :' % self.channel) + string)
def sendnotice( self, string):
if self.msgcount < 6:
self.msgcount += 1
self.send(('NOTICE %s :' % self.channel) + string)
def connect( self ):
self.listensocket.bind(('localhost', 6666))
self.listensocket.listen(5)
self.socket.connect( ( self.server, self.port ) )
self.send( 'NICK %s' % self.nick )
self.send( 'USER %s a a :%s' % ( self.username, self.realname ) )
self.send( 'JOIN %s' % self.channel )
def sendTurnChangeMsg(self, data):
print "Trying to read data on turn change"
f = filereader.read(settings.teamfile)
game = 0
for g in f:
if g.name == self.gamenamedata:
game = g
break
if game == 0:
print "Peliä " + data + " ei ole olemassa!"
return
try:
gs = dom4status.query(settings.domserver, g.port)
self.sendmsg(game.name + "n vuoro vaihtui! Vuoro " + str(gs.turn) + " alkoi!")
for nation in gs.nations:
if nation.statusnum == 254 and nation.name in game.players:
self.sendmsg(game.players[nation.name] + " tipahti pelistä!")
elif nation.statusnum == 254:
self.sendmsg("Desuiitta tipahti pelistä!")
except:
self.sendmsg("Virhe! " + game.name + "n servuun ei saa yhteyttä!")
def turnChange(self, conn):
print "yhteys"
while True:
data = conn.recv(2048)
print data
if not data:
break
else:
self.turnChangeTimer = Timer(10, self.sendTurnChangeMsg, args=([data]))
self.turnChangeTimer.start()
conn.close()
def listenTurnChange(self):
while not self.done:
conn, addr = self.listensocket.accept()
print "avataan yhteys"
t = threading.Thread(target=self.turnChange, args=(conn,))
t.start()
def check( self, line ):
print line
line = line.split(' ')
# vastataan pingiin muuten serveri katkaisee yhteyden
if line[0] == 'PING':
self.send( 'PONG :abc' )
elif len(line) > 1 and (line[1] == '437' or line[1] == '433'):
if self.nick == settings.nick2:
self.send( 'QUIT')
self.socket.close()
self.done = 1
else:
self.nick = settings.nick2
self.send( 'NICK %s' % self.nick )
self.send( 'JOIN %s' % self.channel )
try:
if line[2][0] != '#':
return
# suoritetaan komennot jos niitä on tullut
self.commands[ line[3] ].main( self , line )
except:
pass
def mainloop( self ):
buffer = ''
while not self.done:
# vastaanotetaan dataa
buffer += self.socket.recv( 4096 )
buffer = buffer.split( '\r\n' )
for line in buffer[0:-1]:
self.check( line )
buffer = buffer[-1]
print "Suljetaan botti..."
self.tmr.cancel()
class Input():
def __init__(self):
self.irc = None
def setBot(self, irc):
self.irc = irc
def read_keyboard(self):
command = ""
if self.irc is not None:
while True:
command = sys.stdin.readline().lower().rstrip()
print command
if command == 'quit':
self.irc.send( 'QUIT' )
self.irc.socket.close()
self.irc.done = 1
break;
def main():
input = Input()
thread_input = threading.Thread(target=input.read_keyboard)
irc = Ircbot()
input.setBot(irc)
irc.connect()
thread = threading.Thread(target=irc.listenTurnChange)
thread_input.start()
thread.start()
irc.mainloop()
try:
thread_input._Thread__stop()
except:
print(str(thread_input.getName()) + ' could not be terminated')
try:
thread._Thread__stop()
except:
print(str(thread.getName()) + ' could not be terminated')
sys.exit()
if __name__ == '__main__': main()