#!/usr/bin/ruby -Ke
require 'rice/irc'
require 'rice/observer'
require 'kconv'
require 'ghoti'
require 'getopts'
require 'thread'

getopts(nil, 'server:', 'channel:', 'nick:')

SERVER = $OPT_server || '127.0.0.1'
PORT = 6667
NICK = $OPT_nick || 'GhotiUser'
USER = 'ghotitest'
REAL = 'Ghoti'
PASS = ''
CHANNEL = $OPT_channel || '#GhotiTest'

p NICK

class GhotiClient < RICE::SimpleClient
	def initialize(nick, user, real, pass, channel, name = nil)
		super(nick, user, real, pass, channel)
		@ghoti = Ghoti.new(ARGV[0])
		@ghoti_tm = Time.now
		@ghoti_mutex = Mutex.new
	end

	def ghoti_talk(subject,channel)
		@ghoti_mutex.synchronize do
			res = false
			case Time.now - @ghoti_tm
			when 0..2
				res = true if rand(100) < 5
			when 2..10
				res = true if rand(100) < 50
			else
				res = true if rand(100) < 80
			end
			
			if res
				ghoti_msg, *tmp = @ghoti.talk
				
				sleep(rand*5)
				@ghoti_tm = Time.now
			else
				ghoti_msg = ''
			end
			
			subject.push(privmsg(channel, ghoti_msg.tojis))
		end
	end

	def response_for_privmsg(subject, message)
		begin
			@ghoti_mutex.synchronize do
				msg = message.params[1..-1].join(' ')

				@ghoti.memorize(msg.toeuc)
			end
			ghoti_talk(subject,message.params[0])
		rescue
			p [$!,$@]
			raise
		end
	end

	def response_for_ping(subject, message)
		begin
			subject.push pong(message.params[0])
			ghoti_talk(subject,CHANNEL) if rand(100) < 5
		rescue
			p [$!,$@]
			raise
		end
	end

end

o = GhotiClient.new(NICK, USER, REAL, PASS, CHANNEL)
c = RICE::Connection.new(SERVER, PORT)
c.add_observer(o)
begin
	c.start
rescue RICE::Connection::Closed
	log_puts("closed.")
end

    Source: geocities.com/svnseeds/ghoti

               ( geocities.com/svnseeds)