mad_chatter 0.2.9 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/.gitignore +2 -1
  2. data/.rvmrc +1 -0
  3. data/Gemfile.lock +1 -3
  4. data/README.md +25 -20
  5. data/lib/mad_chatter/actions/dsl.rb +4 -10
  6. data/lib/mad_chatter/actions.rb +29 -0
  7. data/lib/mad_chatter/channel.rb +56 -0
  8. data/lib/mad_chatter/config.rb +22 -5
  9. data/lib/mad_chatter/connection.rb +29 -0
  10. data/lib/mad_chatter/extensions.rb +12 -0
  11. data/lib/mad_chatter/markdown_renderer.rb +6 -0
  12. data/lib/mad_chatter/message.rb +39 -12
  13. data/lib/mad_chatter/message_history.rb +8 -5
  14. data/lib/mad_chatter/message_listener.rb +17 -0
  15. data/lib/mad_chatter/message_listeners/code.rb +30 -0
  16. data/lib/mad_chatter/message_listeners/image.rb +27 -0
  17. data/lib/mad_chatter/message_listeners/join.rb +30 -0
  18. data/lib/mad_chatter/message_listeners/markdown.rb +45 -0
  19. data/lib/mad_chatter/message_listeners/nick.rb +26 -0
  20. data/lib/mad_chatter/server.rb +0 -55
  21. data/lib/mad_chatter/servers/em_websocket.rb +5 -14
  22. data/lib/mad_chatter/user.rb +78 -0
  23. data/lib/mad_chatter/users.rb +32 -24
  24. data/lib/mad_chatter/version.rb +1 -1
  25. data/lib/mad_chatter.rb +98 -34
  26. data/mad_chatter.gemspec +1 -1
  27. data/screenshot.png +0 -0
  28. data/spec/actions_spec.rb +7 -0
  29. data/spec/message_history_spec.rb +12 -10
  30. data/spec/message_listeners/code_spec.rb +37 -0
  31. data/spec/message_listeners/join_spec.rb +34 -0
  32. data/spec/message_listeners/markdown_spec.rb +47 -0
  33. data/spec/message_spec.rb +28 -16
  34. data/spec/server_spec.rb +43 -0
  35. data/spec/users_spec.rb +23 -0
  36. data/templates/extensions/example.rb +1 -1
  37. data/templates/extensions.rb +16 -14
  38. data/templates/web/css/bootstrap.css +3363 -0
  39. data/templates/web/css/bootstrap.min.css +610 -0
  40. data/templates/web/css/bootstrap.min.responsive.css +3 -0
  41. data/templates/web/css/bootstrap.responsive.css +567 -0
  42. data/templates/web/css/mad_chatter.css +59 -0
  43. data/templates/web/{styles.css → css/styles.css} +26 -20
  44. data/templates/web/img/glyphicons-halflings-white.png +0 -0
  45. data/templates/web/img/glyphicons-halflings.png +0 -0
  46. data/templates/web/index.html +173 -40
  47. data/templates/web/js/bootstrap.js +1722 -0
  48. data/templates/web/js/bootstrap.min.js +1 -0
  49. data/templates/web/js/mad_chatter.js +292 -0
  50. data/templates/web/{mad_chatter_actions.js → js/mad_chatter_actions.js} +0 -0
  51. data/templates/web/js/mad_chatter_config.js +3 -0
  52. data/templates/web/js/swfobject.js +4 -0
  53. data/templates/web/js/web_socket.js +389 -0
  54. data/templates/web/swf/WebSocketMain.swf +0 -0
  55. metadata +65 -62
  56. data/TODO.txt +0 -10
  57. data/templates/web/mad_chatter.js +0 -161
  58. data/templates/web/mad_chatter_config.js +0 -1
  59. data/templates/web/markitup/jquery.markitup.js +0 -593
  60. data/templates/web/markitup/sets/markdown/images/bold.png +0 -0
  61. data/templates/web/markitup/sets/markdown/images/code.png +0 -0
  62. data/templates/web/markitup/sets/markdown/images/h1.png +0 -0
  63. data/templates/web/markitup/sets/markdown/images/h2.png +0 -0
  64. data/templates/web/markitup/sets/markdown/images/h3.png +0 -0
  65. data/templates/web/markitup/sets/markdown/images/h4.png +0 -0
  66. data/templates/web/markitup/sets/markdown/images/h5.png +0 -0
  67. data/templates/web/markitup/sets/markdown/images/h6.png +0 -0
  68. data/templates/web/markitup/sets/markdown/images/italic.png +0 -0
  69. data/templates/web/markitup/sets/markdown/images/link.png +0 -0
  70. data/templates/web/markitup/sets/markdown/images/list-bullet.png +0 -0
  71. data/templates/web/markitup/sets/markdown/images/list-numeric.png +0 -0
  72. data/templates/web/markitup/sets/markdown/images/picture.png +0 -0
  73. data/templates/web/markitup/sets/markdown/images/preview.png +0 -0
  74. data/templates/web/markitup/sets/markdown/images/quotes.png +0 -0
  75. data/templates/web/markitup/sets/markdown/readme.txt +0 -11
  76. data/templates/web/markitup/sets/markdown/style.css +0 -6
  77. data/templates/web/markitup/skins/mad_chatter/images/handle.png +0 -0
  78. data/templates/web/markitup/skins/mad_chatter/images/menu.png +0 -0
  79. data/templates/web/markitup/skins/mad_chatter/images/submenu.png +0 -0
  80. data/templates/web/markitup/skins/mad_chatter/style.css +0 -121
