Class: IRCBot
- Inherits:
-
Object
- Object
- IRCBot
- Defined in:
- lib/rirc.rb
Instance Method Summary collapse
- #action(dest, message) ⇒ Object
- #add_admin(nick) ⇒ Object
- #add_chan(channel) ⇒ Object
- #add_ignore(nick) ⇒ Object
- #admins ⇒ Object
- #auth(nickserv_pass) ⇒ Object
-
#backlog ⇒ Object
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.
- #channels ⇒ Object
- #connect ⇒ Object
- #connect_pass(pass) ⇒ Object
- #connect_ssl ⇒ Object
- #create_log ⇒ Object
- #ctcp(dest, message) ⇒ Object
- #identify(nickserv_pass) ⇒ Object
- #ignore ⇒ Object
-
#initialize(network, port, nick, user_name, real_name) ⇒ IRCBot
constructor
A new instance of IRCBot.
- #join(channel) ⇒ Object
- #join_channels(channels_s) ⇒ Object
- #names(dest) ⇒ Object
- #network ⇒ Object
- #nick(nick) ⇒ Object
- #nick_name ⇒ Object
- #notice(dest, message) ⇒ Object
- #on(type, &block) ⇒ Object
- #parse(msg) ⇒ Object
- #part(dest, message) ⇒ Object
- #port ⇒ Object
- #privmsg(dest, message) ⇒ Object
- #quit(message) ⇒ Object
- #read ⇒ Object
- #real_name ⇒ Object
- #remove_admin(nick) ⇒ Object
- #remove_ignore(nick) ⇒ Object
- #rm_chan(channel) ⇒ Object
- #say(message) ⇒ Object
- #set_admins(admins_s) ⇒ Object
- #setup(use_ssl, use_pass, pass, nickserv_pass, channels_s) ⇒ Object
- #socket ⇒ Object
- #start! ⇒ Object
- #stop!(reg, &block) ⇒ Object
- #user_name ⇒ Object
Constructor Details
#initialize(network, port, nick, user_name, real_name) ⇒ IRCBot
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# 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 |
Instance Method Details
#action(dest, message) ⇒ Object
585 586 587 588 |
# File 'lib/rirc.rb', line 585 def action(dest, ) privmsg(dest, "\01ACTION #{message}\07\01") end |
#add_admin(nick) ⇒ Object
669 670 671 |
# File 'lib/rirc.rb', line 669 def add_admin(nick) if !@admins.include? nick then @admins.push(nick) end end |
#add_chan(channel) ⇒ Object
610 611 612 |
# File 'lib/rirc.rb', line 610 def add_chan(channel) if !@channels.include? channel then @channels.push(channel) end end |
#add_ignore(nick) ⇒ Object
677 678 679 |
# File 'lib/rirc.rb', line 677 def add_ignore(nick) @ignore.push(nick) end |
#admins ⇒ Object
511 512 513 |
# File 'lib/rirc.rb', line 511 def admins return @admins end |
#auth(nickserv_pass) ⇒ Object
628 629 630 631 632 633 634 635 |
# 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 ⇒ Object
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
499 500 501 |
# File 'lib/rirc.rb', line 499 def backlog return @backlog end |
#channels ⇒ Object
507 508 509 |
# File 'lib/rirc.rb', line 507 def channels return @channels end |
#connect ⇒ Object
556 557 558 559 |
# File 'lib/rirc.rb', line 556 def connect @socket = TCPSocket.open(@network, @port) end |
#connect_pass(pass) ⇒ Object
569 570 571 572 |
# File 'lib/rirc.rb', line 569 def connect_pass(pass) say "PASS #{pass}" end |
#connect_ssl ⇒ Object
561 562 563 564 565 566 567 |
# 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 ⇒ Object
705 706 707 708 709 710 711 712 713 |
# 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) ⇒ Object
595 596 597 598 |
# File 'lib/rirc.rb', line 595 def ctcp(dest, ) privmsg(dest, "\01VERSION #{message}\07\01") end |
#identify(nickserv_pass) ⇒ Object
624 625 626 |
# File 'lib/rirc.rb', line 624 def identify(nickserv_pass) say "PRIVMSG nickserv :identify #{nickserv_pass}" end |
#ignore ⇒ Object
503 504 505 |
# File 'lib/rirc.rb', line 503 def ignore return @ignore end |
#join(channel) ⇒ Object
550 551 552 553 554 |
# 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) ⇒ Object
701 702 703 |
# File 'lib/rirc.rb', line 701 def join_channels(channels_s) channels_s.each { |a| self.join(a) } end |
#names(dest) ⇒ Object
619 620 621 622 |
# File 'lib/rirc.rb', line 619 def names(dest) say "NAMES #{dest}" end |
#network ⇒ Object
515 516 517 518 |
# File 'lib/rirc.rb', line 515 def network return @network end |
#nick(nick) ⇒ Object
574 575 576 577 578 |
# File 'lib/rirc.rb', line 574 def nick(nick) @nick = nick say "NICK #{nick}" end |
#nick_name ⇒ Object
525 526 527 528 |
# File 'lib/rirc.rb', line 525 def nick_name return @nick end |
#notice(dest, message) ⇒ Object
590 591 592 593 |
# File 'lib/rirc.rb', line 590 def notice(dest, ) say "NOTICE #{dest} :#{message}" end |
#on(type, &block) ⇒ Object
685 686 687 688 689 |
# File 'lib/rirc.rb', line 685 def on(type, &block) type = type.to_s @hooks[type] ||= [] @hooks[type] << block end |
#parse(msg) ⇒ Object
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 |
# File 'lib/rirc.rb', line 653 def parse(msg) = msg.match(/^(:(?<prefix>\S+) )?(?<command>\S+)( (?!:)(?<params>.+?))?( :(?<trail>.+))?$/) nick_n = [:prefix].to_s.split("!")[0] command = [:command].to_s chan = [:params].to_s = [:trail].to_s = .chomp if chan == @nick then chan = nick_n end ircmsg = .new(command, nick_n, chan, , msg) return ircmsg end |
#part(dest, message) ⇒ Object
600 601 602 603 604 |
# File 'lib/rirc.rb', line 600 def part(dest, ) @channels.delete_if { |a| dest == a } say "PART #{dest} :#{message}" end |
#port ⇒ Object
520 521 522 523 |
# File 'lib/rirc.rb', line 520 def port return @port end |
#privmsg(dest, message) ⇒ Object
580 581 582 583 |
# File 'lib/rirc.rb', line 580 def privmsg(dest, ) say "PRIVMSG #{dest} :#{message}" end |
#quit(message) ⇒ Object
614 615 616 617 |
# File 'lib/rirc.rb', line 614 def quit() say "QUIT :#{message}" end |
#read ⇒ Object
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 |
# 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 ⇒ Object
535 536 537 538 |
# File 'lib/rirc.rb', line 535 def real_name return @real_name end |
#remove_admin(nick) ⇒ Object
673 674 675 |
# File 'lib/rirc.rb', line 673 def remove_admin(nick) @admins.delete_if { |a| a == nick } end |
#remove_ignore(nick) ⇒ Object
681 682 683 |
# File 'lib/rirc.rb', line 681 def remove_ignore(nick) @ignore.delete_if { |a| a == nick } end |
#rm_chan(channel) ⇒ Object
606 607 608 |
# File 'lib/rirc.rb', line 606 def rm_chan(channel) @channels.delete_if { |a| channel == a } end |
#say(message) ⇒ Object
545 546 547 548 |
# File 'lib/rirc.rb', line 545 def say() @socket.puts end |
#set_admins(admins_s) ⇒ Object
697 698 699 |
# 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) ⇒ Object
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 |
# 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 ⇒ Object
540 541 542 543 |
# File 'lib/rirc.rb', line 540 def socket return @socket end |
#start! ⇒ Object
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 |
# 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.) } end rescue => e # do not do anything end begin hooks = @stop_hooks i = 0 @stop_regs.each do |j| if msg. =~ 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) ⇒ Object
691 692 693 694 695 |
# 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 ⇒ Object
530 531 532 533 |
# File 'lib/rirc.rb', line 530 def user_name return @user_name end |