Class: Net::IRC::Client

Inherits:
Object
  • Object
show all
Includes:
Net::IRC, Constants
Defined in:
lib/net/irc/client.rb

Defined Under Namespace

Modules: ChannelManager

Constant Summary

Constants included from Constants

Net::IRC::Constants::ADMIN, Net::IRC::Constants::AWAY, Net::IRC::Constants::CONNECT, Net::IRC::Constants::DIE, Net::IRC::Constants::ERROR, Net::IRC::Constants::ERR_ALREADYREGISTRED, Net::IRC::Constants::ERR_BADCHANMASK, Net::IRC::Constants::ERR_BADCHANNELKEY, Net::IRC::Constants::ERR_BADMASK, Net::IRC::Constants::ERR_BANLISTFULL, Net::IRC::Constants::ERR_BANNEDFROMCHAN, Net::IRC::Constants::ERR_CANNOTSENDTOCHAN, Net::IRC::Constants::ERR_CANTKILLSERVER, Net::IRC::Constants::ERR_CHANNELISFULL, Net::IRC::Constants::ERR_CHANOPRIVSNEEDED, Net::IRC::Constants::ERR_ERRONEUSNICKNAME, Net::IRC::Constants::ERR_FILEERROR, Net::IRC::Constants::ERR_INVITEONLYCHAN, Net::IRC::Constants::ERR_KEYSET, Net::IRC::Constants::ERR_NEEDMOREPARAMS, Net::IRC::Constants::ERR_NICKCOLLISION, Net::IRC::Constants::ERR_NICKNAMEINUSE, Net::IRC::Constants::ERR_NOADMININFO, Net::IRC::Constants::ERR_NOCHANMODES, Net::IRC::Constants::ERR_NOLOGIN, Net::IRC::Constants::ERR_NOMOTD, Net::IRC::Constants::ERR_NONICKNAMEGIVEN, Net::IRC::Constants::ERR_NOOPERHOST, Net::IRC::Constants::ERR_NOORIGIN, Net::IRC::Constants::ERR_NOPERMFORHOST, Net::IRC::Constants::ERR_NOPRIVILEGES, Net::IRC::Constants::ERR_NORECIPIENT, Net::IRC::Constants::ERR_NOSERVICEHOST, Net::IRC::Constants::ERR_NOSUCHCHANNEL, Net::IRC::Constants::ERR_NOSUCHNICK, Net::IRC::Constants::ERR_NOSUCHSERVER, Net::IRC::Constants::ERR_NOSUCHSERVICE, Net::IRC::Constants::ERR_NOTEXTTOSEND, Net::IRC::Constants::ERR_NOTONCHANNEL, Net::IRC::Constants::ERR_NOTOPLEVEL, Net::IRC::Constants::ERR_NOTREGISTERED, Net::IRC::Constants::ERR_PASSWDMISMATCH, Net::IRC::Constants::ERR_RESTRICTED, Net::IRC::Constants::ERR_SUMMONDISABLED, Net::IRC::Constants::ERR_TOOMANYCHANNELS, Net::IRC::Constants::ERR_TOOMANYTARGETS, Net::IRC::Constants::ERR_UMODEUNKNOWNFLAG, Net::IRC::Constants::ERR_UNAVAILRESOURCE, Net::IRC::Constants::ERR_UNIQOPPRIVSNEEDED, Net::IRC::Constants::ERR_UNKNOWNCOMMAND, Net::IRC::Constants::ERR_UNKNOWNMODE, Net::IRC::Constants::ERR_USERNOTINCHANNEL, Net::IRC::Constants::ERR_USERONCHANNEL, Net::IRC::Constants::ERR_USERSDISABLED, Net::IRC::Constants::ERR_USERSDONTMATCH, Net::IRC::Constants::ERR_WASNOSUCHNICK, Net::IRC::Constants::ERR_WILDTOPLEVEL, Net::IRC::Constants::ERR_YOUREBANNEDCREEP, Net::IRC::Constants::ERR_YOUWILLBEBANNED, Net::IRC::Constants::INFO, Net::IRC::Constants::INVITE, Net::IRC::Constants::ISON, Net::IRC::Constants::JOIN, Net::IRC::Constants::KICK, Net::IRC::Constants::KILL, Net::IRC::Constants::LINKS, Net::IRC::Constants::LIST, Net::IRC::Constants::LUSERS, Net::IRC::Constants::MODE, Net::IRC::Constants::MOTD, Net::IRC::Constants::NAMES, Net::IRC::Constants::NICK, Net::IRC::Constants::NOTICE, Net::IRC::Constants::OPER, Net::IRC::Constants::PART, Net::IRC::Constants::PASS, Net::IRC::Constants::PING, Net::IRC::Constants::PONG, Net::IRC::Constants::PRIVMSG, Net::IRC::Constants::QUIT, Net::IRC::Constants::REHASH, Net::IRC::Constants::RESTART, Net::IRC::Constants::RPL_ADMINEMAIL, Net::IRC::Constants::RPL_ADMINLOC1, Net::IRC::Constants::RPL_ADMINLOC2, Net::IRC::Constants::RPL_ADMINME, Net::IRC::Constants::RPL_AWAY, Net::IRC::Constants::RPL_BANLIST, Net::IRC::Constants::RPL_CHANNELMODEIS, Net::IRC::Constants::RPL_CLOSEEND, Net::IRC::Constants::RPL_CLOSING, Net::IRC::Constants::RPL_CREATED, Net::IRC::Constants::RPL_ENDOFBANLIST, Net::IRC::Constants::RPL_ENDOFEXCEPTLIST, Net::IRC::Constants::RPL_ENDOFINFO, Net::IRC::Constants::RPL_ENDOFINVITELIST, Net::IRC::Constants::RPL_ENDOFLINKS, Net::IRC::Constants::RPL_ENDOFMOTD, Net::IRC::Constants::RPL_ENDOFNAMES, Net::IRC::Constants::RPL_ENDOFSERVICES, Net::IRC::Constants::RPL_ENDOFSTATS, Net::IRC::Constants::RPL_ENDOFUSERS, Net::IRC::Constants::RPL_ENDOFWHO, Net::IRC::Constants::RPL_ENDOFWHOIS, Net::IRC::Constants::RPL_ENDOFWHOWAS, Net::IRC::Constants::RPL_EXCEPTLIST, Net::IRC::Constants::RPL_INFO, Net::IRC::Constants::RPL_INFOSTART, Net::IRC::Constants::RPL_INVITELIST, Net::IRC::Constants::RPL_INVITING, Net::IRC::Constants::RPL_ISON, Net::IRC::Constants::RPL_ISUPPORT, Net::IRC::Constants::RPL_KILLDONE, Net::IRC::Constants::RPL_LINKS, Net::IRC::Constants::RPL_LIST, Net::IRC::Constants::RPL_LISTEND, Net::IRC::Constants::RPL_LISTSTART, Net::IRC::Constants::RPL_LUSERCHANNELS, Net::IRC::Constants::RPL_LUSERCLIENT, Net::IRC::Constants::RPL_LUSERME, Net::IRC::Constants::RPL_LUSEROP, Net::IRC::Constants::RPL_LUSERUNKNOWN, Net::IRC::Constants::RPL_MOTD, Net::IRC::Constants::RPL_MOTDSTART, Net::IRC::Constants::RPL_MYINFO, Net::IRC::Constants::RPL_MYPORTIS, Net::IRC::Constants::RPL_NAMREPLY, Net::IRC::Constants::RPL_NONE, Net::IRC::Constants::RPL_NOTOPIC, Net::IRC::Constants::RPL_NOUSERS, Net::IRC::Constants::RPL_NOWAWAY, Net::IRC::Constants::RPL_REHASHING, Net::IRC::Constants::RPL_SERVICE, Net::IRC::Constants::RPL_SERVICEINFO, Net::IRC::Constants::RPL_SERVLIST, Net::IRC::Constants::RPL_SERVLISTEND, Net::IRC::Constants::RPL_STATSBLINE, Net::IRC::Constants::RPL_STATSCLINE, Net::IRC::Constants::RPL_STATSCOMMANDS, Net::IRC::Constants::RPL_STATSDLINE, Net::IRC::Constants::RPL_STATSHLINE, Net::IRC::Constants::RPL_STATSILINE, Net::IRC::Constants::RPL_STATSKLINE, Net::IRC::Constants::RPL_STATSLINKINFO, Net::IRC::Constants::RPL_STATSLLINE, Net::IRC::Constants::RPL_STATSNLINE, Net::IRC::Constants::RPL_STATSOLINE, Net::IRC::Constants::RPL_STATSPING, Net::IRC::Constants::RPL_STATSQLINE, Net::IRC::Constants::RPL_STATSSLINE, Net::IRC::Constants::RPL_STATSUPTIME, Net::IRC::Constants::RPL_STATSVLINE, Net::IRC::Constants::RPL_STATSYLINE, Net::IRC::Constants::RPL_SUMMONING, Net::IRC::Constants::RPL_TIME, Net::IRC::Constants::RPL_TOPIC, Net::IRC::Constants::RPL_TRACECLASS, Net::IRC::Constants::RPL_TRACECONNECTING, Net::IRC::Constants::RPL_TRACEEND, Net::IRC::Constants::RPL_TRACEHANDSHAKE, Net::IRC::Constants::RPL_TRACELINK, Net::IRC::Constants::RPL_TRACELOG, Net::IRC::Constants::RPL_TRACENEWTYPE, Net::IRC::Constants::RPL_TRACEOPERATOR, Net::IRC::Constants::RPL_TRACERECONNECT, Net::IRC::Constants::RPL_TRACESERVER, Net::IRC::Constants::RPL_TRACESERVICE, Net::IRC::Constants::RPL_TRACEUNKNOWN, Net::IRC::Constants::RPL_TRACEUSER, Net::IRC::Constants::RPL_TRYAGAIN, Net::IRC::Constants::RPL_UMODEIS, Net::IRC::Constants::RPL_UNAWAY, Net::IRC::Constants::RPL_UNIQOPIS, Net::IRC::Constants::RPL_USERHOST, Net::IRC::Constants::RPL_USERS, Net::IRC::Constants::RPL_USERSSTART, Net::IRC::Constants::RPL_VERSION, Net::IRC::Constants::RPL_WELCOME, Net::IRC::Constants::RPL_WHOISCHANNELS, Net::IRC::Constants::RPL_WHOISCHANOP, Net::IRC::Constants::RPL_WHOISIDLE, Net::IRC::Constants::RPL_WHOISOPERATOR, Net::IRC::Constants::RPL_WHOISSERVER, Net::IRC::Constants::RPL_WHOISUSER, Net::IRC::Constants::RPL_WHOREPLY, Net::IRC::Constants::RPL_WHOWASUSER, Net::IRC::Constants::RPL_YOUREOPER, Net::IRC::Constants::RPL_YOURESERVICE, Net::IRC::Constants::RPL_YOURHOST, Net::IRC::Constants::SERVICE, Net::IRC::Constants::SERVLIST, Net::IRC::Constants::SQUERY, Net::IRC::Constants::SQUIT, Net::IRC::Constants::STATS, Net::IRC::Constants::SUMMON, Net::IRC::Constants::TIME, Net::IRC::Constants::TOPIC, Net::IRC::Constants::TRACE, Net::IRC::Constants::USER, Net::IRC::Constants::USERHOST, Net::IRC::Constants::USERS, Net::IRC::Constants::VERSION, Net::IRC::Constants::WALLOPS, Net::IRC::Constants::WHO, Net::IRC::Constants::WHOIS, Net::IRC::Constants::WHOWAS

