aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/share/python/matrix/matrix.py
blob: 0630a12d2d62313e7cadd137074547fe177a605e (plain)
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
#!/usr/bin/python
# -*- encoding: utf-8 -*-

import xml.etree.ElementTree as ET
import re
import urllib
import urllib2
import ConfigParser
import optparse
import os
import sys

reURI = re.compile(r'''^(?P<scheme>[a-zA-Z0-9]+)://                         # scheme
                        ((?P<avatar>[^:@]+)(:(?P<password>[^@]+))?@)?       # avatar name and password (optional)
                        (?P<host>[^:/]+)(:(?P<port>\d+))?                   # host, port (optional)
                        (?P<path>/.*)                                       # path
                       $''', re.IGNORECASE | re.VERBOSE)
reLOC = re.compile(r'''^/(?P<region>[^/]+)/          # region name
                         (?P<x>\d+)/                 # X position
                         (?P<y>\d+)/                 # Y position
                         (?P<z>\d+)                  # Z position
                    ''', re.IGNORECASE | re.VERBOSE)

if __name__ == '__main__':
    parser = optparse.OptionParser()
    parser.add_option('-c', '--config', dest = 'config', help = 'config file', metavar = 'CONFIG')
    parser.add_option('-s', '--secondlife', dest = 'client', help = 'location of secondlife client', metavar = 'SL-CLIENT')
    (options, args) = parser.parse_args()

    # 
    # we using ~/.matrixcfg to store the location of the secondlife client
    #
    if not options.config:
        options.config = '~/.matrixcfg'

    cfgPath = os.path.expanduser(options.config)

    # 
    # if ~/.matrixcfg does not exist we are in trouble...
    #
    if not os.path.exists(cfgPath) and not options.client:
        print '''oops, i've no clue where your secondlife client lives around here.
                 i suggest you run me with the "--secondlife path-of-secondlife-client" argument.'''
        sys.exit(1)

    #
    # ok, either ~/.matrixcfg does exist or we are asked to create it
    #
    config = ConfigParser.ConfigParser()
    if  os.path.exists(cfgPath):
        config.readfp(open(cfgPath))

    if options.client:
        if config.has_option('secondlife', 'client'):
            config.remove_option('secondlife', 'client')
        if not config.has_section('secondlife'):
            config.add_section('secondlife')
        config.set('secondlife', 'client', options.client)
        
        cfg = open(cfgPath, mode = 'w+')
        config.write(cfg)
        cfg.close()

    client = config.get('secondlife', 'client')


    #
    # sanity check: URI supplied?
    #
    if not sys.argv:
        print 'missing opensim/matrix URI'
        sys.exit(1)

    # 
    # parse URI and extract scheme. host, port?, avatar?, password?
    #
    uri = sys.argv.pop()
    match = reURI.match(uri)
    if not match or not match.group('scheme') or not match.group('host'):
        print 'hmm... cannot parse URI %s, giving up' % uri
        sys.exit(1)

    scheme = match.group('scheme')
    host = match.group('host')
    port = match.group('port')
    avatar = match.group('avatar')
    password = match.group('password')
    path = match.group('path')

    #
    # sanity check: matrix: or opensim: scheme?
    #
    if scheme != 'matrix' and scheme != 'opensim':
        print 'hmm...unknown scheme %s, calling it a day' % scheme

    #
    # get grid info from OpenSim server
    #
    gridname = None
    gridnick = None
    login = None
    welcome = None
    economy = None
    
    #
    # construct GridInfo URL
    #
    if port:
        gridInfoURI = 'http://%s:%d/get_grid_info' % (host, int(port))
    else:
        gridInfoURI = 'http://%s/get_grid_info' % (host)

    #
    # try to retrieve GridInfo 
    #
    try:
        gridInfoXml = ET.parse(urllib2.urlopen(gridInfoURI))
        
        gridname = gridInfoXml.findtext('/gridname')
        gridnick = gridInfoXml.findtext('/gridnick')
        login = gridInfoXml.findtext('/login')
        welcome = gridInfoXml.findtext('/welcome')
        economy = gridInfoXml.findtext('/economy')

    except urllib2.URLError:
        print 'oops, failed to retrieve grid info, proceeding with guestimates...'

    #
    # fallback: use supplied uri in case GridInfo drew a blank
    #
    if not login: login = uri

    # 
    # ok, got everything, now construct the command line
    #
    clientArgs = ['matrix: %s' % gridnick]
    clientArgs += ['-loginuri', login]

    if welcome: clientArgs += ['-loginpage', welcome]
    if economy: clientArgs += ['-helperuri', economy]

    if avatar and password:
        clientArgs += ['-login']
        clientArgs += urllib.unquote(avatar).split()
        clientArgs += [password]

    # 
    # take a closer look at path: if it's a /region/X/Y/Z pattern, use
    # it as the "SLURL
    #
    match = reLOC.match(path)
    if match:
        loc = 'secondlife:///%s/%d/%d/%d' % (match.group('region'),
                                             int(match.group('x')), 
                                             int(match.group('y')),
                                             int(match.group('z')))
        clientArgs += [loc]

    # 
    # all systems go
    #
    os.execv(client, clientArgs)