diff options
author | Dr Scofield | 2008-10-01 20:18:57 +0000 |
---|---|---|
committer | Dr Scofield | 2008-10-01 20:18:57 +0000 |
commit | e7cd583c1edcea7f600064fac89a94a52a84a553 (patch) | |
tree | 91be8efa11dad887b2e985fcdc0e4b8dbac2c4eb /share | |
parent | remove tests for inventory (diff) | |
download | opensim-SC-e7cd583c1edcea7f600064fac89a94a52a84a553.zip opensim-SC-e7cd583c1edcea7f600064fac89a94a52a84a553.tar.gz opensim-SC-e7cd583c1edcea7f600064fac89a94a52a84a553.tar.bz2 opensim-SC-e7cd583c1edcea7f600064fac89a94a52a84a553.tar.xz |
removing asterisk: it's now living at http://forge.opensimulator.org/gf/project/asteriskvoice/
Diffstat (limited to 'share')
-rw-r--r-- | share/python/asterisk/asterisk-opensim.cfg | 16 | ||||
-rwxr-xr-x | share/python/asterisk/asterisk-opensim.py | 231 | ||||
-rw-r--r-- | share/python/asterisk/create-region.sql | 4 | ||||
-rw-r--r-- | share/python/asterisk/create-table.sql | 62 | ||||
-rw-r--r-- | share/python/asterisk/create-user.sql | 5 | ||||
-rwxr-xr-x | share/python/asterisk/test-client.py | 28 | ||||
-rwxr-xr-x | share/python/matrix/matrix.py | 272 |
7 files changed, 0 insertions, 618 deletions
diff --git a/share/python/asterisk/asterisk-opensim.cfg b/share/python/asterisk/asterisk-opensim.cfg deleted file mode 100644 index 174dd7a..0000000 --- a/share/python/asterisk/asterisk-opensim.cfg +++ /dev/null | |||
@@ -1,16 +0,0 @@ | |||
1 | [xmlrpc] | ||
2 | baseurl = http://127.0.0.1:53263/ | ||
3 | debug = true | ||
4 | |||
5 | [mysql] | ||
6 | server = localhost | ||
7 | database = asterisk | ||
8 | user = asterisk | ||
9 | password = asterisk | ||
10 | debug = true | ||
11 | |||
12 | [mysql-templates] | ||
13 | tables = create-table.sql | ||
14 | user = create-user.sql | ||
15 | region = create-region.sql | ||
16 | debug = true | ||
diff --git a/share/python/asterisk/asterisk-opensim.py b/share/python/asterisk/asterisk-opensim.py deleted file mode 100755 index aad72eb..0000000 --- a/share/python/asterisk/asterisk-opensim.py +++ /dev/null | |||
@@ -1,231 +0,0 @@ | |||
1 | #!/usr/bin/python | ||
2 | # -*- encoding: utf-8 -*- | ||
3 | |||
4 | from __future__ import with_statement | ||
5 | |||
6 | import base64 | ||
7 | import ConfigParser | ||
8 | import optparse | ||
9 | import MySQLdb | ||
10 | import re | ||
11 | import SimpleXMLRPCServer | ||
12 | import socket | ||
13 | import sys | ||
14 | import uuid | ||
15 | |||
16 | class AsteriskOpenSimServerException(Exception): | ||
17 | pass | ||
18 | |||
19 | class AsteriskOpenSimServer(SimpleXMLRPCServer.SimpleXMLRPCServer): | ||
20 | '''Subclassed SimpleXMLRPCServer to be able to muck around with the socket options. | ||
21 | ''' | ||
22 | |||
23 | def __init__(self, config): | ||
24 | baseURL = config.get('xmlrpc', 'baseurl') | ||
25 | match = reURLParser.match(baseURL) | ||
26 | if not match: | ||
27 | raise AsteriskOpenSimServerException('baseURL "%s" is not a well-formed URL' % (baseURL)) | ||
28 | |||
29 | host = 'localhost' | ||
30 | port = 80 | ||
31 | path = None | ||
32 | if match.group('host'): | ||
33 | host = match.group('host') | ||
34 | port = int(match.group('port')) | ||
35 | else: | ||
36 | host = match.group('hostonly') | ||
37 | |||
38 | self.__host = host | ||
39 | self.__port = port | ||
40 | |||
41 | SimpleXMLRPCServer.SimpleXMLRPCServer.__init__(self,(host, port)) | ||
42 | |||
43 | def host(self): | ||
44 | return self.__host | ||
45 | |||
46 | def port(self): | ||
47 | return self.__port | ||
48 | |||
49 | def server_bind(self): | ||
50 | self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
51 | SimpleXMLRPCServer.SimpleXMLRPCServer.server_bind(self) | ||
52 | |||
53 | |||
54 | class AsteriskFrontend(object): | ||
55 | '''AsteriskFrontend serves as an XmlRpc function dispatcher. | ||
56 | ''' | ||
57 | |||
58 | def __init__(self, config, db): | ||
59 | '''Constructor to take note of the AsteriskDB object. | ||
60 | ''' | ||
61 | self.__db = db | ||
62 | try: | ||
63 | self.__debug = config.getboolean('dispatcher', 'debug') | ||
64 | except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): | ||
65 | self.__debug = False | ||
66 | |||
67 | def account_update(self, request): | ||
68 | '''update (or create) the SIP account data in the Asterisk RealTime DB. | ||
69 | |||
70 | OpenSim's AsteriskVoiceModule will call this method each | ||
71 | time it receives a ProvisionVoiceAccount request. | ||
72 | ''' | ||
73 | print '[asterisk-opensim] account_update: new request' | ||
74 | |||
75 | for p in ['admin_password', 'username', 'password']: | ||
76 | if p not in request: | ||
77 | print '[asterisk-opensim] account_update: failed: missing password' | ||
78 | return { 'success': 'false', 'error': 'missing parameter "%s"' % (p)} | ||
79 | |||
80 | # turn base64 binary UUID into proper UUID | ||
81 | user = request['username'].partition('@')[0] | ||
82 | user = user.lstrip('x').replace('-','+').replace('_','/') | ||
83 | user = uuid.UUID(bytes = base64.standard_b64decode(user)) | ||
84 | |||
85 | if self.__debug: print '[asterisk-opensim]: account_update: user %s' % user | ||
86 | |||
87 | error = self.__db.AccountUpdate(user = user, password = request['password']) | ||
88 | if error: | ||
89 | print '[asterisk-opensim]: DB.AccountUpdate failed' | ||
90 | return { 'success': 'false', 'error': error} | ||
91 | |||
92 | print '[asterisk-opensim] account_update: done' | ||
93 | return { 'success': 'true'} | ||
94 | |||
95 | |||
96 | def region_update(self, request): | ||
97 | '''update (or create) a VoIP conference call for a region. | ||
98 | |||
99 | OpenSim's AsteriskVoiceModule will call this method each time it | ||
100 | receives a ParcelVoiceInfo request. | ||
101 | ''' | ||
102 | print '[asterisk-opensim] region_update: new request' | ||
103 | |||
104 | for p in ['admin_password', 'region']: | ||
105 | if p not in request: | ||
106 | print '[asterisk-opensim] region_update: failed: missing password' | ||
107 | return { 'success': 'false', 'error': 'missing parameter "%s"' % (p)} | ||
108 | |||
109 | region = request['region'].partition('@')[0] | ||
110 | if self.__debug: print '[asterisk-opensim]: region_update: region %s' % user | ||
111 | |||
112 | error = self.__db.RegionUpdate(region = region) | ||
113 | if error: | ||
114 | print '[asterisk-opensim]: DB.RegionUpdate failed' | ||
115 | return { 'success': 'false', 'error': error} | ||
116 | |||
117 | print '[asterisk-opensim] region_update: done' | ||
118 | return { 'success': 'true' } | ||
119 | |||
120 | class AsteriskDBException(Exception): | ||
121 | pass | ||
122 | |||
123 | class AsteriskDB(object): | ||
124 | '''AsteriskDB maintains the connection to Asterisk's MySQL database. | ||
125 | ''' | ||
126 | def __init__(self, config): | ||
127 | # configure from config object | ||
128 | self.__server = config.get('mysql', 'server') | ||
129 | self.__database = config.get('mysql', 'database') | ||
130 | self.__user = config.get('mysql', 'user') | ||
131 | self.__password = config.get('mysql', 'password') | ||
132 | |||
133 | try: | ||
134 | self.__debug = config.getboolean('mysql', 'debug') | ||
135 | self.__debug_t = config.getboolean('mysql-templates', 'debug') | ||
136 | except ConfigParser.NoOptionError: | ||
137 | self.__debug = False | ||
138 | |||
139 | self.__tablesTemplate = self.__loadTemplate(config, 'tables') | ||
140 | self.__userTemplate = self.__loadTemplate(config, 'user') | ||
141 | self.__regionTemplate = self.__loadTemplate(config, 'region') | ||
142 | |||
143 | self.__mysql = MySQLdb.connect(host = self.__server, db = self.__database, | ||
144 | user = self.__user, passwd = self.__password) | ||
145 | if self.__assertDBExists(): | ||
146 | raise AsteriskDBException('could not initialize DB') | ||
147 | |||
148 | def __loadTemplate(self, config, templateName): | ||
149 | template = config.get('mysql-templates', templateName) | ||
150 | t = '' | ||
151 | with open(template, 'r') as templateFile: | ||
152 | for line in templateFile: | ||
153 | line = line.rstrip('\n') | ||
154 | t += line | ||
155 | return t.split(';') | ||
156 | |||
157 | |||
158 | def __assertDBExists(self): | ||
159 | '''Assert that DB tables exist. | ||
160 | ''' | ||
161 | try: | ||
162 | cursor = self.__mysql.cursor() | ||
163 | for sql in self.__tablesTemplate[:]: | ||
164 | if not sql: continue | ||
165 | sql = sql % { 'database': self.__database } | ||
166 | if self.__debug: print 'AsteriskDB.__assertDBExists: %s' % sql | ||
167 | cursor.execute(sql) | ||
168 | cursor.fetchall() | ||
169 | cursor.close() | ||
170 | except MySQLdb.Error, e: | ||
171 | if self.__debug: print 'AsteriskDB.__assertDBExists: Error %d: %s' % (e.args[0], e.args[1]) | ||
172 | return e.args[1] | ||
173 | return None | ||
174 | |||
175 | def AccountUpdate(self, user, password): | ||
176 | print 'AsteriskDB.AccountUpdate: user %s' % (user) | ||
177 | try: | ||
178 | cursor = self.__mysql.cursor() | ||
179 | for sql in self.__userTemplate[:]: | ||
180 | if not sql: continue | ||
181 | sql = sql % { 'database': self.__database, 'username': user, 'password': password } | ||
182 | if self.__debug_t: print 'AsteriskDB.AccountUpdate: sql: %s' % sql | ||
183 | cursor.execute(sql) | ||
184 | cursor.fetchall() | ||
185 | cursor.close() | ||
186 | except MySQLdb.Error, e: | ||
187 | if self.__debug: print 'AsteriskDB.RegionUpdate: Error %d: %s' % (e.args[0], e.args[1]) | ||
188 | return e.args[1] | ||
189 | return None | ||
190 | |||
191 | def RegionUpdate(self, region): | ||
192 | print 'AsteriskDB.RegionUpdate: region %s' % (region) | ||
193 | try: | ||
194 | cursor = self.__mysql.cursor() | ||
195 | for sql in self.__regionTemplate[:]: | ||
196 | if not sql: continue | ||
197 | sql = sql % { 'database': self.__database, 'regionname': region } | ||
198 | if self.__debug_t: print 'AsteriskDB.RegionUpdate: sql: %s' % sql | ||
199 | cursor.execute(sql) | ||
200 | res = cursor.fetchall() | ||
201 | except MySQLdb.Error, e: | ||
202 | if self.__debug: print 'AsteriskDB.RegionUpdate: Error %d: %s' % (e.args[0], e.args[1]) | ||
203 | return e.args[1] | ||
204 | return None | ||
205 | |||
206 | |||
207 | reURLParser = re.compile(r'^http://((?P<host>[^/]+):(?P<port>\d+)|(?P<hostonly>[^/]+))/', re.IGNORECASE) | ||
208 | |||
209 | # main | ||
210 | if __name__ == '__main__': | ||
211 | |||
212 | parser = optparse.OptionParser() | ||
213 | parser.add_option('-c', '--config', dest = 'config', help = 'config file', metavar = 'CONFIG') | ||
214 | (options, args) = parser.parse_args() | ||
215 | |||
216 | if not options.config: | ||
217 | parser.error('missing option config') | ||
218 | sys.exit(1) | ||
219 | |||
220 | config = ConfigParser.ConfigParser() | ||
221 | config.readfp(open(options.config)) | ||
222 | |||
223 | server = AsteriskOpenSimServer(config) | ||
224 | server.register_introspection_functions() | ||
225 | server.register_instance(AsteriskFrontend(config, AsteriskDB(config))) | ||
226 | |||
227 | # get cracking | ||
228 | print '[asterisk-opensim] server ready on %s:%d' % (server.host(), server.port()) | ||
229 | server.serve_forever() | ||
230 | |||
231 | sys.exit(0) | ||
diff --git a/share/python/asterisk/create-region.sql b/share/python/asterisk/create-region.sql deleted file mode 100644 index a11e77a..0000000 --- a/share/python/asterisk/create-region.sql +++ /dev/null | |||
@@ -1,4 +0,0 @@ | |||
1 | USE %(database)s; | ||
2 | REPLACE INTO `extensions_table` (context,exten,priority,app,appdata) VALUES ('avatare', '%(regionname)s', 1, 'Answer', ''); | ||
3 | REPLACE INTO `extensions_table` (context,exten,priority,app,appdata) VALUES ('avatare', '%(regionname)s', 2, 'Wait', '1'); | ||
4 | REPLACE INTO `extensions_table` (context,exten,priority,app,appdata) VALUES ('avatare', '%(regionname)s', 3, 'Meetme', '%(regionname)s|Acdi'); \ No newline at end of file | ||
diff --git a/share/python/asterisk/create-table.sql b/share/python/asterisk/create-table.sql deleted file mode 100644 index 4fe190f..0000000 --- a/share/python/asterisk/create-table.sql +++ /dev/null | |||
@@ -1,62 +0,0 @@ | |||
1 | USE %(database)s; | ||
2 | CREATE TABLE IF NOT EXISTS `ast_sipfriends` ( | ||
3 | `id` int(11) NOT NULL auto_increment, | ||
4 | `name` varchar(80) NOT NULL default '', | ||
5 | `host` varchar(31) NOT NULL default 'dynamic', | ||
6 | `nat` varchar(5) NOT NULL default 'route', | ||
7 | `type` enum('user','peer','friend') NOT NULL default 'friend', | ||
8 | `accountcode` varchar(20) default NULL, | ||
9 | `amaflags` varchar(13) default NULL, | ||
10 | `callgroup` varchar(10) default NULL, | ||
11 | `callerid` varchar(80) default NULL, | ||
12 | `call-limit` int(11) NOT NULL default '0', | ||
13 | `cancallforward` char(3) default 'no', | ||
14 | `canreinvite` char(3) default 'no', | ||
15 | `context` varchar(80) default 'pre_outgoing', | ||
16 | `defaultip` varchar(15) default NULL, | ||
17 | `dtmfmode` varchar(7) default NULL, | ||
18 | `fromuser` varchar(80) default NULL, | ||
19 | `fromdomain` varchar(80) default NULL, | ||
20 | `insecure` varchar(4) default NULL, | ||
21 | `language` char(2) default NULL, | ||
22 | `mailbox` varchar(50) default NULL, | ||
23 | `md5secret` varchar(80) default NULL, | ||
24 | `deny` varchar(95) default NULL, | ||
25 | `permit` varchar(95) default NULL, | ||
26 | `mask` varchar(95) default NULL, | ||
27 | `musiconhold` varchar(100) default NULL, | ||
28 | `pickupgroup` varchar(10) default NULL, | ||
29 | `qualify` char(3) default NULL, | ||
30 | `regexten` varchar(80) default NULL, | ||
31 | `restrictcid` char(3) default NULL, | ||
32 | `rtptimeout` char(3) default NULL, | ||
33 | `rtpholdtimeout` char(3) default NULL, | ||
34 | `secret` varchar(80) default NULL, | ||
35 | `setvar` varchar(100) default NULL, | ||
36 | `disallow` varchar(100) default 'all', | ||
37 | `allow` varchar(100) default 'g729', | ||
38 | `fullcontact` varchar(80) NOT NULL default '', | ||
39 | `ipaddr` varchar(15) NOT NULL default '', | ||
40 | `port` smallint(5) unsigned NOT NULL default '0', | ||
41 | `regserver` varchar(100) default NULL, | ||
42 | `regseconds` int(11) NOT NULL default '0', | ||
43 | `username` varchar(80) NOT NULL default '', | ||
44 | `user` varchar(255) NOT NULL, | ||
45 | `placement` varchar(255) NOT NULL, | ||
46 | `description` varchar(255) NOT NULL, | ||
47 | `delay` int(4) NOT NULL default '0', | ||
48 | `sortorder` int(11) NOT NULL default '1', | ||
49 | PRIMARY KEY (`id`), | ||
50 | UNIQUE KEY `name` (`name`) | ||
51 | ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ; | ||
52 | |||
53 | CREATE TABLE IF NOT EXISTS `extensions_table` ( | ||
54 | `id` int(11) NOT NULL auto_increment, | ||
55 | `context` varchar(20) NOT NULL default '', | ||
56 | `exten` varchar(36) NOT NULL default '', | ||
57 | `priority` tinyint(4) NOT NULL default '0', | ||
58 | `app` varchar(20) NOT NULL default '', | ||
59 | `appdata` varchar(128) NOT NULL default '', | ||
60 | PRIMARY KEY (`context`,`exten`,`priority`), | ||
61 | KEY `id` (`id`) | ||
62 | ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; \ No newline at end of file | ||
diff --git a/share/python/asterisk/create-user.sql b/share/python/asterisk/create-user.sql deleted file mode 100644 index 68b8147..0000000 --- a/share/python/asterisk/create-user.sql +++ /dev/null | |||
@@ -1,5 +0,0 @@ | |||
1 | USE %(database)s; | ||
2 | REPLACE INTO ast_sipfriends (port,context,disallow,allow,type,secret,host,name) VALUES ('5060','avatare','all','ulaw','friend','%(password)s','dynamic','%(username)s'); | ||
3 | REPLACE INTO `extensions_table` (context,exten,priority,app,appdata) VALUES ('avatare', '%(username)s', 1, 'Answer', ''); | ||
4 | REPLACE INTO `extensions_table` (context,exten,priority,app,appdata) VALUES ('avatare', '%(username)s', 2, 'Wait', '1'); | ||
5 | REPLACE INTO `extensions_table` (context,exten,priority,app,appdata) VALUES ('avatare', '%(username)s', 3, 'Dial', 'SIP/%(username)s,60'); \ No newline at end of file | ||
diff --git a/share/python/asterisk/test-client.py b/share/python/asterisk/test-client.py deleted file mode 100755 index d1872cd..0000000 --- a/share/python/asterisk/test-client.py +++ /dev/null | |||
@@ -1,28 +0,0 @@ | |||
1 | #!/usr/bin/python | ||
2 | # -*- encoding: utf-8 -*- | ||
3 | |||
4 | import xmlrpclib | ||
5 | |||
6 | # XML-RPC URL (http_listener_port) | ||
7 | asteriskServerURL = 'http://127.0.0.1:53263' | ||
8 | |||
9 | # instantiate server object | ||
10 | asteriskServer = xmlrpclib.Server(asteriskServerURL) | ||
11 | |||
12 | try: | ||
13 | # invoke admin_alert: requires password and message | ||
14 | res = asteriskServer.region_update({ | ||
15 | 'admin_password': 'c00lstuff', | ||
16 | 'region' : '941ae087-a7da-43b4-900b-9fe48387ae57@secondlife.zurich.ibm.com' | ||
17 | }) | ||
18 | print res | ||
19 | |||
20 | res = asteriskServer.account_update({ | ||
21 | 'admin_password': 'c00lstuff', | ||
22 | 'username' : '0780d90b-1939-4152-a283-8d1261fb1b68@secondlife.zurich.ibm.com', | ||
23 | 'password' : '$1$dd02c7c2232759874e1c205587017bed' | ||
24 | }) | ||
25 | print res | ||
26 | except Exception, e: | ||
27 | print e | ||
28 | |||
diff --git a/share/python/matrix/matrix.py b/share/python/matrix/matrix.py deleted file mode 100755 index be7ec25..0000000 --- a/share/python/matrix/matrix.py +++ /dev/null | |||
@@ -1,272 +0,0 @@ | |||
1 | #!/usr/bin/python | ||
2 | # -*- encoding: utf-8 -*- | ||
3 | # Copyright (c) Contributors, http://opensimulator.org/ | ||
4 | # See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
5 | # | ||
6 | # Redistribution and use in source and binary forms, with or without | ||
7 | # modification, are permitted provided that the following conditions are met: | ||
8 | # * Redistributions of source code must retain the above copyright | ||
9 | # notice, this list of conditions and the following disclaimer. | ||
10 | # * Redistributions in binary form must reproduce the above copyright | ||
11 | # notice, this list of conditions and the following disclaimer in the | ||
12 | # documentation and/or other materials provided with the distribution. | ||
13 | # * Neither the name of the OpenSim Project nor the | ||
14 | # names of its contributors may be used to endorse or promote products | ||
15 | # derived from this software without specific prior written permission. | ||
16 | # | ||
17 | # THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR | ||
18 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
19 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
20 | # DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
21 | # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | ||
23 | # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
24 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | ||
25 | # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | ||
26 | # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | ||
27 | # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
28 | |||
29 | import xml.etree.ElementTree as ET | ||
30 | import re | ||
31 | import urllib | ||
32 | import urllib2 | ||
33 | import ConfigParser | ||
34 | import optparse | ||
35 | import os | ||
36 | import sys | ||
37 | |||
38 | def longHelp(): | ||
39 | print ''' | ||
40 | matrix.py is a little launcher tool that knows about the GridInfo | ||
41 | protocol. it expects the grid coordinates to be passed as a | ||
42 | command line argument either as a "matrix:" or as an "opensim:" style | ||
43 | URI: | ||
44 | |||
45 | matrix://osgrid.org:8002/ | ||
46 | |||
47 | you can also provide region/X/Y/Z coordinates: | ||
48 | |||
49 | matrix://osgrid.org:8002/Wright%20Plaza/128/50/75 | ||
50 | |||
51 | and, it also understands avatar names and passwords: | ||
52 | |||
53 | matrix://mr%20smart:secretpassword@osgrid.org:8002/Wright%20Plaza/128/50/75 | ||
54 | |||
55 | when you run it the first time, it will complain about a missing | ||
56 | .matrixcfg file --- this is needed so that it can remember where your | ||
57 | secondlife client lives on your box. to generate that file, simply run | ||
58 | |||
59 | matrix.py --secondlife path-to-your-secondlife-client-executable | ||
60 | |||
61 | ''' | ||
62 | |||
63 | def ParseOptions(): | ||
64 | '''Parse the command line options and setup options. | ||
65 | ''' | ||
66 | |||
67 | parser = optparse.OptionParser() | ||
68 | parser.add_option('-c', '--config', dest = 'config', help = 'config file', metavar = 'CONFIG') | ||
69 | parser.add_option('-s', '--secondlife', dest = 'client', help = 'location of secondlife client', metavar = 'SL-CLIENT') | ||
70 | parser.add_option('-l', '--longhelp', action='store_true', dest = 'longhelp', help = 'longhelp') | ||
71 | (options, args) = parser.parse_args() | ||
72 | |||
73 | if options.longhelp: | ||
74 | parser.print_help() | ||
75 | longHelp() | ||
76 | sys.exit(0) | ||
77 | |||
78 | return options | ||
79 | |||
80 | def ParseConfig(options): | ||
81 | '''Ensure configuration exists and parse it. | ||
82 | ''' | ||
83 | # | ||
84 | # we are using ~/.matrixcfg to store the location of the | ||
85 | # secondlife client, os.path.normpath and os.path.expanduser | ||
86 | # should make sure that we are fine cross-platform | ||
87 | # | ||
88 | if not options.config: | ||
89 | options.config = '~/.matrixcfg' | ||
90 | |||
91 | cfgPath = os.path.normpath(os.path.expanduser(options.config)) | ||
92 | |||
93 | # | ||
94 | # if ~/.matrixcfg does not exist we are in trouble... | ||
95 | # | ||
96 | if not os.path.exists(cfgPath) and not options.client: | ||
97 | print '''oops, i've no clue where your secondlife client lives around here. | ||
98 | i suggest you run me with the "--secondlife path-of-secondlife-client" argument.''' | ||
99 | sys.exit(1) | ||
100 | |||
101 | # | ||
102 | # ok, either ~/.matrixcfg does exist or we are asked to create it | ||
103 | # | ||
104 | config = ConfigParser.ConfigParser() | ||
105 | if os.path.exists(cfgPath): | ||
106 | config.readfp(open(cfgPath)) | ||
107 | |||
108 | if options.client: | ||
109 | if config.has_option('secondlife', 'client'): | ||
110 | config.remove_option('secondlife', 'client') | ||
111 | if not config.has_section('secondlife'): | ||
112 | config.add_section('secondlife') | ||
113 | config.set('secondlife', 'client', options.client) | ||
114 | |||
115 | cfg = open(cfgPath, mode = 'w+') | ||
116 | config.write(cfg) | ||
117 | cfg.close() | ||
118 | |||
119 | return config.get('secondlife', 'client') | ||
120 | |||
121 | |||
122 | # | ||
123 | # regex: parse a URI | ||
124 | # | ||
125 | reURI = re.compile(r'''^(?P<scheme>[a-zA-Z0-9]+):// # scheme | ||
126 | ((?P<avatar>[^:@]+)(:(?P<password>[^@]+))?@)? # avatar name and password (optional) | ||
127 | (?P<host>[^:/]+)(:(?P<port>\d+))? # host, port (optional) | ||
128 | (?P<path>/.*) # path | ||
129 | $''', re.IGNORECASE | re.VERBOSE) | ||
130 | |||
131 | # | ||
132 | # regex: parse path as location | ||
133 | # | ||
134 | reLOC = re.compile(r'''^/(?P<region>[^/]+)/ # region name | ||
135 | (?P<x>\d+)/ # X position | ||
136 | (?P<y>\d+)/ # Y position | ||
137 | (?P<z>\d+) # Z position | ||
138 | ''', re.IGNORECASE | re.VERBOSE) | ||
139 | |||
140 | def ParseUri(uri): | ||
141 | '''Parse a URI and return its constituent parts. | ||
142 | ''' | ||
143 | |||
144 | match = reURI.match(uri) | ||
145 | if not match or not match.group('scheme') or not match.group('host'): | ||
146 | print 'hmm... cannot parse URI %s, giving up' % uri | ||
147 | sys.exit(1) | ||
148 | |||
149 | scheme = match.group('scheme') | ||
150 | host = match.group('host') | ||
151 | port = match.group('port') | ||
152 | avatar = match.group('avatar') | ||
153 | password = match.group('password') | ||
154 | path = match.group('path') | ||
155 | |||
156 | return (scheme, host, port, avatar, password, path) | ||
157 | |||
158 | def ParsePath(path): | ||
159 | '''Try and parse path as /region/X/Y/Z. | ||
160 | ''' | ||
161 | |||
162 | loc = None | ||
163 | match = reLOC.match(path) | ||
164 | if match: | ||
165 | loc = 'secondlife:///%s/%d/%d/%d' % (match.group('region'), | ||
166 | int(match.group('x')), | ||
167 | int(match.group('y')), | ||
168 | int(match.group('z'))) | ||
169 | return loc | ||
170 | |||
171 | |||
172 | def GetGridInfo(host, port): | ||
173 | '''Invoke /get_grid_info on target grid and obtain additional parameters | ||
174 | ''' | ||
175 | |||
176 | gridname = None | ||
177 | gridnick = None | ||
178 | login = None | ||
179 | welcome = None | ||
180 | economy = None | ||
181 | |||
182 | # | ||
183 | # construct GridInfo URL | ||
184 | # | ||
185 | if port: | ||
186 | gridInfoURI = 'http://%s:%d/get_grid_info' % (host, int(port)) | ||
187 | else: | ||
188 | gridInfoURI = 'http://%s/get_grid_info' % (host) | ||
189 | |||
190 | # | ||
191 | # try to retrieve GridInfo | ||
192 | # | ||
193 | try: | ||
194 | gridInfoXml = ET.parse(urllib2.urlopen(gridInfoURI)) | ||
195 | |||
196 | gridname = gridInfoXml.findtext('/gridname') | ||
197 | gridnick = gridInfoXml.findtext('/gridnick') | ||
198 | login = gridInfoXml.findtext('/login') | ||
199 | welcome = gridInfoXml.findtext('/welcome') | ||
200 | economy = gridInfoXml.findtext('/economy') | ||
201 | authenticator = gridInfoXml.findtext('/authenticator') | ||
202 | |||
203 | except urllib2.URLError: | ||
204 | print 'oops, failed to retrieve grid info, proceeding with guestimates...' | ||
205 | |||
206 | return (gridname, gridnick, login, welcome, economy) | ||
207 | |||
208 | |||
209 | def StartClient(client, nick, login, welcome, economy, avatar, password, location): | ||
210 | clientArgs = [ client ] | ||
211 | clientArgs += ['-loginuri', login] | ||
212 | |||
213 | if welcome: clientArgs += ['-loginpage', welcome] | ||
214 | if economy: clientArgs += ['-helperuri', economy] | ||
215 | |||
216 | if avatar and password: | ||
217 | clientArgs += ['-login'] | ||
218 | clientArgs += urllib.unquote(avatar).split() | ||
219 | clientArgs += [password] | ||
220 | |||
221 | if location: | ||
222 | clientArgs += [location] | ||
223 | |||
224 | # | ||
225 | # all systems go | ||
226 | # | ||
227 | os.execv(client, clientArgs) | ||
228 | |||
229 | |||
230 | if __name__ == '__main__': | ||
231 | # | ||
232 | # parse command line options and deal with help requests | ||
233 | # | ||
234 | options = ParseOptions() | ||
235 | client = ParseConfig(options) | ||
236 | |||
237 | # | ||
238 | # sanity check: URI supplied? | ||
239 | # | ||
240 | if not sys.argv: | ||
241 | print 'missing opensim/matrix URI' | ||
242 | sys.exit(1) | ||
243 | |||
244 | # | ||
245 | # parse URI and extract scheme. host, port?, avatar?, password? | ||
246 | # | ||
247 | uri = sys.argv.pop() | ||
248 | (scheme, host, port, avatar, password, path) = ParseUri(uri) | ||
249 | |||
250 | # | ||
251 | # sanity check: matrix: or opensim: scheme? | ||
252 | # | ||
253 | if scheme != 'matrix' and scheme != 'opensim': | ||
254 | print 'hmm...unknown scheme %s, calling it a day' % scheme | ||
255 | |||
256 | # | ||
257 | # get grid info from OpenSim server | ||
258 | # | ||
259 | (gridname, gridnick, login, welcome, economy) = GetGridInfo(host, port) | ||
260 | |||
261 | # | ||
262 | # fallback: use supplied uri in case GridInfo drew a blank | ||
263 | # | ||
264 | if not login: login = uri | ||
265 | |||
266 | # | ||
267 | # take a closer look at path: if it's a /region/X/Y/Z pattern, use | ||
268 | # it as the "SLURL | ||
269 | # | ||
270 | location = ParsePath(path) | ||
271 | StartClient(client, gridnick, login, welcome, economy, avatar, password, location) | ||
272 | |||