beerbot 0.1.5 → 0.2.0.pre.1

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.
@@ -231,7 +231,7 @@ module BeerBot
231
231
  end
232
232
 
233
233
 
234
- # Return irc-conformat string from a botmsg hash.
234
+ # Return irc-conformat string from a botmsg.
235
235
  #
236
236
  # Generates nil if it can't handle 'botmsg'.
237
237
 
@@ -6,115 +6,84 @@
6
6
  # see <http://www.gnu.org/licenses/>.
7
7
 
8
8
  require_relative '../00.utils/utils'
9
- require_relative '../01.bot/BotMsgMore'
10
9
 
11
10
  module BeerBot
12
11
 
13
- # Dispatchers receive incoming messages from a connection and
14
- # decide what to do with them.
12
+ # Dispatchers receive incoming messages from a protocol object and
13
+ # dispatches them (usually to an instance of Bot).
14
+ #
15
+ # Dispatcher#receive takes:
16
+ # 1) event - a symbol representing an event eg :msg, :join etc
17
+ # 2) args - an array representing arguments
15
18
 
16
19
  module Dispatchers
17
20
 
18
- # This dispatcher receives generic events (provided by a parser
19
- # for a given protocol eg irc) and does something with them.
20
- #
21
- # Dispatcher#receive receives the messages.
21
+ # This class is basically a glorified struct with a receive method
22
+ # that is the default way to dispatch to an instance of Bot.
22
23
  #
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:
27
- # 1) pass in a block at instantiation time
28
- # 2) pass in a block using #set_receive
29
- # 3) subclass this class and write your own #receive
24
+ # You have several options if you want to customize...
25
+ # 1) subclass if you want to pre or post filter
26
+ # 2) override #receive with your own singleton receive (see tests)
27
+ # 3) make a new Dispatcher-like class
30
28
 
31
29
  class Dispatcher
32
30
 
33
31
  Utils = BeerBot::Utils
34
32
  BotMsg = BeerBot::BotMsg
35
- BotMsgMore = BeerBot::BotMsgMore
36
33
 
37
- attr_accessor :bot,:nick,:prefix,:world,:more
34
+ attr_accessor :bot,:nick,:prefix,:config
38
35
 
39
- def initialize bot,nick,prefix:',',world:nil,&block
36
+ def initialize bot,nick,prefix:',',config:nil
40
37
  @bot = bot
41
- @more = BotMsgMore.new
42
-
43
38
  @nick = nick
44
- @get_nick_cmd = Utils.make_prefix_parser(nick)
45
- @nickrx = Regexp.new("^#{nick}$",'i')
46
-
47
39
  @prefix = prefix
40
+ @config = config
41
+ @get_nick_cmd = Utils.make_prefix_parser(nick)
42
+ @nickrx = Regexp.new("^#{nick}$",'i')
48
43
  @get_prefix_cmd = Utils.make_prefix_parser(prefix)
49
-
50
- @world = world
51
-
52
- if block_given? then
53
- @block = block
54
- end
55
44
  end
56
45
 
57
- # Set a receiving proc.
46
+ # Receive generic events emitted by a protocol class and
47
+ # dispatch to an instance of Bot.
58
48
  #
59
- # If no block given, @block is set to nil and #receive is used.
60
-
61
- def set_receive &block
62
- if block_given? then
63
- @block = block
64
- else
65
- @block = nil
66
- end
67
- end
49
+ # eg the output from BeerBot::Protocol::IRC.parse .
68
50
 
69
51
  def receive event,args
70
52
 
71
- if @block then
72
- return self.instance_exec(event,*args,&@block)
73
- end
74
-
75
53
  replies = nil
76
54
 
77
55
  # Otherwise, here is the default behaviour...
78
56
 
79
57
  case event
80
58
  when :unknown
81
- replies = @bot.event(event,args:args)
59
+ replies = @bot.event(event,args:args,config:@config)
82
60
  when :default
83
- replies = @bot.event(event,args:args)
61
+ replies = @bot.event(event,args:args,config:@config)
84
62
 
85
63
  when :nick
86
64
  old,nick = args
87
- @world.nick(old,nick) if @world
88
- replies = @bot.event(event,old:old,nick:nick)
65
+ replies = @bot.event(event,old:old,nick:nick,config:@config)
89
66
 
90
67
  when :quit
91
68
  nick,msg = args
92
- @world.quit(nick) if @world
93
- replies = @bot.event(event,nick:nick,msg:msg)
69
+ replies = @bot.event(event,nick:nick,msg:msg,config:@config)
94
70
  when :part
95
71
  nick,channel = args
96
- @world.part(nick,channel) if @world
97
- replies = @bot.event(event,nick:nick,channel:channel)
72
+ replies = @bot.event(event,nick:nick,channel:channel,config:@config)
98
73
  when :join
99
74
  nick,channel = args
100
75
  me = (@nickrx === nick)
101
- @world.join(nick,channel) if @world
102
- replies = @bot.event(event,me:me,nick:nick,channel:channel)
76
+ replies = @bot.event(event,me:me,nick:nick,channel:channel,config:@config)
103
77
  when :chanlist