Constants included from Net::IRC

COMMANDS, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Net::IRC

ctcp_decode, ctcp_dequote, ctcp_encode, ctcp_quote

Constructor Details

#initialize(host, port, opts = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/net/irc/client.rb', line 8

def initialize(host, port, opts={})
  @host          = host
  @port          = port
  @opts          = OpenStruct.new(opts)
  @log           = @opts.logger || ::Logger.new($stdout)
  @server_config = Message::ServerConfig.new
  @channels = {
#      "#channel" => {
#        :modes => [],
#        :users => [],
#      }
  }
  @channels.extend(MonitorMixin)
end

Instance Attribute Details

#channelsObject (readonly)

Returns the value of attribute channels.



6
7
8
# File 'lib/net/irc/client.rb', line 6

def channels
  @channels
end

#hostObject (readonly)

Returns the value of attribute host.



5
6
7
# File 'lib/net/irc/client.rb', line 5

def host
  @host
end

#optsObject (readonly)

Returns the value of attribute opts.



5
6
7
# File 'lib/net/irc/client.rb', line 5

def opts
  @opts
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/net/irc/client.rb', line 5

def port
  @port
end

#prefixObject (readonly)

Returns the value of attribute prefix.



6
7
8
# File 'lib/net/irc/client.rb', line 6

