Class: Net::IRC::Server::Session

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

Constant Summary

Constants included from Constants

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

Constants included from Net::IRC

COMMANDS, VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Net::IRC

ctcp_decode, ctcp_dequote, ctcp_encode, ctcp_quote

Constructor Details

#initialize(server, socket, logger, opts = {}) ⇒ Session

Returns a new instance of Session.



81
82
83
# File 'lib/net/irc/server.rb', line 81

def initialize(server, socket, logger, opts={})
	@server, @socket, @log, @opts = server, socket, logger, opts
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Do nothing. This is for avoiding error on calling super. So you can always call super at subclass.



164
165
# File 'lib/net/irc/server.rb', line 164

def method_missing(name, *args)
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



59
60
61
# File 'lib/net/irc/server.rb', line 59

def host
  @host
end

#nickObject (readonly)

Returns the value of attribute nick.



59
60
61
# File 'lib/net/irc/server.rb', line 59

def nick
  @nick
end

#prefixObject (readonly)

Returns the value of attribute prefix.



59
60
61
# File 'lib/net/irc/server.rb', line 59

def prefix
  @prefix
end

#realObject (readonly)

Returns the value of attribute real.



59
60
61
# File 'lib/net/irc/server.rb', line 59

def real
  @real
end

Class Method Details

.start(*args) ⇒ Object



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

def self.start(*args)
	new(*args).start
end

Instance Method Details

#available_channel_modesObject

Override subclass.



77
78
79
# File 'lib/net/irc/server.rb', line 77

def available_channel_modes
	"spknm"
end

#available_user_modesObject

Override subclass.



72
73
74
# File 'lib/net/irc/server.rb', line 72

def available_user_modes
	"eixwy"
end

#finishObject

Close this session.



112
113
114
115
116
117
118
# File 'lib/net/irc/server.rb', line 112

def finish
	begin
		@socket.close
	rescue
	end
	on_disconnected
end

#on_connectedObject

Call when socket connected.



144
145
# File 'lib/net/irc/server.rb', line 144

def on_connected
end

#on_disconnectedObject

Call when socket closed.



148
149
# File 'lib/net/irc/server.rb', line 148

def on_disconnected
end

#on_message(m) ⇒ Object

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



153
154
# File 'lib/net/irc/server.rb', line 153

def on_message(m)
end

#on_nick(m) ⇒ Object

Default NICK callback. Set @nick.



128
129
130
131
# File 'lib/net/irc/server.rb', line 128

def on_nick(m)
	@nick = m.params[0]
	@prefix = Prefix.new("#{@nick}!#{@user}@#{@host}") if defined? @prefix
end

#on_pass(m) ⇒ Object

Default PASS callback. Set @pass.



122
123
124
# File 'lib/net/irc/server.rb', line 122

def on_pass(m)
	@pass = m.params[0]
end

#on_ping(m) ⇒ Object

Default PING callback. Response PONG.



157
158
159
# File 'lib/net/irc/server.rb', line 157

def on_ping(m)
	post server_name, PONG, m.params[0]
end

#on_user(m) ⇒ Object

Default USER callback. Set @user, @real, @host and call initial_message.



135
136
137
138
139
140
141
# File 'lib/net/irc/server.rb', line 135

def on_user(m)
	@user, @real = m.params[0], m.params[3]
	@nick ||= @user
	@host = @socket.peeraddr[2]
	@prefix = Prefix.new("#{@nick}!#{@user}@#{@host}")
	initial_message
end

#server_nameObject

Override subclass.



62
63
64
# File 'lib/net/irc/server.rb', line 62

def server_name
	"net-irc"
end

#server_versionObject

Override subclass.



67
68
69
# File 'lib/net/irc/server.rb', line 67

def server_version
	"0.0.0"
end

#startObject

Start session loop.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/net/irc/server.rb', line 90

def start
	on_connected
	while l = @socket.gets
		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)

			break if m.command == QUIT
		rescue Message::InvalidMessage
			@log.error "MessageParse: " + l.inspect
		end
	end
rescue IOError
ensure
	finish
end