104
78
  channel,users = args
105
- if @world then
106
- users.each {|user|
107
- @world.join(user,channel)
108
- }
109
- end
110
- replies = @bot.event(event,channel:channel,users:users)
79
+ replies = @bot.event(event,channel:channel,users:users,config:@config)
111
80
  when :chanlistend
112
81
  # ignore
113
82
 
114
83
  when :action
115
84
  from,to,action = args
116
85
  me = (@nickrx === to)
117
- replies = @bot.action(action,from:from,to:to,me:me,world:world)
86
+ replies = @bot.action(action,from:from,to:to,me:me,config:@config)
118
87
 
119
88
  when :msg
120
89
  from,to,msg = args
@@ -131,9 +100,6 @@ module BeerBot
131
100
 
132
101
  if cmd then
133
102
  case cmd
134
- # dispatch more-filtering...
135
- when /^more!*|moar!*$/i
136
- replies = @more.more(to)
137
103
  # dispatch help...
138
104
  when /^\s*help(?:\s+(.*))?$/
139
105
  if $1.nil? then
@@ -141,14 +107,14 @@ module BeerBot
141
107
  else
142
108
  args = $1.strip.split(/\s+/)
143
109
  end
144
- replies = @bot.help(args,from:from,to:to,me:me,world:world)
110
+ replies = @bot.help(args,from:from,to:to,me:me,config:@config)
145
111
  # dispatch cmd...
146
112
  else
147
- replies = @bot.cmd(cmd,from:from,to:to,me:me,world:world)
113
+ replies = @bot.cmd(cmd,from:from,to:to,me:me,config:@config)
148
114
  end
149
115
  else
150
116
  # We're just hearing something on a channel...
151
- replies = @bot.hear(msg,from:from,to:to,me:me,world:world)
117
+ replies = @bot.hear(msg,from:from,to:to,me:me,config:@config)
152
118
  end
153
119
 
154
120
  else
@@ -159,8 +125,7 @@ module BeerBot
159
125
  when String # assume protocol string eg irc
160
126
  replies
161
127
  when Hash,Array,Proc
162
- # more-filter the reply...
163
- replies = @more.filter(replies)
128
+ BotMsg.to_a(replies)
164
129
  else
165
130
  []
166
131
  end
@@ -7,16 +7,44 @@
7
7
 
8
8
  module BeerBot
9
9
 
10
- # Config should be loaded with the json from a config file before
10
+ # Config can be loaded with the json from a config file before
11
11
  # initialisation of the system.
12
12
  #
13
+ # Config might be a bit of a misnomer, think of this as "an
14
+ # injectable thing that contains a lot of useful information".
15
+ #
13
16
  # It should be available to things like bot modules eg
14
17
  # BeerBot::Config['datadir']
15
18
  #
16
19
 
17
- Config = {}
20
+ class Config < Hash
21
+
22
+ # Should point to 'the' instance of scheduler used by this bot.
23
+
24
+ attr_accessor :scheduler
25
+
26
+ # Should reference the Bot instance.
27
+ #
28
+ # This will allow modules to inspect the bot module list and to
29
+ # override normal cmd behaviour (using Bot#set_cmd ).
30
+
31
+ attr_accessor :bot
18
32
 
19
- class << Config
33
+ # A queue that allows users of config to enqueue outgoing bot
34
+ # msg's actively.
35
+ #
36
+ # (passive = "as response to a command via Bot#cmd").
37
+
38
+ def out
39
+ @queue ||= Queue.new
40
+ end
41
+
42
+ def initialize **kargs
43
+ # Defaults
44
+ self['cmd_prefix'] = ','
45
+ self['nick'] = 'beerbot'
46
+ self.merge(kargs)
47
+ end
20
48
 
21
49
  def load config
22
50
  self.reject!{true}
@@ -40,6 +68,11 @@ module BeerBot
40
68
 
41
69
  # Return path for module data dir -- a place where the module can
42
70
  # stash data.
71
+ #
72
+ # module_data('foo') => <datadir>/modules/foo
73
+ # module_data('foo') {
74
+ # ... set pwd to this dir...
75
+ # }
43
76
 
44
77
  def module_data name,&block
45
78
  self.validate!
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.5
4
+ version: 0.2.0.pre.1
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-05-03 00:00:00.000000000 Z
11
+ date: 2014-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -84,30 +84,23 @@ description: '["Out of the box you get an irc bot, but beerbot could be so much
84
84
  ["Daniel Bush"]]'
85
85
  email: dlb.id.au@gmail.com
86
86
  executables:
