Fixed GaltackClientIgnoreResentCmdsMixin
[galtack.git] / galtack / net_server_list.py
blobd7285e1b84d6e827801007e5471613d05cfd14f7
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """
4 GalTacK networking - Galcon server list retrieval.
5 """
6 # Copyright (C) 2007 Felix Rabe <public@felixrabe.textdriven.com>
7 # Copyright (C) 2007 Michael Carter
9 # Permission is hereby granted, free of charge, to any person obtaining a
10 # copy of this software and associated documentation files (the
11 # "Software"), to deal in the Software without restriction, including
12 # without limitation the rights to use, copy, modify, merge, publish,
13 # distribute, sublicense, and/or sell copies of the Software, and to permit
14 # persons to whom the Software is furnished to do so, subject to the
15 # following conditions:
17 # The above copyright notice and this permission notice shall be included
18 # in all copies or substantial portions of the Software.
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
25 # OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
26 # THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 # Recommended line length or text width: 75 characters.
30 import urllib
32 from twisted.internet import reactor, defer
33 from twisted.internet.protocol import ClientFactory
34 from twisted.web import http
36 from galtack.net_common import *
39 def get_server_list(user_info):
40 """
41 Retrieve a list of servers (games).
42 """
43 deferred = defer.Deferred()
44 factory = ServerListClientFactory(deferred, user_info)
45 reactor.connectTCP("imitationpickles.org", 80, factory)
46 return deferred
49 class ServerListException(Exception): pass
52 class ServerListClient(http.HTTPClient):
53 """
54 Galcon HTTP client for retrieving the list of servers (games) from
55 www.imitationpickles.org.
56 """
58 URL = "/galcon/api.php"
60 def __init__(self, deferred, user_info):
61 self.__deferred = deferred
62 self.__user_info = user_info
64 def connectionMade(self):
65 url_args = {
66 'action': 'list',
67 'reg_email': self.__user_info.email,
68 'name': self.__user_info.name,
69 'passwd': self.__user_info.passwd,
70 'platform': self.__user_info.platform,
71 'version': self.__user_info.version,
73 url = self.URL + '?' + urllib.urlencode(url_args.items())
74 self.sendCommand('GET', url)
75 self.sendHeader('Accept-Encoding', 'identity')
76 self.sendHeader('Host', 'www.imitationpickles.org')
77 self.sendHeader('Connection', 'close')
78 self.sendHeader('User-agent', 'Python-urllib/2.4')
79 self.endHeaders()
81 def handleResponse(self, response):
82 try:
83 lines = response.split('\n')
84 if lines[0].startswith("ERROR:"):
85 raise ServerListException, lines[0].split(None, 1)[1]
86 current_version = lines[0].split()[1].split(".")
87 server_lines = lines[3:]
88 server_info_list = []
89 for server_line in server_lines:
90 if server_line == "": continue
91 owner_allowed, name, version, more = server_line.split('|')
92 owner, allowed = owner_allowed.split('\t')
93 num, pwd_protected, addr, secret, status = more.split('\t')
94 server_info_list.append(ServerInfo(
95 owner = owner,
96 allowed = allowed,
97 name = name,
98 version = version,
99 num = int(num),
100 pwd_protected = bool(int(pwd_protected)),
101 addr = addr,
102 secret = secret,
103 status = status,
104 # bots_ok = owner.lower() == 'korin',
105 bots_ok = (status == "open"),
107 self.__deferred.callback((current_version, server_info_list))
108 except:
109 self.__deferred.errback()
112 class ServerListClientFactory(ClientFactory):
114 Galcon HTTP client factory for retrieving the list of servers (games)
115 from www.imitationpickles.org.
117 Usage:
118 >>> deferred = defer.Deferred()
119 >>> user_info = UserInfo(email, name, passwd, platform, version)
120 >>> factory = ServerListClientFactory(deferred, user_info)
121 >>> reactor.connectTCP("imitationpickles.org", 80, factory)
123 deferred callback / errback signatures:
124 def cb((current_version, server_info_list)) # return None
125 def eb(failure) # return None
128 protocol = ServerListClient
130 def __init__(self, deferred, user_info):
131 self.__deferred = deferred
132 self.__user_info = user_info
134 def buildProtocol(self, addr):
135 return self.protocol(self.__deferred, self.__user_info)
138 if __name__ == "__main__":
139 import sys
140 print "Running this Python module is not yet supported."
141 sys.exit(1)