Report abuse

diff --git a/lib/cyanchat-relay/cc_connection.rb b/lib/cyanchat-relay/cc_connection.rb
--- a/lib/cyanchat-relay/cc_connection.rb
+++ b/lib/cyanchat-relay/cc_connection.rb
@@ -7,16 +7,18 @@ module CCRelay
 
   class CCConnection
 
     attr_accessor :irc_conn
 
     def initialize(logger)
       @logger = logger
 
+      @mutex = Mutex.new
+
       @cc_nickname  = 'IRC'
       @cc_port      = 1812
     end
 
     # per http://daringfireball.net/2009/11/liberal_regex_for_matching_urls
     # and http://alanstorm.com/url_regex_explained
     URL_REGEX = %r{\b(([\w-]+://?|www[.])[^\s()<>]+(?:(?:\([\w\d)]+\)[^\s()<>]*)+|([^[:punct:]\s]|/)))}
 
@@ -27,17 +29,19 @@ module CCRelay
     end
 
     def send_room_message(text)
       # Format
       formatted_text = text.format_message_for_cc
       censored_text = text.censor_message_for_cc
 
       # Send
-      @internal_conn.send_room_message(censored_text)
+      @mutex.synchronize do
+        @internal_conn.send_room_message(censored_text)
+      end
 
       # Find URLs
       Thread.new do
         # Shorten URLs if at least 30 chars or censored
         shortened_text = text.gsub(URL_REGEX) do |match|
           if match.length >= 30 || match != match.censor_message_for_cc
             # Get shortened URL
             begin
@@ -46,17 +50,19 @@ module CCRelay
               short_url_message = ('Shortened URL: ' + shortened_url).censor_message_for_cc
             rescue OpenURI::HTTPError
               shortened_url = match
 
               short_url_message = 'Failed to shorten a URL.'
             end
 
             # Notify
-            @internal_conn.send_room_message(short_url_message)
+            @mutex.synchronize do
+              @internal_conn.send_room_message(short_url_message)
+            end
 
             # Done
             shortened_url
           end
         end
       end
 
       # Notify reformat
diff --git a/lib/cyanchat-relay/irc_connection.rb b/lib/cyanchat-relay/irc_connection.rb
--- a/lib/cyanchat-relay/irc_connection.rb
+++ b/lib/cyanchat-relay/irc_connection.rb
@@ -4,27 +4,31 @@ module CCRelay
 
   class IRCConnection
 
     attr_accessor :cc_conn
 
     def initialize(logger)
       @logger = logger
 
+      @mutex = Mutex.new
+
       @fetch_users_infix = ''
 
       @irc_network    = 'chat.mystlore.com'
       @irc_channel    = '#cyanchat'
       @irc_nicknames  = [ 'CC', 'CC_', 'CC__' ]
       @irc_username   = 'CC'
       @irc_realname   = 'CyanChat-to-IRC relay bot'
     end
 
     def send_room_message(text)
-      @internal_conn.msg(@irc_channel, text.format_message_for_irc)
+      @mutex.synchronize do
+        @internal_conn.msg(@irc_channel, text.format_message_for_irc)
+      end
     end
 
     def setup
       # Create main IRC connection
       @internal_conn = Net::YAIL.new(
         :address    => @irc_network,
         :nicknames  => @irc_nicknames,
         :username   => @irc_username,
@@ -73,17 +77,19 @@ module CCRelay
     end
 
     def close
       @running = false
       @internal_conn.stop_listening
     end
 
     def cyanchat_message_filtered(text)
-      @internal_conn.msg(@irc_channel, "Message reformatted: #{text}")
+      @mutex.synchronize do
+        @internal_conn.msg(@irc_channel, "Message reformatted: #{text}")
+      end
     end
 
   private
 
     def incoming_welcome(text, args)
       @logger.log 'irc', "Received welcome: args=#{args.inspect} text=#{text.inspect}"
 
       # Join