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, ReplyWithTarget, RplCreated, RplEndofmotd, RplEndofnames, RplGlobalusers, RplIsupport, RplLocalusers, 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.2"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, port = nil) ⇒ IRC

Returns a new instance of IRC.



653
654
655
656
657
658
# File 'lib/net/irc.rb', line 653

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, realname, address, port = nil, &block) ⇒ Object



648
649
650
# File 'lib/net/irc.rb', line 648

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

Instance Method Details

#ctcp(target, text) ⇒ Object



711
712
713
# File 'lib/net/irc.rb', line 711

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

#ctcp_ping(target, arg = nil) ⇒ Object



719
720
721
# File 'lib/net/irc.rb', line 719

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

#ctcp_time(target, time = nil) ⇒ Object



723
724
725
726
727
# File 'lib/net/irc.rb', line 723

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



715
716
717
# File 'lib/net/irc.rb', line 715

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

#eachObject



684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
# File 'lib/net/irc.rb', line 684

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)


680
681
682
# File 'lib/net/irc.rb', line 680

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

#join(channels = nil) ⇒ Object



729
730
731
732
733
734
735
736
737
738
739
740
# File 'lib/net/irc.rb', line 729

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



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

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

#notice(target, text) ⇒ Object



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

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

#part(channels, message = nil) ⇒ Object



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

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

#pong(server, target = nil) ⇒ Object



758
759
760
# File 'lib/net/irc.rb', line 758

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

#privmsg(target, text) ⇒ Object



762
763
764
# File 'lib/net/irc.rb', line 762

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

#quit(text = nil) ⇒ Object



766
767
768
# File 'lib/net/irc.rb', line 766

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

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

Raises:

  • (IOError)


664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
# File 'lib/net/irc.rb', line 664

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

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

#started?Boolean

Returns:

  • (Boolean)


660
661
662
# File 'lib/net/irc.rb', line 660

def started?
  @started
end

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



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

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