beerbot 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/BeerBot.rb +8 -6
- data/lib/BeerBot/00.utils/param_expand.rb +105 -0
- data/lib/BeerBot/00.utils/{paramExpand.rb → sentence_expand.rb} +1 -1
- data/lib/BeerBot/00.utils/utils.rb +0 -83
- data/lib/BeerBot/{02.bot/bot.rb → 01.bot/Bot.rb} +19 -20
- data/lib/BeerBot/01.bot/BotModule.rb +16 -0
- data/lib/BeerBot/{03.more → 01.bot}/BotMsgMore.rb +5 -4
- data/lib/BeerBot/01.bot/botmsg.rb +105 -0
- data/lib/BeerBot/{01.protocols → 02.protocols}/irc.rb +19 -14
- data/lib/BeerBot/06.dispatchers/{irc.rb → dispatcher.rb} +28 -26
- data/lib/RunIRC.rb +8 -6
- metadata +12 -10
- data/lib/BeerBot/01.protocols/botmsg.rb +0 -59
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b0520c21dd31300f06b2ebf9968b47b637b0053a
|
4
|
+
data.tar.gz: 2bf90aba38d40d9de6c8f23148bac96f7b5358b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 541a57a15d442bd567a71ce3fa000bd4f215fa1bd0ae5596620dc700860ad2db2702906fbbf3f88d877a1f12b62a0524563c211213ca9bfdcc91f0f296e8c3de
|
7
|
+
data.tar.gz: 0272651fdee9bb932ff00c3a3247cdec27f18353a603a0999f0b6b978ca4d6d601263b585672d5f8c19e612f06e0089f56804bfb2da68c35dfc29a72faa54160
|
data/lib/BeerBot.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
1
|
require 'CronR' # For scheduler
|
2
2
|
|
3
3
|
require_relative 'BeerBot/00.utils/utils'
|
4
|
-
require_relative 'BeerBot/00.utils/
|
4
|
+
require_relative 'BeerBot/00.utils/param_expand'
|
5
|
+
require_relative 'BeerBot/00.utils/sentence_expand'
|
5
6
|
require_relative 'BeerBot/00.utils/DataFile'
|
6
7
|
require_relative 'BeerBot/00.utils/InOut'
|
7
8
|
require_relative 'BeerBot/00.utils/world/IRCWorld'
|
8
9
|
require_relative 'BeerBot/01.connect/IRCConnection'
|
9
|
-
require_relative 'BeerBot/01.
|
10
|
-
require_relative 'BeerBot/01.
|
11
|
-
require_relative 'BeerBot/
|
12
|
-
require_relative 'BeerBot/
|
13
|
-
require_relative 'BeerBot/
|
10
|
+
require_relative 'BeerBot/01.bot/botmsg'
|
11
|
+
require_relative 'BeerBot/01.bot/BotModule'
|
12
|
+
require_relative 'BeerBot/01.bot/Bot'
|
13
|
+
require_relative 'BeerBot/01.bot/BotMsgMore'
|
14
|
+
require_relative 'BeerBot/02.protocols/irc'
|
15
|
+
require_relative 'BeerBot/06.dispatchers/dispatcher'
|
14
16
|
require_relative 'BeerBot/70.scheduler/scheduler'
|
15
17
|
require_relative 'BeerBot/Config'
|
16
18
|
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# The files in this directory are part of BeerBot, a a ruby irc bot library.
|
2
|
+
# Copyright (C) 2014 Daniel Bush
|
3
|
+
# This program is distributed under the terms of the GNU
|
4
|
+
# General Public License. A copy of the license should be
|
5
|
+
# enclosed with this project in the file LICENSE. If not
|
6
|
+
# see <http://www.gnu.org/licenses/>.
|
7
|
+
|
8
|
+
|
9
|
+
module BeerBot
|
10
|
+
|
11
|
+
module Utils
|
12
|
+
|
13
|
+
module ParamExpand
|
14
|
+
|
15
|
+
# Look for parameters in a string.
|
16
|
+
#
|
17
|
+
# Numeric parameters => "... ::1 ... ::2 "
|
18
|
+
# Key parameters => "... ::foo ... ::bar "
|
19
|
+
# Multi => "... ::foo|::1 ... ::bar|::foo|::1 "
|
20
|
+
#
|
21
|
+
# (?: ) is a non-capturing group.
|
22
|
+
|
23
|
+
def self.scan_param msg
|
24
|
+
matches = msg.scan(/(?:::[^\W\s:|]*(?:\|::[^\W\s:|]*)*)/)
|
25
|
+
matches.map{|m|
|
26
|
+
a = m.split('|').map{|m2|
|
27
|
+
m2 = m2.sub('::','')
|
28
|
+
case m2
|
29
|
+
when /^\d+$/
|
30
|
+
m2.to_i
|
31
|
+
else
|
32
|
+
m2
|
33
|
+
end
|
34
|
+
}
|
35
|
+
[m,a]
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
# Expand a string with numeric and key parameters using data
|
40
|
+
# provided.
|
41
|
+
#
|
42
|
+
# Parameters should be preceded with a double-colon in the msg.
|
43
|
+
# Numeric parameters are matched to 'args'.
|
44
|
+
# So ::1 => args[0] etc
|
45
|
+
#
|
46
|
+
# 'expand' will return the expanded string as best it can and an
|
47
|
+
# error object.
|
48
|
+
# The error object will tell you if there weren't enough
|
49
|
+
# parameters in args to satisfy the numeric parameters in the
|
50
|
+
# string.
|
51
|
+
#
|
52
|
+
# Example:
|
53
|
+
#
|
54
|
+
# "::1 ::foo ::bar|::1" using ('a',foo:'b')
|
55
|
+
# => "a b a"
|
56
|
+
|
57
|
+
def self.expand msg,*args,**kargs
|
58
|
+
errargs = []
|
59
|
+
params = self.scan_param(msg)
|
60
|
+
# Do the big ones first.
|
61
|
+
params = params.sort{|a,b|b[1].size<=>a[1].size}
|
62
|
+
params.each {|i|
|
63
|
+
# pattern: "::1|::foo"
|
64
|
+
# parts: [1,'foo']
|
65
|
+
pattern,parts = i
|
66
|
+
found = false
|
67
|
+
_errargs = []
|
68
|
+
_errkargs = []
|
69
|
+
parts.each {|part|
|
70
|
+
v = nil
|
71
|
+
case part
|
72
|
+
when Fixnum
|
73
|
+
v = args[part-1]
|
74
|
+
_errargs.push(part) unless v
|
75
|
+
else
|
76
|
+
if part == '' then # pattern is or contains '::' which has part ''.
|
77
|
+
v = ''
|
78
|
+
else
|
79
|
+
v = kargs[part.to_sym]
|
80
|
+
_errkargs.push(part) unless v
|
81
|
+
end
|
82
|
+
end
|
83
|
+
if v then
|
84
|
+
if v == '' then
|
85
|
+
#byebug
|
86
|
+
# Squeeze spaces. Not perfect, but it'll do.
|
87
|
+
msg = msg.gsub(/ ?#{pattern.gsub('|','\|')} ?/,' ')
|
88
|
+
else
|
89
|
+
msg = msg.gsub(pattern,v.to_s)
|
90
|
+
end
|
91
|
+
found = true
|
92
|
+
end
|
93
|
+
}
|
94
|
+
unless found then
|
95
|
+
errargs += _errargs
|
96
|
+
end
|
97
|
+
}
|
98
|
+
[msg,errargs]
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
@@ -10,30 +10,6 @@ module BeerBot
|
|
10
10
|
|
11
11
|
module Utils
|
12
12
|
|
13
|
-
# Look for parameters in a string.
|
14
|
-
#
|
15
|
-
# Numeric parameters => "... ::1 ... ::2 "
|
16
|
-
# Key parameters => "... ::foo ... ::bar "
|
17
|
-
# Multi => "... ::foo|::1 ... ::bar|::foo|::1 "
|
18
|
-
#
|
19
|
-
# (?: ) is a non-capturing group.
|
20
|
-
|
21
|
-
def self.scan_param msg
|
22
|
-
matches = msg.scan(/(?:::[^\W\s:|]*(?:\|::[^\W\s:|]*)*)/)
|
23
|
-
matches.map{|m|
|
24
|
-
a = m.split('|').map{|m2|
|
25
|
-
m2 = m2.sub('::','')
|
26
|
-
case m2
|
27
|
-
when /\d+/
|
28
|
-
m2.to_i
|
29
|
-
else
|
30
|
-
m2
|
31
|
-
end
|
32
|
-
}
|
33
|
-
[m,a]
|
34
|
-
}
|
35
|
-
end
|
36
|
-
|
37
13
|
# Return a parser that takes string msg and extracts a specified
|
38
14
|
# prefix at beginning.
|
39
15
|
#
|
@@ -53,65 +29,6 @@ module BeerBot
|
|
53
29
|
}
|
54
30
|
end
|
55
31
|
|
56
|
-
# Expand a string with numeric and key parameters using data
|
57
|
-
# provided.
|
58
|
-
#
|
59
|
-
# Parameters should be preceded with a double-colon in the msg.
|
60
|
-
# Numeric parameters are matched to 'args'.
|
61
|
-
# So ::1 => args[0] etc
|
62
|
-
#
|
63
|
-
# 'expand' will return the expanded string as best it can and an
|
64
|
-
# error object.
|
65
|
-
# The error object will tell you if there weren't enough
|
66
|
-
# parameters in args to satisfy the numeric parameters in the
|
67
|
-
# string.
|
68
|
-
#
|
69
|
-
# ("::1 ::foo ::bar|::1",'a',foo:'b') => "a b a"
|
70
|
-
|
71
|
-
def self.expand msg,*args,**kargs
|
72
|
-
errargs = []
|
73
|
-
params = self.scan_param(msg)
|
74
|
-
# Do the big ones first.
|
75
|
-
params = params.sort{|a,b|b[1].size<=>a[1].size}
|
76
|
-
params.each {|i|
|
77
|
-
# pattern: "::1|::foo"
|
78
|
-
# parts: [1,'foo']
|
79
|
-
pattern,parts = i
|
80
|
-
found = false
|
81
|
-
_errargs = []
|
82
|
-
_errkargs = []
|
83
|
-
parts.each {|part|
|
84
|
-
v = nil
|
85
|
-
case part
|
86
|
-
when Fixnum
|
87
|
-
v = args[part-1]
|
88
|
-
_errargs.push(part) unless v
|
89
|
-
else
|
90
|
-
if part == '' then # pattern is or contains '::' which has part ''.
|
91
|
-
v = ''
|
92
|
-
else
|
93
|
-
v = kargs[part.to_sym]
|
94
|
-
_errkargs.push(part) unless v
|
95
|
-
end
|
96
|
-
end
|
97
|
-
if v then
|
98
|
-
if v == '' then
|
99
|
-
#byebug
|
100
|
-
# Squeeze spaces. Not perfect, but it'll do.
|
101
|
-
msg = msg.gsub(/ ?#{pattern.gsub('|','\|')} ?/,' ')
|
102
|
-
else
|
103
|
-
msg = msg.gsub(pattern,v.to_s)
|
104
|
-
end
|
105
|
-
found = true
|
106
|
-
end
|
107
|
-
}
|
108
|
-
unless found then
|
109
|
-
errargs += _errargs
|
110
|
-
end
|
111
|
-
}
|
112
|
-
[msg,errargs]
|
113
|
-
end
|
114
|
-
|
115
32
|
# Regex that looks for a sed command eg
|
116
33
|
# "s/pattern/replacement/flags" in a string.
|
117
34
|
#
|
@@ -5,25 +5,11 @@
|
|
5
5
|
# enclosed with this project in the file LICENSE. If not
|
6
6
|
# see <http://www.gnu.org/licenses/>.
|
7
7
|
|
8
|
-
require_relative '
|
8
|
+
require_relative 'botmsg.rb'
|
9
|
+
require_relative 'BotModule.rb'
|
9
10
|
|
10
11
|
module BeerBot
|
11
12
|
|
12
|
-
BotMsg = BeerBot::Protocol::BotMsg
|
13
|
-
|
14
|
-
# Represents a bot module and may contain a reference to the loaded
|
15
|
-
# ruby module or any errors associated with loading it.
|
16
|
-
|
17
|
-
class BotModule < Hash
|
18
|
-
def initialize name,status:false,mod:nil,modname:nil,errors:[]
|
19
|
-
self[:status] = status
|
20
|
-
self[:name] = name # The module name.
|
21
|
-
self[:mod] = mod # The loaded ruby module.
|
22
|
-
self[:modname] = modname
|
23
|
-
self[:errors] = errors
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
13
|
# Represents a sequence of BotModule instances.
|
28
14
|
|
29
15
|
class Bot < Array
|
@@ -114,24 +100,37 @@ module BeerBot
|
|
114
100
|
}
|
115
101
|
end
|
116
102
|
|
103
|
+
# Process messages addressed directly to the bot.
|
104
|
+
|
117
105
|
def cmd msg,from:nil,to:nil,me:false,world:nil
|
118
106
|
if @cmd then
|
119
|
-
|
120
|
-
return botmsg
|
107
|
+
@cmd.call(msg,from:from,to:to,world:world,me:me)
|
121
108
|
else
|
122
109
|
self.run(:cmd,msg,from:from,to:to,world:world,me:me)
|
123
110
|
end
|
124
111
|
end
|
125
112
|
|
113
|
+
# Process messages the bot overhears.
|
114
|
+
|
126
115
|
def hear msg,from:nil,to:nil,me:false,world:nil
|
127
116
|
if @hear then
|
128
|
-
|
129
|
-
return botmsg
|
117
|
+
@hear.call(msg,from:from,to:to,me:me,world:world)
|
130
118
|
else
|
131
119
|
self.run(:hear,msg,from:from,to:to,me:me,world:world)
|
132
120
|
end
|
133
121
|
end
|
134
122
|
|
123
|
+
# Handle events other than being messaged.
|
124
|
+
#
|
125
|
+
# IRC events like joining channels, changing nicks etc.
|
126
|
+
#
|
127
|
+
# Both event and kargs is dependent on the dispatcher which in
|
128
|
+
# turn is dependent on how the protocol (eg irc) is parsed.
|
129
|
+
|
130
|
+
def event event,**kargs
|
131
|
+
self.run(:event,event,**kargs)
|
132
|
+
end
|
133
|
+
|
135
134
|
def help arr,from:nil,to:nil,world:nil,me:false
|
136
135
|
m = []
|
137
136
|
modname,*topics = arr
|
@@ -0,0 +1,16 @@
|
|
1
|
+
|
2
|
+
module BeerBot
|
3
|
+
# Represents a bot module and may contain a reference to the loaded
|
4
|
+
# ruby module or any errors associated with loading it.
|
5
|
+
|
6
|
+
class BotModule < Hash
|
7
|
+
def initialize name,status:false,mod:nil,modname:nil,errors:[]
|
8
|
+
self[:status] = status
|
9
|
+
self[:name] = name # The module name.
|
10
|
+
self[:mod] = mod # The loaded ruby module.
|
11
|
+
self[:modname] = modname
|
12
|
+
self[:errors] = errors
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -9,16 +9,17 @@ require_relative '../00.utils/More'
|
|
9
9
|
|
10
10
|
module BeerBot
|
11
11
|
|
12
|
-
# More-based
|
12
|
+
# More-based filter that can be applied to botmsg's.
|
13
13
|
#
|
14
|
-
# #filter expects a botmsg and returns an array botmsg
|
14
|
+
# #filter expects a botmsg and returns an array, either of botmsg's
|
15
|
+
# or emtpy.
|
15
16
|
|
16
17
|
class BotMsgMore < ::BeerBot::Utils::More
|
17
18
|
def filter botmsg
|
18
|
-
|
19
|
+
arr = BeerBot::BotMsg.to_a(botmsg)
|
20
|
+
# At this point if arr isn't a valid bot msg we'll get [].
|
19
21
|
replies = []
|
20
22
|
by_to = Hash.new{|h,k| h[k]=[]}
|
21
|
-
arr = BeerBot::Protocol::BotMsg.to_a(botmsg)
|
22
23
|
|
23
24
|
arr.inject(by_to){|h,v| h[v[:to]].push(v); h}
|
24
25
|
by_to.each_pair{|to,a|
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# The files in this directory are part of BeerBot, a a ruby irc bot library.
|
2
|
+
# Copyright (C) 2013,2014 Daniel Bush
|
3
|
+
# This program is distributed under the terms of the GNU
|
4
|
+
# General Public License. A copy of the license should be
|
5
|
+
# enclosed with this project in the file LICENSE. If not
|
6
|
+
# see <http://www.gnu.org/licenses/>.
|
7
|
+
|
8
|
+
module BeerBot
|
9
|
+
|
10
|
+
# BotMsg's are hashes or arrays of hashes and represent a bot
|
11
|
+
# response (usually to some input eg from an irc server).
|
12
|
+
#
|
13
|
+
# The hash is generally expected to have the following keys:
|
14
|
+
#
|
15
|
+
# :msg
|
16
|
+
# :to
|
17
|
+
#
|
18
|
+
# but may carry additional ones like
|
19
|
+
#
|
20
|
+
# :action (will be used instead of :msg)
|
21
|
+
#
|
22
|
+
# The array-form of the botmsg might look like this:
|
23
|
+
#
|
24
|
+
# [msg:'ho!',to:'#chan1']
|
25
|
+
#
|
26
|
+
# and it is easy to add extra messages this way:
|
27
|
+
#
|
28
|
+
# [msg:'ho!',to:'#chan1'] + [msg:'ho again!',to:'#chan1']
|
29
|
+
#
|
30
|
+
# etc
|
31
|
+
|
32
|
+
module BotMsg
|
33
|
+
|
34
|
+
# Determine if botmsg is a valid botmsg.
|
35
|
+
|
36
|
+
def self.valid? botmsg
|
37
|
+
case botmsg
|
38
|
+
when Hash
|
39
|
+
a = botmsg.has_key?(:to)
|
40
|
+
b = (botmsg.has_key?(:msg) || botmsg.has_key?(:action))
|
41
|
+
a && b
|
42
|
+
when Array
|
43
|
+
botmsg.inject(true){|s,v|
|
44
|
+
# Members must be hash...
|
45
|
+
break false unless v.kind_of?(Hash)
|
46
|
+
s = s && self.valid?(v);
|
47
|
+
break false unless s;
|
48
|
+
s
|
49
|
+
}
|
50
|
+
when Proc
|
51
|
+
false
|
52
|
+
else
|
53
|
+
false
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Convert botmsg to an array of one or more botmsg hashes.
|
58
|
+
#
|
59
|
+
# Returns array of botmsg hashes or empty array if not given
|
60
|
+
# something that is valid.
|
61
|
+
#
|
62
|
+
# Proc's are executed to retrieve an array or hash.
|
63
|
+
#
|
64
|
+
# Array => Array
|
65
|
+
# Hash => [Hash]
|
66
|
+
# Proc => [Hash]
|
67
|
+
|
68
|
+
def self.to_a botmsg
|
69
|
+
case botmsg
|
70
|
+
when Hash
|
71
|
+
return [] unless self.valid?(botmsg)
|
72
|
+
return [botmsg]
|
73
|
+
when Array
|
74
|
+
return [] unless self.valid?(botmsg)
|
75
|
+
return botmsg
|
76
|
+
when Proc
|
77
|
+
return self.to_a(botmsg.call)
|
78
|
+
else
|
79
|
+
return []
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# Convert botmsg to an action if it starts with '*'.
|
84
|
+
|
85
|
+
def self.actionify botmsg
|
86
|
+
case botmsg
|
87
|
+
when Hash
|
88
|
+
case botmsg[:msg]
|
89
|
+
when /^\*\s*/
|
90
|
+
botmsg[:action] = botmsg[:msg].sub(/^\*\s*/,'')
|
91
|
+
botmsg[:msg] = nil
|
92
|
+
end
|
93
|
+
botmsg
|
94
|
+
when Array
|
95
|
+
botmsg.map {|b|
|
96
|
+
self.actionify(b)
|
97
|
+
}
|
98
|
+
else
|
99
|
+
botmsg
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
@@ -11,8 +11,11 @@ module BeerBot
|
|
11
11
|
|
12
12
|
module IRC
|
13
13
|
|
14
|
-
#
|
15
|
-
#
|
14
|
+
# This class represents an irc message broken down into its
|
15
|
+
# major constituent parts.
|
16
|
+
#
|
17
|
+
# These include the prefix, command, parameters and trailing
|
18
|
+
# components of an irc message.
|
16
19
|
|
17
20
|
class IRCMessage < Hash
|
18
21
|
|
@@ -110,18 +113,24 @@ module BeerBot
|
|
110
113
|
|
111
114
|
end
|
112
115
|
|
113
|
-
# Parse raw irc string and then yield
|
116
|
+
# Parse raw irc string and then yield or return a generic
|
117
|
+
# representation of the event.
|
118
|
+
#
|
119
|
+
# Returns [event,*args]
|
120
|
+
#
|
121
|
+
# Parse's job is to take the constituents parts of an irc
|
122
|
+
# message, identify the type of event and return a generic
|
123
|
+
# representation of it.
|
124
|
+
#
|
125
|
+
# So for instance, an irc privmsg (message) is represented as
|
126
|
+
# [:msg,from,to,msg]
|
114
127
|
#
|
115
|
-
# The lambda receives an incoming irc string, tries to parse
|
116
|
-
# it and act on it.
|
117
|
-
# The lambda should return nil or a botmsg hash.
|
118
128
|
# Note that connection readiness and PONG protocol are handled by
|
119
129
|
# the irc connection, not here.
|
120
130
|
|
121
|
-
def self.parse str
|
131
|
+
def self.parse str
|
122
132
|
|
123
133
|
m = IRCMessage.new(str)
|
124
|
-
#puts "[parse] #{m}"
|
125
134
|
result = []
|
126
135
|
|
127
136
|
case m[:command]
|
@@ -180,17 +189,13 @@ module BeerBot
|
|
180
189
|
msg = m[:trailing].strip
|
181
190
|
from = m[:prefix][:nick].strip
|
182
191
|
to = m[:params][0].strip unless m[:params].empty?
|
183
|
-
result = [:
|
192
|
+
result = [:msg,from,to,msg]
|
184
193
|
|
185
194
|
else # command we don't handle
|
186
195
|
result = [:default,m]
|
187
196
|
end
|
188
197
|
|
189
|
-
|
190
|
-
yield result
|
191
|
-
else
|
192
|
-
result
|
193
|
-
end
|
198
|
+
result
|
194
199
|
end
|
195
200
|
|
196
201
|
# Return irc-conformat string from a botmsg hash.
|
@@ -6,8 +6,7 @@
|
|
6
6
|
# see <http://www.gnu.org/licenses/>.
|
7
7
|
|
8
8
|
require_relative '../00.utils/utils'
|
9
|
-
require_relative '../01.
|
10
|
-
require_relative '../03.more/BotMsgMore'
|
9
|
+
require_relative '../01.bot/BotMsgMore'
|
11
10
|
|
12
11
|
module BeerBot
|
13
12
|
|
@@ -16,20 +15,24 @@ module BeerBot
|
|
16
15
|
|
17
16
|
module Dispatchers
|
18
17
|
|
19
|
-
# This dispatcher
|
20
|
-
#
|
18
|
+
# This dispatcher receives generic events (provided by a parser
|
19
|
+
# for a given protocol eg irc) and does something with them.
|
21
20
|
#
|
22
|
-
#
|
23
|
-
#
|
21
|
+
# Dispatcher#receive receives the messages.
|
22
|
+
#
|
23
|
+
# There are several ways to specify what the dispatcher should do:
|
24
|
+
# 1) just get the result of #receive
|
25
|
+
#
|
26
|
+
# Or, override #receive's behaviour:
|
24
27
|
# 1) pass in a block at instantiation time
|
25
|
-
# 2)
|
28
|
+
# 2) pass in a block using #set_receive
|
26
29
|
# 3) subclass this class and write your own #receive
|
27
30
|
|
28
|
-
class
|
31
|
+
class Dispatcher
|
29
32
|
|
30
|
-
|
31
|
-
|
32
|
-
BotMsgMore
|
33
|
+
Utils = BeerBot::Utils
|
34
|
+
BotMsg = BeerBot::BotMsg
|
35
|
+
BotMsgMore = BeerBot::BotMsgMore
|
33
36
|
|
34
37
|
attr_accessor :bot,:nick,:prefix,:world,:more
|
35
38
|
|
@@ -63,44 +66,44 @@ module BeerBot
|
|
63
66
|
end
|
64
67
|
end
|
65
68
|
|
66
|
-
def
|
67
|
-
IRC.parse(irc_str)
|
68
|
-
end
|
69
|
-
|
70
|
-
def receive irc_str
|
71
|
-
|
72
|
-
event,*args = self.parse(irc_str)
|
69
|
+
def receive event,args
|
73
70
|
|
74
71
|
if @block then
|
75
72
|
return self.instance_exec(event,*args,&@block)
|
76
73
|
end
|
77
74
|
|
75
|
+
replies = nil
|
76
|
+
|
78
77
|
# Otherwise, here is the default behaviour...
|
79
78
|
|
80
79
|
case event
|
81
80
|
when :unknown
|
82
|
-
|
81
|
+
replies = @bot.event(event,args:args)
|
83
82
|
when :default
|
84
|
-
|
83
|
+
replies = @bot.event(event,args:args)
|
85
84
|
when :nick
|
86
85
|
old,nick = args
|
87
86
|
@world.nick(old,nick) if @world
|
87
|
+
replies = @bot.event(event,old:old,nick:nick)
|
88
88
|
when :part
|
89
89
|
nick,channel = args
|
90
90
|
@world.part(nick,channel) if @world
|
91
|
+
replies = @bot.event(event,nick:nick,channel:channel)
|
91
92
|
when :join
|
92
93
|
nick,channel = args
|
94
|
+
me = (@nickrx === nick)
|
93
95
|
@world.join(nick,channel) if @world
|
96
|
+
replies = @bot.event(event,me:me,nick:nick,channel:channel)
|
94
97
|
when :chanlist
|
95
|
-
#p "[dispatcher] :chanlist"
|
96
98
|
channel,users = args
|
97
99
|
if @world then
|
98
100
|
users.each {|user|
|
99
101
|
@world.join(user,channel)
|
100
102
|
}
|
101
103
|
end
|
104
|
+
replies = @bot.event(event,channel:channel,users:users)
|
102
105
|
|
103
|
-
when :
|
106
|
+
when :msg
|
104
107
|
|
105
108
|
from,to,msg = args
|
106
109
|
|
@@ -137,18 +140,17 @@ module BeerBot
|
|
137
140
|
end
|
138
141
|
|
139
142
|
else
|
140
|
-
puts "
|
143
|
+
puts "[dispatcher] unrecognised event: '#{event}'"
|
141
144
|
end
|
142
145
|
|
143
146
|
case replies
|
144
|
-
when String # assume
|
147
|
+
when String # assume protocol string eg irc
|
145
148
|
replies
|
146
149
|
when Hash,Array,Proc
|
147
150
|
# more-filter the reply...
|
148
151
|
replies = @more.filter(replies)
|
149
|
-
IRC.to_irc(replies)
|
150
152
|
else
|
151
|
-
|
153
|
+
[]
|
152
154
|
end
|
153
155
|
|
154
156
|
end
|
data/lib/RunIRC.rb
CHANGED
@@ -28,7 +28,7 @@ class BeerBot::RunIRC
|
|
28
28
|
IRCConnection = BeerBot::IRCConnection
|
29
29
|
IRC = BeerBot::Protocol::IRC
|
30
30
|
Bot = BeerBot::Bot
|
31
|
-
|
31
|
+
Dispatcher = BeerBot::Dispatchers::Dispatcher
|
32
32
|
Scheduler = BeerBot::Scheduler
|
33
33
|
|
34
34
|
attr_accessor :config,:bot,:scheduler,:dispatcher,:world,:conn,:postq,:parse,:more
|
@@ -53,7 +53,7 @@ class BeerBot::RunIRC
|
|
53
53
|
@world = IRCWorld.new(config['nick'])
|
54
54
|
|
55
55
|
# Dispatcher which receives messages and interacts with the bot.
|
56
|
-
@dispatcher =
|
56
|
+
@dispatcher = Dispatcher.new(
|
57
57
|
@bot,
|
58
58
|
config['nick'],
|
59
59
|
prefix:config['cmd_prefix'],
|
@@ -68,12 +68,14 @@ class BeerBot::RunIRC
|
|
68
68
|
nick:config['nick'],
|
69
69
|
server:config['server'])
|
70
70
|
|
71
|
-
# Dispatcher thread takes stuff from
|
72
|
-
# it...
|
71
|
+
# Dispatcher thread takes stuff coming from the irc connection and does
|
72
|
+
# something with it...
|
73
73
|
|
74
74
|
@dispatcher_thread = InOut.new(inq:@conn.queue,outq:@conn.writeq) {|input|
|
75
75
|
str,raw = input
|
76
|
-
|
76
|
+
event,*args = IRC.parse(str)
|
77
|
+
replies = @dispatcher.receive(event,args)
|
78
|
+
IRC.to_irc(replies)
|
77
79
|
}
|
78
80
|
@dispatcher_thread.start!
|
79
81
|
|
@@ -85,7 +87,7 @@ class BeerBot::RunIRC
|
|
85
87
|
@scheduler_thread = InOut.new(inq:@scheduler.queue,outq:@conn.writeq) {|cron_job|
|
86
88
|
puts "<< scheduler #{cron_job.inspect}"
|
87
89
|
puts "<< scheduler #{@scheduler.time}"
|
88
|
-
IRC.to_irc(cron_job.
|
90
|
+
IRC.to_irc(cron_job.run)
|
89
91
|
}
|
90
92
|
@scheduler_thread.start!
|
91
93
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: beerbot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Bush
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -88,20 +88,22 @@ executables:
|
|
88
88
|
extensions: []
|
89
89
|
extra_rdoc_files: []
|
90
90
|
files:
|
91
|
+
- lib/BeerBot/00.utils/utils.rb
|
92
|
+
- lib/BeerBot/00.utils/param_expand.rb
|
93
|
+
- lib/BeerBot/00.utils/sentence_expand.rb
|
91
94
|
- lib/BeerBot/00.utils/DataFile.rb
|
92
95
|
- lib/BeerBot/00.utils/InOut.rb
|
93
96
|
- lib/BeerBot/00.utils/More.rb
|
94
|
-
- lib/BeerBot/00.utils/paramExpand.rb
|
95
|
-
- lib/BeerBot/00.utils/utils.rb
|
96
|
-
- lib/BeerBot/00.utils/world/IRCWorld.rb
|
97
97
|
- lib/BeerBot/00.utils/world/World.rb
|
98
|
+
- lib/BeerBot/00.utils/world/IRCWorld.rb
|
98
99
|
- lib/BeerBot/01.connect/Connection.rb
|
99
100
|
- lib/BeerBot/01.connect/IRCConnection.rb
|
100
|
-
- lib/BeerBot/01.
|
101
|
-
- lib/BeerBot/01.
|
102
|
-
- lib/BeerBot/
|
103
|
-
- lib/BeerBot/
|
104
|
-
- lib/BeerBot/
|
101
|
+
- lib/BeerBot/01.bot/botmsg.rb
|
102
|
+
- lib/BeerBot/01.bot/BotModule.rb
|
103
|
+
- lib/BeerBot/01.bot/Bot.rb
|
104
|
+
- lib/BeerBot/01.bot/BotMsgMore.rb
|
105
|
+
- lib/BeerBot/02.protocols/irc.rb
|
106
|
+
- lib/BeerBot/06.dispatchers/dispatcher.rb
|
105
107
|
- lib/BeerBot/70.scheduler/scheduler.rb
|
106
108
|
- lib/BeerBot/Config.rb
|
107
109
|
- lib/BeerBot.rb
|
@@ -1,59 +0,0 @@
|
|
1
|
-
# The files in this directory are part of BeerBot, a a ruby irc bot library.
|
2
|
-
# Copyright (C) 2013,2014 Daniel Bush
|
3
|
-
# This program is distributed under the terms of the GNU
|
4
|
-
# General Public License. A copy of the license should be
|
5
|
-
# enclosed with this project in the file LICENSE. If not
|
6
|
-
# see <http://www.gnu.org/licenses/>.
|
7
|
-
|
8
|
-
module BeerBot
|
9
|
-
|
10
|
-
module Protocol
|
11
|
-
|
12
|
-
module BotMsg
|
13
|
-
|
14
|
-
# Convert botmsg to an array of one or more botmsg hashes.
|
15
|
-
#
|
16
|
-
# Proc's are executed to retrieve an array or hash.
|
17
|
-
#
|
18
|
-
# Array => Array
|
19
|
-
# Hash => [Hash]
|
20
|
-
# Proc => [Hash]
|
21
|
-
|
22
|
-
def self.to_a botmsg
|
23
|
-
case botmsg
|
24
|
-
when Hash
|
25
|
-
return [botmsg]
|
26
|
-
when Array
|
27
|
-
return botmsg
|
28
|
-
when Proc
|
29
|
-
return self.to_a(botmsg.call)
|
30
|
-
else
|
31
|
-
return []
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
# Convert botmsg to an action if it starts with '*'.
|
36
|
-
|
37
|
-
def self.actionify botmsg
|
38
|
-
case botmsg
|
39
|
-
when Hash
|
40
|
-
case botmsg[:msg]
|
41
|
-
when /^\*\s*/
|
42
|
-
botmsg[:action] = botmsg[:msg].sub(/^\*\s*/,'')
|
43
|
-
botmsg[:msg] = nil
|
44
|
-
end
|
45
|
-
botmsg
|
46
|
-
when Array
|
47
|
-
botmsg.map {|b|
|
48
|
-
self.actionify(b)
|
49
|
-
}
|
50
|
-
else
|
51
|
-
botmsg
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
|
58
|
-
end
|
59
|
-
end
|