This wiki is out of date, use the continuation of this wiki instead

Tutorial:IRCchat

From FenixWiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 19:47, 21 November 2007 (edit)
Sandman (Talk | contribs)
(Example code)
← Previous diff
Current revision (09:15, 27 March 2008) (edit) (undo)
Sandman (Talk | contribs)
m (Example code)
 
Line 38: Line 38:
End End
 +Process Main()
Private Private
int netID; // NET ID int netID; // NET ID

Current revision


This is an example for making an IRC chat client with Network.dll. Note that this is a very basic client, so no userlist, etc.

Should traces of message remain on the screen, this is because of Fenix' background restore system. Setting restore_type to complete_restore fixes that.

Note that this is code for Fenix versions 0.93 and up. Should the code work for other versions as well, be so kind to note this here.

[edit] Example code


// Network.dll
include "network.fh";

/* Globals */

Global
    
    // Struct containing settings
    Struct settings
        string server = "irc.blitzed.org";
        word port = 6667;
        string channel = "#bilge";
        string nick = "Chatter";
        string realname = "Chatter of the Chat";
    End

    // IRC related data
    Struct IRC
        int connected;
    End

End

/* Declares */

Declare Function String textinput();
End

Process Main()
Private
    int netID; // NET ID
Begin

    // Set target FPS at 100fps
    set_fps(100,0);

    // Connect to server and join channel
    net_init(0,10,1);
    netid = irc_connect();

    // Start input process
    input(netid);

    // Wait until key ESC
    Repeat
        frame;
    Until(key(_ESC));

    // Disconnect and quit.
    irc_disconnect(netid,"Ended");
    net_quit();
    let_me_alone();

End

//Input process
Process input(int netid)
Private
    string message;
Begin
    Loop
        if( (message = textinput()) != "" )
            irc_chat(netid,message);
        end
        frame;
    End
End

// Connect to IRC
Function int irc_connect()
Begin

    // Check if already connected
    if(irc.connected)
        return -1;
    end

    // Connect
    x=net_connect(settings.server,settings.port,false);
    if(x<0)
        return x;
    end
    net_separator(x,chr(13)+chr(10),2);

    // Start the message receiver process
    irc_receiver(x);

    // Send nick data
    irc_send(x,"USER " + settings.nick + " 8 * :" + settings.realname);
    irc_send(x,"NICK " + settings.nick);

    // Wait until connectec
    while(!irc.connected)
        frame;
    end

    // Join the channel
    irc_send(x,"JOIN " + settings.channel);

    return x;

End

// Disconnect from IRC
Function int irc_disconnect(int netid, string message)
Begin

    irc_send(netid,"QUIT :" + message);
    return net_disconnect(netid);

End

// Receive and handle incoming messages from the IRC server
Process irc_receiver(int netid)
Private
    string message;
Begin

    Loop
        Switch(Net.Incoming[netid])
            case NET_STATUS_DISCONNECTED:
                out("# Disconnected!");
            end
            case NET_STATUS_INACTIVE:
            end
            case NET_STATUS_ACTIVE:
                while( len(message=net_recv(netid))>0 )
                    irc_handlemessage(netid,message);
                end
            end
            case NET_STATUS_ESTABLISHED:
                out("# Connected!");
            end
        End
        frame;
    End

End

// Handle a message
Function int irc_handlemessage(int netid, string message)
Private
    int params;
    string pointer param_original;
    string pointer param;
    string sender;
    string command;
    string all_params;
Begin

    // Obtain list of parameters
    param = param_original = alloc(100*sizeof(string));
    params = split(" ",message,param,100);

    // Obtain sender
    if(param[0][0]==":")
        sender = substr(param[0],1,len(param[0])-1);
        param++;
    end

    // Obtain command and all params
    command = param[0];
    all_params = substr(message,len(sender)+len(command)+3);

    say("% " + message);

    // Handle commands
    if(param[0]=="PING") // According to IRC protocol
        irc_send(netid,"PONG " + param[1]);
        return 0;
    elseif(param[0]=="PRIVMSG") // Handles incoming chat message
        // Strips the sender of not wanted information
        for(x=len(sender)-1; x>0; x--)
            if(sender[x]=="!")
                break;
            end
        end
        sender = substr(sender,0,x);

        // Strips the all_params to just the chat message
        all_params = substr(all_params,len(param[1])+1,len(all_params)-1);
        if(all_params[0]==":")
            all_params = substr(all_params,1,len(all_params)-1);
        end

        // Output message
        out(sender + "> " + all_params);
        return 0;
    elseif(param[0]=="MODE") // According to IRC protocol
        // MODE commands get sent when done connecting
        irc.connected = true;
        return 0;
    end

    return -1;

OnExit
    free(param_original);
End

// Write message onscreen
Global
    out_txt[99];
    max_txt = 18;
End
Function out(string message)
Begin
    if(out_txt[max_txt-1]!=0)
        delete_text(out_txt[max_txt-1]);
    end
    for(x=max_txt-1; x>0; x--)
        out_txt[x] = out_txt[x-1];
        move_text(out_txt[x],10,190-x*10);
    end
    out_txt[0] = write(0,10,190,0,message);

    say("< " + out_txt[0] + " > " + message);
End

// Sends a message to the IRC server
Function int irc_send(int netid,string message)
Begin
    out("<<< " + message);
    return net_sendrn(netid,message);
End

// Sends a chatmessage to the IRC server, to the channel
Function int irc_chat(int netid,string message)
Begin
    out(settings.nick + "> " + message);
    return net_sendrn(netid,"PRIVMSG " + settings.channel + " :" + message);
End

// Obtains text input
Function String textinput()
Private
    String str;
    int t;
    int t2;
    byte last_ascii;
    int txtid;
Begin

    // show what you type in top left corner
    txtid = write_string(0,0,0,0,&str);

    // clean the string
    str = "";

    // get input from the user
    // pressing enter will end the loop
    Loop
        if(ascii!=0&&last_ascii==ascii) // check if a key is pressed and if the same key was pressed last frame
            if(t==0||t>fps/4) // check if the key was just pressed or it has been pressed for 0.25 seconds
                if(t==0||t2>fps/30) // check if the key was just pressed or it has been pressed for the last 0.03 seconds
                    t2=0;
                    switch(ascii) // handle input
                        case 8: //backspace
                            str = substr(str,0,len(str)-1);
                        end
                        case 13: //enter
                            break;
                        end
                        default: //addkey
                            say(ascii);
                            str+=chr(ascii);
                        end
                    end
                end
                t2++;
            end
            t++;
        else
            t = t2 = 0; // reset
        end
        last_ascii = ascii;
        frame;
    End

    // delete the text used
    delete_text(txtid);

    // return the typed string
    return str;

End

Used in example: set_fps(), net_init(), key(), net_quit(), let_me_alone(), net_connect(), net_separator(), chr(), net_disconnect(), len(), net_recv(), alloc(), split(), sizeof(), substr(), say(), free(), delete_text(), move_text(), write(), net_sendrn(), write_string(), declare, function, switch, ascii, fps

Also used is the textinput tutorial.

This will result in something like:
Image:IRCchat.PNG

Personal tools