/usr/lib/ruby/1.8/irc/irc.rb is in libnet-irc-ruby 0.14-5.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | =begin header
Internet Relay Chat Protocol Library -- Main Part
$Author: knu $
$Date: 2001/01/31 10:55:28 $
Copyright (C) 1998-2000 Hiroshi IGARASHI
=end
require 'socket'
require 'thread'
require 'kconv'
require 'localize'
require 'irc/const'
module IRC
class Message
=begin
IRC messages in IRC protocol
=end
include Constants
attr_accessor(:prefix)
attr_accessor(:command)
attr_accessor(:params)
attr_accessor(:trailing)
attr_accessor(:str) # for debug
=begin
=end
def initialize(command, trailing=nil, prefix=nil, *params)
=begin
initialize Message
command:String
trailing=nil:String
prefix=nil:String
*params:String
=end
@prefix = prefix
@command = command
@params = params
@trailing = trailing
@str = to_s
end
def to_s
=begin
String representation suitable for sending
=end
buf = ''
buf << ":" << @prefix << ' ' unless @prefix.nil?
buf << @command
buf << ' ' << @params.join(' ') unless @params.empty?
buf << " :" << @trailing unless @trailing.nil?
buf
end
alias_method(:to_extern, :to_s)
=begin
alias of to_s
=end
end
class << Message
def parse(str)
=begin
parse IRC message and create a new Message object.
=end
strbuf = str.dup
strbuf.sub!(/^:(\S+)\s+/, '')
prefix = $1 if $1
strbuf.sub!(/^(\S+)/, '')
command = $1 if $1
params = []
#while strbuf.sub!(/^\s+([^\s:]+)/, '')
while strbuf.sub!(/^\s+([^\s:][^\s]*)/, '')
params << $1
end
strbuf.sub!(/^\s+:([^\r\n]+)(\r\n|\r|\n)?$/, '')
trailing = $1 if $1
msg = new(command, trailing, prefix, *params)
msg.str = str # for debug
msg
end
end
class User
=begin
represent IRC user
=end
attr_reader(:nick)
attr_reader(:user)
attr_reader(:host)
def initialize(nick, user=nil, host=nil)
@nick, @user, @host = nick, user, host
end
def to_s
"#{@nick}(#{user}) at #{@host}"
end
def ==(other)
case other
when User
@nick == other.nick && @user == other.user && @host == other.host
else
false
end
end
end
class << User
def parse(str)
str =~ /([^\s!@]+)(?:!([^\s@]+))?(?:@(\S+))?/
User::new($1, $2, $3)
end
end
class LogMessage
=begin
class which represents log message
=end
attr_reader(:timestamp)
=begin
:Time the time when this object was sent
=end
attr_reader(:sender)
=begin
:Object the object which sent object
=end
attr_reader(:ident)
=begin
:String identifier for message type
=end
attr_reader(:message)
=begin
:String log message string
=end
def initialize(sender, ident, message)
=begin
initialization
sender:Object the object which sent object
ident:String identifier for message type
message:String string as message
message:Message message object
=end
@timestamp = Time.now
@sender, @ident, @message = sender, ident, message
end
def to_s
=begin
string in standard output form
=end
buf = @timestamp.to_s
case @sender
when Connection, Client
buf << " <#{@ident}> #{@message}"
when Agent
buf << " [#{@sender.nick}] #{@message}"
end
buf
end
end
class Connection
=begin
represent IRC connection
=end
include Constants
def Message(command, trailing=nil, prefix=nil, *params)
=begin
alias of IRC::Message::new
=end
Message.new(command, trailing, prefix, *params)
end
def initialize(log_queue=nil)
=begin
initialize Connection
log_queue:Queue -- queue object into which log message are put.
=end
@log_queue = log_queue
end
private
def putlog(ident, str)
=begin
put a log message.
=end
#@log_queue.push(Time.now.to_s+": "+str.to_s) unless @log_queue.nil?
@log_queue.push(LogMessage.new(self, ident, str)) unless @log_queue.nil?
end
public
def connect(server, port=DEFAULT_PORT)
=begin
Connect to server which has hostname given by argument server.
If sub-class of BasicSocket is given, it will be used.
server:String -- server name.
server:BasicSocket -- socket already connected to server.
port=DEFAULT_PORT:Fixnum -- TCP port number. 6667 will be used when omitted.
=end
if server.is_a?(BasicSocket)
@socket = server
else
@server = server
@port = port
@socket = TCPsocket.open(@server, @port)
end
@socket.set_codesys("JIS")
end
def disconnect
=begin
=end
@socket.flush
@socket.shutdown(2)
@socket.close
end
def send(*args)
=begin
send IRC message.
send(message)
message:Message a IRC message to be send
send(command, trailing, prefix, *params)
Send a message which consists of given arguments.
Refer to Message#initialize about the meaning of the arguments.
=end
case args.length
when 1
case args[0]
when Message
message = args[0]
@socket.lprint(message, CRLF)
putlog("send", message)
when String
command = args[0]
message = Message(command)
@socket.lprint(message, CRLF)
putlog("send1", message)
else
putlog("send", "Type error(#{message.inspect}).")
end
when 0, 2
putlog("send", "Argument number error.")
else
message = Message(*args)
@socket.lprint(message, CRLF)
putlog("send1", message)
end
end
# def send(message)
# @socket.lprint(message, CRLF)
# putlog("send", message.to_s)
# end
def recv
=begin
Receive IRC message and return as Message object.
This method will block.
=end
str = @socket.lgets
unless str.nil?
message = Message.parse(str)
else
message = nil
end
putlog("recv", message)
message
end
def sendPASS(password)
send(Message(CMD_PASS, nil, nil, password))
end
def sendNICK(nick)
send(Message(CMD_NICK, nil, nil, nick))
end
def sendUSER(nick, username, hostname, servername, realname)
send(Message(CMD_USER, realname, nick,
username, hostname, servername))
end
def sendQUIT(nick, quit_message)
send(Message(CMD_QUIT, quit_message, nick))
end
=begin
send IRC messages about IRC connection.
=end
def sendPING(nick, servers)
send(Message(CMD_PING, nil, nick, servers))
end
def sendPONG(nick, daemons, phrase=nil)
send(Message(CMD_PONG, phrase, nick, daemons))
end
=begin
send IRC messages PING/PONG.
=end
# # send IRC messages about channel
# def sendJOIN(nick, channels, keys)
# if channels.is_a?(Array)
# channels = channels.join(",")
# end
# if keys.is_a?(Array)
# keys = keys.join(",")
# end
# send(Message(CMD_JOIN, nil, nick, channels, keys))
# end
# def sendPART(nick, channels)
# if channels.is_a?(Array)
# channels = channels.join(",")
# end
# send(Message(CMD_PART, nil, nick, channels))
# end
# # send IRC messages about sending messages
# def sendPRIVMSG(nick, message, *channels)
# send(Message(CMD_PRIVMSG, message, nick, *channels))
# end
end
end
|