cinchize 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/LICENSE +1 -1
  2. data/README.rdoc +15 -5
  3. data/VERSION +1 -1
  4. data/cinchize.gemspec +56 -0
  5. data/lib/cinchize.rb +82 -25
  6. metadata +4 -3
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009 Victor Bergöö
1
+ Copyright (c) 2010 Victor Bergöö
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.rdoc CHANGED
@@ -2,17 +2,25 @@
2
2
 
3
3
  An early version of program which purpose is to daemonize Cinch ircbots from "simple" json config-files.
4
4
 
5
+ == Installation
6
+
7
+ gem install cinchize
8
+
5
9
  == Usage
6
10
 
7
11
  Basic
8
12
 
9
- cinchize start -- server
13
+ cinchize network
14
+
15
+ The config file can either be in the current working directory or in /etc, should be called cinchize.json, i.e. /etc/cinchize.json, use -s to look for the file in /etc
16
+
17
+ cinchize -s network
10
18
 
11
- Specify a config file, if no file is specified the cinchize is looking for it in /etc/cinchize.json
19
+ To daemonzize, use -d
12
20
 
13
- cinchize start -- -f config.json server
21
+ cinchize -d network
14
22
 
15
- Server is the name you call the a server config in the servers part of the config file, i.e. "freenode", "my_cute_bot", "quakenet" or similar.
23
+ Network is the name you call the a server config in the servers part of the config file, i.e. "freenode", "my_cute_bot", "quakenet" or similar.
16
24
 
17
25
  == Config-file
18
26
 
@@ -34,7 +42,9 @@ All config options can be skipped and the bot will use the defaults, but it coul
34
42
  {
35
43
  "module" : "plugin-module",
36
44
  "class" : "Cinch::Plugins::User::SomePlugin",
37
- "options" : { }
45
+ "options" : {
46
+ "foo" : "bar"
47
+ }
38
48
  }
39
49
  ]
