david-minibot 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/TODO +4 -3
- data/lib/minibot/commands.rb +19 -4
- data/lib/minibot/daemon.rb +30 -21
- data/lib/minibot/events.rb +1 -1
- data/lib/minibot/logger.rb +44 -0
- metadata +4 -3
data/TODO
CHANGED
|
@@ -9,6 +9,7 @@ TODO:
|
|
|
9
9
|
* Detect when using a registered nick.
|
|
10
10
|
* Add ability to reply both to public (by prefixing the username) and private messages
|
|
11
11
|
* Bot should reconnect upon disconnection
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
12
|
+
* IRC protocol error handling (probably each command should return its own errors).
|
|
13
|
+
* Trap signals.
|
|
14
|
+
* Use non blocking IO for logging when available.
|
|
15
|
+
* Find out what's the problem with heinlein.freenode.net
|
data/lib/minibot/commands.rb
CHANGED
|
@@ -12,19 +12,34 @@ module MiniBot
|
|
|
12
12
|
topic = author = timestamp = nil
|
|
13
13
|
server.write "TOPIC #{channel}", *EXPECTED_REPLIES_TOPIC do |code, reply|
|
|
14
14
|
if code == RPL_TOPIC
|
|
15
|
-
topic = reply
|
|
15
|
+
channel, topic = reply.split /\s+/, 2
|
|
16
16
|
elsif code == RPL_TOPIC_META
|
|
17
|
-
author, timestamp = *reply.split
|
|
17
|
+
channel, author, timestamp = *reply.split
|
|
18
18
|
end
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
[ topic, author, timestamp && Time.at(timestamp.to_i) ]
|
|
21
|
+
[ topic && topic[1 .. -1], author, timestamp && Time.at(timestamp.to_i) ]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def say(target, message)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def set_user(username, realname)
|
|
28
|
+
@server.write "USER #{username} 0 xxx :#{realname}"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def set_nick(nick)
|
|
32
|
+
@server.write "NICK #{nick}"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def nickserv(command)
|
|
36
|
+
@server.write "NICKSERV #{command}"
|
|
22
37
|
end
|
|
23
38
|
|
|
24
39
|
private
|
|
25
40
|
|
|
26
41
|
def pong
|
|
27
|
-
write "PONG #{@host_name}"
|
|
42
|
+
@server.write "PONG #{@host_name}"
|
|
28
43
|
end
|
|
29
44
|
end
|
|
30
45
|
end
|
data/lib/minibot/daemon.rb
CHANGED
|
@@ -5,39 +5,53 @@ module MiniBot
|
|
|
5
5
|
include Events
|
|
6
6
|
include Commands
|
|
7
7
|
|
|
8
|
-
attr_reader :
|
|
9
|
-
|
|
10
|
-
DEFAULTS = {
|
|
11
|
-
:port => 6667,
|
|
12
|
-
:channels => []
|
|
13
|
-
}
|
|
8
|
+
attr_reader :server
|
|
14
9
|
|
|
15
10
|
def run
|
|
16
11
|
begin
|
|
17
|
-
connect @
|
|
18
|
-
|
|
19
|
-
|
|
12
|
+
connect @servername, @port
|
|
13
|
+
login @username, @realname
|
|
14
|
+
identify @nick, @nickserv_pwd
|
|
20
15
|
main_loop
|
|
16
|
+
rescue Exception => e
|
|
17
|
+
handle_exception(e)
|
|
18
|
+
raise e
|
|
21
19
|
ensure
|
|
22
20
|
disconnect
|
|
23
21
|
end
|
|
24
22
|
end
|
|
25
23
|
|
|
24
|
+
protected
|
|
25
|
+
|
|
26
|
+
def handle_exception(e)
|
|
27
|
+
end
|
|
28
|
+
|
|
26
29
|
private
|
|
27
30
|
|
|
28
|
-
def
|
|
31
|
+
def initialize(config)
|
|
32
|
+
@servername = config[:server]
|
|
33
|
+
@port = config[:port] || 6667
|
|
34
|
+
@nick = config[:nick]
|
|
35
|
+
@nickserv_pwd = config[:nickserv_pwd]
|
|
36
|
+
@username = config[:username] || @nick
|
|
37
|
+
@realname = config[:realname] || @nick
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def connect(server, port = 6667)
|
|
29
41
|
@server = Server.connect server, port
|
|
30
42
|
end
|
|
31
43
|
|
|
32
|
-
def
|
|
33
|
-
|
|
44
|
+
def login(username, realname)
|
|
45
|
+
set_user username, realname
|
|
34
46
|
end
|
|
35
47
|
|
|
36
|
-
def
|
|
37
|
-
|
|
48
|
+
def identify(nick, password = nil)
|
|
49
|
+
set_nick(nick)
|
|
50
|
+
nickserv("identify #{password}") if password
|
|
51
|
+
end
|
|
38
52
|
|
|
39
|
-
|
|
40
|
-
@
|
|
53
|
+
def disconnect
|
|
54
|
+
@server.disconnect
|
|
41
55
|
end
|
|
42
56
|
|
|
43
57
|
def main_loop
|
|
@@ -45,11 +59,6 @@ module MiniBot
|
|
|
45
59
|
dispatch msg
|
|
46
60
|
end
|
|
47
61
|
end
|
|
48
|
-
|
|
49
|
-
def authenticate(nick, username, realname)
|
|
50
|
-
@server.write "USER #{username} 0 xxx :#{realname}"
|
|
51
|
-
@server.write "NICK #{nick}"
|
|
52
|
-
end
|
|
53
62
|
end
|
|
54
63
|
end
|
|
55
64
|
|
data/lib/minibot/events.rb
CHANGED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
module MiniBot
|
|
2
|
+
class Logger
|
|
3
|
+
@levels = {}
|
|
4
|
+
|
|
5
|
+
class << self
|
|
6
|
+
def with_level(level, &block)
|
|
7
|
+
@levels[level] = block
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
attr_reader :levels
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
attr_reader :level
|
|
14
|
+
|
|
15
|
+
def write(str)
|
|
16
|
+
time = Time.now.strftime("%Y/%m/%d %H:%M:%S")
|
|
17
|
+
@target.printf "<%s> [%s] %s\n",time, level, str
|
|
18
|
+
@target.flush
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def level=(level)
|
|
22
|
+
@level = level.to_sym
|
|
23
|
+
self.class.levels[@level].call(self)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def initialize(file_name_or_io)
|
|
29
|
+
@target = if String === file_name_or_io
|
|
30
|
+
File.new(file_name_or_io, "w")
|
|
31
|
+
else
|
|
32
|
+
file_name_or_io
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def close
|
|
37
|
+
@target.close
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
module Kernel
|
|
43
|
+
attr_accessor :logger
|
|
44
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: david-minibot
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0
|
|
4
|
+
version: 0.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- David Leal
|
|
@@ -9,7 +9,7 @@ autorequire: minibot
|
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
11
|
|
|
12
|
-
date: 2008-06-
|
|
12
|
+
date: 2008-06-25 00:00:00 -07:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies: []
|
|
15
15
|
|
|
@@ -32,6 +32,7 @@ files:
|
|
|
32
32
|
- lib/minibot/commands.rb
|
|
33
33
|
- lib/minibot/daemon.rb
|
|
34
34
|
- lib/minibot/server.rb
|
|
35
|
+
- lib/minibot/logger.rb
|
|
35
36
|
- lib/minibot/events.rb
|
|
36
37
|
- lib/minibot/constants.rb
|
|
37
38
|
- lib/minibot.rb
|
|
@@ -57,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
57
58
|
requirements: []
|
|
58
59
|
|
|
59
60
|
rubyforge_project:
|
|
60
|
-
rubygems_version: 1.0
|
|
61
|
+
rubygems_version: 1.2.0
|
|
61
62
|
signing_key:
|
|
62
63
|
specification_version: 2
|
|
63
64
|
summary: A mini Ruby IRC bot framework
|