Nano-IRC client in Forth

  • Follow


Here it is: an IRC nano-client, with an extremely narrow purpose,
i.e. group-talk with people on #forth.

It's 85 lines of Forth, and needs a socket library (e.g. see my 
home pages).

For reference: the source code for ircll (text mode UNIX IRC client)
is around 1.64 MBytes of (C) text.

I tried to clean out iForthisms, but didn't test the code resulting 
from that operation.

The output to the console has a huge amount of address-info in front 
of each message, it would be nice to compact that to the nick of the 
talker. But for my own purposes, this version is usable.

-marcel
-- ----------
( *
  * LANGUAGE    : ANS Forth
  * PROJECT     : Forth Environments
  * DESCRIPTION : A nano-IRC client
  * CATEGORY    : Utility - check RFC-1459 for more functionality
  * AUTHOR      : Marcel Hendrix 
  * LAST CHANGE : Sunday, December 31, 2006, 14:12 PM, Marcel Hendrix; works for Linux with UTYPE 
  * LAST CHANGE : Saturday, December 30, 2006, 1:46 AM, Marcel Hendrix 
  * )

	DECIMAL
	NEEDS -sockets
	\ $+   concatenates two strings ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 )
	\ +CR  appends ^M and ^J control characters (network convention)
	ANEW -irc

0 [IF]
   Use socket API functions to perform IRC communication using TCP. 
   Connect to an (internally specified) IRC server, using predefined 
   REALNAME, USERNAME, and NICK strings.

   BUGS: There may be an "echo" when you resume HEAR after a SPEAK (text from other
         users typed during your absence is echoed prefixed with your nick).
[THEN]

\ standard tools --------------------------------------------------------------

0 VALUE lsocket				\ socket for IRC comms
0 VALUE #resp				\ response size
CREATE  response$ 512 CHARS ALLOT 	\ buffers response from server

\ Force interpretation of ^M and ^J (so they are the same in Windows and Linux)
: UTYPE		BOUNDS ?DO  I C@ EMIT  LOOP ; 				 ( -- )
: SEND$		lsocket WRITE-SOCKET ; 			        ( c-addr u -- )
: CMD		+CR SEND$ ;				        ( c-addr u -- )
: RECEIVE$	lsocket response$ 512 READ-SOCKET TO #resp DROP ;	 ( -- )
: ECHO-ALL 	BEGIN  RECEIVE$ #resp  WHILE  response$ #resp UTYPE  REPEAT ;

\ -----------------------------------------------------------------------------

6667 =: IPPORT_IRC 	\ standard IRC port address

CREATE server		," irc.freenode.net"
CREATE username		," mhx"
CREATE realname		," marcel hendrix"
CREATE nick		LINUX? [IF] ," BirdReynolds" [ELSE] ," gorgonzola" [THEN]

\ The server may challenge our vitality
: ?TEST-PING ( -- )
	response$ S" PING" TUCK COMPARE 
	0= IF  'O' response$ CHAR+ C! 
		response$ #resp +CR SEND$
		CLEAR #resp
	ENDIF ; 

: IRC-OPEN ( -- )
	server COUNT IPPORT_IRC OPEN-SERVICE TO lsocket 
	lsocket FALSE BLOCKING-MODE
	0 SET-SOCKET-TIMEOUT  RECEIVE$ 
	S" NICK " nick COUNT $+ CMD
	S" USER " username COUNT $+ S"  8 * :" $+ realname COUNT $+ CMD 
	S" JOIN #forth" CMD ;

: IRC-CLOSE ( -- ) 
	S" QUIT :a quit that really quits" CMD
	lsocket CLOSE-SOCKET ; 

: SPEAK ( -- )
	S" PRIVMSG #forth :" 0 WORD COUNT $+  
	CR ECHO-ALL CMD ; 

: HEAR ( -- )
	CR
	BEGIN 
	  RECEIVE$ 
	  ?TEST-PING
	  response$ #resp UTYPE
	  EKEY?
	UNTIL 
	EKEY DROP ;

: .ABOUT ( -- )
	CR ." Usage: IRC-OPEN    -- connect to #forth"
	CR ."        HEAR        -- listen what they say"
	CR ."        SPEAK text  -- write a message (text) to all on #forth"
	CR ."        IRC-CLOSE   -- disconnect from #forth" ; 

1
Reply mhx (2133) 12/31/2006 7:41:57 PM

Marcel Hendrix wrote:
> Here it is: an IRC nano-client, with an extremely narrow purpose,
> i.e. group-talk with people on #forth.
>
> It's 85 lines of Forth, and needs a socket library (e.g. see my
> home pages).

Nice work!  That reminds me.  I think it would be really productive
if the Forth community could collaborate on a "FNL (Forth Net
Library) similair to how the FSL was built.  Perhaps if the "sockets"
wordset was used as a reference implementation with other
libraries built on top of it?

Regards,

John M. Drake

0
Reply johnmdrake (128) 1/1/2007 4:31:41 AM


