Guest User

Untitled

a guest
Mar 6th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. #############################################
  3. #### RbIRC - by CodeBlock and duckinator ####
  4. #############################################
  5. require 'rubygems'
  6. require 'wx' # GUI Library
  7. include Wx # Eliminate need for WX::
  8. require "open3"
  9. require 'socket'
  10.  
  11. class Run < Frame
  12. def initialize
  13. @info = {
  14. "name" => "RbIRC",
  15. "by" => "CodeBlock and duckinator",
  16. "version" => "1.0"
  17. }
  18. @irc = IRC.new
  19.  
  20. super(nil,-1,"#{@info['name']}",nil,Size.new(700,400))
  21. @window = Panel.new(self)
  22. sizer = BoxSizer.new(VERTICAL)
  23. @window.set_sizer(sizer)
  24. @backscroll = []
  25.  
  26. $textarea = TextCtrl.new(@window,-1,nil,nil,nil,TE_MULTILINE|TE_READONLY)
  27. sizer.add($textarea, 1, Wx::GROW|ALL, 2)
  28.  
  29. @text = TextCtrl.new(@window,3,nil,nil,nil,TE_PROCESS_ENTER)
  30. sizer.add(@text, 0, Wx::GROW|ALL, 2)
  31.  
  32. @status = StatusBar.new(self)
  33. self.status_bar = @status
  34. @status.push_status_text "Welcome to #{@info['name']} by #{@info['by']}."
  35.  
  36. evt_text_enter(3) do |input|
  37. @backscroll << input.get_string
  38. parse input.get_string
  39. @text.clear
  40. end
  41.  
  42.  
  43. filemenu = Menu.new()
  44. filemenu.append(1,"&Connect...\tCtrl-L","Connect...")
  45. filemenu.append_separator
  46. filemenu.append(ID_ANY,"&Quit...\tCtrl-Q","Quit...")
  47.  
  48. helpmenu = Menu.new()
  49. helpmenu.append(ID_ABOUT,"&About","About")
  50.  
  51.  
  52. menu_bar = MenuBar.new()
  53. menu_bar.append(filemenu,"&File")
  54. menu_bar.append(helpmenu,"&Help")
  55. set_menu_bar(menu_bar)
  56.  
  57.  
  58. # Menu Actions
  59. evt_menu(1) do on_connect end
  60. evt_menu(ID_EXIT) do on_quit end
  61. evt_menu(ID_ABOUT) do on_abt end
  62.  
  63.  
  64. #@text.evt_key_down do |keycode|
  65. # @text.append_text keycode.get_unicode_key()
  66. # if keycode.get_key_code == 315 # down arrow
  67. # # do something
  68. # end
  69. #end
  70.  
  71. @text.set_focus # Give focus to the input bar.
  72. show()
  73. end
  74. def parse(input)
  75. case input
  76. when /^\/exec (.*)/i
  77. firstcommand = $1
  78. if $1 =~ /^-o (.*)/
  79. command = $1
  80. irc = true
  81. else
  82. command = firstcommand
  83. irc = false
  84. end
  85. Open3.popen3(command) do |stdin,stdout,stderr|
  86. $textarea.append_text stdout.gets.to_s + "\n"
  87. $textarea.append_text stderr.gets.to_s + "\n"
  88. if irc == true
  89. @irc.privmsg stdout.gets.to_s.strip, @irc.curchan
  90. @irc.privmsg stderr.gets.to_s.strip, @irc.curchan
  91. end
  92.  
  93. end
  94. when /^\/oldcommands/i
  95. @backscroll.each do |bs|
  96. puts "=> #{bs}"
  97. end
  98. when /^\/msg (.+?) (.+)/i
  99. @irc.privmsg $2, $1
  100. when /^\/notice (.+?) (.+)/i
  101. @irc.notice $2, $1
  102. when /^\/connect/i
  103. @irc.connect("irc.freenode.net", 6667, "CodeBlock|RbIRC", "#duckinator")
  104. when /^\/quit (.+)/i
  105. @irc.quit $1
  106. when /^\/quit/i
  107. @irc.quit "#{@info['name']} :: Ruby IRC Client Using WxRuby :: Created by #{@info['by']}"
  108. when /^\/me (.*)/i
  109. @irc.act $1, @irc.curchan
  110. else
  111. #$textarea.append_text input + "\n"
  112. @irc.privmsg input, @irc.curchan
  113. end
  114. end
  115. def on_abt
  116. Wx::about_box(
  117. :name => "#{@info['name']} - by #{@info['by']}",
  118. :version => "#{@info['name']}",
  119. :description => "An IRC Client in WxRuby."
  120. )
  121. end
  122. def on_connect
  123. @irc.connect("irc.freenode.net", 6667, "CodeBlock|RbIRC", "#duckinator")
  124. end
  125. end
  126.  
  127. class Client < App
  128. def on_init
  129. Run.new
  130. end
  131. end
  132.  
  133. class IRC
  134. def initialize
  135. @current = {
  136. "chan" => nil,
  137. "server" => nil,
  138. "nick" => nil
  139. }
  140. @servers = {}
  141. end
  142. def send(data)
  143. @servers[@current['server']]['sock'].send "#{data}\n",0
  144. #$textarea.append_text "#{data}\n"
  145. end
  146. def connect (server, port, nick, chans)
  147. @servers[server.downcase] = {
  148. "sock" => TCPSocket.open(server,port),
  149. "chans" => chans.split(','),
  150. "nick" => nick
  151. }
  152. @current['server'] = server.downcase
  153. @current['chan'] = @servers[server.downcase]['chans'][0]
  154. @current['nick' ] = nick
  155. send "USER #{nick} #{nick} #{nick} :#{nick}"
  156. send "NICK #{nick}"
  157. send "JOIN #{chans}"
  158. end
  159. def privmsg(msg, recip) # Send Message
  160. send "PRIVMSG #{recip} :#{msg}"
  161. if recip == curchan
  162. $textarea.append_text "<#{curnick}> #{msg}\n"
  163. elsif
  164. $textarea.append_text "<#{curnick}/#{recip}> #{msg}\n"
  165. end
  166. end
  167. def notice(msg, recip) # Send Notice
  168. send "NOTICE #{recip} :#{msg}"
  169. if recip == curchan
  170. $textarea.append_text "-#{curnick}- #{msg}\n"
  171. elsif
  172. $textarea.append_text "-#{curnick}/#{recip}- #{msg}\n"
  173. end
  174. end
  175. def act(msg, recip) # Send ACtion
  176. privmsg "#{1.chr}ACTION #{msg}#{1.chr}", recip
  177. if recip == curchan
  178. $textarea.append_text "<#{curnick}> #{msg}\n"
  179. elsif
  180. $textarea.append_text "<#{curnick}/#{recip}> #{msg}\n"
  181. end
  182. end
  183. def quit(msg) # Quit
  184. send "QUIT #{msg}\n"
  185. end
  186.  
  187. def set_curchan(chan)
  188. $textarea.append_text "\n -- Now talking in #{curserv}/#{chan} --\n"
  189. @current['chan'] = chan
  190. end
  191. def curchan
  192. return @current['chan']
  193. end
  194. def set_curserv(server)
  195. $textarea.append_text "\n -- Now talking in #{server}/#{curchan}"
  196. @current['server'] = server
  197. end
  198. def curserv
  199. return @current['server']
  200. end
  201. def set_curnick(nick)
  202. $textarea.append_text " -- You are now known as #{nick} --"
  203. @current['nick'] = nick
  204. send "NICK #{nick}"
  205. end
  206. def curnick
  207. return @current['nick']
  208. end
  209. end
  210.  
  211. Client.new.main_loop
Add Comment
Please, Sign In to add comment