mad_chatter 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/.gitignore +2 -0
  2. data/.travis.yml +12 -0
  3. data/README.md +3 -5
  4. data/bin/mad_chatter +1 -1
  5. data/lib/mad_chatter.rb +22 -4
  6. data/lib/mad_chatter/config.rb +1 -6
  7. data/lib/mad_chatter/message.rb +5 -3
  8. data/lib/mad_chatter/message_listeners/channel_commands.rb +53 -0
  9. data/lib/mad_chatter/message_listeners/code.rb +0 -3
  10. data/lib/mad_chatter/message_listeners/markdown.rb +11 -5
  11. data/lib/mad_chatter/message_listeners/nick.rb +1 -1
  12. data/lib/mad_chatter/user.rb +1 -7
  13. data/lib/mad_chatter/version.rb +1 -1
  14. data/mad_chatter.gemspec +8 -7
  15. data/spec/mad_chatter_spec.rb +17 -0
  16. data/spec/message_listeners/{join_spec.rb → channel_commands_spec.rb} +17 -5
  17. data/spec/message_listeners/code_spec.rb +1 -1
  18. data/spec/message_listeners/markdown_spec.rb +12 -8
  19. data/spec/message_spec.rb +13 -3
  20. data/spec/spec_helper.rb +4 -7
  21. data/templates/web/css/mad_chatter.css +1 -1
  22. data/templates/web/index.html +35 -7
  23. data/templates/web/js/mad_chatter.js +33 -22
  24. data/templates/web/js/mad_chatter_config.js +1 -1
  25. metadata +28 -53
  26. data/Gemfile.lock +0 -53
  27. data/lib/mad_chatter/actions/base.rb +0 -26
  28. data/lib/mad_chatter/actions/dsl.rb +0 -38
  29. data/lib/mad_chatter/actions/join.rb +0 -23
  30. data/lib/mad_chatter/actions/rename.rb +0 -24
  31. data/lib/mad_chatter/connection.rb +0 -29
  32. data/lib/mad_chatter/markdown_renderer.rb +0 -16
  33. data/lib/mad_chatter/message_listeners/join.rb +0 -30
  34. data/lib/mad_chatter/servers/juggernaut.rb +0 -9
  35. data/lib/mad_chatter/servers/websocket_rack.rb +0 -9
  36. data/lib/mad_chatter/users.rb +0 -40
  37. data/spec/actions_spec.rb +0 -7
  38. data/spec/server_spec.rb +0 -43
  39. data/spec/users_spec.rb +0 -23
  40. data/templates/web/css/styles.css +0 -166
