1 import nil
.string
, nil
.thread
, utility
, craw
, packets
, time
, configuration
, threading
3 class bncs_packet_handler_class
:
5 self
.lock
= threading
.Lock()
6 self
.whois_name_queue
= []
7 self
.whois_handler_queue
= []
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
, '(*', ')')
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 : ]
46 def whois(self
, name
, handler
):
48 print 'Received /whois None request'
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
55 handler(self
.account_map
[name
])
59 self
.whois_name_queue
.append(name
)
60 self
.whois_handler_queue
.append((name
, handler
))
63 def whois_thread(self
):
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 : ]
75 time
.sleep(configuration
.whois_delay
)