/usr/lib/ruby/1.8/irc/client.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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | =begin header
Internet Relay Chat Client Library
$Author: knu $
$Date: 2001/01/31 10:55:28 $
Copyright (C) 1998-2000 Hiroshi IGARASHI
=end
require 'irc/irc'
require 'irc/agent'
if $DEBUG
class << Thread
alias _start start
def start(&iter)
eprintln("Thread created(#{caller}).")
_start(&iter)
end
end
end
module IRC
class NewAgentException < Exception
=begin
¥¨¡¼¥¸¥§¥ó¥È¤¬À¸À®¤Ç¤¤Ê¤«¤Ã¤¿¤³¤È¤ò¼¨¤¹Îã³°
=end
end
class Stop < Exception
end
class Client
=begin
IRC¥¯¥é¥¤¥¢¥ó¥È¤È¤·¤Æ¤Îµ¡Ç½¤ÎÏÈÁȤߤò¼ÂÁõ¤¹¤ë¥¯¥é¥¹
=end
include Constants
attr_reader(:nick)
=begin
nick:String ¥Ë¥Ã¥¯¥Í¡¼¥à
=end
attr_reader(:agents)
=begin
agents:Hash ÁȤ߹þ¤Þ¤ì¤Æ¤¤¤ë¥¨¡¼¥¸¥§¥ó¥È
=end
attr_reader(:join_channels)
=begin
join_channels:Array of String join¤·¤Æ¤¤¤ë¥Á¥ã¥ó¥Í¥ë
=end
def initialize(server, nick, username, realname=username)
=begin
Client¤Î½é´ü²½¤ò¹Ô¤¦
server:String ¥µ¡¼¥Ð̾
nick:String ¥Ë¥Ã¥¯¥Í¡¼¥à
username:String ¥æ¡¼¥¶Ì¾(¥í¥°¥¤¥ó̾)
realname:String ¼Â̾
=end
@server = server
@nick = nick
@username = username
@realname = realname
#@passive_agents = {} # String¢ªPassiveAgent
#@active_agents = {} # String¢ªActiveAgent
@agents = {} # String¢ªAgent
@join_channels = []
end
def connect
=begin
¥µ¡¼¥Ð¤Ø¤ÎÀܳ¤ò¹Ô¤¦
=end
@connection.connect(@server, 6667)
@connection.sendPASS("xxx")
@connection.sendNICK(@nick)
@connection.sendUSER(nil, @username,
"hostname", "servername", @realname)
end
def disconnect
=begin
¥µ¡¼¥Ð¤È¤ÎÀܳ¤òÀÚ¤ë
=end
@connection.sendQUIT(@nick, nil)
@connection.disconnect
end
def putlog(sender, ident, str)
=begin
¥í¥°¤ò¥¥å¡¼¤ËÆþ¤ì¤ë
=end
@log_queue.push(LogMessage.new(sender, ident, str)) unless @log_queue.nil?
end
private
def _putlog(ident, str)
=begin
¥í¥°¤ò¥¥å¡¼¤ËÆþ¤ì¤ë
=end
#leprintln(ident , " ", str)
putlog(self, ident, str)
end
public
def newAgent(name)
=begin
¥¨¡¼¥¸¥§¥ó¥È¤òÀ¸À®¤¹¤ë¡£
Í¿¤¨¤é¤ì¤¿Ì¾Á°¤Î¥¨¡¼¥¸¥§¥ó¥È¤ò¥×¥é¥°¥¤¥ó¥¹¥¯¥ê¥×¥È¤ò´ð¤Ë
À¸À®¤·¡¢¤½¤ì¤òÊÖ¤¹¡£
=end
# ¥¨¡¼¥¸¥§¥ó¥È̾¢ª¥×¥é¥°¥¤¥ó¥¹¥¯¥ê¥×¥È̾
script_name = "cpi/" + name + ".cpi"
begin
script = File.open(script_name).read
rescue Exception
_putlog("newAgent:#{name}",
"Reading agent plugin script '#{script_name}' raises exception(#{$!}).")
raise NewAgentException.new
end
begin
agent = eval(script)
p(agent) if $DEBUG
if agent.nil?
_putlog("newAgent:#{name}",
"Agent plugin script return nil (will not be registerd).")
else
agent.name = name
agent.script_name = script_name
end
rescue Exception
_putlog("newAgent:#{name}",
"Agent plugin script raises exception(#{$!}).")
raise NewAgentException.new
end
unless agent.is_a?(Agent)
_putlog("newAgent:#{name}", "Agent type warning(#{agent.class}).")
end
_putlog("newAgent:#{name}", "Agent generated(#{name}).")
agent
end
def registAgent(name, agent)
=begin
¥¨¡¼¥¸¥§¥ó¥È¤ÎÅÐÏ¿
=end
case agent
when Agent
if @agents[name].nil?
@agents[name] = agent
else
p(@agents) if $DEBUG
_putlog("registAgent", "duplicate registration of agent(#{name}).")
end
else
_putlog("registAgent", "agent type error.")
end
_putlog("registAgent", "agent registered(#{name}).")
agent
end
def findAgent(name)
agent = @agents[name]
# if agent.nil?
# _putlog("findAgent, "No such agent registered(#{name}).")
# end
agent
end
def removeAgent(name)
=begin
¥¨¡¼¥¸¥§¥ó¥È¤Îºï½ü
=end
unless @agents[name].nil?
@agents[name] = nil
else
_putlog("removeAgent", "can't remove agent(#{name}).")
end
end
def startAgent(name)
=begin
¥¨¡¼¥¸¥§¥ó¥È¤Îµ¯Æ°
¡Ê´û¤Ëµ¯Æ°¤·¤Æ¤¤¤¿¤é²¿¤â¤·¤Ê¤¤¡Ë
=end
_putlog("startAgent", "called(#{name}).")
old_agent = findAgent(name)
if old_agent.nil?
# µ¯Æ°¤·¤Æ¤¤¤Ê¤¤
begin
new_agent = newAgent(name)
return nil if new_agent.nil?
rescue NewAgentException
_putlog("startAgent", "can't start agent(#{name}).")
return nil
end
begin
case new_agent
when ActiveAgent
Thread.start do
new_agent.start(self)
end
when PassiveAgent
new_agent.start(self)
else
end
registAgent(name, new_agent)
rescue Exception
_putlog("startAgent:#{name}",
"Agent#start raises exception(#{$!}).")
end
else
# µ¯Æ°¤·¤Æ¤¤¤ë¤Î¤Ç¤½¤ì¤òÊÖ¤¹
old_agent
end
end
def stopAgent(name)
=begin
¥¨¡¼¥¸¥§¥ó¥È¤ÎÄä»ß
=end
agent = findAgent(name)
agent.stop unless agent.nil?
end
def restartAgent(name)
=begin
¥¨¡¼¥¸¥§¥ó¥È¤ÎºÆµ¯Æ°¤ò¤¹¤ë¡£
»ØÄꤵ¤ì¤¿Ì¾Á°¤Î¥¨¡¼¥¸¥§¥ó¥È¤òÄä»ß¡¦ºï½ü¤·¤¿¸å¡¢
¥×¥é¥°¥¤¥ó¥¹¥¯¥ê¥×¥È¤«¤é¥¨¡¼¥¸¥§¥ó¥È¤òºÆÀ¸À®¤·¤ÆÅÐÏ¿¤¹¤ë¡£
name:String ¥¨¡¼¥¸¥§¥ó¥È¤Î̾Á°¡£
=end
stopAgent(name)
removeAgent(name)
startAgent(name)
end
def evolveAgent(name)
=begin
¥¨¡¼¥¸¥§¥ó¥È¤Î¿Ê²½
¡Êmessage_thread¤«¤é¸Æ¤Ó½Ð¤µ¤Ê¤¤¤È¡¢¥á¥Ã¥»¡¼¥¸¤ò¼õ¤±¼è¤ê¤½¤³¤Ê¤¦
²ÄǽÀ¤¬¤¢¤ë¡Ë
=end
end
def start(init_cpi="init")
=begin
¥¯¥é¥¤¥¢¥ó¥È¤È¤·¤Æ¤ÎÆ°ºî¤ò³«»Ï¤¹¤ë¡£
init_cpi:String µ¯Æ°¥¹¥¯¥ê¥×¥È
=end
@syslog_agent = startAgent("syslog")
@log_queue = Queue.new
_putlog("client", "started.")
eprintln("client started.") if $DEBUG
@connection = Connection::new(@log_queue)
connect
_putlog("client", "connected to server.")
startAgent(init_cpi)
startThreads
#Thread.join(@message_thread) # obsoleted
@message_thread.join
# syslog¤ÎÄä»ß
@syslog_agent.stop
# log_thread¤ÎÄä»ß
_putlog("client", "stopped.")
@log_thread.raise(Stop.new)
#Thread.join(@log_thread) # obsoleted
@log_thread.join
p(@log_queue) if $DEBUG
end
def stop
#Thread.start do
_stop
#end
end
private
def _stop
# Agent¤ÎÄä»ß
@agents.each do |name, agent|
case agent
when @syslog_agent
leprintln("@syslog_agent skipped.") if $DEBUG
when ActiveAgent
agent.stop
when PassiveAgent
agent.stop
else
end
end
# Connection¤ÎÀÚÃÇ
disconnect
# message_thread¤ÎÄä»ß
@message_thread.raise(Stop.new)
#raise(Stop.new)
end
public
def startThreads
=begin
¥¹¥ì¥Ã¥É¤òµ¯Æ°¤¹¤ë
=end
@message_thread = Thread.start {
eprintln("message_thread started.") if $DEBUG
#_putlog("debug", "message_thread started.")
begin
handleMessageLoop
rescue Stop
end
eprintln("message_thread stopped.") if $DEBUG
#_putlog("debug", "message_thread stopped.")
}
eprintln("message_thread created.") if $DEBUG
@log_thread = Thread.start {
eprintln("log_thread started.") if $DEBUG
#_putlog("debug", "log_thread started.")
handleLogLoop
eprintln("log_thread stopped.") if $DEBUG
#_putlog("debug", "log_thread stopped.")
}
eprintln("log_thread created.") if $DEBUG
eprintln("threads created.") if $DEBUG
p([@message_thread, @log_thread]) if $DEBUG
end
def handleMessageLoop
=begin
¥µ¡¼¥Ð¤È¤Î¤ä¤ê¼è¤ê¤ò¤¹¤ë¡£
=end
loop do
msg = @connection.recv
if msg.nil?
_putlog("handleMessageLoop", "Abnormal terminated.")
break
end
#p msg
handleMessageInternal(msg)
distributeMessage(msg)
end
end
def handleMessageInternal(msg)
=begin
¥á¥Ã¥»¡¼¥¸¤ò³ÆÆâÉô¥Ï¥ó¥É¥é¤Ë¿¶¤êʬ¤±¤ë¡£
=end
#lprintln("IRCClient#handleMessage")
# msg¤ËÂФ¹¤ë½èÍý
case msg.command
when CMD_PING
handlePING(msg)
else
# numeric reply/error¤ËÂФ¹¤ë½èÍý
#leprintln("numeric reply/error¤ËÂФ¹¤ë½èÍý")
name = NAME_TABLE[msg.command]
unless name.nil?
#_putlog("(#{name})", "#{msg.to_s}")
else
#raise "Unknown message#{msg.inspect}."
#leprintln("Unknown message #{msg.inspect}.")
#_putlog("(unknown msg)", "#{msg.to_s}")
end
end
end
def handlePING(msg)
=begin
¥á¥Ã¥»¡¼¥¸¥Ï¥ó¥É¥é¡£
¥µ¥Ö¥¯¥é¥¹¤Ç¥ª¡¼¥Ð¡¼¥é¥¤¥É¤µ¤ì¤ë¤³¤È¤¬´üÂÔ¤µ¤ì¤Æ¤¤¤ë¡£
=end
@connection.sendPONG(nil, nil, msg.trailing)
end
def distributeMessage(msg)
=begin
¥á¥Ã¥»¡¼¥¸¤ÎÇÛÉÛ
=end
@agents.each do |name, agent|
case agent
when ActiveAgent
unless agent.message_queue.nil?
agent.message_queue.push(msg)
end
when PassiveAgent
begin
agent.notifyMessage(msg)
rescue
_putlog("distributeMessage",
"Agent(#{name}) raise exception: #{$!}.")
end
else
_putlog("distributeMessage", "Agent type error.")
end
end
end
def handleLogLoop
=begin
¥í¥°¤Î½èÍý
=end
begin
loop do
log = @log_queue.pop
#lprintln(log)
eprintln("*********************") unless log.is_a?(LogMessage)
#p(log)
distributeLog(log)
end
rescue Stop
until @log_queue.empty?
log = @log_queue.pop
eprintln("*********************") unless log.is_a?(LogMessage)
distributeLog(log)
end
end
end
def distributeLog(log)
=begin
¥í¥°¤ÎÇÛÉÛ
=end
@agents.each do |name, agent|
#p([name, agent])
case agent
when ActiveAgent
unless agent.log_queue.nil?
agent.log_queue.push(log)
end
when PassiveAgent
agent.notifyLog(log)
else
leprintln("distributeLog: ", "Agent(#{name}) type error.") if $DEBUG
#_putlog("distributeLog", "Agent type error.")
end
end
end
#
# ¥á¥Ã¥»¡¼¥¸Á÷¿®¥á¥½¥Ã¥É
#
def join(channels, keys)
if channels.is_a?(Array)
channels = channels.join(",")
end
if keys.is_a?(Array)
keys = keys.join(",")
end
@connection.send(CMD_JOIN, nil, @nick, channels, keys)
end
def part(channels)
if channels.is_a?(Array)
channels = channels.join(",")
end
@connection.send(CMD_PART, nil, @nick, channels)
end
=begin
¥Á¥ã¥ó¥Í¥ëÁàºî¤Ë´Ø¤¹¤ëIRC¥á¥Ã¥»¡¼¥¸
=end
def privmsg(message, *channels)
@connection.send(CMD_PRIVMSG, message, @nick, *channels)
end
def action(message, *channels)
@connection.send(CMD_PRIVMSG, "\001ACTION #{message}\001", @nick, *channels)
end
=begin
¥á¥Ã¥»¡¼¥¸Á÷¿®¤Ë´Ø¤¹¤ëIRC¥á¥Ã¥»¡¼¥¸
=end
end
end
|