data/.gitignore CHANGED
@@ -4,3 +4,5 @@ pkg/*
4
4
  .daemon
5
5
  coverage/
6
6
  .project
7
+ Gemfile.lock
8
+
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ # - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ # - jruby-18mode # JRuby in 1.8 mode
7
+ # - jruby-19mode # JRuby in 1.9 mode
8
+ # - rbx-18mode
9
+ - rbx-19mode
10
+ # - ree
11
+ script: bundle exec rspec spec
12
+
data/README.md CHANGED
@@ -77,17 +77,15 @@ Once you've got your chat server running and being used by other people, you mig
77
77
 
78
78
  ## Getting Help / Providing Feedback
79
79
 
80
- Feel free to submit a GitHub issue, or post to the [Google Group](https://groups.google.com/group/mad-chatter), or send me a message on GitHub.
80
+ Feel free to submit bug reports and feature requests to our [GitHub Issues page](https://github.com/andrewhavens/mad_chatter/issues), or post to the [Google Group](https://groups.google.com/group/mad-chatter), or send me a message on GitHub.
81
81
 
82
82
  You can also vote on upcoming features: https://madchatter.uservoice.com
83
83
 
84
84
  ## Contributing
85
85
 
86
- Please fork and send pull requests! Or submit issues if you have suggestions on how to improve.
87
-
88
- ## TODO
86
+ [![Build Status](https://secure.travis-ci.org/andrewhavens/mad_chatter.png)](http://travis-ci.org/andrewhavens/mad_chatter)
89
87
 
90
- - Maybe add support for alternative web socket servers like Juggernaut, Socket.io, or Cramp and web-socket-js
88
+ Please fork and send pull requests! Or submit issues if you have suggestions on how to improve.
91
89
 
92
90
  ##Copyright
93
91
 
data/bin/mad_chatter CHANGED
@@ -11,7 +11,7 @@ module MadChatter
11
11
  File.expand_path('../../', __FILE__)
12
12
  end
13
13
 
14
- desc "new [name]", "Generates a new Mad Chatter chatroom application"
14
+ desc "new [name]", "Generates a new Mad Chatter application"
15
15
  def new(name)
16
16
  copy_file "templates/config.yml", "#{name}/config.yml"
17
17
  copy_file "templates/extensions.rb", "#{name}/extensions.rb"
data/lib/mad_chatter.rb CHANGED
@@ -14,13 +14,12 @@ require 'mad_chatter/servers/em_websocket'
14
14
 
15
15
  require 'mad_chatter/channel'
16
16
  require 'mad_chatter/user'
17
- require 'mad_chatter/users'
18
17
 
19
18
  require 'mad_chatter/message'
20
19
  require 'mad_chatter/message_history'
21
20
 
22
21
  require 'mad_chatter/actions'
23
- require 'mad_chatter/message_listeners/join'
22
+ require 'mad_chatter/message_listeners/channel_commands'
24
23
  require 'mad_chatter/message_listeners/markdown'
25
24
  require 'mad_chatter/message_listeners/nick'
26
25
  require 'mad_chatter/message_listeners/image'
@@ -31,8 +30,6 @@ require 'mad_chatter/message_listener'
31
30
 
32
31
  module MadChatter
33
32
 
34
- # attr_accessor :users, :channels, :message_listeners
35
-
36
33
  class << self
37
34
 
38
35
  def users
@@ -47,6 +44,7 @@ module MadChatter
47
44
  users.each do |user|
48
45
  return user if user.token == token
49
46
  end
47
+ return nil
50
48
  end
51
49
 
52
50
  def channels
@@ -61,12 +59,14 @@ module MadChatter
61
59
  channels.each do |channel|
62
60
  return channel if channel.id == id
63
61
  end
62
+ return nil
64
63
  end
65
64
 
66
65
  def find_channel_by_name(name)
67
66
  channels.each do |channel|
68
67
  return channel if channel.name == name
69
68
  end
69
+ return nil
70
70
  end
71
71
 
72
72
  def message_listeners
@@ -85,6 +85,7 @@ module MadChatter
85
85
 
86
86
  def message_received(json)
87
87
  msg = JSON.parse(json)
88
+ # puts 'received: ' + msg['message']
88
89
 
89
90
  if msg['token'].nil?
90
91
  return # Token is required to send messages
@@ -108,5 +109,22 @@ module MadChatter
108
109
  end
109
110
  end
110
111
 
112
+ def send_channels_list
113
+ json = channels_list
114
+ users.each do |user|
115
+ user.send(json)
116
+ end
117
+ end
118
+
119
+ def channels_list
120
+ channels_hash = channels.collect do |c|
121
+ {:id => c.id, :name => c.name }
122
+ end
123
+ JSON.generate({
124
+ type: 'channels',
125
+ json: channels_hash,
126
+ })
127
+ end
128
+
111
129
  end
112
130
  end
@@ -24,14 +24,9 @@ module MadChatter
24
24
  end
25
25
 
26
26
  def init_default_message_listeners
27
- %w{ Join Nick Image Code Markdown }.each do |listener|
27
+ %w{ ChannelCommands Nick Image Code Markdown }.each do |listener|
28
28
  MadChatter.message_listeners << Object.const_get('MadChatter').const_get('MessageListeners').const_get(listener).new
29
29
  end
30
- # MadChatter.message_listeners << MadChatter::MessageListeners::Join.new
31
- # MadChatter.message_listeners << MadChatter::MessageListeners::Nick.new
32
- # MadChatter.message_listeners << MadChatter::MessageListeners::Image.new
33
- # MadChatter.message_listeners << MadChatter::MessageListeners::Code.new
34
- # MadChatter.message_listeners << MadChatter::MessageListeners::Markdown.new
35
30
  end
36
31
 
37
32
  def init_extensions
@@ -3,9 +3,9 @@ require 'cgi'
3
3
  module MadChatter
4
4
  class Message
5
5
 
6
- attr_accessor :type, :original_text, :filtered_text, :html, :token, :channel, :growl, :add_to_history
6
+ attr_accessor :type, :original_text, :filtered_text, :html, :token, :channel, :growl, :add_to_history, :timestamp
7
7
 
8
- def initialize(type, text = nil, token = nil, channel = nil)
8
+ def initialize(type, text = nil, token = nil, channel_id = nil)
9
9
  @type = type
10
10
  if text
11
11
  @original_text = text
@@ -14,8 +14,9 @@ module MadChatter
14
14
  @growl = text
15
15
  end
16
16
  @token = token
17
- @channel = channel
17
+ @channel = channel_id
18
18
  @add_to_history = true
19
+ @timestamp = Time.now.to_i
19
20
  end
20
21
 
21
22
  def username=(username)
@@ -48,6 +49,7 @@ module MadChatter
48
49
  username: username,
49
50
  channel: @channel,
50
51
  growl: @growl,
52
+ time: @timestamp
51
53
  })
52
54
  end
53
55
 
@@ -0,0 +1,53 @@
1
+ module MadChatter
2
+ module MessageListeners
3
+ class ChannelCommands
4
+
5
+ include MadChatter::Actions
6
+
7
+ def handle(message)
8
+ @message = message
9
+ case @message.text
10
+ when '/join'
11
+ join_channel
12
+ stop_message_handling
13
+ when %r{^/channel create}
14
+ create_channel
15
+ stop_message_handling
16
+ when %r{^/channel rename}
17
+ rename_channel
18
+ stop_message_handling
19
+ end
20
+ end
21
+
22
+ def join_channel
23
+ user = MadChatter.find_user_by_token(@message.token)
24
+
25
+ unless user && @message.channel
26
+ stop_message_handling # user should already exist, and channel id is required
27
+ end
28
+
29
+ channel = MadChatter.find_channel_by_id(@message.channel)
30
+
31
+ unless channel
32
+ stop_message_handling # you cant join a channel that doesnt exist
33
+ end
34
+
35
+ channel.add_user(user)
36
+ end
37
+
38
+ def create_channel
39
+ channel_name = %r{^/channel create (.+)}.match(@message.text).captures[0]
40
+ MadChatter.channels << MadChatter::Channel.new(channel_name)
41
+ MadChatter.send_channels_list
42
+ end
43
+
44
+ def rename_channel
45
+ channel_name = %r{^/channel rename (.+)}.match(@message.text).captures[0]
46
+ channel = MadChatter.find_channel_by_id(@message.channel)
47
+ channel.name = channel_name
48
+ MadChatter.send_channels_list
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -8,9 +8,7 @@ module MadChatter
8
8
 
9
9
  def handle(msg)
10
10
  if msg.original_text =~ @@regex
11
- # puts msg.original_text
12
11
  code = parse(msg.original_text)
13
- # puts code
14
12
  message = MadChatter::Message.new('message', nil, msg.token, msg.channel)
15
13
  message.html = "<pre>" + message.filter(code) + "</pre>"
16
14
  message.growl = msg.username + ' has shared a code sample' if msg.username
@@ -22,7 +20,6 @@ module MadChatter
22
20
 
23
21
  def parse(text)
24
22
  text.sub!('/code', '')
25
- # @@regex.match(message_text).captures[0]
26
23
  end
27
24
 
28
25
  end
@@ -14,6 +14,9 @@ module MadChatter
14
14
  text.gsub!(/^[\w\<][^\n]*\n+/) do |x|
15
15
  x =~ /\n{2}/ ? x : (x.strip!; x << " \n")
16
16
  end
17
+
18
+ # inline code (backticks)
19
+ text.gsub!(%r{(^|\s)(\`)(.+?)\2(\s|$)}, %{\\1<code>\\3</code>\\4})
17
20
 
18
21
  # autolink email addresses
19
22
  text.gsub!(/([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})/i) do |x|
@@ -24,18 +27,21 @@ module MadChatter
24
27
  text.gsub!(/(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/(\S)+)?/i) do |x|
25
28
  href = $&
26
29
  # check if we're in a markdown link
27
- if $` =~ /[(]/
30
+ if $` =~ /[(]/
28
31
  x = href
29
- else
32
+ else
30
33
  x = %Q{<a target="_blank" href="#{href}">#{href}</a>}
31
- end
34
+ end
32
35
  end
33
36
 
37
+ # reference style links: [link text](http://link.url)
38
+ text.gsub!(%r{\[([^\]]+)\]\((\S+(?=\)))\)}, %{<a target="_blank" href="\\2">\\1</a>})
39
+
34
40
  # bold (must come before italic)
35
- text.gsub!(%r{(^|\s)(\*\*|__)(.+?)\2(\s|$)}x, %{\\1<strong>\\3</strong>\\4})
41
+ text.gsub!(%r{(^|\s)(\*\*|__)(.+?)\2(\s|$)}, %{\\1<strong>\\3</strong>\\4})
36
42
 
37
43
  # italic
38
- text.gsub!(%r{(^|\s)([*_])(.+?)\2(\s|$)}x, %{\\1<em>\\3</em>\\4})
44
+ text.gsub!(%r{(^|\s)([*_])(.+?)\2(\s|$)}, %{\\1<em>\\3</em>\\4})
39
45
 
40
46
  return text
41
47
  end
@@ -7,7 +7,7 @@ module MadChatter
7
7
  @@regex = %r{^/nick (.+)}
8
8
 
9
9
  def handle(message)
10
- if message.text =~ @@regex
10
+ if @@regex =~ message.text
11
11
  username = parse_username(message.text)
12
12
  user = message.user
13
13
  if user
@@ -35,13 +35,7 @@ module MadChatter
35
35
  end
36
36
 
37
37
  def send_channels
38
- channels = MadChatter.channels.collect do |c|
39
- {:id => c.id, :name => c.name }
40
- end
41
- send JSON.generate({
42
- type: 'channels',
43
- json: channels,
44
- })
38
+ send MadChatter.channels_list
45
39
  end
46
40
 
47
41
  def update_username(username)
@@ -1,5 +1,5 @@
1
1
  module MadChatter
2
2
 
3
- VERSION = '0.3.1'
3
+ VERSION = '0.3.2'
4
4
 
5
5
  end
data/mad_chatter.gemspec CHANGED
@@ -21,14 +21,15 @@ Gem::Specification.new do |s|
21
21
  s.add_runtime_dependency "thor"
22
22
  s.add_runtime_dependency "eventmachine"
23
23
  s.add_runtime_dependency "em-websocket"
24
- # s.add_runtime_dependency "redcarpet"
25
24
  s.add_runtime_dependency "daemons", "1.1.4"
26
25
 
27
26
  s.add_development_dependency "rspec"
28
- s.add_development_dependency "shoulda" # do we need this?
29
- s.add_development_dependency 'simplecov'
30
- s.add_development_dependency 'guard'
31
- s.add_development_dependency 'guard-rspec'
32
- s.add_development_dependency 'rb-fsevent'
33
- s.add_development_dependency 'ruby-growl'
27
+ # s.add_development_dependency "shoulda" # do we need this?
28
+ unless ENV["CI"]
29
+ s.add_development_dependency 'simplecov'
30
+ s.add_development_dependency 'guard'
31
+ s.add_development_dependency 'guard-rspec'
32
+ s.add_development_dependency 'rb-fsevent'
33
+ s.add_development_dependency 'ruby-growl'
34
+ end
34
35
  end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe MadChatter do
4
+
5
+ before(:each) do
6
+ MadChatter.users = []
7
+ MadChatter.channels = []
8
+ end
9
+
10
+ it 'should provide the ability to find a channel by name' do
11
+ channel = MadChatter::Channel.new('My special room')
12
+ MadChatter.channels << channel
13
+ MadChatter.find_channel_by_name('My special room').should == channel
14
+ MadChatter.find_channel_by_name('doesnt exist').should == nil
15
+ end
16
+
17
+ end
@@ -1,18 +1,18 @@
1
- require_relative '../spec_helper'
1
+ require 'spec_helper'
2
2
 
3
- describe MadChatter::MessageListeners::Join do
3
+ describe MadChatter::MessageListeners::ChannelCommands do
4
4
 
5
5
  let (:server) { MadChatter::Server.new({'websocket_backend' => 'MadChatter::Servers::EventMachineWebSocket'}) }
6
6
  let (:user) { MadChatter::User.new('usertoken') }
7
7
  let (:channel) { MadChatter::Channel.new('myroom') }
8
- let (:listener) { MadChatter::MessageListeners::Join.new }
8
+ let (:listener) { MadChatter::MessageListeners::ChannelCommands.new }
9
9
 
10
10
  before(:each) do
11
11
  MadChatter.users = []
12
12
  MadChatter.channels = []
13
13
  end
14
14
 
15
- it 'should add user to list of users in server and channel' do
15
+ it 'should allow a user to join a channel' do
16
16
  MadChatter.users << user
17
17
  MadChatter.channels << channel
18
18
  begin
@@ -25,10 +25,22 @@ describe MadChatter::MessageListeners::Join do
25
25
  end
26
26
  end
27
27
 
28
- it 'should parse messages correctly' do
28
+ it 'should allow /join to exist in a normal message' do
29
29
  message = MadChatter::Message.new('message', 'this /join should not count')
30
30
  listener.handle(message)
31
31
  # how to check that nothing happened?
32
32
  end
33
33
 
34
+ it 'should allow a user to create a channel' do
35
+ MadChatter.users << user
36
+ MadChatter.channels << channel
37
+ begin
38
+ MadChatter.find_channel_by_name('Foobar Room').should be_nil
39
+ message = MadChatter::Message.new('message', '/channel create Foobar Room', user.token, channel.id)
40
+ listener.handle(message)
41
+ MadChatter.find_channel_by_name('Foobar Room').should_not be_nil
42
+ rescue RuntimeError
43
+ end
44
+ end
45
+
34
46
  end
@@ -1,4 +1,4 @@
1
- require_relative '../spec_helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe MadChatter::MessageListeners::Code do
4
4
 
@@ -1,5 +1,4 @@
1
- require_relative '../spec_helper'
2
- require 'mad_chatter/message_listeners/markdown'
1
+ require 'spec_helper'
3
2
 
4
3
  describe MadChatter::MessageListeners::Markdown do
5
4
 
@@ -37,11 +36,16 @@ describe MadChatter::MessageListeners::Markdown do
37
36
  message.html.should == 'here is a <strong>strong</strong> statement'
38
37
  end
39
38
 
40
- # TODO
41
- # it 'should interpret markdown style links' do
42
- # message = MadChatter::Message.new('message', "[link me up](http://www.example.com)")
43
- # listener.handle(message)
44
- # message.html.should == '<a target="_blank" href="http://www.example.com">link me up</a>'
45
- # end
39
+ it 'should parse backticks as inline code' do
40
+ message = MadChatter::Message.new('message', "this has `some code` in it")
41
+ listener.handle(message)
42
+ message.html.should == 'this has <code>some code</code> in it'
43
+ end
44
+
45
+ it 'should parse markdown style links' do
46
+ message = MadChatter::Message.new('message', "[link me up](http://www.example.com)")
47
+ listener.handle(message)
48
+ message.html.should == '<a target="_blank" href="http://www.example.com">link me up</a>'
49
+ end
46
50
 
47
51
  end