ircbot 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README +47 -0
- data/Rakefile +56 -0
- data/bin/ircbot +50 -0
- data/lib/irc/agent.rb +177 -0
- data/lib/irc/client.rb +476 -0
- data/lib/irc/const.rb +242 -0
- data/lib/irc/irc.rb +324 -0
- data/lib/irc/localize.rb +260 -0
- data/lib/ircbot.rb +41 -0
- data/lib/ircbot/agent_manager.rb +89 -0
- data/lib/ircbot/config_client.rb +369 -0
- data/lib/ircbot/core_ext/digest.rb +14 -0
- data/lib/ircbot/core_ext/irc.rb +61 -0
- data/lib/ircbot/core_ext/rand-polimorphism.rb +23 -0
- data/lib/ircbot/core_ext/writefile.rb +45 -0
- data/lib/ircbot/framework.rb +50 -0
- data/lib/ircbot/ordered_hash.rb +91 -0
- data/lib/ircbot/reply_client.rb +328 -0
- metadata +82 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 [maiha@wota.jp]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
ircbot
|
2
|
+
===
|
3
|
+
|
4
|
+
old fashioned irc bot
|
5
|
+
|
6
|
+
|
7
|
+
Config
|
8
|
+
======
|
9
|
+
|
10
|
+
Edit "config/xxx.dat" as your environment.
|
11
|
+
|
12
|
+
|
13
|
+
Usage
|
14
|
+
=====
|
15
|
+
|
16
|
+
ircbot -f config/xxx.dat
|
17
|
+
|
18
|
+
|
19
|
+
Command
|
20
|
+
=======
|
21
|
+
|
22
|
+
* (nick|realname).COMMAND arg
|
23
|
+
# same as `arg > (nick|realname).COMMAND'
|
24
|
+
* (nick|realname).help
|
25
|
+
* (nick|realname).restart(CPI_NAME = nil)
|
26
|
+
* (nick|realname).register(CPI_NAME)
|
27
|
+
* (nick|realname).remove(CPI_NAME)
|
28
|
+
|
29
|
+
|
30
|
+
CPI
|
31
|
+
===
|
32
|
+
|
33
|
+
In ircbot's world, we define modules as cpi.
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
Notice
|
38
|
+
======
|
39
|
+
|
40
|
+
Main code and cpi modules should be euc-japan (KCODE=e).
|
41
|
+
|
42
|
+
|
43
|
+
Author
|
44
|
+
======
|
45
|
+
|
46
|
+
maiha@wota.jp
|
47
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
|
4
|
+
#require 'merb-core'
|
5
|
+
#require 'merb-core/tasks/merb'
|
6
|
+
|
7
|
+
GEM_NAME = "ircbot"
|
8
|
+
AUTHOR = "maiha"
|
9
|
+
EMAIL = "maiha@wota.jp"
|
10
|
+
HOMEPAGE = "http://github.com/maiha/ircbot"
|
11
|
+
SUMMARY = "old fashioned irc bot"
|
12
|
+
GEM_VERSION = "0.0.2"
|
13
|
+
|
14
|
+
spec = Gem::Specification.new do |s|
|
15
|
+
s.rubyforge_project = 'asakusarb'
|
16
|
+
s.executables = ["ircbot"]
|
17
|
+
s.name = GEM_NAME
|
18
|
+
s.version = GEM_VERSION
|
19
|
+
s.platform = Gem::Platform::RUBY
|
20
|
+
s.has_rdoc = true
|
21
|
+
s.extra_rdoc_files = ["README", "MIT-LICENSE"]
|
22
|
+
s.summary = SUMMARY
|
23
|
+
s.description = s.summary
|
24
|
+
s.author = AUTHOR
|
25
|
+
s.email = EMAIL
|
26
|
+
s.homepage = HOMEPAGE
|
27
|
+
s.require_path = 'lib'
|
28
|
+
s.add_dependency('activesupport', '>= 2.0.0')
|
29
|
+
s.files = %w(MIT-LICENSE README Rakefile) + Dir.glob("{lib,spec,app,public,stubs}/**/*")
|
30
|
+
end
|
31
|
+
|
32
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
33
|
+
pkg.gem_spec = spec
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "Install the gem"
|
37
|
+
task :install do
|
38
|
+
Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
|
39
|
+
end
|
40
|
+
|
41
|
+
desc "Uninstall the gem"
|
42
|
+
task :uninstall do
|
43
|
+
Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
|
44
|
+
end
|
45
|
+
|
46
|
+
desc "Create a gemspec file"
|
47
|
+
task :gemspec do
|
48
|
+
File.open("#{GEM_NAME}.gemspec", "w") do |file|
|
49
|
+
file.puts spec.to_ruby
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
require 'spec/rake/spectask'
|
54
|
+
#require 'merb-core/test/tasks/spectasks'
|
55
|
+
desc 'Default: run spec examples'
|
56
|
+
task :default => 'spec'
|
data/bin/ircbot
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# clear ARGV for Irb
|
3
|
+
|
4
|
+
require 'ircbot'
|
5
|
+
|
6
|
+
$KCODE = 'e'
|
7
|
+
|
8
|
+
# Sync
|
9
|
+
STDOUT.sync = true
|
10
|
+
STDERR.sync = true
|
11
|
+
|
12
|
+
debug = nil
|
13
|
+
config = nil
|
14
|
+
cpi = Pathname(Dir.getwd) + "cpi"
|
15
|
+
|
16
|
+
while (arg = ARGV.shift)
|
17
|
+
case arg
|
18
|
+
when /^-d$/ ; debug = true
|
19
|
+
when /^-f$/ ; config = ARGV.shift
|
20
|
+
when /^-c$/ ; cpi = ARGV.shift
|
21
|
+
else
|
22
|
+
puts "invalid argument: `#{arg}'"
|
23
|
+
exit
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
unless config
|
28
|
+
raise "Specify your config file\nusage: #{$0} -f config/xxx.dat"
|
29
|
+
end
|
30
|
+
|
31
|
+
# set cpi directory
|
32
|
+
Ircbot.push_path :cpi, cpi, "**/*.cpi"
|
33
|
+
|
34
|
+
|
35
|
+
module Ircbot
|
36
|
+
class Kicker < Ircbot::ReplyClient
|
37
|
+
rescue_from(SocketError) {
|
38
|
+
raise Ircbot::Recover, 30.minutes
|
39
|
+
}
|
40
|
+
|
41
|
+
rescue_from(Errno::EPIPE, Errno::ECONNRESET, Errno::ETIMEDOUT, IRC::AbnormalTerminated) {
|
42
|
+
raise Ircbot::Recover, 300.minutes
|
43
|
+
}
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
irc = Ircbot::ReplyClient.read_config(config, debug)
|
49
|
+
irc.start
|
50
|
+
puts "Bye"
|
data/lib/irc/agent.rb
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
=begin header
|
2
|
+
Internet Relay Chat Agent Library
|
3
|
+
|
4
|
+
$Author: knu $
|
5
|
+
$Date: 2001/01/31 10:55:28 $
|
6
|
+
|
7
|
+
Copyright (C) 1998-2000 Hiroshi IGARASHI
|
8
|
+
=end
|
9
|
+
|
10
|
+
#require 'irc'
|
11
|
+
#require 'ircclient'
|
12
|
+
|
13
|
+
module IRC
|
14
|
+
class Agent
|
15
|
+
=begin
|
16
|
+
IRC���ư���agent�����Ȥߤ����륯�饹��
|
17
|
+
����������åɤ�override�������Ѥ���
|
18
|
+
=end
|
19
|
+
|
20
|
+
include Constants
|
21
|
+
|
22
|
+
#attr_reader(:name)
|
23
|
+
attr_accessor(:name)
|
24
|
+
=begin
|
25
|
+
�����������̾(������ץ�̾��1��1�б�):String
|
26
|
+
=end
|
27
|
+
attr_reader(:nick)
|
28
|
+
=begin
|
29
|
+
�����������̾:String
|
30
|
+
=end
|
31
|
+
attr_reader(:timestamp)
|
32
|
+
=begin
|
33
|
+
��������:Time
|
34
|
+
=end
|
35
|
+
attr_accessor(:script_name)
|
36
|
+
=begin
|
37
|
+
������������ץȥե�����̾:String
|
38
|
+
=end
|
39
|
+
|
40
|
+
def initialize(nick=__id__.to_s)
|
41
|
+
=begin
|
42
|
+
�����Τ߹Ԥ��ʼºݤν������start�ǹԤ���
|
43
|
+
=end
|
44
|
+
@nick = nick
|
45
|
+
@timestamp = Time.now
|
46
|
+
end
|
47
|
+
def start(client)
|
48
|
+
=begin
|
49
|
+
���������ư
|
50
|
+
client:Client ����Agent���Ȥ߹��ޤ��Client���֥�������
|
51
|
+
=end
|
52
|
+
@client = client
|
53
|
+
putlog("start", "started.")
|
54
|
+
main
|
55
|
+
end
|
56
|
+
def restart(old_agent)
|
57
|
+
=begin
|
58
|
+
�Ƶ�ư
|
59
|
+
old_agent:Agent nil�ʤ�и��ߤξ��֤Τޤ�ư���
|
60
|
+
=end
|
61
|
+
end
|
62
|
+
def stop
|
63
|
+
=begin
|
64
|
+
���
|
65
|
+
=end
|
66
|
+
putlog("stop", "stopped.")
|
67
|
+
#leprintln("#{@nick} stoped.")
|
68
|
+
end
|
69
|
+
|
70
|
+
def putlog(ident, str)
|
71
|
+
@client.putlog(self, ident, str)
|
72
|
+
end
|
73
|
+
def join(channels, keys)
|
74
|
+
@client.join(channels, keys)
|
75
|
+
end
|
76
|
+
def part(channels)
|
77
|
+
@client.part(channels)
|
78
|
+
end
|
79
|
+
def privmsg(message, *channels)
|
80
|
+
@client.privmsg(message, *channels)
|
81
|
+
end
|
82
|
+
def action(message, *channels)
|
83
|
+
@client.action(message, *channels)
|
84
|
+
end
|
85
|
+
=begin
|
86
|
+
Client�ε�ǽ�θƽ�
|
87
|
+
=end
|
88
|
+
|
89
|
+
#
|
90
|
+
# �����С��饤�ɤ���뤳�Ȥ����Ԥ�����å�
|
91
|
+
#
|
92
|
+
|
93
|
+
def main
|
94
|
+
=begin
|
95
|
+
�ᥤ��
|
96
|
+
=end
|
97
|
+
end
|
98
|
+
def terminate
|
99
|
+
=begin
|
100
|
+
��߽���
|
101
|
+
=end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
class ActiveAgent < Agent
|
106
|
+
attr_reader(:thread)
|
107
|
+
=begin
|
108
|
+
���Υ���������ȤΥ���å�:Thread
|
109
|
+
=end
|
110
|
+
attr_reader(:message_queue)
|
111
|
+
=begin
|
112
|
+
��å��������塼:Queue
|
113
|
+
nil�ΤȤ�����������ݤ��Ƥ��뤳�Ȥ�
|
114
|
+
=end
|
115
|
+
attr_reader(:log_queue)
|
116
|
+
=begin
|
117
|
+
�������塼:Queue
|
118
|
+
nil�ΤȤ�����������ݤ��Ƥ��뤳�Ȥ�
|
119
|
+
=end
|
120
|
+
|
121
|
+
def start(client)
|
122
|
+
=begin
|
123
|
+
��ư
|
124
|
+
=end
|
125
|
+
@client = client
|
126
|
+
@thread = Thread.current
|
127
|
+
putlog("start", "started.")
|
128
|
+
# ǽưŪ��ư��
|
129
|
+
begin
|
130
|
+
main
|
131
|
+
rescue Stop
|
132
|
+
# ��߽���
|
133
|
+
terminate
|
134
|
+
end
|
135
|
+
end
|
136
|
+
def stop
|
137
|
+
=begin
|
138
|
+
���
|
139
|
+
=end
|
140
|
+
@thread.raise(Stop.new)
|
141
|
+
super
|
142
|
+
end
|
143
|
+
#
|
144
|
+
def evolve
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
class PassiveAgent < Agent
|
149
|
+
#
|
150
|
+
# def start(client)
|
151
|
+
# super
|
152
|
+
# # ����¾�ν��������
|
153
|
+
# # �֥��å�������return���ʤ��ƤϤʤ�ʤ���
|
154
|
+
# end
|
155
|
+
def notifyMessage(msg)
|
156
|
+
=begin
|
157
|
+
��������������
|
158
|
+
���������ƤΥ�å����������Τ�����
|
159
|
+
=end
|
160
|
+
end
|
161
|
+
def notifyLog(log)
|
162
|
+
=begin
|
163
|
+
������������
|
164
|
+
���������ƤΥ��������Τ�����
|
165
|
+
=end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
class TemporaryAgent < Agent
|
170
|
+
def start(client)
|
171
|
+
super
|
172
|
+
# ���Ū�ʽ���
|
173
|
+
# �֥��å�������return���ʤ��ƤϤʤ�ʤ���
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
data/lib/irc/client.rb
ADDED
@@ -0,0 +1,476 @@
|
|
1
|
+
=begin header
|
2
|
+
Internet Relay Chat Client Library
|
3
|
+
|
4
|
+
$Author: knu $
|
5
|
+
$Date: 2001/01/31 10:55:28 $
|
6
|
+
|
7
|
+
Copyright (C) 1998-2000 Hiroshi IGARASHI
|
8
|
+
=end
|
9
|
+
|
10
|
+
require 'irc/irc'
|
11
|
+
require 'irc/agent'
|
12
|
+
|
13
|
+
if $DEBUG
|
14
|
+
class << Thread
|
15
|
+
alias _start start
|
16
|
+
def start(&iter)
|
17
|
+
eprintln("Thread created(#{caller}).")
|
18
|
+
_start(&iter)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module IRC
|
24
|
+
|
25
|
+
class NewAgentException < Exception
|
26
|
+
=begin
|
27
|
+
����������Ȥ������Ǥ��ʤ��ä����Ȥ��㳰
|
28
|
+
=end
|
29
|
+
end
|
30
|
+
|
31
|
+
class Stop < Exception
|
32
|
+
end
|
33
|
+
|
34
|
+
class Client
|
35
|
+
=begin
|
36
|
+
IRC���饤����ȤȤ��Ƥε�ǽ�����Ȥߤ�������륯�饹
|
37
|
+
=end
|
38
|
+
|
39
|
+
include Constants
|
40
|
+
|
41
|
+
attr_reader(:nick)
|
42
|
+
=begin
|
43
|
+
nick:String �˥å��͡���
|
44
|
+
=end
|
45
|
+
attr_reader(:agents)
|
46
|
+
=begin
|
47
|
+
agents:Hash �Ȥ߹��ޤ�Ƥ��륨���������
|
48
|
+
=end
|
49
|
+
attr_reader(:join_channels)
|
50
|
+
=begin
|
51
|
+
join_channels:Array of String join���Ƥ�������ͥ�
|
52
|
+
=end
|
53
|
+
|
54
|
+
def initialize(server, nick, username, realname=username)
|
55
|
+
=begin
|
56
|
+
Client�ν������Ԥ�
|
57
|
+
server:String ������̾
|
58
|
+
nick:String �˥å��͡���
|
59
|
+
username:String �桼��̾(��������̾)
|
60
|
+
realname:String ��̾
|
61
|
+
=end
|
62
|
+
@server = server
|
63
|
+
@nick = nick
|
64
|
+
@username = username
|
65
|
+
@realname = realname
|
66
|
+
#@passive_agents = {} # String��PassiveAgent
|
67
|
+
#@active_agents = {} # String��ActiveAgent
|
68
|
+
@agents = {} # String��Agent
|
69
|
+
@join_channels = []
|
70
|
+
end
|
71
|
+
|
72
|
+
def connect
|
73
|
+
=begin
|
74
|
+
�����Фؤ���³��Ԥ�
|
75
|
+
=end
|
76
|
+
@connection.connect(@server, 6667)
|
77
|
+
@connection.sendPASS("xxx")
|
78
|
+
@connection.sendNICK(@nick)
|
79
|
+
@connection.sendUSER(nil, @username,
|
80
|
+
"hostname", "servername", @realname)
|
81
|
+
end
|
82
|
+
|
83
|
+
def disconnect
|
84
|
+
=begin
|
85
|
+
�����ФȤ���³���ڤ�
|
86
|
+
=end
|
87
|
+
@connection.sendQUIT(@nick, nil)
|
88
|
+
@connection.disconnect
|
89
|
+
end
|
90
|
+
|
91
|
+
def putlog(sender, ident, str)
|
92
|
+
=begin
|
93
|
+
�����塼�������
|
94
|
+
=end
|
95
|
+
@log_queue.push(LogMessage.new(sender, ident, str)) unless @log_queue.nil?
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
def _putlog(ident, str)
|
100
|
+
=begin
|
101
|
+
�����塼�������
|
102
|
+
=end
|
103
|
+
#leprintln(ident , " ", str)
|
104
|
+
putlog(self, ident, str)
|
105
|
+
end
|
106
|
+
public
|
107
|
+
|
108
|
+
def newAgent(name)
|
109
|
+
=begin
|
110
|
+
����������Ȥ��������롣
|
111
|
+
Ϳ����줿̾���Υ���������Ȥ�ץ饰������ץȤ���
|
112
|
+
��������������֤���
|
113
|
+
=end
|
114
|
+
# �����������̾���ץ饰������ץ�̾
|
115
|
+
script_name = "cpi/" + name + ".cpi"
|
116
|
+
begin
|
117
|
+
script = File.open(script_name).read
|
118
|
+
rescue Exception
|
119
|
+
_putlog("newAgent:#{name}",
|
120
|
+
"Reading agent plugin script '#{script_name}' raises exception(#{$!}).")
|
121
|
+
raise NewAgentException.new
|
122
|
+
end
|
123
|
+
begin
|
124
|
+
agent = eval(script)
|
125
|
+
p(agent) if $DEBUG
|
126
|
+
if agent.nil?
|
127
|
+
_putlog("newAgent:#{name}",
|
128
|
+
"Agent plugin script return nil (will not be registerd).")
|
129
|
+
else
|
130
|
+
agent.name = name
|
131
|
+
agent.script_name = script_name
|
132
|
+
end
|
133
|
+
rescue Exception
|
134
|
+
_putlog("newAgent:#{name}",
|
135
|
+
"Agent plugin script raises exception(#{$!}).")
|
136
|
+
raise NewAgentException.new
|
137
|
+
end
|
138
|
+
unless agent.is_a?(Agent)
|
139
|
+
_putlog("newAgent:#{name}", "Agent type warning(#{agent.type}).")
|
140
|
+
end
|
141
|
+
_putlog("newAgent:#{name}", "Agent generated(#{name}).")
|
142
|
+
agent
|
143
|
+
end
|
144
|
+
|
145
|
+
def registAgent(name, agent)
|
146
|
+
=begin
|
147
|
+
����������Ȥ���Ͽ
|
148
|
+
=end
|
149
|
+
case agent
|
150
|
+
when Agent
|
151
|
+
if @agents[name].nil?
|
152
|
+
@agents[name] = agent
|
153
|
+
else
|
154
|
+
p(@agents) if $DEBUG
|
155
|
+
_putlog("registAgent", "duplicate registration of agent(#{name}).")
|
156
|
+
end
|
157
|
+
else
|
158
|
+
_putlog("registAgent", "agent type error.")
|
159
|
+
end
|
160
|
+
_putlog("registAgent", "agent registered(#{name}).")
|
161
|
+
agent
|
162
|
+
end
|
163
|
+
|
164
|
+
def findAgent(name)
|
165
|
+
agent = @agents[name]
|
166
|
+
# if agent.nil?
|
167
|
+
# _putlog("findAgent, "No such agent registered(#{name}).")
|
168
|
+
# end
|
169
|
+
agent
|
170
|
+
end
|
171
|
+
|
172
|
+
def removeAgent(name)
|
173
|
+
=begin
|
174
|
+
����������Ȥκ��
|
175
|
+
=end
|
176
|
+
unless @agents[name].nil?
|
177
|
+
@agents[name] = nil
|
178
|
+
else
|
179
|
+
_putlog("removeAgent", "can't remove agent(#{name}).")
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
def startAgent(name)
|
184
|
+
=begin
|
185
|
+
����������Ȥε�ư
|
186
|
+
�ʴ��˵�ư���Ƥ����鲿�⤷�ʤ���
|
187
|
+
=end
|
188
|
+
_putlog("startAgent", "called(#{name}).")
|
189
|
+
old_agent = findAgent(name)
|
190
|
+
if old_agent.nil?
|
191
|
+
# ��ư���Ƥ��ʤ�
|
192
|
+
begin
|
193
|
+
new_agent = newAgent(name)
|
194
|
+
return nil if new_agent.nil?
|
195
|
+
rescue NewAgentException
|
196
|
+
_putlog("startAgent", "can't start agent(#{name}).")
|
197
|
+
return nil
|
198
|
+
end
|
199
|
+
begin
|
200
|
+
case new_agent
|
201
|
+
when ActiveAgent
|
202
|
+
Thread.start do
|
203
|
+
new_agent.start(self)
|
204
|
+
end
|
205
|
+
when PassiveAgent
|
206
|
+
new_agent.start(self)
|
207
|
+
else
|
208
|
+
end
|
209
|
+
registAgent(name, new_agent)
|
210
|
+
rescue Exception
|
211
|
+
_putlog("startAgent:#{name}",
|
212
|
+
"Agent#start raises exception(#{$!}).")
|
213
|
+
end
|
214
|
+
else
|
215
|
+
# ��ư���Ƥ���ΤǤ�����֤�
|
216
|
+
old_agent
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
def stopAgent(name)
|
221
|
+
=begin
|
222
|
+
����������Ȥ����
|
223
|
+
=end
|
224
|
+
agent = findAgent(name)
|
225
|
+
agent.stop unless agent.nil?
|
226
|
+
end
|
227
|
+
|
228
|
+
def restartAgent(name)
|
229
|
+
=begin
|
230
|
+
����������ȤκƵ�ư�롣
|
231
|
+
���ꤵ�줿̾���Υ���������Ȥ���ߡ���������塢
|
232
|
+
�ץ饰������ץȤ��饨��������Ȥ������������Ͽ���롣
|
233
|
+
name:String ����������Ȥ�̾����
|
234
|
+
=end
|
235
|
+
stopAgent(name)
|
236
|
+
removeAgent(name)
|
237
|
+
startAgent(name)
|
238
|
+
end
|
239
|
+
|
240
|
+
def evolveAgent(name)
|
241
|
+
=begin
|
242
|
+
����������Ȥοʲ�
|
243
|
+
��message_thread����ƤӽФ��ʤ��ȡ���å������������ꤽ���ʤ�
|
244
|
+
��ǽ���������
|
245
|
+
=end
|
246
|
+
end
|
247
|
+
|
248
|
+
def start(init_cpi="init")
|
249
|
+
=begin
|
250
|
+
���饤����ȤȤ��Ƥ�ư��Ϥ��롣
|
251
|
+
init_cpi:String ��ư������ץ�
|
252
|
+
=end
|
253
|
+
@syslog_agent = startAgent("syslog")
|
254
|
+
@log_queue = Queue.new
|
255
|
+
_putlog("client", "started.")
|
256
|
+
eprintln("client started.") if $DEBUG
|
257
|
+
@connection = Connection::new(@log_queue)
|
258
|
+
connect
|
259
|
+
_putlog("client", "connected to server.")
|
260
|
+
startAgent(init_cpi)
|
261
|
+
|
262
|
+
startThreads
|
263
|
+
|
264
|
+
#Thread.join(@message_thread) # obsoleted
|
265
|
+
@message_thread.join
|
266
|
+
# syslog�����
|
267
|
+
@syslog_agent.stop
|
268
|
+
# log_thread�����
|
269
|
+
_putlog("client", "stopped.")
|
270
|
+
@log_thread.raise(Stop.new)
|
271
|
+
#Thread.join(@log_thread) # obsoleted
|
272
|
+
@log_thread.join
|
273
|
+
p(@log_queue) if $DEBUG
|
274
|
+
end
|
275
|
+
|
276
|
+
def stop
|
277
|
+
#Thread.start do
|
278
|
+
_stop
|
279
|
+
#end
|
280
|
+
end
|
281
|
+
private
|
282
|
+
def _stop
|
283
|
+
# Agent�����
|
284
|
+
@agents.each do |name, agent|
|
285
|
+
case agent
|
286
|
+
when @syslog_agent
|
287
|
+
leprintln("@syslog_agent skipped.") if $DEBUG
|
288
|
+
when ActiveAgent
|
289
|
+
agent.stop
|
290
|
+
when PassiveAgent
|
291
|
+
agent.stop
|
292
|
+
else
|
293
|
+
end
|
294
|
+
end
|
295
|
+
# Connection������
|
296
|
+
disconnect
|
297
|
+
# message_thread�����
|
298
|
+
@message_thread.raise(Stop.new)
|
299
|
+
#raise(Stop.new)
|
300
|
+
end
|
301
|
+
public
|
302
|
+
|
303
|
+
def startThreads
|
304
|
+
=begin
|
305
|
+
����åɤ�ư����
|
306
|
+
=end
|
307
|
+
@message_thread = Thread.start {
|
308
|
+
eprintln("message_thread started.") if $DEBUG
|
309
|
+
#_putlog("debug", "message_thread started.")
|
310
|
+
begin
|
311
|
+
handleMessageLoop
|
312
|
+
rescue Stop
|
313
|
+
end
|
314
|
+
eprintln("message_thread stopped.") if $DEBUG
|
315
|
+
#_putlog("debug", "message_thread stopped.")
|
316
|
+
}
|
317
|
+
eprintln("message_thread created.") if $DEBUG
|
318
|
+
@log_thread = Thread.start {
|
319
|
+
eprintln("log_thread started.") if $DEBUG
|
320
|
+
#_putlog("debug", "log_thread started.")
|
321
|
+
handleLogLoop
|
322
|
+
eprintln("log_thread stopped.") if $DEBUG
|
323
|
+
#_putlog("debug", "log_thread stopped.")
|
324
|
+
}
|
325
|
+
eprintln("log_thread created.") if $DEBUG
|
326
|
+
eprintln("threads created.") if $DEBUG
|
327
|
+
p([@message_thread, @log_thread]) if $DEBUG
|
328
|
+
end
|
329
|
+
|
330
|
+
def handleMessageLoop
|
331
|
+
=begin
|
332
|
+
�����ФȤΤ����롣
|
333
|
+
=end
|
334
|
+
loop do
|
335
|
+
msg = @connection.recv
|
336
|
+
if msg.nil?
|
337
|
+
_putlog("handleMessageLoop", "Abnormal terminated.")
|
338
|
+
break
|
339
|
+
end
|
340
|
+
#p msg
|
341
|
+
handleMessageInternal(msg)
|
342
|
+
distributeMessage(msg)
|
343
|
+
end
|
344
|
+
end
|
345
|
+
|
346
|
+
def handleMessageInternal(msg)
|
347
|
+
=begin
|
348
|
+
��å�������������ϥ�ɥ�˿���ʬ���롣
|
349
|
+
=end
|
350
|
+
#lprintln("IRCClient#handleMessage")
|
351
|
+
# msg���������
|
352
|
+
case msg.command
|
353
|
+
when CMD_PING
|
354
|
+
handlePING(msg)
|
355
|
+
else
|
356
|
+
# numeric reply/error���������
|
357
|
+
#leprintln("numeric reply/error���������")
|
358
|
+
name = NAME_TABLE[msg.command]
|
359
|
+
unless name.nil?
|
360
|
+
#_putlog("(#{name})", "#{msg.to_s}")
|
361
|
+
else
|
362
|
+
#raise "Unknown message#{msg.inspect}."
|
363
|
+
#leprintln("Unknown message #{msg.inspect}.")
|
364
|
+
#_putlog("(unknown msg)", "#{msg.to_s}")
|
365
|
+
end
|
366
|
+
end
|
367
|
+
end
|
368
|
+
|
369
|
+
def handlePING(msg)
|
370
|
+
=begin
|
371
|
+
��å������ϥ�ɥ顣
|
372
|
+
���֥��饹�ǥ����С��饤�ɤ���뤳�Ȥ����Ԥ���Ƥ��롣
|
373
|
+
=end
|
374
|
+
@connection.sendPONG(nil, nil, msg.trailing)
|
375
|
+
end
|
376
|
+
|
377
|
+
def distributeMessage(msg)
|
378
|
+
=begin
|
379
|
+
������������
|
380
|
+
=end
|
381
|
+
@agents.each do |name, agent|
|
382
|
+
case agent
|
383
|
+
when ActiveAgent
|
384
|
+
unless agent.message_queue.nil?
|
385
|
+
agent.message_queue.push(msg)
|
386
|
+
end
|
387
|
+
when PassiveAgent
|
388
|
+
begin
|
389
|
+
agent.notifyMessage(msg)
|
390
|
+
rescue
|
391
|
+
_putlog("distributeMessage",
|
392
|
+
"Agent(#{name}) raise exception: #{$!}.")
|
393
|
+
end
|
394
|
+
else
|
395
|
+
_putlog("distributeMessage", "Agent type error.")
|
396
|
+
end
|
397
|
+
end
|
398
|
+
end
|
399
|
+
|
400
|
+
def handleLogLoop
|
401
|
+
=begin
|
402
|
+
�������
|
403
|
+
=end
|
404
|
+
begin
|
405
|
+
loop do
|
406
|
+
log = @log_queue.pop
|
407
|
+
#lprintln(log)
|
408
|
+
eprintln("*********************") unless log.is_a?(LogMessage)
|
409
|
+
#p(log)
|
410
|
+
distributeLog(log)
|
411
|
+
end
|
412
|
+
rescue Stop
|
413
|
+
until @log_queue.empty?
|
414
|
+
log = @log_queue.pop
|
415
|
+
eprintln("*********************") unless log.is_a?(LogMessage)
|
416
|
+
distributeLog(log)
|
417
|
+
end
|
418
|
+
end
|
419
|
+
end
|
420
|
+
|
421
|
+
def distributeLog(log)
|
422
|
+
=begin
|
423
|
+
����������
|
424
|
+
=end
|
425
|
+
@agents.each do |name, agent|
|
426
|
+
#p([name, agent])
|
427
|
+
case agent
|
428
|
+
when ActiveAgent
|
429
|
+
unless agent.log_queue.nil?
|
430
|
+
agent.log_queue.push(log)
|
431
|
+
end
|
432
|
+
when PassiveAgent
|
433
|
+
agent.notifyLog(log)
|
434
|
+
else
|
435
|
+
leprintln("distributeLog: ", "Agent(#{name}) type error.") if $DEBUG
|
436
|
+
#_putlog("distributeLog", "Agent type error.")
|
437
|
+
end
|
438
|
+
end
|
439
|
+
end
|
440
|
+
|
441
|
+
#
|
442
|
+
# ��å�����������å�
|
443
|
+
#
|
444
|
+
|
445
|
+
def join(channels, keys)
|
446
|
+
if channels.is_a?(Array)
|
447
|
+
channels = channels.join(",")
|
448
|
+
end
|
449
|
+
if keys.is_a?(Array)
|
450
|
+
keys = keys.join(",")
|
451
|
+
end
|
452
|
+
@connection.send(CMD_JOIN, nil, @nick, channels, keys)
|
453
|
+
end
|
454
|
+
def part(channels)
|
455
|
+
if channels.is_a?(Array)
|
456
|
+
channels = channels.join(",")
|
457
|
+
end
|
458
|
+
@connection.send(CMD_PART, nil, @nick, channels)
|
459
|
+
end
|
460
|
+
=begin
|
461
|
+
�����ͥ����˴ؤ���IRC��å�����
|
462
|
+
=end
|
463
|
+
|
464
|
+
def privmsg(message, *channels)
|
465
|
+
@connection.send(CMD_PRIVMSG, message, @nick, *channels)
|
466
|
+
end
|
467
|
+
|
468
|
+
def action(message, *channels)
|
469
|
+
@connection.send(CMD_PRIVMSG, "\001ACTION #{message}\001", @nick, *channels)
|
470
|
+
end
|
471
|
+
=begin
|
472
|
+
��å����������˴ؤ���IRC��å�����
|
473
|
+
=end
|
474
|
+
end
|
475
|
+
end
|
476
|
+
|