<commit_before>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% @author <NAME> <<EMAIL>> %%%
%%% @copyright (c) 2013, <NAME>         %%%
%%% @doc                                    %%%
%%% @end                                    %%%
%%%                                         %%%
%%% Created 2013-08-17 by <NAME>        %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-module(channel).
-behaviour(gen_server).

%%% API
-export([start_link/0, join/2, nicklist/1, part/1, send/2]).

%%% gen_server callbacks
-export([init/1, handle_call/3, handle_info/2, handle_cast/2,
                 terminate/2, code_change/3]).

-include("./channel_records.hrl").

%%%%%%%%%%%%%%%%%%
%%% Client API %%%
%%%%%%%%%%%%%%%%%%
start_link() ->
    gen_server:start_link(?MODULE, #state{}, []).

join(Pid, Username) ->
    gen_server:call(Pid, {join, Username}).

nicklist(Pid) ->
    gen_server:call(Pid, nicklist).

part(Pid) ->
    gen_server:call(Pid, part).

send(Pid, MessageText) ->
    gen_server:cast(Pid, {send, self(), MessageText}).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% gen_server callbacks %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
init(State) -> {ok, State}. %% no treatment of info here!

handle_call({join, #user{}=User}, From, #state{}=State) ->
    {Status, NewState} = add_user(User, From, State),
    {reply, Status, NewState};
handle_call({join, Username}, From, #state{}=State) ->
    {Status, NewState} = add_user(Username, From, State),
    broadcast_join_message(Username, NewState),
    {reply, Status, NewState};
handle_call(nicklist, _From, #state{}=State) ->
    {reply, list_usernames(State), State};
handle_call(part, From, #state{}=State) ->
    {FromPid, _} = From,
    User = user_from_pid(FromPid, State),
    Username = User#user.username,
    broadcast_part_message(Username, State),
    NewState = remove_user(From, State),
    {reply, ok, NewState}.
%handle_call({send, MessageText}, From, #state{}=State) ->
    %broadcast(MessageText, From, State),
    %{reply, { ok, MessageText }, State}.

handle_cast({send, FromPid, MessageText}, #state{}=State) ->
    broadcast(MessageText, FromPid, State),
    {noreply, State};
handle_cast(_, #state{}=State) ->
    io:format("No idea", State).

handle_info(Msg, State) ->
    io:format("Unexpected message: ~p~n",[Msg]),
    {noreply, State}.

terminate(normal, _State) ->
    io:format("terminating..."),
    ok.

code_change(_OldVsn, State, _Extra) ->
    %% Code changing is not presently supported.  This
    %% function exists to satisfy the behaviour.
    {ok, State}.

%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Internal functions %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
list_usernames(State) ->
    MapToUsername = fun(X) ->
        X#user.username
    end,
    lists:map(MapToUsername, State#state.listeners).

add_user(Username, {FromPid, _Ref}, State) ->
    case lists:any(fun(X) -> X#user.username == Username end, State#state.listeners) of
        true -> {duplicate_username, State};
        false -> NewUser = #user{username=Username, pid=FromPid},
                 {ok, State#state{listeners=[NewUser|State#state.listeners]}}
    end;
add_user(#user{}=User, {FromPid, _Ref}, State) ->
    case lists:any(fun(X) -> X#user.username == User#user.username end, State#state.listeners) of
        true -> {duplicate_username, State};
        false -> {ok, State#state{listeners=[User|State#state.listeners]}}<commit_msg>From to _From in add_user since it is not used<commit_after>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% @author <NAME> <<EMAIL>> %%%
%%% @copyright (c) 2013, <NAME>         %%%
%%% @doc                                    %%%
%%% @end                                    %%%
%%%                                         %%%
%%% Created 2013-08-17 by <NAME>        %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-module(channel).
-behaviour(gen_server).

%%% API
-export([start_link/0, join/2, nicklist/1, part/1, send/2]).

%%% gen_server callbacks
-export([init/1, handle_call/3, handle_info/2, handle_cast/2,
                 terminate/2, code_change/3]).

-include("./channel_records.hrl").

%%%%%%%%%%%%%%%%%%
%%% Client API %%%
%%%%%%%%%%%%%%%%%%
start_link() ->
    gen_server:start_link(?MODULE, #state{}, []).

join(Pid, Username) ->
    gen_server:call(Pid, {join, Username}).

nicklist(Pid) ->
    gen_server:call(Pid, nicklist).

part(Pid) ->
    gen_server:call(Pid, part).

send(Pid, MessageText) ->
    gen_server:cast(Pid, {send, self(), MessageText}).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% gen_server callbacks %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
init(State) -> {ok, State}. %% no treatment of info here!

handle_call({join, #user{}=User}, From, #state{}=State) ->
    {Status, NewState} = add_user(User, From, State),
    {reply, Status, NewState};
handle_call({join, Username}, From, #state{}=State) ->
    {Status, NewState} = add_user(Username, From, State),
    broadcast_join_message(Username, NewState),
    {reply, Status, NewState};
handle_call(nicklist, _From, #state{}=State) ->
    {reply, list_usernames(State), State};
handle_call(part, From, #state{}=State) ->
    {FromPid, _} = From,
    User = user_from_pid(FromPid, State),
    Username = User#user.username,
    broadcast_part_message(Username, State),
    NewState = remove_user(From, State),
    {reply, ok, NewState}.
%handle_call({send, MessageText}, From, #state{}=State) ->
    %broadcast(MessageText, From, State),
    %{reply, { ok, MessageText }, State}.

handle_cast({send, FromPid, MessageText}, #state{}=State) ->
    broadcast(MessageText, FromPid, State),
    {noreply, State};
handle_cast(_, #state{}=State) ->
    io:format("No idea", State).

handle_info(Msg, State) ->
    io:format("Unexpected message: ~p~n",[Msg]),
    {noreply, State}.

terminate(normal, _State) ->
    io:format("terminating..."),
    ok.

code_change(_OldVsn, State, _Extra) ->
    %% Code changing is not presently supported.  This
    %% function exists to satisfy the behaviour.
    {ok, State}.

%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Internal functions %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
list_usernames(State) ->
    MapToUsername = fun(X) ->
        X#user.username
    end,
    lists:map(MapToUsername, State#state.listeners).

add_user(Username, {FromPid, _Ref}, State) ->
    case lists:any(fun(X) -> X#user.username == Username end, State#state.listeners) of
        true -> {duplicate_username, State};
        false -> NewUser = #user{username=Username, pid=FromPid},
                 {ok, State#state{listeners=[NewUser|State#state.listeners]}}
    end;
add_user(#user{}=User, _From, State) ->
    case lists:any(fun(X) -> X#user.username == User#user.username end, State#state.listeners) of
        true -> {duplicate_username, State};
        false -> {ok, State#state{listeners=[User|State#state.listeners]}}
{'fraction_non_alphanumeric': 0.235911847359858, 'fraction_numerical': 0.006803727259281171, 'mean_word_length': 3.673116793365584, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3987198', 'n_tokens_mistral': 2256, 'n_tokens_neox': 2193, 'n_words': 435}