agile-isaac 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/isaac.gemspec +2 -2
  2. data/lib/isaac.rb +35 -5
  3. data/lib/isaac/config.rb +21 -21
  4. metadata +2 -2
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "isaac"
3
- s.version = "0.0.4"
4
- s.date = "2008-12-17"
3
+ s.version = "0.0.5"
4
+ s.date = "2008-12-18"
5
5
  s.summary = "The smallish DSL for writing IRC bots"
6
6
  s.email = "mike@cryingwhilecoding.com"
7
7
  s.homepage = "http://github.com/agile/isaac"
@@ -1,6 +1,7 @@
1
1
  require 'socket'
2
- require 'isaac/config'
3
2
  require 'logger'
3
+ $:.unshift File.dirname(__FILE__)
4
+ require 'isaac/config'
4
5
 
5
6
  module Isaac
6
7
 
@@ -18,10 +19,13 @@ module Isaac
18
19
  end
19
20
 
20
21
  def self.logger
21
- @logger ||= Logger.new(Config.log_file || "isaac.log")
22
+ unless @logger
23
+ @logger = Logger.new(Config.log_file || "isaac.log")
24
+ logger.datetime_format = "%Y-%m-%d %H:%M:%S"
25
+ end
26
+ @logger
22
27
  end
23
28
 
24
- #Config = Struct.new(:nick, :server, :port, :server_pass, :username, :realname, :version, :verbose, :nick_pass)
25
29
 
26
30
  # These are top level methods you use to construct your bot.
27
31
  class Application
@@ -113,8 +117,34 @@ module Isaac
113
117
  end
114
118
  end
115
119
 
120
+ def required_config
121
+ %w(nick server port username realname)
122
+ end
123
+
124
+ def have_required_config?
125
+ required_config.all? do |r|
126
+ !config[r].nil?
127
+ end
128
+ end
129
+
116
130
  def connect
117
131
  begin
132
+ unless have_required_config?
133
+ msg = "Required configuration data is missing: #{required_config.delete_if {|c| !config[c].nil? }.join(', ')}\n"
134
+ msg += "you need to either add them to your config.yml or in a config block, eg:\n"
135
+ msg +=<<-CODE
136
+ config do |c|
137
+ c.nick = 'foo'
138
+ c.server = 'irc.example.com'
139
+ c.port = 6667
140
+ c.username = 'foo'
141
+ c.realname = 'foo'
142
+ end
143
+ CODE
144
+ puts msg
145
+ logger.error msg
146
+ exit
147
+ end
118
148
  logger.info "Connecting to #{config.server} at port #{config.port}"
119
149
  @irc = TCPSocket.open(config.server, config.port)
120
150
  logger.info "Connection established."
@@ -130,7 +160,7 @@ module Isaac
130
160
  handle line
131
161
  end
132
162
  rescue Interrupt => e
133
- puts "Disconnected! An error occurred: #{e.inspect}"
163
+ logger.info "Disconnected! An error occurred: #{e.inspect}"
134
164
  #rescue Timeout::Error => e
135
165
  # puts "Timeout: #{e}. Reconnecting."
136
166
  # connect
@@ -144,7 +174,7 @@ module Isaac
144
174
 
145
175
  # This is one hell of a nasty method. Something should be done, I suppose.
146
176
  def handle(line)
147
- puts "> #{line}" if config.verbose
177
+ logger.info "> #{line}" if config.verbose
148
178
 
149
179
  case line
150
180
  when /^:(\S+)!\S+ PRIVMSG \S+ :?\001VERSION\001/
@@ -1,3 +1,5 @@
1
+ require 'yaml'
2
+
1
3
  module Isaac
2
4
  module Config
3
5
  class << self
@@ -13,11 +15,11 @@ module Isaac
13
15
 
14
16
  def config(network=nil)
15
17
  network ||= default_network
16
- @data.config[network] || {}
18
+ @data && @data.config[network] || {}
17
19
  end
18
20
 
19
21
  def networks
20
- @data.networks
22
+ @data && @data.networks || []
21
23
  end
22
24
 
23
25
  def default_network
@@ -36,6 +38,14 @@ module Isaac
36
38
  @dirty = bool
37
39
  end
38
40
 
41
+ def [](arg)
42
+ config[arg]
43
+ end
44
+
45
+ def []=(arg,val)
46
+ config[arg] = val
47
+ end
48
+
39
49
  def write_config(filename=nil)
40
50
  File.open(filename || data.config_filename, 'w') do |file|
41
51
  file << data.to_yaml
@@ -55,9 +65,9 @@ module Isaac
55
65
  class Data
56
66
  attr_accessor :config, :config_filename
57
67
 
58
- def initialize(config=nil)
59
- self.config = read_config(config)
60
- self.config_filename = config || default_config_file
68
+ def initialize(filename=nil)
69
+ self.config_filename = filename || default_config_file
70
+ @config = read_config(config_filename)
61
71
  end
62
72
 
63
73
  def config_dir
@@ -72,28 +82,18 @@ module Isaac
72
82
  config.keys
73
83
  end
74
84
 
75
- def read_config(config=nil)
76
- begin
77
- require 'erb'
78
- require 'yaml'
79
- rescue LoadError
80
- retry if require 'rubygems'
81
- end
82
- begin
83
- config ||= config_filename
84
- self.config_filename = config unless config == config_filename
85
- YAML::load(ERB.new(IO.read(config)).result)
86
- rescue
87
- {"default" => {}}
88
- end
85
+ def read_config(filename=nil)
86
+ self.config_filename = filename if filename
87
+ Isaac.logger.debug "Loading config from #{config_filename}"
88
+ YAML.load_file(config_filename) rescue {"default" => {}}
89
89
  end
90
90
 
91
91
  def to_yaml
92
92
  config.to_yaml
93
93
  end
94
94
 
95
- def self.load(config=nil)
96
- Isaac::Config.data = new(config)
95
+ def self.load(filename=nil)
96
+ Isaac::Config.data = new(filename)
97
97
  end
98
98
  end
99
99
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: agile-isaac
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harry Vangberg
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2008-12-17 00:00:00 -08:00
13
+ date: 2008-12-18 00:00:00 -08:00
14
14
  default_executable:
15
15
  dependencies: []
16
16