Class: Sanguinews::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/sanguinews/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Config

Returns a new instance of Config.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/sanguinews/config.rb', line 134

def initialize(args)
  @data = {}
  @data[:filemode] = false
  @data[:recursive] = false
  @data[:files] = []

  parse_options!(args)

  # Parse options in config file
  if @data[:config] && File.exist?(File.expand_path(@data[:config]))
	config = @data[:config]
  else
	config = File.expand_path("~/.sanguinews.conf")
  end

  if File.exist?(config)
	parse_config(config)
  else
	config_gen
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



20
21
22
# File 'lib/sanguinews/config.rb', line 20

def config
  @config
end

#dataObject (readonly)

Returns the value of attribute data.



20
21
22
# File 'lib/sanguinews/config.rb', line 20

def data
  @data
end

Instance Method Details

#config_genObject



125
126
127
128
129
130
131
132
# File 'lib/sanguinews/config.rb', line 125

def config_gen
  puts "It looks like you are launching sanguinews for the first time."
  `cp #{File.expand_path(File.dirname(__FILE__)) + '/../../sample.conf'} ~/.sanguinews.conf`
  puts "Sample config was copied to your home directory."
  puts "Please edit #{File.expand_path('~/.sanguinews.conf')} in your favourite text editor."
  puts "Relaunch the application when ready."
  exit
end

#modeObject



39
40
41
# File 'lib/sanguinews/config.rb', line 39

def mode
  self.ssl ? :tls : :original
end

#parse_config(config) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/sanguinews/config.rb', line 28

def parse_config(config)
  config = ParseConfig.new(config)
  config.get_params().each do |key|
	value = config[key]
	value = true if value == 'yes'
	value = false if value == 'no'
	value = value.to_i if %w(connections article_size reconnect_delay).include? key
	@data[key.to_sym] ||= value
  end
end

#parse_options!(args) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
# File 'lib/sanguinews/config.rb', line 43

def parse_options!(args)
  # version and legal info presented to user
  banner = []
  banner << ""
  banner << "sanguinews v#{Sanguinews::VERSION}. Copyright (c) 2013-2014 Tadeus Dobrovolskij."
  banner << "Comes with ABSOLUTELY NO WARRANTY. Distributed under GPL v2 license(http://www.gnu.org/licenses/gpl-2.0.txt)."
  banner << "sanguinews is a simple nntp(usenet) binary poster. It supports multithreading and SSL. More info in README."
  banner << ""
  # option parser
  
  opt_parser = OptionParser.new do |opt|
    opt.banner = "Usage: sanguinews [OPTIONS] [DIRECTORY] | -f FILE1..[FILEX]"
    opt.separator  ""
    opt.separator  "Options"
  
    opt.on("-c", "--config CONFIG", "use different config file") do |cfg|
      @config = cfg
    end
    opt.on("-C", "--check", "check headers while uploading; slow but reliable") do
      @data[:header_check] = true
    end
    opt.on("-f", "--file FILE", "upload FILE, treat all additional parameters as files") do |file|
      @data[:filemode] = true
      @data[:files] << file if File.file?(File.expand_path(file))
    end
    opt.on("-g", "--groups GROUP_LIST", "use these groups(comma separated) for upload") do |group_list|
      @data[:groups] = group_list
    end
    opt.on("-h", "--help", "help") do
      banner.each do |msg|
        puts msg
      end
      puts opt_parser
      puts
      exit
    end
    opt.on("-p", "--password PASSWORD", "use PASSWORD as your password(overwrites config file)") do |password|
      @data[:password] = password
    end
    opt.on("-r", "--recursive", "process all files under each directory recursively") do
      @data[:recursive] = true
    end
    opt.on("-u", "--user USERNAME", "use USERNAME as your username(overwrites config file)") do |username|
      @data[:username] = username
    end
    opt.on("-v", "--verbose", "be verbose?") do
      @data[:verbose] = true
    end
    opt.on("-V", "--version", "print version information and then exit") do
      puts Sanguinews::VERSION
      exit
    end
  end
  
  begin
    opt_parser.parse!(args)
  rescue OptionParser::InvalidOption, OptionParser::MissingArgument
    puts opt_parser
    exit 1
  end
  
  # in file mode treat every additional parameter as a file
  if !args.empty? && @data[:filemode]
    args.each do |file|
      next unless File.file?(File.expand_path(file))
      @data[:files] << file.to_s
    end
  end
  
  # exit when no file list is provided
  if @data[:files].empty?
	if @data[:filemode] || args.empty?
      puts "You need to specify something to upload!"
      puts opt_parser
      exit 1
	else
	  args[0].end_with?('/') ? @data[:directory] = args[0] : @data[:directory] = args[0] + '/'
    end
  end
  
end