-
Notifications
You must be signed in to change notification settings - Fork 166
/
gamespy_profile_server.py
797 lines (660 loc) · 29.7 KB
/
gamespy_profile_server.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
"""DWC Network Server Emulator
Copyright (C) 2014 polaris-
Copyright (C) 2014 ToadKing
Copyright (C) 2014 AdmiralCurtiss
Copyright (C) 2014 msoucy
Copyright (C) 2015 Sepalani
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import logging
import time
import traceback
from twisted.internet.protocol import Factory
from twisted.internet.endpoints import serverFromString
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
from twisted.internet.error import ReactorAlreadyRunning
import gamespy.gs_database as gs_database
import gamespy.gs_query as gs_query
import gamespy.gs_utility as gs_utils
import other.utils as utils
import dwc_config
logger = dwc_config.get_logger('GameSpyProfileServer')
address = dwc_config.get_ip_port('GameSpyProfileServer')
class GameSpyProfileServer(object):
def __init__(self):
pass
def start(self):
endpoint = serverFromString(
reactor,
"tcp:%d:interface=%s" % (address[1], address[0])
)
conn = endpoint.listen(PlayerFactory())
try:
if not reactor.running:
reactor.run(installSignalHandlers=0)
except ReactorAlreadyRunning:
pass
class PlayerFactory(Factory):
def __init__(self):
"""Player Factory.
Instead of storing the sessions in the database, it might make more
sense to store them in the PlayerFactory.
"""
logger.log(logging.INFO,
"Now listening for connections on %s:%d...",
address[0], address[1])
self.sessions = {}
def buildProtocol(self, address):
return PlayerSession(self.sessions, address)
class PlayerSession(LineReceiver):
def __init__(self, sessions, address):
self.setRawMode() # We're dealing with binary data so set to raw mode
self.db = gs_database.GamespyDatabase()
self.sessions = sessions
self.address = address
# Stores any unparsable/incomplete commands until the next
# rawDataReceived
self.remaining_message = ""
self.profileid = 0
self.gameid = ""
self.buddies = []
self.blocked = []
self.status = ""
self.statstring = ""
self.locstring = ""
self.keepalive = int(time.time())
self.sesskey = ""
self.sdkrevision = "0"
def log(self, level, msg, *args, **kwargs):
if not self.profileid:
if not self.gameid:
logger.log(level, "[%s:%d] " + msg,
self.address.host, self.address.port,
*args, **kwargs)
else:
logger.log(level, "[%s:%d | %s] " + msg,
self.address.host, self.address.port, self.gameid,
*args, **kwargs)
else:
if not self.gameid:
logger.log(level, "[%s:%d | %d] " + msg,
self.address.host, self.address.port,
self.profileid, *args, **kwargs)
else:
logger.log(level, "[%s:%d | %d | %s] " + msg,
self.address.host, self.address.port,
self.profileid, self.gameid, *args, **kwargs)
def get_ip_as_int(self, address):
ipaddress = 0
if address is not None:
for n in address.split('.'):
ipaddress = (ipaddress << 8) | int(n)
return ipaddress
def connectionMade(self):
try:
self.transport.setTcpKeepAlive(1)
self.log(logging.INFO,
"Received connection from %s:%d",
self.address.host, self.address.port)
# Create new session id
self.session = ""
# Generate a random challenge string
self.challenge = utils.generate_random_str(
10, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
)
# The first command sent to the client is always a login challenge
# containing the server challenge key.
msg = gs_query.create_gamespy_message([
('__cmd__', "lc"),
('__cmd_val__', "1"),
('challenge', self.challenge),
('id', "1"),
])
self.log(logging.DEBUG, "SENDING: '%s'...", msg)
self.transport.write(bytes(msg))
except:
self.log(logging.ERROR,
"Unknown exception: %s",
traceback.format_exc())
def connectionLost(self, reason):
try:
self.log(logging.INFO, "%s", "Client disconnected")
self.status = "0"
self.statstring = "Offline"
self.locstring = ""
self.send_status_to_friends()
if self.profileid in self.sessions:
del self.sessions[self.profileid]
self.db.delete_session(self.sesskey)
self.log(logging.INFO, "Deleted session %s", self.session)
except:
self.log(logging.ERROR,
"Unknown exception: %s",
traceback.format_exc())
def rawDataReceived(self, data):
try:
self.log(logging.DEBUG, "RESPONSE: '%s'...", data)
# In the case where command string is too big to fit into one
# read, any parts that could not be successfully parsed are
# stored in the variable remaining_message. On the next
# rawDataReceived command, the remaining message and the data
# are combined to create a full command.
data = self.remaining_message + data
# Check to make sure the data buffer starts with a valid command.
if len(data) > 0 and data[0] != '\\':
# There is data in the buffer but it doesn't start with a \ so
# there's no chance of it being valid. Look for the first
# instance of \final\ and remove everything before it. If
# \final\ is not in the command string then ignore it.
final = "\\final\\"
data = data[data.index(final) + len(final):] \
if final in data else ""
commands, self.remaining_message = \
gs_query.parse_gamespy_message(data)
cmds = {
"login": self.perform_login,
"logout": self.perform_logout,
"getprofile": self.perform_getprofile,
"updatepro": self.perform_updatepro,
"ka": self.perform_ka,
"status": self.perform_status,
"bm": self.perform_bm,
"addbuddy": self.perform_addbuddy,
"delbuddy": self.perform_delbuddy,
"authadd": self.perform_authadd,
}
def cmd_err(data_parsed):
# Maybe write unknown commands to a separate file later so
# new data can be collected more easily?
self.log(logging.ERROR,
"Found unknown command, don't know how to handle"
" '%s'.", data_parsed['__cmd__'])
for data_parsed in commands:
# self.log(-1, data_parsed)
self.log(logging.DEBUG, "%s", data_parsed)
cmds.get(data_parsed['__cmd__'], cmd_err)(data_parsed)
except:
self.log(logging.ERROR,
"Unknown exception: %s",
traceback.format_exc())
def perform_login(self, data_parsed):
authtoken_parsed = gs_utils.parse_authtoken(data_parsed['authtoken'],
self.db)
if authtoken_parsed is None:
self.log(logging.WARNING, "%s", "Invalid Authtoken.")
msg = gs_query.create_gamespy_message([
('__cmd__', "error"),
('__cmd_val__', ""),
('err', '266'),
('fatal', ''),
('errmsg', 'There was an error validating the'
' pre-authentication.'),
('id', data_parsed['id']),
])
self.transport.write(bytes(msg))
return
if 'sdkrevision' in data_parsed:
self.sdkrevision = data_parsed['sdkrevision']
# Verify the client's response
valid_response = gs_utils.generate_response(
self.challenge,
authtoken_parsed['challenge'],
data_parsed['challenge'],
data_parsed['authtoken']
)
if data_parsed['response'] != valid_response:
self.log(logging.ERROR,
"ERROR: Got invalid response."
" Got %s, expected %s",
data_parsed['response'], valid_response)
proof = gs_utils.generate_proof(
self.challenge,
authtoken_parsed['challenge'],
data_parsed['challenge'],
data_parsed['authtoken']
)
userid, profileid, gsbrcd, uniquenick = \
gs_utils.login_profile_via_parsed_authtoken(authtoken_parsed,
self.db)
if profileid is not None:
# Successfully logged in or created account, continue
# creating session.
loginticket = gs_utils.base64_encode(
utils.generate_random_str(16)
)
self.sesskey = self.db.create_session(profileid, loginticket)
self.sessions[profileid] = self
self.buddies = self.db.get_buddy_list(self.profileid)
self.blocked = self.db.get_blocked_list(self.profileid)
if self.sdkrevision == "11": # Used in Tatsunoko vs Capcom
def make_list(data):
return [str(d['buddyProfileId'])
for d in data
if d['status'] == 1]
block_list = make_list(self.blocked)
msg = gs_query.create_gamespy_message([
('__cmd__', "blk"),
('__cmd_val__', str(len(block_list))),
('list', ','.join(block_list)),
])
self.log(logging.DEBUG, "SENDING: %s", msg)
self.transport.write(bytes(msg))
buddy_list = make_list(self.buddies)
msg = gs_query.create_gamespy_message([
('__cmd__', "bdy"),
('__cmd_val__', str(len(buddy_list))),
('list', ','.join(buddy_list)),
])
self.log(logging.DEBUG, "SENDING: %s", msg)
self.transport.write(bytes(msg))
msg = gs_query.create_gamespy_message([
('__cmd__', "lc"),
('__cmd_val__', "2"),
('sesskey', self.sesskey),
('proof', proof),
('userid', userid),
('profileid', profileid),
('uniquenick', uniquenick),
# Some kind of token... don't know it gets used or generated,
# but it doesn't seem to have any negative effects if it's
# not properly generated.
('lt', loginticket),
('id', data_parsed['id']),
])
# Take the first 4 letters of gsbrcd instead of gamecd because
# they should be consistent across game regions. For example, the
# US version of Metroid Prime Hunters has the gamecd "AMHE" and
# the first 4 letters of gsbrcd are "AMHE". However, the Japanese
# version of Metroid Prime Hunters has the gamecd "AMHJ" with the
# first 4 letters of bsbrcd as "AMHE". Tetris DS is the other way,
# with the first 4 letters as the Japanese version (ATRJ) while
# the gamecd is region specific (ATRE for US and ATRJ for JP).
# gameid is used to send all people on the player's friends list a
# status updates, so don't make it region specific.
self.gameid = gsbrcd[:4]
self.profileid = int(profileid)
self.log(logging.DEBUG, "SENDING: %s", msg)
self.transport.write(bytes(msg))
# Get pending messages.
self.get_pending_messages()
# Send any friend statuses when the user logs in.
# This will allow the user to see if their friends are hosting a
# game as soon as they log in.
self.get_status_from_friends()
self.send_status_to_friends()
# profile = self.db.get_profile_from_profileid(profileid)
# if profile is not None:
# self.statstring = profile['stat']
# self.locstring = profile['loc']
else:
self.log(logging.INFO, "%s", "Invalid password or banned user")
msg = gs_query.create_gamespy_message([
('__cmd__', "error"),
('__cmd_val__', ""),
('err', '256'),
('fatal', ''),
('errmsg', 'Login failed.'),
('id', data_parsed['id']),
])
self.log(logging.DEBUG, "SENDING: %s", msg)
self.transport.write(bytes(msg))
def perform_logout(self, data_parsed):
self.log(logging.INFO,
"Session %s has logged off",
data_parsed['sesskey'])
self.db.delete_session(data_parsed['sesskey'])
if self.profileid in self.sessions:
del self.sessions[self.profileid]
self.transport.loseConnection()
def perform_getprofile(self, data_parsed):
# profile = self.db.get_profile_from_session_key(
# data_parsed['sesskey']
# )
profile = self.db.get_profile_from_profileid(data_parsed['profileid'])
# Wii example:
# \pi\\profileid\474888031\nick\5pde5vhn1WR9E2g1t533\userid\442778352
# \email\5pde5vhn1WR9E2g1t533@nds\sig\b126556e5ee62d4da9629dfad0f6b2a8
# \uniquenick\5pde5vhn1WR9E2g1t533\pid\11\lon\0.000000\lat\0.000000
# \loc\\id\2\final\
sig = utils.generate_random_hex_str(32)
msg_d = [
('__cmd__', "pi"),
('__cmd_val__', ""),
('profileid', profile['profileid']),
('nick', profile['uniquenick']),
('userid', profile['userid']),
('email', profile['email']),
('sig', sig),
('uniquenick', profile['uniquenick']),
('pid', profile['pid']),
]
if profile['firstname']:
# Wii gets a firstname
msg_d.append(('firstname', profile['firstname']))
if profile['lastname']:
msg_d.append(('lastname', profile['lastname']))
msg_d.extend([
('lon', profile['lon']),
('lat', profile['lat']),
('loc', profile['loc']),
('id', data_parsed['id']),
])
msg = gs_query.create_gamespy_message(msg_d)
self.log(logging.DEBUG, "SENDING: %s", msg)
self.transport.write(bytes(msg))
def perform_updatepro(self, data_parsed):
"""Wii example:
\updatepro\\sesskey\199714190\firstname\Wii:2555151656076614@WR9E
\partnerid\11\final\
Remove any fields not related to what we should be updating.
To avoid any crashes, make sure the key is actually in the dictionary
before removing it.
"""
if "__cmd__" in data_parsed:
data_parsed.pop('__cmd__')
if "__cmd_val__" in data_parsed:
data_parsed.pop('__cmd_val__')
if "updatepro" in data_parsed:
data_parsed.pop('updatepro')
if "partnerid" in data_parsed:
data_parsed.pop('partnerid')
if "sesskey" in data_parsed:
data_parsed.pop('sesskey')
# Create a list of fields to be updated.
for f in data_parsed:
self.db.update_profile(self.profileid, (f, data_parsed[f]))
def perform_ka(self, data_parsed):
self.keepalive = int(time.time())
msg = gs_query.create_gamespy_message([
('__cmd__', "ka"),
('__cmd_val__', ""),
])
self.transport.write(msg)
def perform_status(self, data_parsed):
self.sesskey = data_parsed['sesskey']
self.status = data_parsed['__cmd_val__']
self.statstring = data_parsed['statstring']
self.locstring = data_parsed['locstring']
# Send authorization requests to client
self.get_buddy_requests()
# Send authorizationed message to client
self.get_buddy_authorized()
self.send_status_to_friends()
def perform_bm(self, data_parsed):
# Message to/from clients?
if data_parsed['__cmd_val__'] in ("1", "5", "102", "103"):
if "t" in data_parsed:
# Send message to the profile id in "t"
dest_profileid = int(data_parsed['t'])
dest_profile_buddies = self.db.get_buddy_list(dest_profileid)
dest_msg = data_parsed['msg']
not_buddies = False
# Check if the user is buddies with the target user before
# sending message.
if not_buddies:
for buddy in self.buddies:
if buddy['userProfileId'] == dest_profileid:
not_buddies = True
break
if not_buddies:
for buddy in dest_profile_buddies:
if buddy['userProfileId'] == self.profile:
not_buddies = True
break
# Send error to user if they tried to send a message to
# someone who isn't a buddy.
if not_buddies:
msg = gs_query.create_gamespy_message([
('__cmd__', "error"),
('__cmd_val__', ""),
('err', 2305),
('errmsg',
"The profile the message was to be sent to is not"
" a buddy."),
('id', 1),
])
logger.log(logging.DEBUG,
"Trying to send message to someone who isn't"
" a buddy: %s", msg)
self.transport.write(msg)
return
msg = gs_query.create_gamespy_message([
('__cmd__', "bm"),
('__cmd_val__', "1"),
('f', self.profileid),
('msg', dest_msg),
])
if dest_profileid in self.sessions:
self.log(logging.DEBUG,
"SENDING TO %s:%s: %s",
self.sessions[dest_profileid].address.host,
self.sessions[dest_profileid].address.port, msg)
self.sessions[dest_profileid].transport.write(bytes(msg))
self.send_status_to_friends(dest_profileid)
self.get_status_from_friends(dest_profileid)
else:
if data_parsed['__cmd_val__'] == "1":
self.log(logging.DEBUG,
"Saving message to %d: %s",
dest_profileid, msg)
self.db.save_pending_message(self.profileid,
dest_profileid, msg)
else:
msg = gs_query.create_gamespy_message([
('__cmd__', "error"),
('__cmd_val__', ""),
('err', 2307),
('errmsg', "The buddy to send a message to"
" is offline."),
('id', 1),
])
logger.log(logging.DEBUG,
"Trying to send message to someone who"
" isn't online: %s", msg)
self.transport.write(msg)
def perform_addbuddy(self, data_parsed):
newprofileid = int(data_parsed['newprofileid'])
if newprofileid == self.profileid:
logger.log(logging.DEBUG,
"Can't add self as friend: %d == %d",
newprofileid, self.profileid)
return
# Sample:
# \addbuddy\\sesskey\231601763\newprofileid\476756820\reason\\final\
self.buddies = self.db.get_buddy_list(self.profileid)
buddy_exists = False
for buddy in self.buddies:
if buddy['buddyProfileId'] == newprofileid:
buddy_exists = True
break
if not buddy_exists:
self.db.add_buddy(self.profileid, newprofileid)
if newprofileid in self.sessions:
logger.log(logging.DEBUG,
"User is online, sending direct request from"
" profile id %d to profile id %d...",
self.profileid, newprofileid)
# TODO: Add a way to check if a profile id is already a buddy
# using SQL
other_player_authorized = False
target_buddy_list = self.db.get_buddy_list(newprofileid)
logger.log(logging.DEBUG, "%s", target_buddy_list)
for buddy in target_buddy_list:
if buddy['buddyProfileId'] == self.profileid and \
not buddy['blocked']:
other_player_authorized = True
break
if other_player_authorized:
logger.log(logging.DEBUG,
"Automatic authorization: %d (target) already"
" has %d (source) as a friend.",
newprofileid, self.profileid)
# Force them both to add each other
self.send_buddy_request(self.sessions[newprofileid],
self.profileid)
self.send_buddy_request(self.sessions[self.profileid],
newprofileid)
self.send_bm4(newprofileid)
self.db.auth_buddy(newprofileid, self.profileid)
self.db.auth_buddy(self.profileid, newprofileid)
self.send_status_to_friends(newprofileid)
self.get_status_from_friends(newprofileid)
else:
self.send_buddy_request(self.sessions[newprofileid],
self.profileid)
else:
# Trying to add someone who is already a friend.
# Just send status updates.
self.send_status_to_friends(newprofileid)
self.get_status_from_friends(newprofileid)
self.buddies = self.db.get_buddy_list(self.profileid)
def send_bm4(self, playerid):
date = int(time.time())
msg = gs_query.create_gamespy_message([
('__cmd__', "bm"),
('__cmd_val__', "4"),
('f', playerid),
('date', date),
('msg', ""),
])
self.transport.write(bytes(msg))
def perform_delbuddy(self, data_parsed):
"""Sample:
\delbuddy\\sesskey\61913621\delprofileid\1\final\
"""
self.db.delete_buddy(self.profileid, int(data_parsed['delprofileid']))
self.buddies = self.db.get_buddy_list(self.profileid)
def perform_authadd(self, data_parsed):
"""Authorize the other person's friend request.
Sample:
\authadd\\sesskey\231587549\fromprofileid\217936895
\sig\f259f26d3273f8bda23c7c5e4bd8c5aa\final\
"""
target_profile = int(data_parsed['fromprofileid'])
self.db.auth_buddy(target_profile, self.profileid)
self.get_buddy_authorized()
self.buddies = self.db.get_buddy_list(self.profileid)
self.send_bm4(target_profile)
self.send_status_to_friends(target_profile)
self.get_status_from_friends(target_profile)
def send_status_to_friends(self, buddy_profileid=None):
"""TODO: Cache buddy list so we don't have to query the
database every time."""
self.buddies = self.db.get_buddy_list(self.profileid)
if self.status == "0" and self.statstring == "Offline":
# Going offline, don't need to send the other information.
status_msg = "|s|%s|ss|%s" % (self.status, self.statstring)
else:
status_msg = "|s|%s|ss|%s|ls|%s|ip|%d|p|0|qm|0" % (
self.status, self.statstring, self.locstring,
self.get_ip_as_int(self.address.host)
)
msg = gs_query.create_gamespy_message([
('__cmd__', "bm"),
('__cmd_val__', "100"),
('f', self.profileid),
('msg', status_msg),
])
buddy_list = self.buddies
if buddy_profileid is not None:
buddy_list = [{"buddyProfileId": buddy_profileid}]
for buddy in buddy_list:
if buddy['buddyProfileId'] in self.sessions:
# self.log(logging.DEBUG,
# "Sending status to buddy id %s (%s:%d): %s",
# str(buddy['buddyProfileId']),
# self.sessions[
# buddy['buddyProfileId']
# ].address.host,
# self.sessions[
# buddy['buddyProfileId']
# ].address.port, msg)
self.sessions[buddy['buddyProfileId']].transport \
.write(bytes(msg))
def get_status_from_friends(self, buddy_profileid=None):
"""This will be called when the player logs in.
Grab the player's buddy list and check the current sessions to
see if anyone is online. If they are online, make them send an update
to the calling client.
"""
self.buddies = self.db.get_buddy_list(self.profileid)
buddy_list = self.buddies
if buddy_profileid is not None:
buddy_list = [{"buddyProfileId": buddy_profileid}]
for buddy in self.buddies:
if buddy['status'] != 1:
continue
if buddy['buddyProfileId'] in self.sessions and \
self.sessions[buddy['buddyProfileId']].gameid == self.gameid:
status_msg = "|s|%s|ss|%s|ls|%s|ip|%d|p|0|qm|0" % (
self.sessions[buddy['buddyProfileId']].status,
self.sessions[buddy['buddyProfileId']].statstring,
self.sessions[buddy['buddyProfileId']].locstring,
self.get_ip_as_int(self.sessions[
buddy['buddyProfileId']
].address.host))
else:
status_msg = "|s|0|ss|Offline"
msg = gs_query.create_gamespy_message([
('__cmd__', "bm"),
('__cmd_val__', "100"),
('f', buddy['buddyProfileId']),
('msg', status_msg),
])
self.transport.write(bytes(msg))
def get_buddy_authorized(self):
buddies = self.db.buddy_need_auth_message(self.profileid)
for buddy in buddies:
msg = gs_query.create_gamespy_message([
('__cmd__', "bm"),
('__cmd_val__', "1"),
('f', buddy['userProfileId']),
('msg', "I have authorized your request to add me to"
" your list"),
])
self.transport.write(bytes(msg))
self.db.buddy_sent_auth_message(buddy['userProfileId'],
buddy['buddyProfileId'])
def get_buddy_requests(self):
"""Get list people who have added the user but haven't been accepted
yet."""
buddies = self.db.get_pending_buddy_requests(self.profileid)
for buddy in buddies:
self.send_buddy_request(self,
buddy['userProfileId'],
buddy['time'])
def send_buddy_request(self, session, profileid, senttime=None):
sig = utils.generate_random_hex_str(32)
msg = "\r\n\r\n"
msg += "|signed|" + sig
if senttime is None:
senttime = int(time.time())
msg = gs_query.create_gamespy_message([
('__cmd__', "bm"),
('__cmd_val__', "2"),
('f', profileid),
('date', senttime),
('msg', msg),
])
session.transport.write(bytes(msg))
def get_pending_messages(self):
messages = self.db.get_pending_messages(self.profileid)
for message in messages:
if message['sourceid'] not in self.blocked:
try:
self.transport.write(bytes(message['msg']))
except:
self.transport.write(bytes(message['msg'], "utf-8"))
if __name__ == "__main__":
gsps = GameSpyProfileServer()
gsps.start()