Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

forced_anon chat

Name: Anonymous 2007-12-04 23:50

Imagine this: you load up a 4chan chat page, and you're connected to another user to chat one on one. Completely at random, with no traces of identity whatsoever. That's it. There could be a button to get a new person if you really don't like who you're talking to or you've seen them before, or you could just hit F5. In the true spirit of Anonymous, you could talk about anything in complete freedom.

I think that could be pretty awesome (and also pretty fail, but such is the way of all things chan), and it's an idea that I've had in the back of my mind for a while. I don't have the wherewithal to make it happen, however, so by /img/'s recommendation I'm here pitching it to /prog/ in the hope that someone will get inspired and make this happen. I'd really like to see it made real. I have to imagine that it would be very simple to make, although finding a good host and getting people to use it is another matter.

Anyone's interest piqued?

Name: Anonymous 2007-12-10 15:24

Piece of shit source for that pile of trash.

-module( server ).
-export( [ start/1, responder/1, listen/2, client/2, client_get_name/2 ] ).

start( Port )
    ->
    % Start up the responder so messages can get sent to it.
    PID_Responder = spawn( server, responder, [ [] ] ),
   
    % Make sure shit works
    PID_Responder ! { print_message, "Responder created!~n" },
   
    % Create the server socket and start the server
    case gen_tcp:listen( Port, [ list, { active, false }, { packet, line } ] ) of
        { ok, ServSock }
            ->
            { ok, Port } = inet:port( ServSock ),
            io:format( "Listening on port ~w.~n", [ Port ] ),
            listen( ServSock, PID_Responder );
        { error, Reason }
            ->
            { error, Reason }
    end.
   
listen( ServerSock, PID_Responder )
    ->
    % Grab the client attempting to connect
    case gen_tcp:accept( ServerSock ) of
        { ok, ClientSock }
            ->
            io:format( "Client connected!~n", [] ),
           
            % Send message to responder
            PID_Responder ! { new_client, ClientSock },
           
            % Continue listening.
            listen( ServerSock, PID_Responder );
           
        Other
            ->
            io:format( "gen_tcp:accept returned ~p. Quitting.~n", [ Other ] )
    end
    .
   
client( { ClientSock, ClientName }, PID_Responder )
    ->   
    % Just wait for something to happen.
    case gen_tcp:recv( ClientSock, 0 ) of
        { ok, RawMsg }    
            ->
            Msg = io_lib:format( "~s", [ RawMsg ] ),
            io:format( "~s sent message: ~s", [ ClientName, Msg ] ),
            PID_Responder ! {
                client_message,
                io_lib:format(
                    "<~s> ~s",
                    [ ClientName, Msg ]
                )
            },
            client( { ClientSock, ClientName }, PID_Responder );
        { error, closed }
            ->
            io:format( "~s disconnected.", [ ClientName ] ),
            PID_Responder ! { client_closed, { ClientSock, ClientName } }
    end
    .

client_get_name( ClientSock, PID_Responder )
    ->
    % Send the request.
    gen_tcp:send( ClientSock, "Enter username: " ),
   
    % Get it back.
    case gen_tcp:recv( ClientSock, 0 ) of
        { ok, ClientName }
            ->
            { _, StrippedClientName1, _ } =
                regexp:sub(
                    ClientName,
                    "^[ \t\r\n]*",
                    ""
                ),
           
            { _, StrippedClientName, _ } =
                regexp:sub(
                    StrippedClientName1,
                    "[ \t\r\n]*$",
                    ""
                ),
           
            PID_Responder ! { client_choose_name, { ClientSock, StrippedClientName } };
        { error, _ }
            ->
            io:format( "Unable to get client name.~n", [] ),
            io:format( "Client disconnected.~n", [] )
    end
    .

% This could be better implemented with a high-order fun + lists:foreach
send_to_all_clients( [ { ClientSock, _ } | Rest ], Msg )
    ->
    gen_tcp:send( ClientSock, Msg ),
    send_to_all_clients( Rest, Msg );
   
send_to_all_clients( [], _ )
    ->
    ok
    .

responder( ClientList )
    ->
    % Message handler
    receive
        { print_message, Message }
            ->
            % Just print the message...
            io:format( Message, [] ),
           
            % Loop
            responder( ClientList );
       
        { new_client, ClientSock }
            ->
            % Spawn a new thread for this client, attempt to get name
            spawn( server, client_get_name, [ ClientSock, self() ] ),
       
            % Loop
            responder( ClientList );
       
        { client_choose_name, { ClientSock, ClientName } }
            ->
            % Make sure the name isn't taken.
            case lists:keysearch( ClientName, 2, ClientList ) of
                false
                    ->
                    % Add the client into the list
                    NewClientList = [ { ClientSock, ClientName } | ClientList ],
           
                    % Send the join message to all users
                    send_to_all_clients(
                        NewClientList,
                        io_lib:format(
                            "~s has joined the chat.~n",
                            [ ClientName ]
                        )
                    ),
                   
                    % Set up the thread to listen for that client
                    spawn( server, client,
                        [ { ClientSock, ClientName }, self() ]
                    ),
                   
                    % Loop with new client list.
                    responder( NewClientList );
                   
                { value, _ }
                    ->
                    % Send them a message
                    gen_tcp:send( ClientSock,
                        io_lib:format(
                            "The name ~s is already taken.~n",
                            [ ClientName ]
                        )
                    ),
                   
                    % Request they choose another name
                    spawn( server, client_get_name, [ ClientSock, self() ] ),
           
                    % Loop with old client list.
                    responder( ClientList )
            end;
           
            % Already looped above.
       
        { client_message, Msg }
            ->
            % Message is preformatted.
           
            % Need to send the message to all clients.
            send_to_all_clients( ClientList, Msg ),
       
            % Loop
            responder( ClientList );
       
        { client_closed, { ClientSock, ClientName } }
            ->
            % Remove the entry from the list, if it exists.
            NewClientList = lists:keydelete( ClientSock, 1, ClientList ),
           
            % Send message to all clients about logoff.
            send_to_all_clients(
                NewClientList,
                io_lib:format( "~s has left the chat.~n", [ ClientName ] )
            ),
       
            % loop
            responder( NewClientList )
    end
    .

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List