ircbot 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/README +43 -21
  2. data/Rakefile +9 -6
  3. data/bin/ircbot +3 -27
  4. data/lib/ircbot.rb +12 -15
  5. data/lib/ircbot/client.rb +32 -0
  6. data/lib/ircbot/client/commands.rb +34 -0
  7. data/lib/ircbot/client/config.rb +55 -0
  8. data/lib/ircbot/client/config/channels.rb +40 -0
  9. data/lib/ircbot/client/config/plugins.rb +16 -0
  10. data/lib/ircbot/client/encoding.rb +17 -0
  11. data/lib/ircbot/client/plugins.rb +64 -0
  12. data/lib/ircbot/core_ext/delegation.rb +135 -0
  13. data/lib/ircbot/core_ext/extending.rb +80 -0
  14. data/lib/ircbot/core_ext/message.rb +18 -0
  15. data/lib/ircbot/framework.rb +22 -40
  16. data/lib/ircbot/plugin.rb +63 -0
  17. data/lib/ircbot/plugins.rb +141 -0
  18. data/lib/ircbot/version.rb +4 -0
  19. data/plugins/echo.rb +14 -0
  20. data/plugins/irc.rb +19 -0
  21. data/plugins/plugins.rb +59 -0
  22. data/spec/config_spec.rb +83 -0
  23. data/spec/fixtures/sama-zu.yml +8 -0
  24. data/spec/framework_spec.rb +18 -0
  25. data/spec/its_helper.rb +15 -0
  26. data/spec/plugin_spec.rb +70 -0
  27. data/spec/plugins_spec.rb +30 -0
  28. data/spec/provide_helper.rb +36 -0
  29. data/spec/spec_helper.rb +16 -0
  30. metadata +40 -19
  31. data/lib/irc/agent.rb +0 -177
  32. data/lib/irc/client.rb +0 -476
  33. data/lib/irc/const.rb +0 -242
  34. data/lib/irc/irc.rb +0 -324
  35. data/lib/irc/localize.rb +0 -260
  36. data/lib/ircbot/agent_manager.rb +0 -89
  37. data/lib/ircbot/config_client.rb +0 -369
  38. data/lib/ircbot/core_ext/digest.rb +0 -14
  39. data/lib/ircbot/core_ext/irc.rb +0 -61
  40. data/lib/ircbot/core_ext/rand-polimorphism.rb +0 -23
  41. data/lib/ircbot/core_ext/writefile.rb +0 -45
  42. data/lib/ircbot/ordered_hash.rb +0 -91
  43. data/lib/ircbot/reply_client.rb +0 -328
@@ -1,177 +0,0 @@
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
-
@@ -1,476 +0,0 @@
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
-