summaryrefslogtreecommitdiffstats
path: root/pykolab/wap_client/__init__.py
blob: 54629611c252d010ba6b31db3feee24d38bc67ff (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
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

import json
import httplib
import sys

sys.path.append('../..')

from pykolab import utils

API_HOSTNAME = "admin.klab.cc"
API_PORT = "80"
API_SCHEME = "http"
API_BASE = "/~vanmeeuwen/kolab-wap/public_html/api"

session_id = None

conn = None

def authenticate(username=None, password=None):
    global session_id

    if username == None:
        username = utils.ask_question("Login", "cn=Directory Manager")

    if password == None:
        password = utils.ask_question("Password", "5auTYwxBK1uGTpy", password=True)

    params = json.dumps(
            {
                    'username': username,
                    'password': password
                }
        )

    response = request('POST', "system.authenticate", params)

    if response.has_key('session_token'):
        session_id = response['session_token']

def connect():
    global conn

    if conn == None:
        conn = httplib.HTTPConnection(API_HOSTNAME, API_PORT)
        conn.connect()

    return conn

def domains_capabilities():
    return request('GET', 'domains.capabilities')

def domains_list():
    return request('GET', 'domains.list')

def get_group_input():
    group_types = group_types_list()

    if len(group_types.keys()) > 1:
        for key in group_types.keys():
            if not key == "status":
                print "%s) %s" % (key,group_types[key]['name'])

        group_type_id = utils.ask_question("Please select the group type")

    elif len(group_types.keys()) > 0:
        print "Automatically selected the only group type available"
        group_type_id = group_types.keys()[0]

    else:
        print "No group types available"
        sys.exit(1)

    if group_types.has_key(group_type_id):
        group_type_info = group_types[group_type_id]['attributes']
    else:
        print "No such group type"
        sys.exit(1)

    params = {
            'group_type_id': group_type_id
        }

    for attribute in group_type_info['form_fields'].keys():
        params[attribute] = utils.ask_question(attribute)

    for attribute in group_type_info['auto_form_fields'].keys():
        exec("retval = group_form_value_generate_%s(params)" % (attribute))
        params[attribute] = retval[attribute]

    return params

def get_user_input():
    user_types = user_types_list()

    if len(user_types.keys()) > 1:
        for key in user_types.keys():
            if not key == "status":
                print "%s) %s" % (key,user_types[key]['name'])

        user_type_id = utils.ask_question("Please select the user type")

    elif len(user_types.keys()) > 0:
        print "Automatically selected the only user type available"
        user_type_id = user_types.keys()[0]

    else:
        print "No user types available"
        sys.exit(1)

    if user_types.has_key(user_type_id):
        user_type_info = user_types[user_type_id]['attributes']
    else:
        print "No such user type"
        sys.exit(1)

    params = {
            'user_type_id': user_type_id
        }

    for attribute in user_type_info['form_fields'].keys():
        params[attribute] = utils.ask_question(attribute)

    for attribute in user_type_info['auto_form_fields'].keys():
        exec("retval = form_value_generate_%s(params)" % (attribute))
        params[attribute] = retval[attribute]

    return params

def group_add(params=None):
    if params == None:
        params = get_group_input()

    params = json.dumps(params)

    return request('POST', 'group.add', params)

def group_form_value_generate_mail(params=None):
    if params == None:
        params = get_user_input()

    params = json.dumps(params)

    return request('POST', 'group_form_value.generate_mail', params)

def group_info():
    group = utils.ask_question("Group email address")
    group = request('GET', 'group.info?group=%s' % (group))
    return group

def group_members_list(group=None):
    if group == None:
        group = utils.ask_question("Group email address")
    group = request('GET', 'group.members_list?group=%s' % (group))
    return group

def group_types_list():
    return request('GET', 'group_types.list')

def groups_list():
    return request('GET', 'groups.list')

def request(method, api_uri, params=None, headers={}):
    global session_id

    if not session_id == None:
        headers["X-Session-Token"] = session_id

    conn = connect()
    conn.request(method.upper(), "%s/%s" % (API_BASE,api_uri), params, headers)
    response = conn.getresponse()
    data = response.read()

    #print method, api_uri, params
    #print data

    try:
        response_data = json.loads(data)
    except ValueError, e:
        # Some data is not JSON
        print "Response data is not JSON"
        sys.exit(1)

    #print response_data

    if response_data['status'] == "OK":
        del response_data['status']
        return response_data['result']
    else:
        return response_data['result']

def system_capabilities():
    return request('GET', 'system.capabilities')

def system_get_domain():
    return request('GET', 'system.get_domain')

def system_select_domain(domain=None):
    if domain == None:
        domain = utils.ask_question("Domain name")
    return request('GET', 'system.select_domain?domain=%s' % (domain))

def user_add(params=None):
    if params == None:
        params = get_user_input()

    params = json.dumps(params)

    return request('POST', 'user.add', params)

def user_delete(params=None):
    if params == None:
        params = {
                'user': utils.ask_question("Username for user to delete", "user")
            }

    params = json.dumps(params)

    return request('POST', 'user.delete', params)

def user_edit(params=None):
    if params == None:
        params = {
                'user': utils.ask_question("Username for user to edit", "user")
            }

    params = json.dumps(params)

    user = request('GET', 'user.info', params)

    return user

def user_form_value_generate_cn(params=None):
    if params == None:
        params = get_user_input()

    params = json.dumps(params)

    return request('POST', 'user_form_value.generate_cn', params)

def user_form_value_generate_displayname(params=None):
    if params == None:
        params = get_user_input()

    params = json.dumps(params)

    return request('POST', 'user_form_value.generate_displayname', params)

def user_form_value_generate_mail(params=None):
    if params == None:
        params = get_user_input()

    params = json.dumps(params)

    return request('POST', 'user_form_value.generate_mail', params)

def user_form_value_generate_password(*args, **kw):
    return request('GET', 'user_form_value.generate_password')

def user_form_value_generate_uid(params=None):
    if params == None:
        params = get_user_input()

    params = json.dumps(params)

    return request('POST', 'user_form_value.generate_uid', params)

def user_form_value_generate_userpassword(*args, **kw):
    result = user_form_value_generate_password()
    return { 'userpassword': result['password'] }

def user_info():
    user = utils.ask_question("User email address")
    user = request('GET', 'user.info?user=%s' % (user))
    return user

def user_types_list():
    return request('GET', 'user_types.list')

def users_list():
    return request('GET', 'users.list')