87
- - run-irc.rb
87
+ - beerbot-run-irc.rb
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
94
- - lib/BeerBot/00.utils/DataFile.rb
95
- - lib/BeerBot/00.utils/InOut.rb
96
- - lib/BeerBot/00.utils/More.rb
97
- - lib/BeerBot/00.utils/world/World.rb
98
- - lib/BeerBot/00.utils/world/IRCWorld.rb
99
- - lib/BeerBot/01.connect/IRCConnection.rb
100
- - lib/BeerBot/01.bot/botmsg.rb
101
- - lib/BeerBot/01.bot/BotModule.rb
102
- - lib/BeerBot/01.bot/Bot.rb
103
- - lib/BeerBot/01.bot/BotMsgMore.rb
104
- - lib/BeerBot/02.protocols/irc.rb
105
- - lib/BeerBot/06.dispatchers/dispatcher.rb
106
- - lib/BeerBot/70.scheduler/scheduler.rb
107
- - lib/BeerBot/Config.rb
108
- - lib/BeerBot.rb
91
+ - lib/beerbot/00.utils/utils.rb
92
+ - lib/beerbot/00.utils/InOut.rb
93
+ - lib/beerbot/01.connect/IRCConnection.rb
94
+ - lib/beerbot/01.bot/BotModule.rb
95
+ - lib/beerbot/01.bot/botmsg.rb
96
+ - lib/beerbot/01.bot/Bot.rb
97
+ - lib/beerbot/02.protocols/irc.rb
98
+ - lib/beerbot/06.dispatchers/dispatcher.rb
99
+ - lib/beerbot/70.scheduler/scheduler.rb
100
+ - lib/beerbot/Config.rb
101
+ - lib/beerbot.rb
109
102
  - lib/RunIRC.rb
110
- - bin/run-irc.rb
103
+ - bin/beerbot-run-irc.rb
111
104
  homepage: http://github.com/danielbush/BeerBot
112
105
  licenses:
113
106
  - GPL
@@ -123,9 +116,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
116
  version: '0'
124
117
  required_rubygems_version: !ruby/object:Gem::Requirement
125
118
  requirements:
126
- - - '>='
119
+ - - '>'
127
120
  - !ruby/object:Gem::Version
128
- version: '0'
121
+ version: 1.3.1
129
122
  requirements: []
130
123
  rubyforge_project:
131
124
  rubygems_version: 2.1.10
@@ -1,22 +0,0 @@
1
- require 'CronR' # For scheduler
2
-
3
- require_relative 'BeerBot/00.utils/utils'
4
- require_relative 'BeerBot/00.utils/param_expand'
5
- require_relative 'BeerBot/00.utils/sentence_expand'
6
- require_relative 'BeerBot/00.utils/DataFile'
7
- require_relative 'BeerBot/00.utils/InOut'
8
- require_relative 'BeerBot/00.utils/world/IRCWorld'
9
- require_relative 'BeerBot/01.connect/IRCConnection'
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'
16
- require_relative 'BeerBot/70.scheduler/scheduler'
17
- require_relative 'BeerBot/Config'
18
-
19
- module BeerBot
20
- module Modules
21
- end
22
- end
@@ -1,103 +0,0 @@
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
- require 'json'
9
-
10
- module BeerBot
11
-
12
- module Utils
13
-
14
- # A class that loads data from a file and allows you to access it
15
- # using the #data method.
16
- #
17
- # If the file is updated (after >=1 sec), #data will reload.
18
-
19
- class DataFile
20
-
21
- attr_reader :reloaded # true if last call to #data reloaded file
22
-
23
- def self.create! filepath
24
- File.open(filepath,'w'){}
25
- self.new(filepath)
26
- end
27
-
28
- def initialize filepath
29
- @filepath = filepath
30
- @data = File.read(filepath)
31
- @mtime = File.stat(filepath).mtime
32
- @reloaded = false
33
- end
34
-
35
- # Load data from file.
36
-
37
- def data
38
- @reloaded = false
39
- return @data unless File.exists?(@filepath)
40
- mtime = File.stat(@filepath).mtime
41
- return @data if mtime == @mtime
42
- puts "Reloading data file #{@filepath}"
43
- @mtime = mtime
44
- @data = File.read(@filepath)
45
- @reloaded = true
46
- @data
47
- end
48
-
49
- def save thing
50
- File.open(@filepath,'w') {|f|
51
- f.write(thing)
52
- }
53
- end
54
-
55
- end
56
-
57
- # Specialised DataFile that parses json.
58
-
59
- class JsonDataFile < DataFile
60
-
61
- attr_reader :json
62
-
63
- def self.create! filepath,data={}
64
- File.open(filepath,'w') {|f| f.puts(data.to_json)}
65
- self.new(filepath)
66
- end
67
-
68
- def initialize filepath
69
- super
70
- @json = JSON.parse(@data)
71
- end
72
-
73
- # Load data from file and parse as json data.
74
-
75
- def data
76
- super
77
- begin
78
- if @reloaded then
79
- json = JSON.parse(@data)
80
- @json = json
81
- end
82
- rescue => e
83
- return @json
84
- end
85
- @json
86
- end
87
-
88
- # Save thing back to file.
89
- #
90
- # Thing is assumed to be a hash or array that we call to_json on.
91
-
92
- def save thing=nil
93
- if thing.nil? then
94
- thing = @json # the thing self.data returns
95
- end
96
- super(thing.to_json)
97
- end
98
-
99
- end
100
-
101
- end
102
-
103
- end