Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- #############################################
- #### RbIRC - by CodeBlock and duckinator ####
- #############################################
- require 'rubygems'
- require 'wx' # GUI Library
- include Wx # Eliminate need for WX::
- require "open3"
- require 'socket'
- class Run < Frame
- def initialize
- @info = {
- "name" => "RbIRC",
- "by" => "CodeBlock and duckinator",
- "version" => "1.0"
- }
- @irc = IRC.new
- super(nil,-1,"#{@info['name']}",nil,Size.new(700,400))
- @window = Panel.new(self)
- sizer = BoxSizer.new(VERTICAL)
- @window.set_sizer(sizer)
- @backscroll = []
- $textarea = TextCtrl.new(@window,-1,nil,nil,nil,TE_MULTILINE|TE_READONLY)
- sizer.add($textarea, 1, Wx::GROW|ALL, 2)
- @text = TextCtrl.new(@window,3,nil,nil,nil,TE_PROCESS_ENTER)
- sizer.add(@text, 0, Wx::GROW|ALL, 2)
- @status = StatusBar.new(self)
- self.status_bar = @status
- @status.push_status_text "Welcome to #{@info['name']} by #{@info['by']}."
- evt_text_enter(3) do |input|
- @backscroll << input.get_string
- parse input.get_string
- @text.clear
- end
- filemenu = Menu.new()
- filemenu.append(1,"&Connect...\tCtrl-L","Connect...")
- filemenu.append_separator
- filemenu.append(ID_ANY,"&Quit...\tCtrl-Q","Quit...")
- helpmenu = Menu.new()
- helpmenu.append(ID_ABOUT,"&About","About")
- menu_bar = MenuBar.new()
- menu_bar.append(filemenu,"&File")
- menu_bar.append(helpmenu,"&Help")
- set_menu_bar(menu_bar)
- # Menu Actions
- evt_menu(1) do on_connect end
- evt_menu(ID_EXIT) do on_quit end
- evt_menu(ID_ABOUT) do on_abt end
- #@text.evt_key_down do |keycode|
- # @text.append_text keycode.get_unicode_key()
- # if keycode.get_key_code == 315 # down arrow
- # # do something
- # end
- #end
- @text.set_focus # Give focus to the input bar.
- show()
- end
- def parse(input)
- case input
- when /^\/exec (.*)/i
- firstcommand = $1
- if $1 =~ /^-o (.*)/
- command = $1
- irc = true
- else
- command = firstcommand
- irc = false
- end
- Open3.popen3(command) do |stdin,stdout,stderr|
- $textarea.append_text stdout.gets.to_s + "\n"
- $textarea.append_text stderr.gets.to_s + "\n"
- if irc == true
- @irc.privmsg stdout.gets.to_s.strip, @irc.curchan
- @irc.privmsg stderr.gets.to_s.strip, @irc.curchan
- end
- end
- when /^\/oldcommands/i
- @backscroll.each do |bs|
- puts "=> #{bs}"
- end
- when /^\/msg (.+?) (.+)/i
- @irc.privmsg $2, $1
- when /^\/notice (.+?) (.+)/i
- @irc.notice $2, $1
- when /^\/connect/i
- @irc.connect("irc.freenode.net", 6667, "CodeBlock|RbIRC", "#duckinator")
- when /^\/quit (.+)/i
- @irc.quit $1
- when /^\/quit/i
- @irc.quit "#{@info['name']} :: Ruby IRC Client Using WxRuby :: Created by #{@info['by']}"
- when /^\/me (.*)/i
- @irc.act $1, @irc.curchan
- else
- #$textarea.append_text input + "\n"
- @irc.privmsg input, @irc.curchan
- end
- end
- def on_abt
- Wx::about_box(
- :name => "#{@info['name']} - by #{@info['by']}",
- :version => "#{@info['name']}",
- :description => "An IRC Client in WxRuby."
- )
- end
- def on_connect
- @irc.connect("irc.freenode.net", 6667, "CodeBlock|RbIRC", "#duckinator")
- end
- end
- class Client < App
- def on_init
- Run.new
- end
- end
- class IRC
- def initialize
- @current = {
- "chan" => nil,
- "server" => nil,
- "nick" => nil
- }
- @servers = {}
- end
- def send(data)
- @servers[@current['server']]['sock'].send "#{data}\n",0
- #$textarea.append_text "#{data}\n"
- end
- def connect (server, port, nick, chans)
- @servers[server.downcase] = {
- "sock" => TCPSocket.open(server,port),
- "chans" => chans.split(','),
- "nick" => nick
- }
- @current['server'] = server.downcase
- @current['chan'] = @servers[server.downcase]['chans'][0]
- @current['nick' ] = nick
- send "USER #{nick} #{nick} #{nick} :#{nick}"
- send "NICK #{nick}"
- send "JOIN #{chans}"
- end
- def privmsg(msg, recip) # Send Message
- send "PRIVMSG #{recip} :#{msg}"
- if recip == curchan
- $textarea.append_text "<#{curnick}> #{msg}\n"
- elsif
- $textarea.append_text "<#{curnick}/#{recip}> #{msg}\n"
- end
- end
- def notice(msg, recip) # Send Notice
- send "NOTICE #{recip} :#{msg}"
- if recip == curchan
- $textarea.append_text "-#{curnick}- #{msg}\n"
- elsif
- $textarea.append_text "-#{curnick}/#{recip}- #{msg}\n"
- end
- end
- def act(msg, recip) # Send ACtion
- privmsg "#{1.chr}ACTION #{msg}#{1.chr}", recip
- if recip == curchan
- $textarea.append_text "<#{curnick}> #{msg}\n"
- elsif
- $textarea.append_text "<#{curnick}/#{recip}> #{msg}\n"
- end
- end
- def quit(msg) # Quit
- send "QUIT #{msg}\n"
- end
- def set_curchan(chan)
- $textarea.append_text "\n -- Now talking in #{curserv}/#{chan} --\n"
- @current['chan'] = chan
- end
- def curchan
- return @current['chan']
- end
- def set_curserv(server)
- $textarea.append_text "\n -- Now talking in #{server}/#{curchan}"
- @current['server'] = server
- end
- def curserv
- return @current['server']
- end
- def set_curnick(nick)
- $textarea.append_text " -- You are now known as #{nick} --"
- @current['nick'] = nick
- send "NICK #{nick}"
- end
- def curnick
- return @current['nick']
- end
- end
- Client.new.main_loop
Add Comment
Please, Sign In to add comment