-
Notifications
You must be signed in to change notification settings - Fork 4
/
cwiga.erl
445 lines (417 loc) · 16 KB
/
cwiga.erl
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
%%----------------------------------------------------------------
%%% @author Yomi Colledge <yomi@boodah.net>
%%% @doc Web interface for Chatterl
%%%
%%% Chatterl Web Gateway, allowing Web based clients to interact
%%% with Chatterl over a RESTful API.
%%%
%%% Allows Chatterl to interface with any web-based interface
%%% Using JSON and XML, sending the requests off to the chatterl_serv
%%% module.
%%%
%%% All calls to CWIGA will only be allowed via a specified IP, which
%%% will be defined with the configuration file.
%%% @end
%%% @copyright 2008-2009 Yomi Colledge
%%%---------------------------------------------------------------
-module(cwiga).
-behaviour(gen_server).
%% API
-export([start/1,stop/0,get_params/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-define(APP, "CWIGA").
-define(SERVER, ?MODULE).
-record(state, {}).
%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% @doc
%% Starts the server
%%
%% @spec start(Port) -> {ok,Pid} | ignore | {error,Error}
%% @end
%%--------------------------------------------------------------------
start(Port) ->
gen_server:start_link({global, ?SERVER}, ?MODULE, [Port], []).
%%--------------------------------------------------------------------
%% @doc
%% Stops the server
%%
%% @spec stop() -> {ok,Pid} | stopped | {error,Error}
%% @end
%%--------------------------------------------------------------------
stop() ->
gen_server:call({global, ?SERVER}, stop, infinity).
%%--------------------------------------------------------------------
%% @doc
%% Dispatches our requests to the relevant handle.
%%
%% Uses clean_path to determine what the action is.
%% @spec dispatch_requests(Request) -> void()
%% @end
%%--------------------------------------------------------------------
dispatch_requests(Req) ->
[Url|Ext] = string:tokens(Req:get(path),"."),
Method = Req:get(method),
Post = Req:parse_post(),
io:format("~p request for ~p with post: ~p~n", [Method, Url, Post]),
Path = string:tokens(Url, "/"),
Response = gen_server:call({global,?MODULE},{Method, Path, get_content_type(Ext), Post, Req}),
Req:respond(Response).
get_path(Req) ->
[Path|_Ext] = string:tokens(Req:get(path),"."),
Path.
%%====================================================================
%% gen_server callbacks
%%====================================================================
%%--------------------------------------------------------------------
%% @doc
%% Initiates the server
%%
%% Function: init(Port) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%%--------------------------------------------------------------------
init([Port]) ->
process_flag(trap_exit, true),
mochiweb_http:start([{port, Port}, {loop, fun dispatch_requests/1}]),
erlang:monitor(process,mochiweb_http),
{ok, #state{}}.
%%--------------------------------------------------------------------
%% @doc
%% Description: Handling call messages
%%
%% @spec handle_call(Request, From, State) -> {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_call(stop, _From, State) ->
io:format("Processing shut down ~s~n", [?APP]),
{stop, normal, stopped, State};
handle_call({'POST',Path,ContentType,Post,Req},_From,State) ->
Reply = handle_response(handle_request('POST',Path,ContentType,Post,Req),ContentType),
{reply, Reply, State};
handle_call({'GET',Path,ContentType,_Post,Req},_From,State) ->
Reply = handle_response(handle_request('GET',Path,ContentType,Req),ContentType),
{reply, Reply, State};
handle_call({'DELETE',Path,ContentType,_Post,Req},_From,State) ->
Reply = handle_response(handle_request('DELETE',Path,ContentType,Req),ContentType),
{reply, Reply, State};
handle_call({_,_Path,ContentType,_Path,Req},_From,State) ->
Reply = send_response(error,{error("Unknown command: " ++ get_path(Req), ContentType),ContentType}),
{reply, Reply, State};
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
%%--------------------------------------------------------------------
%% @doc
%% Handling cast messages
%%
%% @spec handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% @doc
%% Handling all non call/cast messages
%%
%% @spec handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_info({'DOWN', _Ref, _Process, {mochiweb_http, Host}, Reason}, State) ->
io:format("Unable to start mochiweb on ~s:~nReason: ~s~n",[Host,Reason]),
{stop,normal,State};
handle_info(_Info, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% @doc
%% Terminates Chatterl Web Interface, making sure to shutdown mochiweb
%% along side it.
%%
%% @spec terminate({node,Reason},State) -> void()
%% @todo Needs a time out for when the port is already in use.
%% @end
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
io:format("Shutting down ChatterlWeb on: ~s...~n",[node(self())]),
mochiweb_http:stop(),
ok.
%%--------------------------------------------------------------------
%% @doc
%%
%% Convert process state when code is changed
%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
%%
%% @end
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% Handles CWIGA's GET requests
%% @spec handle_request('GET', Url, ContentType, Req) -> HTTPResponse
%%
%% @end
%%--------------------------------------------------------------------
handle_request('GET',["users"],ContentType,Req) ->
manage_request(ContentType,Req,{user_list,[]},false);
handle_request('GET',["groups"],ContentType,Req) ->
manage_request(ContentType,Req,{group_list,[]},false);
handle_request('GET',["users",Client,"connect"],ContentType,_Req) ->
chatterl_mid_man:connect(ContentType,Client);
handle_request('GET',["users",Group,"users"],ContentType,Req) ->
manage_request(ContentType,Req,{user_list,Group},false);
handle_request('GET',["users",Client,"groups"],ContentType,Req) ->
manage_request(ContentType,Req,{user_groups,Client},true);
handle_request('GET',["users",Client,"poll"],ContentType,Req) ->
manage_request(ContentType,Req,{user_poll,Client},false);
handle_request('GET',["groups",Group,"info"],ContentType,Req) ->
manage_request(ContentType,Req,{group_info,Group},false);
handle_request('GET',["groups",Group,"poll"],ContentType,Req) ->
manage_request(ContentType,Req,{group_poll,Group},false);
handle_request('GET',["status","logged_in"],ContentType,Req) ->
manage_request(ContentType,Req,{logged_in,[]},true);
handle_request('GET', _Path, ContentType, Req) ->
error("Unknown command: " ++ get_path(Req), ContentType);
handle_request('DELETE',["users",Client,"disconnect"],ContentType,_Req) ->
chatterl_mid_man:disconnect(ContentType,Client);
handle_request('DELETE',["groups",Group,"drop"],ContentType,Req) ->
manage_request(ContentType,Req,{group_drop,Group},true);
handle_request('DELETE',_,ContentType,Req) ->
error("Unknown command: " ++ get_path(Req), ContentType).
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% Handles CWIGA's POST requests
%% @spec handle_request('POST', Url, ContentType, Path, Req) -> HTTPResponse
%%
%% @end
%%--------------------------------------------------------------------
handle_request('POST',["users","register",Nick],ContentType,Post,_Req) ->
chatterl_mid_man:register(ContentType,{Nick,get_params(["name","email","pass1","pass2"],Post)});
handle_request('POST',["users","login"],ContentType,Post,_Req) ->
chatterl_mid_man:login(ContentType,get_params(["login","pass"],Post));
handle_request('POST',["users","logout"],ContentType,Post,_Req) ->
chatterl_mid_man:logout(ContentType,proplists:get_value("client",Post));
handle_request('POST',["groups",Group,"send"],ContentType,Post,Req) ->
{Sender,Message} = get_params(["client","msg"],Post),
manage_request(ContentType,Req,{group_send,{Group,Sender,Message}},false);
handle_request('POST',["users",Client,"send"],ContentType,Post,Req) ->
{Sender,Message} = get_params(["client","msg"],Post),
manage_request(ContentType,Req,{user_msg,{Client,Sender,Message}},false);
handle_request('POST',["groups",Group,"join"],ContentType,Post,Req) ->
manage_request(ContentType,Req,{group_join,{Group,proplists:get_value("client",Post)}},false);
handle_request('POST',["groups",Group,"leave"],ContentType,Post,Req) ->
manage_request(ContentType,Req,{group_leave,{Group,proplists:get_value("client",Post)}},true);
handle_request('POST',["groups",Group,"create"],ContentType,Post,Req) ->
manage_request(ContentType,Req,{group_create,{Group,proplists:get_value("description",Post)}},true);
handle_request('POST',_Path,ContentType,_Post,Req) ->
error("Unknown command: " ++ get_path(Req), ContentType).
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% Gets our porameters
%%
%% Takes a list of parameters keys used to retrieve their values in the
%% same order.
%% @spec get_params(Params,Post) -> Parameters
%%
%% @end
%%--------------------------------------------------------------------
get_params(Params,Post) ->
list_to_tuple([proplists:get_value(Param,Post) || Param <- Params]).
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% Gets the content type, used to help CWIGA to determine what format
%% to respond in.
%% @spec get_content_type(Type) -> string()
%%
%% @end
%%--------------------------------------------------------------------
get_content_type(Type) ->
case Type of
["json"] -> ["text/json"];
["xml"] -> ["text/xml"];
_ -> ["text/json"]
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Wrapper method used for successful responses
%%
%% @spec check_json_response(Json) -> JSONResponse
%% @end
%%--------------------------------------------------------------------
check_json_response(Json) ->
{struct,[{<<"chatterl">>,
{struct,[{<<"response">>,{struct,[Response]}}]}}]} =
mochijson2:decode(Json),
Response.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Wrapper method used for error responses
%%
%% @spec get_status_code(ResponseType) -> StatusCode
%% @end
%%--------------------------------------------------------------------
get_status_code(ResponseType) ->
case ResponseType of
<<"success">> -> 200;
<<"unauth">> -> 401;
<<"error">> -> 404;
<<"failure">> -> 500
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Used for generic responses
%%
%% @spec response_message(Type,Message,ContentType) -> HTTPResponse
%% @end
%%--------------------------------------------------------------------
response_message(Type,Message,ContentType) ->
message_handler:get_response_body(ContentType,
message_handler:build_carrier(Type, Message)).
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Wrapper method used for error responses
%%
%% @spec error(Message,ContentType) -> HTTPResponse
%% @end
%%--------------------------------------------------------------------
error(Message,ContentType) ->
response_message("error", Message,ContentType).
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Wrapper method used for error responses
%%
%% @spec send_response(ResponseType,{Response,ContentType}) -> HTTPResponse
%% @end
%%--------------------------------------------------------------------
send_response(ResponseType,{Response,ContentType}) ->
StatusCode = get_status_code(ResponseType),
{StatusCode, [{"Content-Type", ContentType}], list_to_binary(Response)}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Sets up the needed response type for our results.
%%
%% @spec handle_response(Response,ContentType) -> HTTPResponse
%% @end
%%--------------------------------------------------------------------
handle_response(Response,ContentType) ->
case ContentType of
["text/xml"] ->
send_response(<<"success">>,{Response,ContentType});
["text/json"] ->
case check_json_response(Response) of
{ResponseType,_} -> send_response(ResponseType,{Response,ContentType})
end
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% Determines whether the client has successfully authenticated.
%%
%% Need to make this more secure.
%% @spec is_auth(Req) -> {ok,Msg}|{error,Error}
%%
%% @end
%%--------------------------------------------------------------------
is_auth(Req) ->
case Req:get_header_value("authorization") of
"Basic "++Base64 ->
Str = base64:mime_decode_to_string(Base64),
case string:tokens(Str, ":") of
[User, Pass] ->
chatterl_store:auth(User,Pass);
_ ->
{error,"Unauthorized authorization."}
end;
_ ->
{error,"Need to authorize"}
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% Manages the calls that need to be passed to manage_auth
%%
%% @spec manage_request(ContentType,Req,{Function,Args},Auth) ->
%% {ok,Msg} | {error,Error}
%%
%% @end
%%--------------------------------------------------------------------
manage_request(ContentType,Req,{Function,Args},Auth) ->
Parameters =
case Args of
[] ->
Fun = fun(CT) -> apply(chatterl_mid_man,Function,[CT]) end,
ContentType;
Params ->
Fun = fun({CT,Arg}) -> apply(chatterl_mid_man,Function,[CT,Arg]) end,
{ContentType,Params}
end,
manage_auth(Auth,{ContentType,Req,{Fun,Parameters}}).
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% Manages all our authorisational based calls
%%
%% @spec manage_auth(Auth,{ContentType,Req,{Fun,Parameters}}) ->
%% {ok,Msg}|{error,Error}
%%
%% @end
%%--------------------------------------------------------------------
manage_auth(Auth,{ContentType,Req,{Fun,Parameters}}) ->
case Auth of
true ->
authorise(ContentType,Req,{Fun,Parameters});
false ->
Fun(Parameters)
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% Authorises a client and then carries out an action.
%%
%% Basic wrapper used to pass parameters to an anonymouse function.
%% @spec authorise(ContentType,Req,{Fun,Params}) -> {ok,Msg}|{error,Error}
%%
%% @end
%%--------------------------------------------------------------------
authorise(ContentType,Req,{Fun,Params}) ->
case is_auth(Req) of
{error,Error} ->
response_message('unauth',Error,ContentType);
{ok,_} ->
Fun(Params)
end.