data/.gitignore CHANGED
@@ -2,4 +2,5 @@
2
2
  .bundle
3
3
  pkg/*
4
4
  .daemon
5
- coverage/
5
+ coverage/
6
+ .project
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm --create use 1.9.2@mad_chatter
data/Gemfile.lock CHANGED
@@ -1,11 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mad_chatter (0.2.8)
4
+ mad_chatter (0.3.0)
5
5
  daemons (= 1.1.4)
6
6
  em-websocket
7
7
  eventmachine
8
- redcarpet
9
8
  thor
10
9
 
11
10
  GEM
@@ -24,7 +23,6 @@ GEM
24
23
  guard (>= 0.8.4)
25
24
  multi_json (1.0.4)
26
25
  rb-fsevent (0.4.3.1)
27
- redcarpet (2.0.1)
28
26
  rspec (2.7.0)
29
27
  rspec-core (~> 2.7.0)
30
28
  rspec-expectations (~> 2.7.0)
data/README.md CHANGED
@@ -1,10 +1,16 @@
1
1
  # Mad Chatter
2
2
 
3
- Mad Chatter is a fun, easy to customize chat server. It's written in Ruby and utilizes HTML 5 Web Sockets for fast communication. It also has support for Markdown.
3
+ Mad Chatter is a fun, easy to customize chat server. It's written in Ruby and utilizes HTML 5 Web Sockets for fast communication.
4
+
5
+ The goal of Mad Chatter is to basically become an open-source version of [Campfire](http://campfirenow.com/), [HipChat](https://www.hipchat.com), or [FlowDock](https://www.flowdock.com). Or at least use those as inspiration.
4
6
 
5
7
  ## Getting Started
6
8
 
7
- To get started, first install the Mad Chatter gem:
9
+ Since Mad Chatter is written in Ruby, you'll need to install Ruby in order to continue. We have a wiki page to help you with that:
10
+
11
+ [How to install Ruby and RubyGems](https://github.com/andrewhavens/mad_chatter/wiki/How-to-install-Ruby-and-RubyGems)
12
+
13
+ Once you have RubyGems installed, you can install the Mad Chatter gem:
8
14
 
9
15
  gem install mad_chatter
10
16
 
@@ -15,27 +21,27 @@ Then generate the directory where your chat application will live:
15
21
  This command will generate the following structure:
16
22
 
17
23
  mychatroom/
18
- config.rb
19
- extensions.rb
20
- web/
21
- index.html
22
- styles.css
23
- mad_chatter.js
24
- mad_chatter_actions.js
24
+ config.yml # for general configuration
25
+ extensions.rb # for writing your own extensions
26
+ web/ # all of the html, css, and javascript live here
25
27
 
26
28
  To start your chat server, navigate to the directory that was just created and run...
27
29
 
28
- mad_chatter start
30
+ mad_chatter preview
31
+
32
+ This will start up the Mad Chatter chat server as well as a simple web server for you to preview/demo your new chat room. Now you can check it out by opening http://localhost:3000 in your browser. You should see something like this:
29
33
 
30
- This will start the web socket server on a specific port. You can now open the index.html file in your browser and start playing. All the files necessary to serve to the client are in the web directory. Feel free to copy/move these files if you'd prefer them somewhere else.
34
+ ![Mad Chatter screenshot](https://raw.github.com/andrewhavens/mad_chatter/master/screenshot.png)
31
35
 
32
- The start command will launch the web socket server in the background. You can use this command in production to start your server and leave it running. If you need to stop the server, simply run...
36
+ When you're ready to have other people use your chat server (friends, family, co-workers, etc) you'll want to host the server and web directory some place that they can access (on a web server, for example, if it will be used outside your local network). You'll also want to start the chat server and leave it running for a long time in the background. This is known as a "daemon". Here are a few useful commands for that:
33
37
 
38
+ mad_chatter start
34
39
  mad_chatter stop
40
+ mad_chatter restart
35
41
 
36
42
  ## Chat Actions
37
43
 
38
- Hopefully, the default chatroom will be relatively intuitive to use. However, there are some extra features that Mad Chatter provides. Every chat message is parsed to see if it is a normal chat message, or if its a special action. These actions are much like IRC commands. For example, if I wanted to change my screen name from Andrew to Andy I could chat this message:
44
+ Mad Chatter can do some special things depending on the chat messages you send. Every chat message is parsed to see if it is a normal chat message, or if its a special action. These actions are much like IRC commands. For example, if I wanted to change my screen name from Andrew to Andy I could chat this message:
39
45
 
40
46
  /nick andy
41
47
 
@@ -49,25 +55,25 @@ Or if I wanted to shake everyone's chat window:
49
55
 
50
56
  /earthquake
51
57
 
52
- You can also create your own actions to do whatever you want.
58
+ You can even create your own actions!
53
59
 
54
60
 
55
61
  ## Customizing
56
62
 
57
63
  The goal of Mad Chatter is to make it easy to create, host, and customize your own chat server. Let's take a look at the different ways you can customize your new chat server.
58
64
 
59
- If you want to customize the html/css of your chatroom, you'll find it in the web directory.
65
+ If you want to customize the html/css of your chatroom, you'll find it in the `web` directory.
60
66
 
61
- There is an example config file that shows a few examples of things you can customize.
67
+ In the `config.yml` file you can see a few things you can customize.
62
68
 
63
- The extensions.rb file is for you to create your own chat extensions. You will find a few examples in that file.
69
+ The `extensions.rb` file is for you to create your own chat extensions. You will find a few examples in that file.
64
70
 
65
71
 
66
72
  ## Mac, Windows, and Linux Wrappers
67
73
 
68
- Once you've got your chat server running and being used by other people, you might be interested in using/distributing an installable application, so you can leave it running and don't need to pull it up in a browser anymore. Here's a list of the currently available GUI wrapper applications:
74
+ Once you've got your chat server running and being used by other people, you might be interested in using/distributing an installable application so your users have the convenience of clicking on an icon, receiving growl notifications, etc. Here's a list of the currently available "wrapper" applications:
69
75
 
70
- * [Mad Chatter for Mac](https://github.com/andrewhavens/mad_chatter_for_mac) (a MacRuby app based on WebKit)
76
+ * [Mad Chatter for Mac](https://github.com/andrewhavens/mad_chatter_for_mac)
71
77
 
72
78
  ## Getting Help / Providing Feedback
73
79
 
@@ -82,7 +88,6 @@ Please fork and send pull requests! Or submit issues if you have suggestions on
82
88
  ## TODO
83
89
 
84
90
  - Maybe add support for alternative web socket servers like Juggernaut, Socket.io, or Cramp and web-socket-js
85
- - Add tests! (I'm new to this, so I need help)
86
91
 
87
92
  ##Copyright
88
93
 
@@ -18,25 +18,19 @@ module MadChatter
18
18
  end
19
19
 
20
20
  def send_message(text)
21
- json = MadChatter::Message.new('message', text).to_json
22
- send_json(json)
23
- MadChatter::MessageHistory.add(json)
21
+ MadChatter::Server.send_message MadChatter::Message.new('message', text)
24
22
  end
25
23
 
26
24
  def send_status_message(text)
27
- json = MadChatter::Message.new('status', text).to_json
28
- send_json(json)
29
- MadChatter::MessageHistory.add(json)
25
+ MadChatter::Server.send_message MadChatter::Message.new('status', text)
30
26
  end
31
27
 
32
28
  def send_users_list
33
- message = MadChatter::Message.new('users', MadChatter::Users.current)
34
- send_json(message.to_json)
29
+ MadChatter::Server.send_message MadChatter::Message.new('users', MadChatter::Users.current)
35
30
  end
36
31
 
37
32
  def send_action(action, *args)
38
- message = MadChatter::Message.new('action', {function: action, args: args})
39
- send_json(message.to_json)
33
+ MadChatter::Server.send_message MadChatter::Message.new('action', {function: action, args: args})
40
34
  end
41
35
 
42
36
  end
@@ -0,0 +1,29 @@
1
+ module MadChatter
2
+ module Actions
3
+
4
+ def stop_message_handling
5
+ raise "Don't call any more message listeners"
6
+ end
7
+
8
+ # def send_json(json)
9
+ # MadChatter.send_json(json)
10
+ # end
11
+
12
+ def send_message(text, from)
13
+ MadChatter.send_message MadChatter::Message.new('message', text, from)
14
+ end
15
+
16
+ def send_status_message(text)
17
+ MadChatter.send_message MadChatter::Message.new('status', text)
18
+ end
19
+
20
+ # def send_users_list
21
+ # send_json MadChatter::Users.to_json
22
+ # end
23
+
24
+ def send_action(action, *args)
25
+ MadChatter.send_message MadChatter::Message.new('action', {function: action, args: args})
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,56 @@
1
+ module MadChatter
2
+ class Channel
3
+
4
+ attr_accessor :id, :name, :users, :message_history
5
+
6
+ def initialize(name = nil)
7
+ @id = MadChatter.channels.count.to_s
8
+ @name = name
9
+ @users = []
10
+ @message_history = MadChatter::MessageHistory.new
11
+ end
12
+
13
+ def add_user(user)
14
+ @users << user
15
+ send_users_list
16
+ @message_history.all.each do |json|
17
+ user.send(json)
18
+ end
19
+ send_message MadChatter::Message.new('status', "#{user.username} has joined the chatroom")
20
+ end
21
+
22
+ def remove_user(user)
23
+ if @users.delete(user)
24
+ send_message MadChatter::Message.new('status', "#{user.username} has left the chatroom")
25
+ send_users_list
26
+ end
27
+ end
28
+
29
+ def send_users_list
30
+ usernames = []
31
+ @users.each do |user|
32
+ usernames << user.username
33
+ end
34
+ json = JSON.generate({
35
+ type: 'users',
36
+ json: usernames,
37
+ channel: @id,
38
+ })
39
+ send_json(json)
40
+ end
41
+
42
+ def send_message(message)
43
+ message.channel = @id
44
+ json = message.to_json
45
+ send_json(json)
46
+ @message_history.add(json) if message.add_to_history?
47
+ end
48
+
49
+ def send_json(json)
50
+ @users.each do |user|
51
+ user.send(json)
52
+ end
53
+ end
54
+
55
+ end
56
+ end
@@ -4,12 +4,14 @@ module MadChatter
4
4
  class << self
5
5
 
6
6
  def init
7
- config = MadChatter::Config.initialize_config
8
- MadChatter::Config.initialize_extensions
7
+ config = init_config
8
+ init_default_message_listeners
9
+ init_extensions
10
+ init_default_channels
9
11
  return config
10
12
  end
11
13
 
12
- def initialize_config
14
+ def init_config
13
15
  config_file = File.join(Dir.pwd, 'config.yml')
14
16
  abort 'Could not find Mad Chatter config.yml file' unless File.exist?(config_file)
15
17
 
@@ -20,12 +22,23 @@ module MadChatter
20
22
  }
21
23
  @config = defaults.merge!(config)
22
24
  end
25
+
26
+ def init_default_message_listeners
27
+ %w{ Join Nick Image Code Markdown }.each do |listener|
28
+ MadChatter.message_listeners << Object.const_get('MadChatter').const_get('MessageListeners').const_get(listener).new
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
+ end
23
36
 
24
- def initialize_extensions
37
+ def init_extensions
25
38
  simple_extensions_file = File.join(Dir.pwd, 'extensions.rb')
26
39
  if File.exist?(simple_extensions_file)
27
40
  file_contents = File.read(simple_extensions_file)
28
- MadChatter::Actions::Base.new.instance_eval file_contents
41
+ # MadChatter::Extensions.module_eval file_contents
29
42
  end
30
43
 
31
44
  # Dir[Dir.pwd + '/extensions/*.rb'].each do |file|
@@ -33,6 +46,10 @@ module MadChatter
33
46
  # end
34
47
  end
35
48
 
49
+ def init_default_channels
50
+ MadChatter.channels << MadChatter::Channel.new('default')
51
+ end
52
+
36
53
  end
37
54
  end
38
55
  end
@@ -0,0 +1,29 @@
1
+ module MadChatter
2
+ class Connection
3
+
4
+ def register_connection(&send_message)
5
+ subscriber_id = MadChatter::Server.main_channel.subscribe(send_message)
6
+ token = generate_token
7
+ send_message.call(MadChatter::Message.new('token', token).to_json)
8
+ @subscribers[subscriber_id] = token
9
+ MadChatter::MessageHistory.all.each do |json|
10
+ send_message.call(json)
11
+ end
12
+ subscriber_id
13
+ end
14
+
15
+ def generate_new_token
16
+ Digest::SHA1.hexdigest(Time.now.to_s)
17
+ end
18
+
19
+ def on_close
20
+ token = @subscribers.delete(id)
21
+ username = MadChatter::Users.find_username_by_token(token)
22
+ MadChatter::Server.main_channel.unsubscribe(id)
23
+ MadChatter::Users.remove(token)
24
+ MadChatter::Server.send_json(MadChatter::Message.new('status', "#{username} has left the chatroom").to_json)
25
+ MadChatter::Server.send_json(MadChatter::Message.new('users', MadChatter::Users.current).to_json)
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,12 @@
1
+ module MadChatter
2
+ module Extensions
3
+
4
+ # include MadChatter::Actions
5
+
6
+ # Used for defining simple extensions
7
+ def on_message(regex, &block)
8
+ MadChatter.message_listeners << MadChatter::MessageListener.new(regex, block)
9
+ end
10
+
11
+ end
12
+ end
@@ -1,10 +1,16 @@
1
1
  module MadChatter
2
2
  class MarkdownRenderer < Redcarpet::Render::HTML
3
+
3
4
  def link(link, title, alt_text)
4
5
  "<a target=\"_blank\" href=\"#{link}\">#{alt_text}</a>"
5
6
  end
7
+
6
8
  def autolink(link, link_type)
7
9
  "<a target=\"_blank\" href=\"#{link}\">#{link}</a>"
8
10
  end
11
+
12
+ def header(text, header_level)
13
+ '#' + text
14
+ end
9
15
  end
10
16
  end
@@ -1,16 +1,40 @@
1
+ require 'cgi'
2
+
1
3
  module MadChatter
2
4
  class Message
3
5
 
4
- attr_accessor :type, :original_text, :filtered_text, :token, :username
6
+ attr_accessor :type, :original_text, :filtered_text, :html, :token, :channel, :growl, :add_to_history
5
7
 
6
- def initialize(type, original_text, token = nil, username = nil)
8
+ def initialize(type, text = nil, token = nil, channel = nil)
7
9
  @type = type
8
- @original_text = original_text
9
- @filtered_text = original_text # if filter is never called, message will be original text
10
+ if text
11
+ @original_text = text
12
+ @filtered_text = filter(text)
13
+ @html = @filtered_text
14
+ @growl = text
15
+ end
10
16
  @token = token
17
+ @channel = channel
18
+ @add_to_history = true
19
+ end
20
+
21
+ def username=(username)
11
22
  @username = username
12
23
  end
13
24
 
25
+ def username
26
+ unless @username
27
+ MadChatter.users.each do |user|
28
+ @username = user.username if user.has_token?(@token)
29
+ end
30
+ end
31
+ @username
32
+ end
33
+
34
+ def user
35
+ MadChatter.find_user_by_token(@token) if @token
36
+ end
37
+
14
38
  # Helper method for returning filtered text.
15
39
  def text
16
40
  @filtered_text
@@ -19,17 +43,20 @@ module MadChatter
19
43
  def to_json
20
44
  JSON.generate({
21
45
  type: @type,
22
- message: @filtered_text,
23
- username: @username,
46
+ text: @original_text,
47
+ html: @html,
48
+ username: username,
49
+ channel: @channel,
50
+ growl: @growl,
24
51
  })
25
52
  end
26
53
 
27
- def filter
28
- @filtered_text = MadChatter.markdown.render(@original_text)
29
- # remove the <p> tags that markdown wraps by default
30
- @filtered_text.sub!(/^<p>/, '')
31
- @filtered_text.sub!(/<\/p>$/, '')
32
- @filtered_text
54
+ def filter(text)
55
+ CGI::escapeHTML(text).strip
56
+ end
57
+
58
+ def add_to_history?
59
+ @add_to_history
33
60
  end
34
61
 
35
62
  end
@@ -1,17 +1,20 @@
1
1
  module MadChatter
2
2
  class MessageHistory
3
3
 
4
- def self.add(message)
5
- @stack ||= []
4
+ def initialize
5
+ @stack = []
6
+ end
7
+
8
+ def add(message)
6
9
  @stack << message
7
10
  @stack = @stack.pop(10) if @stack.length > 10
8
11
  end
9
12
 
10
- def self.all
11
- @stack ||= []
13
+ def all
14
+ @stack
12
15
  end
13
16
 
14
- def self.clear
17
+ def clear
15
18
  @stack = []
16
19
  end
17
20
 
@@ -0,0 +1,17 @@
1
+ module MadChatter
2
+ class MessageListener
3
+ include MadChatter::Actions
4
+
5
+ def initialize(regex = nil, block = nil)
6
+ @regex = regex
7
+ @block = block
8
+ end
9
+
10
+ def handle(message)
11
+ if message.original_text =~ @regex
12
+ args = @regex.match(message.original_text).captures
13
+ @block.call(args)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ module MadChatter
2
+ module MessageListeners
3
+ class Code
4
+
5
+ include MadChatter::Actions
6
+
7
+ @@regex = %r{^/code (.+)}
8
+
9
+ def handle(msg)
10
+ if msg.original_text =~ @@regex
11
+ # puts msg.original_text
12
+ code = parse(msg.original_text)
13
+ # puts code
14
+ message = MadChatter::Message.new('message', nil, msg.token, msg.channel)
15
+ message.html = "<pre>" + message.filter(code) + "</pre>"
16
+ message.growl = msg.username + ' has shared a code sample' if msg.username
17
+ channel = MadChatter::find_channel_by_id(msg.channel)
18
+ channel.send_message(message)
19
+ stop_message_handling
20
+ end
21
+ end
22
+
23
+ def parse(text)
24
+ text.sub!('/code', '')
25
+ # @@regex.match(message_text).captures[0]
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,27 @@
1
+ module MadChatter
2
+ module MessageListeners
3
+ class Image
4
+
5
+ include MadChatter::Actions
6
+
7
+ @@regex = %r{^/img (.+)}
8
+
9
+ def handle(message)
10
+ if message.text =~ @@regex
11
+ img_url = parse_url(message.text)
12
+ img_msg = MadChatter::Message.new('message', img_url, message.token, message.channel)
13
+ img_msg.html = "<img src='#{img_url}'>"
14
+ img_msg.growl = message.username + ' has shared an image'
15
+ channel = MadChatter::find_channel_by_id(message.channel)
16
+ channel.send_message(img_msg)
17
+ stop_message_handling
18
+ end
19
+ end
20
+
21
+ def parse_url(message_text)
22
+ @@regex.match(message_text).captures[0]
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,30 @@
1
+ module MadChatter
2
+ module MessageListeners
3
+ class Join
4
+
5
+ include MadChatter::Actions
6
+
7
+ @@regex = %r{^/join$}
8
+
9
+ def handle(message)
10
+ if message.text =~ @@regex
11
+ user = MadChatter.find_user_by_token(message.token)
12
+
13
+ unless user && message.channel
14
+ stop_message_handling # user should already exist, and channel id is required
15
+ end
16
+
17
+ channel = MadChatter.find_channel_by_id(message.channel)
18
+
19
+ unless channel
20
+ stop_message_handling # you cant join a channel that doesnt exist
21
+ end
22
+
23
+ channel.add_user(user)
24
+ stop_message_handling
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,45 @@
1
+ module MadChatter
2
+ module MessageListeners
3
+ class Markdown
4
+
5
+ # Apply markdown to all messages that pass through
6
+ def handle(message)
7
+ message.html = apply_markdown(message.filtered_text)
8
+ return message
9
+ end
10
+
11
+ def apply_markdown(text)
12
+
13
+ # in very clear cases, let newlines become <br /> tags
14
+ text.gsub!(/^[\w\<][^\n]*\n+/) do |x|
15
+ x =~ /\n{2}/ ? x : (x.strip!; x << " \n")
16
+ end
17
+
18
+ # autolink email addresses
19
+ text.gsub!(/([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})/i) do |x|
20
+ x = %Q{<a href="mailto:#{$&}">#{$&}</a>}
21
+ end
22
+
23
+ # autolink urls
24
+ text.gsub!(/(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/(\S)+)?/i) do |x|
25
+ href = $&
26
+ # check if we're in a markdown link
27
+ if $` =~ /[(]/
28
+ x = href
29
+ else
30
+ x = %Q{<a target="_blank" href="#{href}">#{href}</a>}
31
+ end
32
+ end
33
+
34
+ # bold (must come before italic)
35
+ text.gsub!(%r{(^|\s)(\*\*|__)(.+?)\2(\s|$)}x, %{\\1<strong>\\3</strong>\\4})
36
+
37
+ # italic
38
+ text.gsub!(%r{(^|\s)([*_])(.+?)\2(\s|$)}x, %{\\1<em>\\3</em>\\4})
39
+
40
+ return text
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,26 @@
1
+ module MadChatter
2
+ module MessageListeners
3
+ class Nick
4
+
5
+ include MadChatter::Actions
6
+
7
+ @@regex = %r{^/nick (.+)}
8
+
9
+ def handle(message)
10
+ if message.text =~ @@regex
11
+ username = parse_username(message.text)
12
+ user = message.user
13
+ if user
14
+ user.update_username(username)
15
+ end
16
+ stop_message_handling
17
+ end
18
+ end
19
+
20
+ def parse_username(text)
21
+ @@regex.match(text).captures[0]
22
+ end
23
+
24
+ end
25
+ end
26
+ end