Class: Net::IRC

Inherits:
Protocol
  • Object
show all
Includes:
Enumerable
Defined in:
lib/net/irc.rb

Defined Under Namespace

Classes: CTCP, CTCPAction, CTCPClientinfo, CTCPDcc, CTCPErrmsg, CTCPFinger, CTCPPing, CTCPPlay, CTCPTime, CTCPVersion, ErrNeedreggednick, ErrNicknameinuse, Error, Join, Message, Mode, Nick, Notice, Part, Pass, Ping, Pong, Privmsg, Quit, Reply, ReplyWithChannel, ReplyWithCount, ReplyWithRegistryParameters, ReplyWithTarget, RplCreated, RplEndofmotd, RplEndofnames, RplGlobalusers, RplIsupport, RplLocalusers, RplLoggedin, RplLoggedout, RplLuserchannels, RplLuserclient, RplLuserme, RplLuserop, RplMotd, RplMotdstart, RplMyinfo, RplNamreply, RplStatsconn, RplTopic, RplTopicwhotime, RplWelcome, RplYourhost, User

Constant Summary collapse

USER_MODE_DEFAULT =
0
USER_MODE_RECEIVE_WALLOPS =
4
USER_MODE_INVISIBLE =
8
PORT_DEFAULT =
6667
VERSION =
"0.9.3"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, port = nil) ⇒ IRC

Returns a new instance of IRC.



681
682
683
684
685
686
# File 'lib/net/irc.rb', line 681

def initialize(address, port = nil)
  @address = address
  @port = port || PORT_DEFAULT
  @started = false
  @socket = nil
end

Class Method Details

.loggerObject



11
12
13
# File 'lib/net/irc.rb', line 11

def logger
  @logger ||= Logger.new('net-irc.log')
end

.logger=(logger) ⇒ Object



15
16
17
# File 'lib/net/irc.rb', line 15

def logger=(logger)
  @logger = logger
end

.start(user, password, realname, address, port = nil, &block) ⇒ Object



676
677
678
# File 'lib/net/irc.rb', line 676

def start(user, password, realname, address, port = nil, &block)
  new(address, port).start(user, password, realname, &block)
end

Instance Method Details

#ctcp(target, text) ⇒ Object



739
740
741
# File 'lib/net/irc.rb', line 739

def ctcp(target, text)
  privmsg(target, "\001#{text}\001")
end

#ctcp_ping(target, arg = nil) ⇒ Object



747
748
749
# File 'lib/net/irc.rb', line 747

def ctcp_ping(target, arg = nil)
  notice(target, CTCPPing.new(arg).to_s)
end

#ctcp_time(target, time = nil) ⇒ Object



751
752
753
754
755
# File 'lib/net/irc.rb', line 751

def ctcp_time(target, time = nil)
  time ||= Time.now
  differential = '%.2d%.2d' % (time.utc_offset / 60).divmod(60)
  notice(target, CTCPTime.new(time.strftime("%a, %d %b %Y %H:%M #{differential}")).to_s)
end

#ctcp_version(target, *parameters) ⇒ Object



743
744
745
# File 'lib/net/irc.rb', line 743

def ctcp_version(target, *parameters)
  notice(target, CTCPVersion.new(*parameters).to_s)
end

#eachObject



712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
# File 'lib/net/irc.rb', line 712

def each
  while line = @socket.readline
    IRC.logger.debug "<<<<< #{line.inspect}"

    message = Message.parse(line.chomp)
  
    if message.respond_to? :ctcp
      message.ctcp.each do |ctcp|
        ctcp.source = message.prefix.nickname
        ctcp.target = message.target
        
        yield ctcp
      end
      next if message.text.empty?
    end

    case message
    when Net::IRC::Ping
      pong message.server
    else
      yield message
    end
  end
rescue IOError
  raise if started?
end

#finishObject

Raises:

  • (IOError)


708
709
710
# File 'lib/net/irc.rb', line 708

def finish
  raise IOError, 'IRC session not yet started' if ! started?
end

#join(channels = nil) ⇒ Object



757
758
759
760
761
762
763
764
765
766
767
768
# File 'lib/net/irc.rb', line 757

def join(channels = nil)
  case channels
  when NilClass
    Join.new('0')
  when Hash
    Join.new(channels.keys.join(','), channels.values.join(','))
  when Array
    Join.new(channels.join(','))
  else
    Join.new(channels.to_s)
  end.write(@socket)
end

#nick(nickname) ⇒ Object



770
771
772
# File 'lib/net/irc.rb', line 770

def nick(nickname)
  Nick.new(nickname).write(@socket)
end

#notice(target, text) ⇒ Object



774
775
776
# File 'lib/net/irc.rb', line 774

def notice(target, text)
  Notice.new(target, text).write(@socket)
end

#part(channels, message = nil) ⇒ Object



778
779
780
781
782
783
784
# File 'lib/net/irc.rb', line 778

def part(channels, message = nil)
  if message
    Part.new(Array(channels).join(','), message)
  else
    Part.new(Array(channels).join(','))
  end.write(@socket)
end

#pass(password) ⇒ Object



786
787
788
# File 'lib/net/irc.rb', line 786

def pass(password)
  Pass.new(password).write(@socket)
end

#pong(server, target = nil) ⇒ Object



790
791
792
# File 'lib/net/irc.rb', line 790

def pong(server, target = nil)
  Pong.new(server, target).write(@socket)
end

#privmsg(target, text) ⇒ Object



794
795
796
# File 'lib/net/irc.rb', line 794

def privmsg(target, text)
  Privmsg.new(target, text).write(@socket)
end

#quit(text = nil) ⇒ Object



798
799
800
# File 'lib/net/irc.rb', line 798

def quit(text = nil)
  Quit.new(text).write(@socket)
end

#start(user, password, realname, nickname = nil) ⇒ Object

Raises:

  • (IOError)


692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
# File 'lib/net/irc.rb', line 692

def start(user, password, realname, nickname = nil)
  raise IOError, 'IRC session already started' if started?

  if block_given?
    begin
      do_start(user, password, realname, nickname)
      return yield(self)
    ensure
      do_finish
    end
  else
    do_start(user, password, realname, nickname)
    return self
  end
end

#started?Boolean

Returns:

  • (Boolean)


688
689
690
# File 'lib/net/irc.rb', line 688

def started?
  @started
end

#user(user, realname, mode = nil) ⇒ Object



802
803
804
# File 'lib/net/irc.rb', line 802

def user(user, realname, mode = nil)
  User.new(user, mode || USER_MODE_DEFAULT, realname).write(@socket)
end