#!/usr/bin/ruby -d # # ricaco.rb # -- rica sample code # NISHI Takao # # $Id: ricaco.rb,v 1.4 2001/08/29 05:11:16 zophos Exp $ # require 'rica' require 'ctcpresponder' require 'jcode' require 'curses' class Ricaco%t< %a")) else putstr(msg.string("%T %s >%t:%f< %a")) end else if(msg.isPriv?) putstr(msg.string("%T %s =%f= %a")) else putstr(msg.string("%T %s <%t:%f> %a")) end end end alias on_recv_cmnd_notice on_recv_cmnd_privmsg def on_recv_cmnd_join(msg) if(msg.isSelfMessage?) @currentServer=msg.server @currentChnl=String(msg.args[0]).strip @logininfo[msg.server].push(@currentChnl) @logininfo[msg.server].uniq! end putstr(msg.string("%T %s %t:%f %C %a")) end def on_recv_cmnd_part(msg) if(msg.isSelfMessage?) @logininfo[msg.server].delete(msg.to) end putstr(msg.string("%T %s %t:%f %C %a")) end def getstr #return @talk.getstr return @strq.pop end def putstr(msg) until(msg.empty?) msg=addstrWithScrool(msg) end @view.refresh show_prompt end def prompt show_prompt msg=Kconv::toeuc(self.getstr) msg.chomp! unless(msg.empty?) # # /hoge (except //hoge) is assumed as command 'hoge'. # //hoge is treated as message '/hoge'. # if(msg=~/^\/([^\/].+)/) unless(exec_cmnd($1)) return false end else if(msg=~/^\/(.+)/) msg=$1 end if((!@currentChnl.empty?)&&(!@currentServer.empty?)) self.cmnd_privmsg(@currentServer,@currentChnl,msg) end end end return true end private # # # def getstrloop loop do c=@talk.getch case c when 8 # ^H @instr.chop! when 13 # ^M @strq.push(@instr) @instr="" when 27 # ^[ @talk.getch @talk.getch when 127 @instr.chop! else @instr+=c.chr if(c>127) @instr+=@talk.getch.chr end end show_prompt end end # # line-breaking and scrooling supported 2byte chars # def addstrWithScrool(msg) orgmsg=msg while(msg.size>=@view_cols) msg=msg.chop end if(orgmsg==msg) orgmsg="" else orgmsg=orgmsg[msg.size..-1] end @msgbuf.push(msg+"\n") @cur_lines+=1 if(@cur_lines<@view_lines) @view.setpos(@cur_lines-1,0) @view.addstr(msg+"\n") else while(@cur_lines>=@view_lines) tmp=@msgbuf.shift @cur_lines-=1 end i=0 @msgbuf.each{|m| @view.setpos(i,0) @view.addstr(m) i+=1 } end return orgmsg end def show_prompt p=prompt_str @talk.addstr(@instr+"\n") @talk.setpos(0,p.size+@instr.size) @talk.refresh end def prompt_str str=String(@currentChnl)+"@"+String(@currentServer) if(str=="@") str="" end str+="> " @talk.setpos(0,0) @talk.standout @talk.addstr(str) @talk.standend return str end # commands: # connect to server: # open @server:port:passwd:alias nick username real_name # # close connection of server: # quit @server # # quit this program: # quitall or exit # # issue irc_command to server: # irc_command @server irc command arguments # def exec_cmnd(msg) tmp=msg.strip.split(" ",3) cmnd=String(tmp.shift) server=String(@currentServer) args="" if(cmnd=~/([^\@]+)\@(.+)/) cmnd=$1 tmp.unshift("@"+$2) end unless(tmp[0].nil?) if(tmp[0][0].chr=='@') server=String(tmp.shift) server=server[1..-1] end args=String(tmp.join(" ")) end case cmnd.upcase when "C" if(args=~/([^\@]+)\@(.+)/) @currentServer=$2 @currentChnl=$1 else @currentChnl=args end when "S" @currentChannel="" if(server!=@currentServer) @currentServer=server else @currentServer=args end when "P" tmp=args.split(" ",2) if((!server.empty?)&&(!tmp[0].nil?)) @currentServer=server @currentChnl=String(tmp[0]) cmnd_privmsg(server,String(tmp[0]),String(tmp[1])) end when "OPEN" tmp=args.split(" ",2) if(tmp.empty?) return true end args=String(tmp[1]) server=String(tmp[0]) tmp=server.split(":",4) server=tmp[0] port=6667 unless(tmp[1].nil?) port=tmp[1].to_i end passwd=String(tmp[2]) serveralias=tmp[3] tmp=args.strip.split(" ",3) nick=String(tmp[0]) user=String(tmp[1]) realname=String(tmp[2]) if(user.empty?) user=nick end if(realname.empty?) realname=user end open([server,port,passwd,serveralias],[user,realname],nick) when "EXIT","QUITALL" closeAll return false else if((!server.empty?)&&(!cmnd.empty?)) directcommand(server,(cmnd+" "+args).strip) end end return true end end irc=Ricaco.new ctcp=Rica::CtcpResponder.new ctcp.version="Ricaco 0.3 with "+ctcp.version # # loop until input "/exit" # while(irc.prompt) end exit