40
50
  }
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
data/cinchize.gemspec ADDED
@@ -0,0 +1,56 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{cinchize}
8
+ s.version = "0.0.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Victor Bergöö"]
12
+ s.date = %q{2010-10-31}
13
+ s.default_executable = %q{cinchize}
14
+ s.description = %q{Create dynamic Cinch IRC-bots and daemonize them, without the need of writing any code}
15
+ s.email = %q{victor.bergoo@gmail.com}
16
+ s.executables = ["cinchize"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".gitignore",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "bin/cinchize",
28
+ "cinchize.gemspec",
29
+ "lib/cinchize.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/netfeed/cinchize}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.7}
35
+ s.summary = %q{Create dynamic Cinch IRC-bots and daemonize them}
36
+
37
+ if s.respond_to? :specification_version then
38
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_runtime_dependency(%q<cinch>, [">= 0"])
43
+ s.add_runtime_dependency(%q<daemons>, [">= 0"])
44
+ s.add_runtime_dependency(%q<json>, [">= 0"])
45
+ else
46
+ s.add_dependency(%q<cinch>, [">= 0"])
47
+ s.add_dependency(%q<daemons>, [">= 0"])
48
+ s.add_dependency(%q<json>, [">= 0"])
49
+ end
50
+ else
51
+ s.add_dependency(%q<cinch>, [">= 0"])
52
+ s.add_dependency(%q<daemons>, [">= 0"])
53
+ s.add_dependency(%q<json>, [">= 0"])
54
+ end
55
+ end
56
+
data/lib/cinchize.rb CHANGED
@@ -8,22 +8,53 @@ $LOAD_PATH.unshift(dir) unless $LOAD_PATH.include? dir
8
8
  require 'cinch'
9
9
  require 'daemons'
10
10
  require 'json'
11
+ require 'optparse'
11
12
 
12
13
  module Cinchize
14
+ Options = {
15
+ :ontop => true,
16
+ :system => false,
17
+ :local_config => File.join(Dir.pwd, 'cinchize.json'),
18
+ :system_config => '/etc/cinchize.json',
19
+ }
20
+
13
21
  def self.run
14
- daemon_options, ntw, plugins, plugin_options = config
15
- Daemons.run_proc(daemon_options[:app_name], daemon_options) do
22
+ options = Options.dup
23
+
24
+ ARGV.options do |o|
25
+ o.set_summary_indent ' '
26
+ o.banner = "Usage: #{File.basename $0} [Options] network"
27
+
28
+ o.on("-d", "--daemonize", "Daemonize") {
29
+ options[:ontop] = false
30
+ }
31
+
32
+ o.on("-s", "--system-config", "Use system config") {
33
+ options[:system] = true
34
+ }
35
+
36
+ o.parse!
37
+ end
38
+
39
+ d_options, network, plugins, plugin_options = config(options, ARGV.first)
40
+ daemon = Daemons::ApplicationGroup.new(d_options[:app_name], {
41
+ :ontop => d_options[:ontop],
42
+ :dir => d_options[:dir],
43
+ :dir_mode => d_options[:dir_mode]
44
+ })
45
+ app = daemon.new_application :mode => :none, :log_output => d_options[:log_output]
46
+ app.start
47
+
48
+ loop do
16
49
  bot = Cinch::Bot.new do
17
50
  configure do |c|
18
- ntw.keys.each do |key|
19
- c.send("#{key}=".to_sym, ntw[key])
20
- end
21
-
51
+ network.keys.each { |key| c.send("#{key}=".to_sym, network[key]) }
52
+
22
53
  c.plugins.plugins = plugins
23
54
  c.plugins.options = plugin_options
24
55
  end
25
56
  end
26
-
57
+
27
58
  bot.start
28
59
  end
29
60
  rescue ArgumentError => e
@@ -31,24 +62,14 @@ module Cinchize
31
62
  exit 1
32
63
  end
33
64
 
34
- def self.config
65
+ def self.config options, network
35
66
  cmd_options = []
36
67
 
37
- idx = ARGV.index "--"
38
- unless idx.nil?
39
- cmd_options = ARGV.dup.slice(idx+1..-1)
40
- end
68
+ config_file = options[:system] ? options[:system_config]: options[:local_config]
41
69
 
42
- config_file = "/etc/cinchize.json"
43
- unless cmd_options.index("-f").nil?
44
- config_file = cmd_options[cmd_options.index("-f") + 1]
45
- end
46
-
47
- raise ArgumentError.new "the config file doesn't exist" unless File.exists? config_file
48
- raise ArgumentError.new "needs a network" if cmd_options[-1].nil?
70
+ raise ArgumentError.new "the config file #{config_file} doesn't exist" unless File.exists? config_file
71
+ raise ArgumentError.new "needs a network" if network.nil? or network.empty?
49
72
 
50
- network = cmd_options[-1]
51
-
52
73
  cfg = JSON.parse File.open(config_file, "r").read()
53
74
 
54
75
  raise ArgumentError.new "there's no server config in the config file" unless cfg.has_key? "servers"
@@ -70,7 +91,7 @@ module Cinchize
70
91
  plugin["class"].split("::").inject(Object) { |m,n| clazz = m.const_get(n) }
71
92
  plugins << clazz
72
93
 
73
- plugin_options[plugin["class"]] = plugin["options"] || {}
94
+ plugin_options[clazz] = plugin["options"] || {}
74
95
  rescue LoadError => e
75
96
  puts "error while loading the module: #{e}"
76
97
  rescue NameError => e
@@ -80,16 +101,52 @@ module Cinchize
80
101
 
81
102
  raise ArgumentError.new "no plugins loaded" if plugins.size == 0
82
103
 
83
- dir_mode = cfg["options"]["dir_mode"].nil? ? "normal" : cfg["options"]["dir_mode"]
84
-
85
104
  cfg["options"] ||= {}
105
+ dir_mode = cfg["options"]["dir_mode"].nil? ? "normal" : cfg["options"]["dir_mode"]
106
+
86
107
  daemon_options = {
87
108
  :dir_mode => dir_mode.to_sym,
88
109
  :dir => cfg["options"]["dir"] || Dir.getwd,
89
110
  :log_output => cfg["options"]["log_output"] || false,
90
- :app_name => "cinchize_#{network}"
111
+ :app_name => "cinchize_#{network}",
112
+ :ontop => options[:ontop],
91
113
  }
92
114
 
93
115
  [daemon_options, ntw, plugins, plugin_options]
94
116
  end
95
117
  end
118
+
119
+ # We need to open up Daemons::Application#start_none so we can log the output
120
+ # The original code can be found at:
121
+ # => http://github.com/ghazel/daemons/blob/master/lib/daemons/application.rb#L60
122
+ module Daemons
123
+ class Application
124
+ def start_none
125
+ unless options[:ontop]
126
+ Daemonize.daemonize(output_logfile, @group.app_name)
127
+ else
128
+ Daemonize.simulate
129
+ end
130
+
131
+ @pid.pid = Process.pid
132
+
133
+ at_exit {
134
+ begin; @pid.cleanup; rescue ::Exception; end
135
+ if options[:backtrace] and not options[:ontop] and not $daemons_sigterm
136
+ begin; exception_log(); rescue ::Exception; end
137
+ end
138
+ }
139
+ trap(SIGNAL) {
140
+ begin; @pid.cleanup; rescue ::Exception; end
141
+ $daemons_sigterm = true
142
+
143
+ if options[:hard_exit]
144
+ exit!
145
+ else
146
+ exit
147
+ end
148
+ }
149
+ end
150
+ end
151
+ end
152
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Victor Berg\xC3\xB6\xC3\xB6"
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-24 00:00:00 +02:00
17
+ date: 2010-10-31 00:00:00 +02:00
18
18
  default_executable: cinchize
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -72,6 +72,7 @@ files:
72
72
  - Rakefile
73
73
  - VERSION
74
74
  - bin/cinchize
75
+ - cinchize.gemspec
75
76
  - lib/cinchize.rb
76
77
  has_rdoc: true
77
78
  homepage: http://github.com/netfeed/cinchize