John M. Drake wrote:
> 
> Marcel Hendrix wrote:
> > Here it is: an IRC nano-client, with an extremely narrow purpose,
> > i.e. group-talk with people on #forth.
> >
> > It's 85 lines of Forth, and needs a socket library (e.g. see my
> > home pages).
> >
> > For reference: the source code for ircll (text mode UNIX IRC client)
> > is around 1.64 MBytes of (C) text.
> >
> > I tried to clean out iForthisms, but didn't test the code resulting
> > from that operation.
> 
> As an exercise I'm attempting to port this to another ANS Forth.
> I've chosen SP-Forth as my target.  (I tried Win32Forth, but the
> latest version of Win32Forth has an incompatibility issue that
> prevents it from loading it's own sockets.f library.  The offending
> word is &LOCAL.)

Add the following line at the start:
Synonym &local &of

Jos
==
4ePost: 844 bytes in mail. Elapsed time to buffer: .000088 sec.
0
Reply josv (129) 1/3/2007 5:16:00 PM

Marcel Hendrix wrote:
> Here it is: an IRC nano-client, with an extremely narrow purpose,
> i.e. group-talk with people on #forth.
>
> It's 85 lines of Forth, and needs a socket library (e.g. see my
> home pages).
>
> For reference: the source code for ircll (text mode UNIX IRC client)
> is around 1.64 MBytes of (C) text.
>
> I tried to clean out iForthisms, but didn't test the code resulting
> from that operation.

As an exercise I'm attempting to port this to another ANS Forth.
I've chosen SP-Forth as my target.  (I tried Win32Forth, but the
latest version of Win32Forth has an incompatibility issue that
prevents it from loading it's own sockets.f library.  The offending
word is &LOCAL.)

I was able to find a lot of the non ANS words in the code such as
BOUNDS and ,".  I also figured out code for CR+ and +$ myself.
But there are still a couple of words I'm unsure about.

> 6667 =: IPPORT_IRC 	\ standard IRC port address

I'm guessing that's the same as:

6667 CONSTANT IPPORT_IRC

> : ?TEST-PING ( -- )
> 	response$ S" PING" TUCK COMPARE
> 	0= IF  'O' response$ CHAR+ C!
> 		response$ #resp +CR SEND$
> 		CLEAR #resp
> 	ENDIF ; 

What does "CLEAR #resp" do?
 
Regards,

John M. Drake

0
Reply johnmdrake (128) 1/3/2007 5:43:07 PM

Jos van de Ven wrote:
> John M. Drake wrote:

> > As an exercise I'm attempting to port this to another ANS Forth.
> > I've chosen SP-Forth as my target.  (I tried Win32Forth, but the
> > latest version of Win32Forth has an incompatibility issue that
> > prevents it from loading it's own sockets.f library.  The offending
> > word is &LOCAL.)
>
> Add the following line at the start:
> Synonym &local &of
> 
> Jos

&of doesn't work either.  I'm using version 6.10.05 build 2.

0
Reply johnmdrake (128) 1/3/2007 6:50:27 PM

John M. Drake wrote:
> 
> Jos van de Ven wrote:
> > John M. Drake wrote:
> 
> > > As an exercise I'm attempting to port this to another ANS Forth.
> > > I've chosen SP-Forth as my target.  (I tried Win32Forth, but the
> > > latest version of Win32Forth has an incompatibility issue that
> > > prevents it from loading it's own sockets.f library.  The offending
> > > word is &LOCAL.)
> >
> > Add the following line at the start:
> > Synonym &local &of
> > 
> > Jos
> 
> &of doesn't work either.  I'm using version 6.10.05 build 2.

It should work with Win32Forth V6.11.10
That can be downloaded from:
http://sourceforge.net/project/showfiles.php?group_id=55294

Jos
==
4ePost: 688 bytes in mail. Elapsed time to buffer: .000076 sec.
0
Reply josv (129) 1/3/2007 7:04:35 PM

Jos van de Ven wrote:

> It should work with Win32Forth V6.11.10
> That can be downloaded from:
> http://sourceforge.net/project/showfiles.php?group_id=55294
>
> Jos

Thanks.  I've made the changes and it works now.  I also
got rid of REL>ABS and ABS>REL to stop all of the
irritating "Warning REL>ABS is a deprecated word"
messages.  I've uploaded the new file to the Win32Forth
Yahoo group.

Regards,

John M. Drake

0
Reply johnmdrake (128) 1/4/2007 4:36:37 PM

mhx@iae.nl (Marcel Hendrix) writes:
>I tried to clean out iForthisms, but didn't test the code resulting 
>from that operation.

iForth has a feature that reports non-ANS words (that feature is
annoyingly on by default).  It might help for such cleaning jobs.

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: http://www.forth200x.org/forth200x.html
Forth-Tagung 2007: http://www.complang.tuwien.ac.at/anton/forth-tagung07/
EuroForth 2007: September 13-16, 2007
0
Reply anton (5253) 1/5/2007 8:38:35 PM

7 Replies
842 Views

(page loaded in 0.099 seconds)

Similiar Articles:




7/26/2012 11:32:11 PM


Reply: