class IRCBot
Public Class Methods
new(network, port, nick, user_name, real_name)
click to toggle source
# File lib/rirc.rb, line 465 def initialize(network, port, nick, user_name, real_name) @network = network @port = port @nick = nick @user_name = user_name @real_name = real_name @socket = nil @channels = [] @admins = [] @ignore = [] @hooks = {} @backlog = [] @log_path = "./.log" @err_path = "./.errlog" @stop_hooks = [] @stop_regs = [] end
Public Instance Methods
action(dest, message)
click to toggle source
# File lib/rirc.rb, line 585 def action(dest, message) privmsg(dest, "\01ACTION #{message}\07\01") end
add_admin(nick)
click to toggle source
# File lib/rirc.rb, line 669 def add_admin(nick) if !@admins.include? nick then @admins.push(nick) end end
add_chan(channel)
click to toggle source
# File lib/rirc.rb, line 610 def add_chan(channel) if !@channels.include? channel then @channels.push(channel) end end
add_ignore(nick)
click to toggle source
# File lib/rirc.rb, line 677 def add_ignore(nick) @ignore.push(nick) end
admins()
click to toggle source
# File lib/rirc.rb, line 511 def admins return @admins end
auth(nickserv_pass)
click to toggle source
# File lib/rirc.rb, line 628 def auth(nickserv_pass) say "VERSION" say "USER #{@user_name} * * :#{@real_name}" nick(@nick) if nickserv_pass != "" and nickserv_pass != nil identify(nickserv_pass) end end
backlog()
click to toggle source
def initialize(network, port, nick, user_name
, real_name
, log_path, err_path)
@network = network @port = port @nick = nick @user_name = user_name @real_name = real_name @socket = nil @channels = [] @admins = [] @log_path = log_path @err_path = err_path @ignore = [] @hooks = {} @backlog = []
end
# File lib/rirc.rb, line 499 def backlog return @backlog end
channels()
click to toggle source
# File lib/rirc.rb, line 507 def channels return @channels end
connect()
click to toggle source
# File lib/rirc.rb, line 556 def connect @socket = TCPSocket.open(@network, @port) end
connect_pass(pass)
click to toggle source
# File lib/rirc.rb, line 569 def connect_pass(pass) say "PASS #{pass}" end
connect_ssl()
click to toggle source
# File lib/rirc.rb, line 561 def connect_ssl ssl_context = OpenSSL::SSL::SSLContext.new ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE @socket = OpenSSL::SSL::SSLSocket.new(@socket, ssl_context) @socket.sync = true @socket.connect end
create_log()
click to toggle source
# File lib/rirc.rb, line 705 def create_log if !File.exist?(@log_path) File.open(@log_path, "w+") { |fw| fw.write("Command and Privmsg LOGS") } end if !File.exist?(@err_path) File.open(@err_path, "w+") { |fw| fw.write("Error LOGS") } end end
ctcp(dest, message)
click to toggle source
# File lib/rirc.rb, line 595 def ctcp(dest, message) privmsg(dest, "\01VERSION #{message}\07\01") end
identify(nickserv_pass)
click to toggle source
# File lib/rirc.rb, line 624 def identify(nickserv_pass) say "PRIVMSG nickserv :identify #{nickserv_pass}" end
ignore()
click to toggle source
# File lib/rirc.rb, line 503 def ignore return @ignore end
join(channel)
click to toggle source
# File lib/rirc.rb, line 550 def join(channel) say "JOIN #{channel}" if !@channels.include? channel then @channels.push(channel) end end
join_channels(channels_s)
click to toggle source
# File lib/rirc.rb, line 701 def join_channels(channels_s) channels_s.each { |a| self.join(a) } end
names(dest)
click to toggle source
# File lib/rirc.rb, line 619 def names(dest) say "NAMES #{dest}" end
network()
click to toggle source
# File lib/rirc.rb, line 515 def network return @network end
nick(nick)
click to toggle source
# File lib/rirc.rb, line 574 def nick(nick) @nick = nick say "NICK #{nick}" end
nick_name()
click to toggle source
# File lib/rirc.rb, line 525 def nick_name return @nick end
notice(dest, message)
click to toggle source
# File lib/rirc.rb, line 590 def notice(dest, message) say "NOTICE #{dest} :#{message}" end
on(type, &block)
click to toggle source
# File lib/rirc.rb, line 685 def on(type, &block) type = type.to_s @hooks[type] ||= [] @hooks[type] << block end
parse(msg)
click to toggle source
# File lib/rirc.rb, line 653 def parse(msg) message_reg = msg.match(/^(:(?<prefix>\S+) )?(?<command>\S+)( (?!:)(?<params>.+?))?( :(?<trail>.+))?$/) nick_n = message_reg[:prefix].to_s.split("!")[0] command = message_reg[:command].to_s chan = message_reg[:params].to_s message = message_reg[:trail].to_s message = message.chomp if chan == @nick then chan = nick_n end ircmsg = IRC_message.new(command, nick_n, chan, message, msg) return ircmsg end
part(dest, message)
click to toggle source
# File lib/rirc.rb, line 600 def part(dest, message) @channels.delete_if { |a| dest == a } say "PART #{dest} :#{message}" end
port()
click to toggle source
# File lib/rirc.rb, line 520 def port return @port end
privmsg(dest, message)
click to toggle source
# File lib/rirc.rb, line 580 def privmsg(dest, message) say "PRIVMSG #{dest} :#{message}" end
quit(message)
click to toggle source
# File lib/rirc.rb, line 614 def quit(message) say "QUIT :#{message}" end
read()
click to toggle source
# File lib/rirc.rb, line 637 def read if !@socket.eof msg = @socket.gets if msg.match(/^PING :(.*)$/) say "PONG #{$~[1]}" return "PING" end return msg else return nil end end
real_name()
click to toggle source
# File lib/rirc.rb, line 535 def real_name return @real_name end
remove_admin(nick)
click to toggle source
# File lib/rirc.rb, line 673 def remove_admin(nick) @admins.delete_if { |a| a == nick } end
remove_ignore(nick)
click to toggle source
# File lib/rirc.rb, line 681 def remove_ignore(nick) @ignore.delete_if { |a| a == nick } end
rm_chan(channel)
click to toggle source
# File lib/rirc.rb, line 606 def rm_chan(channel) @channels.delete_if { |a| channel == a } end
say(message)
click to toggle source
# File lib/rirc.rb, line 545 def say(message) @socket.puts message end
set_admins(admins_s)
click to toggle source
# File lib/rirc.rb, line 697 def set_admins(admins_s) admins_s.each { |a| self.add_admin(a) } end
setup(use_ssl, use_pass, pass, nickserv_pass, channels_s)
click to toggle source
# File lib/rirc.rb, line 715 def setup(use_ssl, use_pass, pass, nickserv_pass, channels_s) self.connect if use_ssl then self.connect_ssl end if use_pass then self.connect_pass(pass) end self.auth(nickserv_pass) self.create_log self.join_channels(channels_s) self.on :message do |msg| if msg.nick == msg.channel File.write(@log_path, msg.ircmsg, File.size(@log_path), mode: 'a') end if !self.nick_name == msg.nick and !self.ignore.include? msg.nick @backlog.push(msg) end end end
socket()
click to toggle source
# File lib/rirc.rb, line 540 def socket return @socket end
start!()
click to toggle source
# File lib/rirc.rb, line 736 def start! until self.socket.eof? do ircmsg = self.read msg = self.parse(ircmsg) if ircmsg == "PING" or self.ignore.include?(msg.nick) then next end begin hooks = @hooks['command'] if hooks != nil hooks.each { |h| h.call(msg.channel, msg.command) } end rescue => e # do not do anything end begin hooks = @hooks['message'] if hooks != nil hooks.each { |h| h.call(msg) } end rescue => e # do not do anything end begin hooks = @hooks['ircmsg'] if hooks != nil hooks.each { |h| h.call(msg.nick, msg.command, msg.channel, msg.message) } end rescue => e # do not do anything end begin hooks = @stop_hooks i = 0 @stop_regs.each do |j| if msg.message =~ j and @admins.include? msg.nick hooks[i].call(msg) return end i += 1 end rescue => e # do not do anything end end end
stop!(reg, &block)
click to toggle source
# File lib/rirc.rb, line 691 def stop!(reg, &block) reg = Regexp.new(reg.to_s) @stop_regs.push(reg) @stop_hooks << block end
user_name()
click to toggle source
# File lib/rirc.rb, line 530 def user_name return @user_name end