CRAW now runs on Windows 7 too - the problem was that Windows 7 has moved some functi...
[craw.git] / script / bncs.py
blob0a5996aca964c24927e8f35b70f592c0b2eda410
1 import nil.string, nil.thread, utility, craw, packets, time, configuration, threading
3 class bncs_packet_handler_class:
4 def __init__(self):
5 self.lock = threading.Lock()
6 self.whois_name_queue = []
7 self.whois_handler_queue = []
8 self.account_map = {}
9 self.entering_game = False
10 self.has_thread = False
12 def process_bytes(self, bytes):
13 if not self.has_thread:
14 #print 'Creating whois thread'
15 nil.thread.create_thread(self.whois_thread)
16 self.has_thread = True
18 if packets.entering_game(bytes):
19 self.entering_game = True
21 assignment = packets.parse_assignment(bytes)
22 if assignment != None and self.entering_game:
23 player_id, character_class, player_name, x, y = assignment
24 #print 'Assignment: %s' % player_name
25 self.entering_game = False
26 self.whois(player_name, None)
28 def process_packet(self, packet):
29 if len(packet) >= 2 and packet[0 : 2] == '\xff\x0f':
30 account = nil.string.extract_string(packet, '(*', ')')
31 if account == None:
32 return
34 handler = None
36 self.lock.acquire()
37 if len(self.whois_handler_queue) > 0:
38 name, handler = self.whois_handler_queue[0]
39 self.account_map[name] = account
40 self.whois_handler_queue = self.whois_handler_queue[1 : ]
41 self.lock.release()
43 if handler != None:
44 handler(account)
46 def whois(self, name, handler):
47 if name == None:
48 print 'Received /whois None request'
49 return
51 #print 'Running whois on %s: %s' % (name, repr(self.account_map))
52 if name in self.account_map:
53 #print '%s was cached' % name
54 if handler != None:
55 handler(self.account_map[name])
56 return
58 self.lock.acquire()
59 self.whois_name_queue.append(name)
60 self.whois_handler_queue.append((name, handler))
61 self.lock.release()
63 def whois_thread(self):
64 while True:
65 self.lock.acquire()
66 if len(self.whois_name_queue) > 0:
67 name = self.whois_name_queue[0]
68 #print 'Whois thread is processing %s' % name
69 command = '/whois %s' % name
70 packet = '\xff\x0e' + utility.pack_number(len(command) + 5, 1) + '\x00' + command + '\x00'
71 #Missing BNCS connection check?
72 craw.send_bncs_packet(packet)
73 self.whois_name_queue = self.whois_name_queue[1 : ]
74 self.lock.release()
75 time.sleep(configuration.whois_delay)