-
Notifications
You must be signed in to change notification settings - Fork 4
/
chatterl_store.erl
279 lines (254 loc) · 9.75 KB
/
chatterl_store.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
%%%----------------------------------------------------------------
%%% @author Yomi Colledge <yomi@boodah.net>
%%% @doc Chatterl Store
%%%
%%% Storage mechanism for Chatterl
%%% @end
%%% @copyright 2009 Yomi Colledge
%%%---------------------------------------------------------------
-module(chatterl_store).
-behaviour(gen_server).
%% API
-export([start_link/1,stop/0,group/1,user/1,get_group/1,get_user/1,get_registered/1]).
-export([registered/0,register/2,is_auth/2,auth/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-include_lib("stdlib/include/qlc.hrl").
-include("chatterl.hrl").
-record(state, {}).
%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% @doc
%% Starts the server
%%
%% @spec start_link(Copies) -> {ok,Pid} | ignore | {error,Error}
%% @end
%%--------------------------------------------------------------------
start_link(Copies) ->
gen_server:start_link({global, ?MODULE}, ?MODULE, [Copies], []).
%%--------------------------------------------------------------------
%% @doc
%% Stops the server
%%
%% @spec stop() -> stopped
%% @end
%%--------------------------------------------------------------------
stop() ->
gen_server:call({global,?MODULE}, stop, infinity).
%%--------------------------------------------------------------------
%% @doc
%% Registers a client to chatterl.
%%
%% @spec register(Nickname,{User,Email,Password1,Password2}) -> {ok,Msg} | {error,Msg}
%% @end
%%--------------------------------------------------------------------
register(Nickname,{User,Email,Password1,Password2}) ->
case Password1 =:= Password2 of
false ->
{error,lists:append(Nickname,"'s passwords must match")};
true ->
case is_auth(Nickname,Password1) of
true ->
{error,lists:append(Nickname," is already registered")};
false ->
create_user(Nickname,{User,Email,Password1})
end
end.
%%--------------------------------------------------------------------
%% @doc
%% Determines whether a client is authorised or not
%%
%% @spec is_auth(Username,Password) -> true | false
%% @end
%%--------------------------------------------------------------------
is_auth(Username,Password) ->
case auth(Username,Password) of
{ok, _} -> true;
{error,_} -> false
end.
%%--------------------------------------------------------------------
%% @doc
%% Authorises a client.
%%
%% @spec auth(Username,Password) -> {ok,Msg} | {error,Msg}
%% @end
%%--------------------------------------------------------------------
auth(Username,Password) ->
Q = qlc:q([X#registered_user.nick || X <- mnesia:table(registered_user),
X#registered_user.nick =:= Username,
X#registered_user.password =:= erlang:md5(Password)]),
F = fun() -> qlc:e(Q) end,
{atomic,Result} = mnesia:transaction(F),
case Result of
[Nick] ->
{ok,lists:append(Nick," Authorized")};
[] ->
{error, lists:append("Unable to authorise ",Username)}
end.
%%--------------------------------------------------------------------
%% @doc
%% Retrieves a list of registered clients.
%%
%% @spec registered() -> [RegisteredUsers]
%% @end
%%--------------------------------------------------------------------
registered() ->
Q = qlc:q([{X#registered_user.nick,X#registered_user.firstname,X#registered_user.email} || X <- mnesia:table(registered_user)]),
F = fun() -> qlc:e(Q) end,
{atomic,Result} = mnesia:transaction(F),
Result.
%%--------------------------------------------------------------------
%% @doc
%% Stores a groups state
%%
%% @spec group(Group) -> ok | {error,Error}
%% @end
%%--------------------------------------------------------------------
group(Group) ->
case gen_server:call({global,chatterl_serv},{group_exists,Group}) of
true ->
State = gen_server:call({global,Group},get_state),
F = fun() -> mnesia:write(State) end,
{atomic,Result} = mnesia:transaction(F),
Result;
false -> {error,"Group doesn't exist"}
end.
%%--------------------------------------------------------------------
%% @doc
%% Stores a groups state
%%
%% @spec user(User) -> ok | {error,Error}
%% @end
%%--------------------------------------------------------------------
user(User) ->
case gen_server:call({global,chatterl_serv},{user_exists,User}) of
true ->
State = gen_server:call({global,User},get_state),
F = fun() -> mnesia:write(State) end,
{atomic,Result} = mnesia:transaction(F),
Result;
false -> {error,"User doesn't exist"}
end.
%%--------------------------------------------------------------------
%% @doc
%% Retrieves a groups state.
%%
%% @spec get_group(GroupName) -> GroupState
%% @end
%%--------------------------------------------------------------------
get_group(GroupName) ->
F = fun() -> qlc:e(qlc:q([X || X <- mnesia:table(group), X#group.name =:= GroupName])) end,
{atomic,Result} = mnesia:transaction(F),
Result.
%%--------------------------------------------------------------------
%% @doc
%% Retrieves a groups state.
%%
%% @spec get_user(ClientName) -> GroupState
%% @end
%%--------------------------------------------------------------------
get_user(ClientName) ->
F = fun() -> qlc:e(qlc:q([X || X <- mnesia:table(client), X#client.name =:= ClientName])) end,
{atomic,Result} = mnesia:transaction(F),
Result.
get_registered(User) ->
F = fun() -> mnesia:read({registered_user,User}) end,
{atomic,Result} = mnesia:transaction(F),
Result.
%%====================================================================
%% gen_server callbacks
%%====================================================================
%%--------------------------------------------------------------------
%% Function: init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% Description: Initiates the server
%%--------------------------------------------------------------------
init([Copies]) ->
mnesia:create_schema([node()]),
mnesia:start(),
try
mnesia:table_info(type, group)
catch
exit: _ ->
mnesia:create_table(group,
[{attributes, record_info(fields, group)},
{Copies, [node()]},
{type, bag}])
end,
try
mnesia:table_info(type, client)
catch
exit: _ ->
mnesia:create_table(client,
[{attributes, record_info(fields, client)},
{Copies, [node()]},
{type, bag}])
end,
try
mnesia:table_info(type, registered_user)
catch
exit: _ ->
mnesia:create_table(registered_user,
[{attributes, record_info(fields, registered_user)},
{Copies, [node()]},
{type, bag}])
end,
{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) ->
{stop, normal, stopped, 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(_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) ->
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
%%--------------------------------------------------------------------
create_user(Nickname,{User,Email,Password}) ->
Row = #registered_user{nick=Nickname,firstname=User,email=Email,password=erlang:md5(Password),logged_in=0},
F = fun() -> mnesia:write(Row) end,
case mnesia:transaction(F) of
{aborted,Result} ->
{aborted,Result};
_ -> {ok,lists:append(Nickname," is registered")}
end.