-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverseServer.erl
More file actions
102 lines (90 loc) · 3.84 KB
/
converseServer.erl
File metadata and controls
102 lines (90 loc) · 3.84 KB
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
%%=====================================================
%% Abstract
%%
%% A mail server that allow you to opt out of a mail
%% trail.. you know kind of like a real conversation
%% when you can leave when you want... okay so I cant
%% help with the fact that you may feel rude but hey
%% this is just software :-)
%%
%%=====================================================
%%======================================================
%% Tag the file
%%======================================================
-module(converseServer).
-compile(export_all).
-author("Stephen Bailey").
-email("Stephen.Bailey@stackingit.com").
-vsn( "0.0.0.1" ).
-export( [start/0] ).
-include( "DebugMacros.hrl" ).
%%=====================================================
%% Start and register the process
%%=====================================================
start() ->
register( converse_server, spawn_link( fun() -> loop() end ) ).
%%=====================================================
%% This is here for a quick way to start
%% everything to help with debugging
%% NOTE : this will NUKE your DB so be careful
%%=====================================================
debugStart()->
tracer:trace([?MODULE],[c]),
converseDB:debugStart(),
start().
%%=====================================================
%% Main Message loop
%%=====================================================
loop() ->
receive
{ startConversation, PID, User, Subject, Message, Listeners } ->
PID ! createNewMail( User, Listeners, Subject, Message ),
?MODULE:loop();
{ checkConversations, PID, User } ->
PID ! getConversations( User ),
?MODULE:loop()
end.
%%=====================================================
%% See if there are any mails that the client has waiting for him
%%=====================================================
getConversations( User ) ->
converseDB:getConversation( User ).
%%=====================================================
%% Find that message and make sure this persons is removed from that mail trail
%% in future ( I guess there is nothing stopping someone adding them back in )
%%=====================================================
optOut( MessageId, ClientPid ) ->
?TODO( { optOut, MessageId, ClientPid } ).
%%=====================================================
%% Make a mail and store it for each of the listeners
%% get a unique number for it
%%=====================================================
createNewMail( Author, Listeners, Subject, Message ) ->
%~ {ok, File} = file:open( integer_to_list( getNewMessageID() ), [write] ),
%~ io:format(File, "~p\n", [ { {author,Author}, {listeners,Listeners}, {subject,Subject}, {messsage,Message} } ] ),
%~ file:close(File).
converseDB:addConversation( Author, Subject, Message, Listeners ),
{ newConversationCreated, ok }.
%%=====================================================
%% Note, you cannot remove people, they must opt out if they dont want to play anymore
%%=====================================================
replyMail( AuthorPid, MessageID, AddListeners, Message )->
?TODO( { replyMail, AuthorPid, MessageID, AddListeners, Message } ).
%%=====================================================
%% gets a new ID super incrementing
%%=====================================================
getNewMessageID() ->
case file:consult("MessageIdFile") of
{ok, [{Id}]} ->
saveNewMessageId( Id+1 );
_ ->
saveNewMessageId( 1 )
end.
%%=====================================================
%% Do the udpate and store the "state" on disk
%%=====================================================
saveNewMessageId( Id ) ->
{ok, File} = file:open("MessageIdFile", write),
io:format( File, "~p.\n", [{Id}] ),
file:close(File),
Id.