Increased version number
[galtack.git] / galcon / net_server_list.py
blobc9bed4c288440fe0c9470dbe71044458d593c761
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """
4 Galcon networking - 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 galcon.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 exc = Exception(lines[0].split(None, 1)[1])
86 return self.__deferred.errback(exc)
87 current_version = lines[0].split()[1].split(".")
88 server_lines = lines[3:]
89 server_info_list = []
90 for server_line in server_lines:
91 if server_line == "": continue
92 owner_allowed, name, version, more = server_line.split('|')
93 owner, allowed = owner_allowed.split('\t')
94 num, pwd_protected, addr, secret, status = more.split('\t')
95 server_info_list.append(ServerInfo(
96 owner = owner,
97 allowed = allowed,
98 name = name,
99 version = version,
100 num = int(num),
101 pwd_protected = bool(int(pwd_protected)),
102 addr = addr,
103 secret = secret,
104 status = status,
105 # bots_ok = owner.lower() == 'korin',
106 bots_ok = (status == "open"),
108 self.__deferred.callback((current_version, server_info_list))
109 except:
110 self.__deferred.errback()
113 class ServerListClientFactory(ClientFactory):
115 Galcon HTTP client factory for retrieving the list of servers (games)
116 from www.imitationpickles.org.
118 Usage:
119 >>> deferred = defer.Deferred()
120 >>> user_info = UserInfo(email, name, passwd, platform, version)
121 >>> factory = ServerListClientFactory(deferred, user_info)
122 >>> reactor.connectTCP("imitationpickles.org", 80, factory)
124 deferred callback / errback signatures:
125 def cb((current_version, server_info_list)) # return None
126 def eb(failure) # return None
129 protocol = ServerListClient
131 def __init__(self, deferred, user_info):
132 self.__deferred = deferred
133 self.__user_info = user_info
135 def buildProtocol(self, addr):
136 return self.protocol(self.__deferred, self.__user_info)
139 if __name__ == "__main__":
140 import sys
141 print "Running this Python module is not yet supported."
142 sys.exit(1)