Class: WCC::Conf

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/wcc.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConf

Returns a new instance of Conf.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/wcc.rb', line 80

def initialize
	@options = {}
	
	OptionParser.new do |opts|
		opts.banner =  "Usage: ruby wcc.rb [options] [config-yaml-file]"
		opts.banner += "\nOptions:\n"
		opts.on('-v', '--verbose', 'Output more information') do self[:verbose] = true end
		opts.on('-d', '--debug', 'Enable debug mode') do self[:debug] = true end
		opts.on('--cache-dir DIR', 'Save hash and diff files to DIR') do |dir| self[:cache_dir] = dir end
		opts.on('-s', '--simulate', 'Check for update but do not save hash or diff files') do self[:simulate] = true end
		opts.on('--clean', 'Remove all saved hash and diff files') do self[:clean] = true end
		opts.on('-t', '--tag TAG', 'Set TAG used in output') do |t| self[:tag] = t end
		opts.on('-n', '--no-mails', 'Do not send any emails') do self[:nomails] = true end
		opts.on('-f', '--from MAIL', 'Set From: mail address') do |m| self[:from_mail] = m end
		opts.on('--host HOST', 'Set SMTP host') do |h| self[:host] = h end
		opts.on('--port PORT', 'Set SMTP port') do |p| self[:port] = p end
		#opts.on('--init', '--initialize')
		opts.on('--show-config', 'Show config after loading config file (debug purposes)') do self[:show_config] = true end
		opts.on('-h', '-?', '--help', 'Display this screen') do
			puts opts
			exit
		end
	end.parse!
	
	WCC.logger.progname = 'wcc'

	# latest flag overrides everything
	WCC.logger.level = Logger::ERROR
	WCC.logger.level = Logger::INFO if self[:verbose]
	WCC.logger.level = Logger::DEBUG if self[:debug]
	
	WCC.logger.formatter = LogFormatter.new((self[:verbose] or self[:debug]))

	# main
	WCC.logger.info "No config file given, using default 'conf.yml' file" if ARGV.length == 0

	self[:conf] = ARGV[0] || 'conf.yml'
	
	if !File.exists?(self[:conf])
		WCC.logger.fatal "Config file '#{self[:conf]}' does not exist!"
		exit 1
	end
	
	WCC.logger.debug "Load config from '#{self[:conf]}'"
	
	# may be false if file is empty
	yaml = YAML.load_file(self[:conf])
	if yaml.is_a?(Hash) and (yaml = yaml['conf']).is_a?(Hash)
		@options[:from_mail] ||= yaml['from_addr']
		@options[:cache_dir] ||= yaml['cache_dir']
		@options[:tag] ||= yaml['tag']
		@options[:syslog] ||= yaml['use_syslog']
		@options[:filter_dir] ||= yaml['filterd']
		@options[:template_dir] ||= yaml['templated']
		
		if yaml['email'].is_a?(Hash)
			if yaml['email']['smtp'].is_a?(Hash)
				@options[:mailer] = 'smtp'
				@options[:smtp_host] ||= yaml['email']['smtp']['host']
				# yaml parser should provide an integer here
				@options[:smtp_port] ||= yaml['email']['smtp']['port']
			end
		end
	end
	
	if self[:from_mail].to_s.empty?
		WCC.logger.fatal "No sender mail address given! See help."
		exit 1
	end
	
	if self[:show_config]
		Conf.default.merge(@options).each do |k,v|
			puts "  #{k.to_s} => #{self[k]}"
		end
		exit 0
	end
	
	# create cache dir for hash and diff files
	Dir.mkdir(self[:cache_dir]) unless File.directory?(self[:cache_dir])
	
	if(self[:clean])
		WCC.logger.warn "Cleanup hash and diff files"
		Dir.foreach(self[:cache_dir]) do |f|
			File.delete(self.file(f)) if f =~ /^.*\.(md5|site)$/
		end
	end
	
	# read filter.d
	Dir[File.join(self[:filter_dir], '*.rb')].each { |file| require file }
	
	# attach --no-mails filter
	WCC::Filter.add '--no-mails' do |data|
		!self[:nomails]
	end
end

Class Method Details

.[](key) ⇒ Object



237
# File 'lib/wcc.rb', line 237

def self.[](key); Conf.instance[key] end

.defaultObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/wcc.rb', line 61

def self.default
	@default_conf ||= {
		:verbose => false,
		:debug => false,
		:simulate => false,
		:clean => false,
		:nomails => false,
		# when you want to use ./tmp it must be writeable
		:cache_dir => '/var/tmp/wcc',
		:tag => 'wcc',
		:syslog => false,
		:filter_dir => './filter.d',
		:template_dir => './template.d',
		:mailer => 'smtp',
		:smtp_host => 'localhost',
		:smtp_port => 25
	}
end

.file(path = nil) ⇒ Object



235
# File 'lib/wcc.rb', line 235

def self.file(path = nil) File.join(self[:cache_dir], path) end

.mailerObject



224
225
226
227
228
229
230
231
232
233
# File 'lib/wcc.rb', line 224

def self.mailer
	return @mailer unless @mailer.nil?

	# smtp mailer
	if Conf[:mailer] == 'smtp'
		@mailer = SmtpMailer.new(Conf[:smtp_host], Conf[:smtp_port])
	end

	@mailer
end

.simulate?Boolean

Returns:

  • (Boolean)


236
# File 'lib/wcc.rb', line 236

def self.simulate?; self[:simulate] end

.sitesObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/wcc.rb', line 176

def self.sites
	return @sites unless @sites.nil?
	
	@sites = []
	
	WCC.logger.debug "Load sites from '#{Conf[:conf]}'"
	
	# may be *false* if file is empty
	yaml = YAML.load_file(Conf[:conf])
	
	if not yaml
		WCC.logger.info "No sites loaded"
		return @sites
	end
	
	yaml['sites'].to_a.each do |yaml_site|
		# query --no-mails filter for every site
		frefs = [FilterRef.new('--no-mails')]
		(yaml_site['filters'] || []).each do |entry|
			if entry.is_a?(Hash)
				# hash containing only one key (filter id),
				# the value is the argument hash
				id = entry.keys[0]
				frefs << FilterRef.new(id, entry[id])
			else entry.is_a?(String)
				frefs << FilterRef.new(entry)
			end
		end
		
		if not yaml_site['cookie'].nil?
			cookie = File.open(yaml_site['cookie'], 'r') { |f| f.read }
		end
		
		@sites << Site.new(
			yaml_site['url'], 
			yaml_site['strip_html'] || true,
			yaml_site['emails'].map { |m| MailAddress.new(m) } || [],
			frefs,
			yaml_site['auth'] || {},
			cookie)
	end
	
	WCC.logger.debug @sites.length.to_s + (@sites.length == 1 ? ' site' : ' sites') + " loaded\n" +
		@sites.map { |s| "  #{s.uri.host.to_s}\n    url: #{s.uri.to_s}\n    id: #{s.id}" }.join("\n")
	
	@sites
end

Instance Method Details

#[](key) ⇒ Object

use Conf like a hash containing all options



54
55
56
# File 'lib/wcc.rb', line 54

def [](key)
	@options[key.to_sym] || Conf.default[key.to_sym]
end

#[]=(key, val) ⇒ Object



57
58
59
# File 'lib/wcc.rb', line 57

def []=(key, val)
	@options[key.to_sym] = val unless val.nil?
end