def prefix
  @prefix
end

Instance Method Details

#finishObject

Close connection to server.



59
60
61
62
63
64
65
# File 'lib/net/irc/client.rb', line 59

def finish
  begin
    @socket.close
  rescue
  end
  on_disconnected
end

#on_connectedObject

Call when socket connected.



90
91
# File 'lib/net/irc/client.rb', line 90

def on_connected
end

#on_disconnectedObject

Call when socket closed.



94
95
# File 'lib/net/irc/client.rb', line 94

def on_disconnected
end

#on_message(m) ⇒ Object

Catch all messages. If this method return true, aother callback will not be called.



69
70
# File 'lib/net/irc/client.rb', line 69

def on_message(m)
end

#on_ping(m) ⇒ Object

Default PING callback. Response PONG.



85
86
87
# File 'lib/net/irc/client.rb', line 85

def on_ping(m)
  post PONG, @prefix ? @prefix.nick : ""
end

#on_rpl_isupport(m) ⇒ Object

Default RPL_ISUPPORT callback. This detects server’s configurations.



80
81
82
# File 'lib/net/irc/client.rb', line 80

def on_rpl_isupport(m)
  @server_config.set(m)
end

#on_rpl_welcome(m) ⇒ Object

Default RPL_WELCOME callback. This sets @prefix from the message.



74
75
76
# File 'lib/net/irc/client.rb', line 74

def on_rpl_welcome(m)
  @prefix = Prefix.new(m[1][/\S+\z/])
end

#startObject

Connect to server and start loop.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/net/irc/client.rb', line 24

def start
  # reset config
  @server_config = Message::ServerConfig.new
  @socket = TCPSocket.new(@host, @port)
  on_connected
  post PASS, @opts.pass if @opts.pass
  post NICK, @opts.nick
  post USER, @opts.user, "0", "*", @opts.real

  buffer = BufferedTokenizer.new("\r\n")

  while data = @socket.readpartial(4096)
    buffer.extract(data).each do |line|
      l = "#{line}\r\n"
      begin
        @log.debug "RECEIVE: #{l.chomp}"
        m = Message.parse(l)
        next if on_message(m) === true
        name = "on_#{(COMMANDS[m.command.upcase] || m.command).downcase}"
        send(name, m) if respond_to?(name)
      rescue Exception => e
        warn e
        warn e.backtrace.join("\r\t")
        raise
      rescue Message::InvalidMessage
        @log.error "MessageParse: " + l.inspect
      end
    end
  end
rescue IOError
ensure
  finish
end