Class: Isaac::IRC
- Inherits:
-
Object
- Object
- Isaac::IRC
- Defined in:
- lib/isaac.rb
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize(bot, config) ⇒ IRC
constructor
A new instance of IRC.
- #message(msg) ⇒ Object
- #parse(input) ⇒ Object
- #registered? ⇒ Boolean
Constructor Details
#initialize(bot, config) ⇒ IRC
Returns a new instance of IRC.
106 107 108 109 110 |
# File 'lib/isaac.rb', line 106 def initialize(bot, config) @bot, @config = bot, config @transfered = 0 @registration = [] end |
Instance Method Details
#connect ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/isaac.rb', line 112 def connect tcpsocket = TCPSocket.open(@config.server, @config.port) if @config.ssl require 'openssl' ssl_context = OpenSSL::SSL::SSLContext.new() ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE @socket = OpenSSL::SSL::SSLSocket.new(tcpsocket, ssl_context) @socket.sync = true @socket.connect else @socket = tcpsocket end @queue = Queue.new(@socket, @bot.config.server) "PASS #{@config.password}" if @config.password "NICK #{@config.nick}" "USER #{@config.nick} 0 * :#{@config.realname}" @queue.lock while line = @socket.gets parse line end end |
#message(msg) ⇒ Object
179 180 181 |
# File 'lib/isaac.rb', line 179 def (msg) @queue << msg end |
#parse(input) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/isaac.rb', line 136 def parse(input) puts "<< #{input}" if @bot.config.verbose case input.chomp when /(^:\S+ )?00([1-4])/ @registration << $2.to_i if registered? @queue.unlock @bot.dispatch(:connect) end when /(^:(\S+)!\S+ )?PRIVMSG \S+ :?\001VERSION\001/ "NOTICE #{$2} :\001VERSION #{@bot.config.version}\001" when /^PING (\S+)/ @queue.unlock "PONG #{$1}" when /(^:(\S+)!(\S+) )?PRIVMSG (\S+) :?(.*)/ env = { :nick => $2, :userhost => $3, :channel => $4, :message => $5 } type = env[:channel].match(/^#/) ? :channel : :private @bot.dispatch(type, env) when /(^:(\S+)!(\S+) )?NOTICE (\S+) :?(.*)/ env = { :nick => $2, :userhost => $3, :channel => $4, :message => $5 } type = env[:channel].match(/^#/) ? :chnotice : :notice @bot.dispatch(type, env) when /(^:(\S+)!(\S+) )?NICK (\S+)/ env = { :nick => $2, :userhost => $3, :newnick => $4 } if env[:newnick][0] == 58 || env[:newnick][0] == ":" # Seems Unreal adds a colon before nick changes. Don't think # this is required by the RFC, but it's easy to fix it. :/ env[:newnick].gsub!(":", "") end type = :nick @bot.dispatch(type, env) when /(^:\S+ )?([4-5]\d\d) \S+ (\S+)/ env = {:error => $2.to_i, :message => $2, :nick => $3, :channel => $3} @bot.dispatch(:error, env) when /(^:\S+ )?PONG/ @queue.unlock end end |
#registered? ⇒ Boolean
175 176 177 |
# File 'lib/isaac.rb', line 175 def registered? ([1,2,3,4] - @registration).empty? end |