buddhy 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Readme.md +2 -0
- data/lib/buddhy/bot.rb +12 -0
- data/lib/buddhy/config.rb +26 -0
- data/lib/buddhy/connection.rb +75 -0
- data/lib/buddhy/listener.rb +24 -0
- data/lib/buddhy/message.rb +17 -0
- data/lib/buddhy/plugins/bart_departures.rb +19 -0
- data/lib/buddhy/plugins/le_truc.rb +14 -0
- metadata +64 -0
data/Readme.md
ADDED
data/lib/buddhy/bot.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module Buddhy
|
4
|
+
class Config
|
5
|
+
|
6
|
+
attr_accessor :nick, :name, :server, :channel
|
7
|
+
|
8
|
+
def self.load
|
9
|
+
new.read_yaml
|
10
|
+
end
|
11
|
+
|
12
|
+
def read_yaml
|
13
|
+
root = File.expand_path('../../..', __FILE__)
|
14
|
+
file = File.join(root, 'config', 'buddhy.yml')
|
15
|
+
hash = YAML.load(File.read(file))
|
16
|
+
|
17
|
+
@nick = hash[:nick]
|
18
|
+
@name = hash[:name]
|
19
|
+
@server = hash[:server]
|
20
|
+
@channel = hash[:channel]
|
21
|
+
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'buddhy/listener'
|
3
|
+
|
4
|
+
module Buddhy
|
5
|
+
class Connection
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@listening = false
|
9
|
+
@listener = Listener.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.open(server)
|
13
|
+
new.connect(server)
|
14
|
+
end
|
15
|
+
|
16
|
+
def connect(server)
|
17
|
+
@socket = TCPSocket.open(server, 6667)
|
18
|
+
@listener.register(/PING/) { @socket.puts "PONG" }
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
def join_channel(bot, channel)
|
23
|
+
login(bot.nick, bot.name)
|
24
|
+
join(channel)
|
25
|
+
end
|
26
|
+
|
27
|
+
def login(nick, name)
|
28
|
+
@socket.puts "USER #{nick} 0 * #{name}" # no idea what `0 *` means
|
29
|
+
@socket.puts "NICK #{nick}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def join(channel)
|
33
|
+
@channel = channel
|
34
|
+
@socket.puts "JOIN #{channel}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def close
|
38
|
+
@socket.puts "PART #{@channel}"
|
39
|
+
@socket.puts "QUIT"
|
40
|
+
end
|
41
|
+
|
42
|
+
def say(msg)
|
43
|
+
@socket.puts "PRIVMSG #{@channel} :#{msg}"
|
44
|
+
end
|
45
|
+
|
46
|
+
def listen(pattern, &block)
|
47
|
+
@listener.register(pattern, &block)
|
48
|
+
end
|
49
|
+
|
50
|
+
def listen!
|
51
|
+
setup_trap
|
52
|
+
start_listening
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def setup_trap
|
58
|
+
Kernel.trap("INT") do
|
59
|
+
@socket.puts "PART #{@channel} :parting"
|
60
|
+
@socket.puts "QUIT"
|
61
|
+
@shutdown = true
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def start_listening
|
66
|
+
until @shutdown || @socket.eof?
|
67
|
+
msg = @socket.gets
|
68
|
+
puts msg
|
69
|
+
|
70
|
+
@listener.execute(msg)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'buddhy/message'
|
2
|
+
|
3
|
+
module Buddhy
|
4
|
+
class Listener
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@rules = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def execute(line)
|
11
|
+
@rules.each do |rule|
|
12
|
+
if rule[:pattern].match(line)
|
13
|
+
message = Message.new(line)
|
14
|
+
return rule[:block].call(message)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def register(pattern, &block)
|
20
|
+
@rules.push({ :pattern => pattern, :block => block }) && @rules.last
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bart/station'
|
2
|
+
|
3
|
+
module Buddhy
|
4
|
+
module Plugins
|
5
|
+
class BartDepartures
|
6
|
+
|
7
|
+
def self.execute
|
8
|
+
station = Bart::Station.new('powl')
|
9
|
+
station.load_departures
|
10
|
+
|
11
|
+
station.departures.map do |departure|
|
12
|
+
"#{departure.destination.name}: " +
|
13
|
+
departure.estimates.map { |e| e.time.strftime("%I:%M") }.join(", ")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: buddhy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: "0.1"
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Josh Lubaway
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-04-20 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description:
|
18
|
+
email: dontneedmoreemail@example.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- Readme.md
|
25
|
+
files:
|
26
|
+
- lib/buddhy/bot.rb
|
27
|
+
- lib/buddhy/config.rb
|
28
|
+
- lib/buddhy/connection.rb
|
29
|
+
- lib/buddhy/listener.rb
|
30
|
+
- lib/buddhy/message.rb
|
31
|
+
- lib/buddhy/plugins/bart_departures.rb
|
32
|
+
- lib/buddhy/plugins/le_truc.rb
|
33
|
+
- Readme.md
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://github.com/jish/buddhy
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options:
|
40
|
+
- --main
|
41
|
+
- README.md
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.5.2
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: An IRC bot framework
|
63
|
+
test_files: []
|
64
|
+
|