XiRCON-II

DG is authoring an IRC Client. It is a work in progress. beta @ http://sourceforge.net/project/showfiles.php?group_id=1616&release_id=196759 .


This IRC client is a faithful XiRCON [L1 ] clone regarding how the user scripting operates. Here are some screenshots:

  • Doing Japanese:

http://tomasoft.sf.net/in_a_japanese_channel.gif

  • Doing Chinese:

http://tomasoft.sf.net/263.gif

  • Doing Klingon (in utf-8 over IRC and back using the CTCP/2 method of escaping):

http://tomasoft.sf.net/klingon.gif

  • Elvish (layer 1 ConScript):

http://tomasoft.sourceforge.net/elvish.gif

  • being normal (boring):

http://tomasoft.sf.net/in_an_english_channel.gif


The client has been painfully designed to be 2 complimentary halves:

  1. The back-end : The irc_engine Tcl extension DLL (100% Tcl API only, with a little STL) for handling everything that is not seen (ie. sockets, scripts, default behavior, IAL, etc..).
  2. The UI : Whatever UI the user prefers according to the command control API. It could done in Tk, an implimentation in curses, Win32 GUI, or as odd as streaming html output with form input for posting.

The IRC_Engine back-end --

The irc_engine extension provides a single Incr Tcl class. The UI half (partialy undefined) also provides an Incr Tcl class. The two are brought together using inheritence like so:

 source irc_engine.itcl
 
 # ===============================================================
 # By selecting which IRC::ui class is sourced, we can switch
 # to what UI we want to use.
 # ===============================================================
 source irc_ui.itcl
 
 itcl::class IRC::connection {
 
    # ===============================================
    # Bring the 2 halves together using inheritence.
    # ===============================================
    inherit engine ui
 
    constructor {args} {
        eval engine::constructor $args
    } {}
    public method destroy {} {itcl::delete object $this}
 }
 
 ### Create a connection instance with a couple user scripts.
 set a [IRC::connection #auto someUserScript1.tcl someUserScript2.tcl]
 
 ### Connect to IRC.
 $a connect irc.qeast.net davygrvy DG {yo moma!}

In the above, $a is now the connection object. All behavior of the engine is located in a script file called default.tcl [L2 ]. The behavior of irc_engine can be scripted by the user by loading scripts which are queried for event hooks prior to doing the default behavior. I do have a generic framework to the system, but only Tcl is supported. I have code started for perl, python, and java, but I'll have to get back to it later [L3 ].

Here's where it gets interesting for user scripts.. Each tcl user script is run in its own interpreter. It maintains some of its own commands such as [IRC::on], but aliases most up to the global interp (I also call it the controlling or UI interp). So from the script, a call to IRC::echo ... will alias up to the global as connection0 echo ... where connection0 is the Incr Tcl object. This allows us to have our scripts track with their connection object. Where this gets more interesting is that from this aliasing, I can assume commands that will be in the top-most object that are provided by the UI (thanks to inheritence). The echo method is located in the UI half, yet the user script is in a separate interp of the irc_engine extension half and always linked to its parent object.

irc_engine uses Tcl's event loop. This diagram shows some of the code paths:

http://tomasoft.sf.net/incoming_trail.gif

Tcl::Socket is just a wrapper for Tcl's socket API stuff. When a line from the server is received, it is parsed into its protocol parts [L4 ] [L5 ] and is the container for the parts that will get passed around to the subsiquent modules. IRCSplitAndQ [L6 ] [L7 ] is responsible for any text mappings (or multiple mappings) that need to be decoded, any CTCP/2 encoding escapes, any CTCP unquoting, any mode splitting, and removing/queueing of all embedded CTCP commands prior to the edited original getting posted as a job to the event loop. Phew!

http://tomasoft.sf.net/ircevent_trail.gif

When a job we had posted from IRCSplitAndQ is ready to be serviced by Tcl, our Tcl_EventProc named EvalCallback is called which just sets an interp lock then calls IRCEngine::EvalOneIRCEvent in the correct connection object [L8 ] [L9 ]. Within the Tcl_Event structure is our IRCParse C++ object all prepared to be serviced. First we run PreEvent(line), so certain IRC events such as 'join' can be set into the channels list prior to scripts being run. The same is true in the inverse, as a 'part' event will be removed from the channels list from PostEvent(). Space is left here for the inclusion of an Internal Address List when I get around to it.

Now after PreEvent() is run, all script providers in LIFO order are handed the IRCParse object. If any of the providers return IRCUserScriptProvider::COMPLETED (an enum), then the event is considered complete and all processing stops. If no providers claim the event as completed, then the default script will process it. If the default script doesn't handle it, and the mode for the connection is 'debugging' rather than normal, it will be displayed in red in the status window with event name or numeric. Same as:

 echo "\006CC\006***\006C\006 [event] [join [lrange [args] 1 end]]" status

Without the debugging mode, no red color or event code is displayed. Same as:

 echo "*** [join [lrange [args] 1 end]]" status

Here's an example of the user scripting and how identical it is to the idea of XiRCON. Old-time XiRCON'ers will notice, of course, the new use of namespaces and the msgcat package for handling multilingual strings. We must improve on XiRCON, too.

 package require Xircon 2.0
 
 namespace eval ::IRC {
    on PRIVMSG {
        set dest [lindex [args] 0]
        set msg [lindex [args] 1]

        ### ignore an empty one.
        if {![string length $msg]} {complete; return}

        if {![icomp $dest [my_nick]]} {
            ### private msg
            echo "[color highlight]*[color nick][nick][color highlight]*[color private] $msg" query [nick]

        } elseif {[string index $dest 0] == "\$"} {
            ### server (global) message
            echo "[color highlight]<[color nick][nick][color highlight]>[color default] $msg" status

        } else {
            echo "[color highlight]<[color nick][nick][color highlight]>[color default] $msg" channel $dest

        }
        complete
    }
    on $RPL_TOPICWHOTIME     {
        echo "[color change]*** [mc {Topic for}] [color channel][lindex [args] 1][color change] [mc {was set by}] [color nick][lindex [args] 2][color change] [mc {on}] [clock format [lindex [args] 3]]"
        complete
    }
    on $RPL_UMODEIS         {
        echo "[color change]*** [mc {Your modes are}] [color mode]\"[join [lrange [args] 1 end]]\"" status
        complete
    }
 }

As in XiRCON, these are the commands available to an event hook for asking about the line that fired the event:

http://tomasoft.sf.net/ircparser.gif

Not shown is raw_line and it returns the whole thing as a string prior to any processing. raw_args is also prior to any processing. All return strings except for args which is a list (and is very processed). raw_line will be empty for any embedded event such as ctcp or mode+o. nick will equal host for a server message.

Unlike XiRCON, a PRIVMSG event that contains a CTCP in its entire trailing param, will fire a PRIVMSG anyway, even though it will be empty. It may seem silly at first, but the original raw_line would then have no way to get through. Think about it for a sec... It was a PRIVMSG that came in, and just so happened to contain within it a CTCP. It doesn't make sense to me to drop the original PRIVMSG. That was the event. Same is true for a NOTICE with a ctcp_reply.


The UI Half --

The first major feature is the support of ALL embedded text attributes ever known to mankind and robots alike. It supports ircII, mIrc, ANSI, besirc/hydra, and CTCP/2. As CTCP/2 is the most in features, this is the native usage of the display. The other attribute types are pre-translated up to CTCP/2. The CTCP/2 parser is also in the IRC_Engine as an Itcl class for use by Tk applications.

CTCP/2 embedded text attributes are described @ http://www.lag.net/~robey/ctcp . All attibutes in section 2 are supported -> http://www.lag.net/~robey/ctcp/ctcp2.2.txt . One extension was added using the CTCP/2 method for extending, for doing the tag color types. This will be especially useful for a UI in Tk and allows the display to define how "normal", "highlight", etc.. will be rendered rather than the parser filling in literal values. These tag colors are returned by the color command and are not intended for transmision -- internal use only. Similar to XiRCON's \aXX color codes where X is a hexadecimal number.

It should be noted that the parser reads it all, and whether an implimentation of a UI does actually do the attributes is a bit outside of my claims. For example, if I make a Win32 console text-mode UI for this client, I will have a choice of only 12 colors. As a 24-bit depth is available to the CTCP/2 color attribute, the 24-bit value will need to be truncated for that display type (ie. 256 levels of red get mashed to 3 levels, off/on/bright).

I should slow down for a moment and say that the UI is more of an API than an actual complete and realized implimention.

To better describe the concept of implementation separate from the UI, here is an example of a UI that generates HTML as its output (a little DHTML with CSS) [L10 ]. With DKF's help pointing-out the usefulness of CSS, it sure helped me out.

This is the output [L11 ] of this UI script on a few channels on EfNet [L12 ].

The HTML looks great. I like the timestamp in the balloon help (kinda kitch). I'm amazed how identical I got it to look to my test GUI done with a windows' richedit control. The only problem I see is the handling of whitespace. In the main <div> rule, I tried setting white-space: pre so multiple spaces aren't truncated, but alas, IE6 is not doing this for me. Using an open-ended <pre> works, but wouldn't generate valid HTML in this manner. This must be a bug in IE6..

Yes, IE6 does have bugs. text-decoration: blink is also not supported. It seems if I set the <!DOCTYPE> to strict mode, white-space: pre will start working, but then wrapping stops. So I tried &nbsp; to replace multiple spaces and dropped white-space: pre from the <div> rule. That finally got it.


(5:28 PM 11/13/2003)

I put the test RichEdit terminal window into an MDI frame and used TES to run tcl, so I'm getting closer to a real GUI on win32. Mine is on the left. The goal is to clone the one on the right. None of the menus or toolbar button do anything yet, so I'm using tkcon to control the windows at the moment. The connect dialog is next on the list. Conceptually, all it will do is Tcl_Eval("$a connect ..."); just like I'm doing from tkcon. Sweet, eh? A global interp for controlling the behavior of the app is truly a beautiful thing. I will retain this wonderful concept as much and as deeply as I can for all aspects of the GUI. I'll even store settings in tcl variables.. Who needs C++ for everything? Just make a shell and use Tcl for the logic.. Oh.. That's right.. JO states this in [L13 ]

http://tomasoft.sf.net/x2_screenshot.gif

FWIW, The Win32 api CreateMDIWindow() isn't as well documented as it should be. The last argument is for any create params, but the lParam for WM_CREATE is NOT the same as CreateWindow, nor is it a MDICREATESTRUCT directly as the other docs say. It took me a while to figure it out:

 LRESULT CALLBACK
 StatBoxWndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
 {
    MDICREATESTRUCT *lpCreateParams;

    AllWin32StatBoxThreadedGUI *pStatGUI =
            reinterpret_cast <AllWin32StatBoxThreadedGUI *>
            (::GetWindowLongPtr (hwnd, GWLP_USERDATA));

    switch (iMsg)
    {
        case WM_CREATE:
            // move the AllWin32StatBoxThreadedGUI class pointer into the
            // userdata of the window.
            //
            lpCreateParams = (MDICREATESTRUCT *)((CREATESTRUCT *)lParam)->lpCreateParams;

            ::SetWindowLongPtr (hwnd, GWLP_USERDATA,
                    static_cast <LONG_PTR>(lpCreateParams->lParam));
 ....

It's first an LPCREATESTRUCT as with CreateWindow, and the ->lpCreateParams is not our answer but an LPMDICREATESTRUCT, then we get ->lParam from that for the answer... What a mess.


NEW! (4:05 PM 11/16/2003)

Just added a new MDI child window type to act as a container for Tk [L14 ] [L15 ] . The container protocol seems to be missing the ability to resize, though. I'll leave this for now, and move forward instead of banging my head on the desk trying to fix Tk's broken container protocol. It's fine for the Tcl/Tk Tclet Plugin as it's toplevel doesn't ever get resized.

http://tomasoft.sf.net/x2_tkembed.gif

Cool stuff!! :)


(4:25 PM 2/9/2008)

If this is a blog, I sure am slow to ever post anything. I mainly started this page as a place to write notes to myself essentially (pretending to explain to others my weird little project). So here is another little note to myself..

I did prove that my back-end irc_engine.dll could run on tclhttpd and serve live IRC with html and an unending server connection (transfer-coding = "chunked") which was a fun tclhttpd patch to write. Although my main computer is down today with a sick harddrive controller, it will return and my IRC gateway to the Tcl Chatroom will be alive again:

http://www.pobox.com/~davygrvy/chat/tcl

Note to self: insert gateway script into X-2 source tree so it doesn't get lost forever.

I tried to put together a Jabber->IRC gateway also, but got a bit lost.. I might pick it up again. Rather than how ijchain does it, I want both sides to equal represent each other and have the gateway create Jabber users for those on IRC, and create IRC users for the jabber ones to allow for a transparent way of linking both sides.