jschat 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -43,6 +43,21 @@ The web app must be run alongside the server. The web app must be started in pr
43
43
 
44
44
  The web app currently has no database dependencies, it's a wrapper that links cookies to JsChat server proxies. You can run it on port 80 by configuring Rack or an Apache proxy. I have Apache set up this way on "jschat.org":http://jschat.org.
45
45
 
46
+ h3. Configuration files
47
+
48
+ These are the default locations of the configuration files. You can override them with <code>--config=PATH</code>:
49
+
50
+ * Client: <code>~/.jschat/config.json</code>
51
+ * Server: <code>/etc/jschat/config.json</code>
52
+
53
+ The web app will use the same configuration file as the server so it can find out where the server is.
54
+
55
+ The file format is JSON, like this:
56
+
57
+ <pre>
58
+ { "port": 3001 }
59
+ </pre>
60
+
46
61
  h3. Client Commands
47
62
 
48
63
  * Change name or identify: <code>/nick name</code>
data/bin/jschat-server CHANGED
@@ -2,17 +2,8 @@
2
2
 
3
3
  require 'logger'
4
4
  require 'jschat/server'
5
-
6
- logger = Logger.new(STDERR)
7
- logger = Logger.new(STDOUT)
8
-
9
- ServerConfig = {
10
- :port => 6789,
11
- :ip => '0.0.0.0',
12
- :logger => logger,
13
- :max_message_length => 500
14
- }
5
+ require 'jschat/server-options'
15
6
 
16
7
  EM.run do
17
- EM.start_server ServerConfig[:ip], ServerConfig[:port], JsChat
8
+ EM.start_server ServerConfig['ip'], ServerConfig['port'], JsChat
18
9
  end
data/lib/jschat/client.rb CHANGED
@@ -6,13 +6,14 @@ require 'optparse'
6
6
  require 'time'
7
7
 
8
8
  options = {}
9
-
9
+ default_config_file = '~/.jschat/config.json'
10
10
  ARGV.clone.options do |opts|
11
11
  script_name = File.basename($0)
12
12
  opts.banner = "Usage: #{$0} [options]"
13
13
 
14
14
  opts.separator ""
15
15
 
16
+ opts.on("-c", "--config=PATH", String, "Configuration file location (#{default_config_file})") { |o| options['config'] = o }
16
17
  opts.on("-h", "--hostname=host", String, "JsChat server hostname") { |o| options['hostname'] = o }
17
18
  opts.on("-p", "--port=port", String, "JsChat server port number") { |o| options['port'] = o }
18
19
  opts.on("-r", "--room=#room", String, "Channel to auto-join: remember to escape the hash") { |o| options['room'] = o }
@@ -22,6 +23,18 @@ ARGV.clone.options do |opts|
22
23
  opts.parse!
23
24
  end
24
25
 
26
+ # Command line options will overrides these
27
+ def load_options(path)
28
+ path = File.expand_path path
29
+ if File.exists? path
30
+ JSON.parse(File.read path)
31
+ else
32
+ {}
33
+ end
34
+ end
35
+
36
+ options = load_options(options['config'] || default_config_file).merge options
37
+
25
38
  ClientConfig = {
26
39
  :port => options['port'] || '6789',
27
40
  :ip => options['hostname'] || '0.0.0.0',
@@ -3,16 +3,12 @@ require 'sinatra'
3
3
  require 'sha1'
4
4
  require 'json'
5
5
  require 'sprockets'
6
+ require 'jschat/server-options'
6
7
 
7
8
  set :public, File.join(File.dirname(__FILE__), 'public')
8
9
  set :views, File.join(File.dirname(__FILE__), 'views')
9
10
 
10
11
  module JsChat
11
- Config = {
12
- :ip => '0.0.0.0',
13
- :port => 6789
14
- }
15
-
16
12
  class ConnectionError < Exception ; end
17
13
  end
18
14
 
@@ -90,7 +86,7 @@ class JsChat::Bridge
90
86
  def send_json(h, get_results = true)
91
87
  response = nil
92
88
  h[:cookie] = @cookie if cookie_set?
93
- c = TCPSocket.open(JsChat::Config[:ip], JsChat::Config[:port])
89
+ c = TCPSocket.open(ServerConfig['ip'], ServerConfig['port'])
94
90
  c.send(h.to_json + "\n", 0)
95
91
  if get_results
96
92
  response = c.gets
@@ -0,0 +1,45 @@
1
+ require 'optparse'
2
+
3
+ logger = nil
4
+
5
+ if Object.const_defined? :Logger
6
+ logger = Logger.new(STDERR)
7
+ logger = Logger.new(STDOUT)
8
+ end
9
+
10
+ ServerConfigDefaults = {
11
+ 'port' => 6789,
12
+ 'ip' => '0.0.0.0',
13
+ 'logger' => logger,
14
+ 'max_message_length' => 500
15
+ }
16
+
17
+ # Command line options will overrides these
18
+ def load_options(path)
19
+ path = File.expand_path path
20
+ if File.exists? path
21
+ JSON.parse(File.read path)
22
+ else
23
+ {}
24
+ end
25
+ end
26
+
27
+ options = {}
28
+ default_config_file = '/etc/jschat/config.json'
29
+
30
+ ARGV.clone.options do |opts|
31
+ script_name = File.basename($0)
32
+ opts.banner = "Usage: #{$0} [options]"
33
+
34
+ opts.separator ""
35
+
36
+ opts.on("-c", "--config=PATH", String, "Configuration file location (#{default_config_file}") { |o| options['config'] = o }
37
+ opts.on("-p", "--port=port", String, "Port number") { |o| options['port'] = o }
38
+ opts.on("--help", "-H", "This text") { puts opts; exit 0 }
39
+
40
+ opts.parse!
41
+ end
42
+
43
+ options = load_options(options['config'] || default_config_file).merge options
44
+
45
+ ServerConfig = ServerConfigDefaults.merge options
data/lib/jschat/server.rb CHANGED
@@ -388,11 +388,11 @@ module JsChat
388
388
  end
389
389
 
390
390
  def log(level, message)
391
- if Object.const_defined? :ServerConfig and ServerConfig[:logger]
391
+ if Object.const_defined? :ServerConfig and ServerConfig['logger']
392
392
  if @user
393
393
  message = "#{@user.name} (#{@user.ip}): #{message}"
394
394
  end
395
- ServerConfig[:logger].send level, message
395
+ ServerConfig['logger'].send level, message
396
396
  end
397
397
  end
398
398
 
@@ -434,7 +434,7 @@ module JsChat
434
434
  response = ''
435
435
  disconnect_lagged_users
436
436
 
437
- if data and data.size > ServerConfig[:max_message_length]
437
+ if data and data.size > ServerConfig['max_message_length']
438
438
  raise JsChat::Errors::MessageTooLong.new(:message_too_long, 'Message too long')
439
439
  end
440
440
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Alex R. Young
@@ -183,6 +183,7 @@ files:
183
183
  - lib/jschat/http/views/iphone.erb
184
184
  - lib/jschat/http/views/layout.erb
185
185
  - lib/jschat/http/views/message_form.erb
186
+ - lib/jschat/server-options.rb
186
187
  - lib/jschat/server.rb
187
188
  - test/server_test.rb
188
189
  - test/stateless_test.rb