github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

baphled / chatterl

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 10
    • 0
  • Source
  • Commits
  • Network (0)
  • Issues (0)
  • Downloads (0)
  • Wiki (24)
  • Graphs
  • Tree: 2305612

click here to add a description

click here to add a homepage

  • Branches (2)
    • master
    • web_interface
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Sinan based chat system built in Erlang — Read more

  cancel

http://baphled.lighthouseapp.com/projects/25454-chattterl/overview

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Added units for implementing login functionality to the new CWIGA, also 
done some refactoring to reflect changes. 
baphled (author)
Sat Mar 28 14:50:49 -0700 2009
commit  2305612cff20830b64a417270f4c9613981d6d45
tree    4aab4854726cd911b68c326543b302c68f51963e
parent  2cd08b10afecabc8b4e9d3c23d3193389d4c82e3
chatterl / lib / chatterl / src / cwiga.erl lib/chatterl/src/cwiga.erl
100644 211 lines (187 sloc) 9.027 kb
edit raw blame history
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
%%----------------------------------------------------------------
%%% @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_link/1,stop/0]).
 
-define(APP, "CWIGA").
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).
 
-define(SERVER, ?MODULE).
-record(state, {}).
 
%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
%% Description: Starts the server
%%--------------------------------------------------------------------
start_link(Port) ->
  gen_server:start_link({global, ?SERVER}, ?MODULE, [Port], []).
 
stop() ->
    gen_server:call({global, ?SERVER}, stop, infinity).
 
dispatch_requests(Req) ->
  [Path|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, Path, Post]),
  Response = handle(Method, Path, get_content_type(Ext), Post),
  Req:respond(Response).
 
handle('POST',"/register/" ++ Nick,ContentType,Post) ->
  [{"name",Name},{"email",Email},{"pass1",Pass1},{"pass2",Pass2}] = Post,
  Response = chatterl_mid_man:register(ContentType,{Nick,{Name,Email,Pass1,Pass2}}),
  handle_response(Response,ContentType);
handle('POST',"/groups/send/" ++ Group,ContentType,Post) ->
  [{"client",Sender},{"msg",Message}] = Post,
  Response = chatterl_mid_man:group_send(ContentType,{Group,Sender,Message}),
  handle_response(Response,ContentType);
handle('POST',"/groups/join/" ++ Group,ContentType,Post) ->
  [{"client",Client}] = Post,
  Response = chatterl_mid_man:group_join(ContentType,{Group,Client}),
  handle_response(Response,ContentType);
handle('POST',"/groups/leave/" ++ Group,ContentType,Post) ->
  [{"client",Client}] = Post,
  Response = chatterl_mid_man:group_leave(ContentType,{Group,Client}),
  handle_response(Response,ContentType);
handle('POST',"/login" ,ContentType,Post) ->
  [{"login",Login},{"pass",Pass}] = Post,
  handle_response(chatterl_mid_man:login(ContentType,{Login,Pass}),ContentType);
handle('GET',"/users/connect/" ++ Client,ContentType,_Post) ->
  handle_response(chatterl_mid_man:connect(ContentType,Client),ContentType);
handle('GET',"/users/disconnect/" ++ Client,ContentType,_Post) ->
  handle_response(chatterl_mid_man:disconnect(ContentType,Client),ContentType);
handle('GET',"/users/list/" ++ Group,ContentType,_Post) ->
  handle_response(chatterl_mid_man:user_list(ContentType,Group),ContentType);
handle('GET',"/users/list",ContentType,_Post) ->
  handle_response(chatterl_mid_man:user_list(ContentType),ContentType);
handle('GET',"/users/poll/" ++ Client,ContentType,_Post) ->
  handle_response(chatterl_mid_man:user_poll(ContentType,Client),ContentType);
handle('GET',"/users/groups/" ++ Client,ContentType,_Post) ->
  handle_response(chatterl_mid_man:user_groups(ContentType,Client),ContentType);
handle('GET',"/groups/poll/" ++ Group,ContentType,_Post) ->
  handle_response(chatterl_mid_man:group_poll(ContentType,Group),ContentType);
handle('GET',"/groups/list",ContentType,_Post) ->
  handle_response(chatterl_mid_man:group_list(ContentType),ContentType);
handle('GET',"/groups/info/" ++ Group,ContentType,_Post) ->
  handle_response(chatterl_mid_man:group_info(ContentType,Group),ContentType);
handle(_,Path,ContentType,_) ->
  Response = message_handler:get_response_body(ContentType,
                                               message_handler:build_carrier("error", "Unknown command: " ++Path)),
  error(Response,ContentType).
 
%%====================================================================
%% gen_server callbacks
%%====================================================================
 
%%--------------------------------------------------------------------
%% Function: init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% Description: Initiates the server
%%--------------------------------------------------------------------
init([Port]) ->
  process_flag(trap_exit, true),
  mochiweb_http:start([{port, Port}, {loop, fun dispatch_requests/1}]),
  erlang:monitor(process,mochiweb_http),
  {ok, #state{}}.
 
%%--------------------------------------------------------------------
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% Description: Handling call messages
%%--------------------------------------------------------------------
handle_call(stop, _From, State) ->
    io:format("Processing shut down ~s~n", [?APP]),
    {stop, normal, stopped, State};
handle_call(_Request, _From, State) ->
  Reply = ok,
  {reply, Reply, State}.
 
%%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling cast messages
%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
  {noreply, State}.
 
%%--------------------------------------------------------------------
%% Function: handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling all non call/cast messages
%%--------------------------------------------------------------------
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}.
 
%%--------------------------------------------------------------------
%% Function: terminate(Reason, State) -> void()
%% Description: This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any necessary
%% cleaning up. When it returns, the gen_server terminates with Reason.
%% The return value is ignored.
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
    io:format("Shutting down ChatterlWeb on: ~s...~n",[node(self())]),
    mochiweb_http:stop(),
    ok.
 
%%--------------------------------------------------------------------
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
%% Description: Convert process state when code is changed
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
  {ok, State}.
 
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
 
%%--------------------------------------------------------------------
%% @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.
 
check_json_response(Json) ->
  {struct,[{<<"chatterl">>,{struct,[{<<"response">>,{struct,[Response]}}]}}]} = mochijson2:decode(Json),
  Response.
 
error(Response,ContentType) ->
  {404, [{"Content-Type", ContentType}], list_to_binary(Response)}.
 
failure(Response,ContentType) ->
  {501, [{"Content-Type", ContentType}], list_to_binary(Response)}.
 
success(Response,ContentType) ->
  {200, [{"Content-Type", ContentType}], list_to_binary(Response)}.
 
 
handle_response(Response,ContentType) ->
  case check_json_response(Response) of
    {<<"success">>,_} -> success(Response,ContentType);
    {<<"error">>,_} -> error(Response,ContentType);
    {<<"failure">>,_} -> failure(Response,ContentType)
  end.
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server