HS73RD2CDF73TQUHPHRKUGHIOH3RFX232PR6ZAVXNAQGHS4VRKXQC -define(NOISE_PROTO, "Noise_XK_25519_ChaChaPoly_BLAKE2b").-define(NOISE_PROLOGUE, <<0,8,0,0,3>>).
end.%% Chatinit_loop(EConn) ->Self = self(),Input = spawn(fun() -> input_loop(Self) end),io:setopts([{echo, false}]),chat_loop(EConn, Input).input_loop(EvtLoopPid) ->% TODO: we need better drawing to not trample this prompt with incoming messages but lets not get too crazy herecase io:get_line("> ") ofeof ->EvtLoopPid ! {input_close, eof},exit(normal);{error, Err} ->EvtLoopPid ! {input_close, {error, Err}},exit(error);Line ->EvtLoopPid ! {input_line, Line},input_loop(EvtLoopPid)end.chat_loop(EConn, Input) ->receive{noise, EConn2, Msg} ->io:format("💬 ~s~n", [string:chomp(Msg)]),chat_loop(EConn2, Input);{input_line, InputLine} ->case string:chomp(InputLine) of"quit" ->enoise:close(EConn),exit(normal);Clean ->enoise:send(EConn, Clean ++ "\n")end,chat_loop(EConn, Input);{input_close, Why} ->io:format("Closing ~p~n", [Why]),enoise:send(EConn, "<bye>\n"),enoise:close(EConn),exit(normal);{'EXIT', _Input, _} ->io:format("Input process died~n"),enoise:close(EConn)
input_loop(Server) ->case io:get_line("") ofeof -> exit(normal);{error, _} -> exit(error);Line -> Server ! {input_line, Line}, input_loop(Server)end.chat_loop(EConn, Input) ->receive{noise, EConn2, Msg} ->io:format("💬 ~s~n", [string:chomp(Msg)]),chat_loop(EConn2, Input);{input_line, InputLine} ->case string:chomp(InputLine) of"quit" -> enoise:close(EConn);Clean -> enoise:send(EConn, Clean ++ "\n")end,chat_loop(EConn, Input);{io_request, From, ReplyAs, {get_line, _, _}} ->io:reply(From, ReplyAs, {data, ""}), % Ignore, we drive inputchat_loop(EConn, Input);{io_request, From, ReplyAs, {put_chars, _, Str}} ->io:put_chars(Str),io:reply(From, ReplyAs, ok),chat_loop(EConn, Input);{'EXIT', Input, _} ->io:format("Input process died~n"),enoise:close(EConn)end.
{prologue, <<0, 8, 0, 0, 3>>}],%Pid = spawn(fun() ->case enoise:accept(TcpSock, Opts) of{ok, EConn, _Hs} ->io:format("Handshake successful~n"),init_loop(EConn);{error, Reason} -> io:format("Accept failed: ~p~n", [Reason])end% end),%gen_tcp:controlling_process(TcpSock, Pid),,
{prologue, ?NOISE_PROLOGUE}],case enoise:accept(TcpSock, Opts) of{ok, EConn, _Hs} ->io:format("Handshake successful~n"),init_loop(EConn),io:format("Chat ended~n");{error, Reason} ->io:format("Accept failed: ~p~n", [Reason])end,