Browse files

Improve nick matching regex to match punctuation and end of string. F…

…ixes gh-2.
  • Loading branch information...
1 parent d3303d9 commit c681400b3ddd8ff672bc23d452915a2eb5e4c1ba @zerowidth zerowidth committed Jul 6, 2011
Showing with 51 additions and 3 deletions.
  1. +8 −3 lib/camper_van/channel.rb
  2. +43 −0 spec/camper_van/channel_spec.rb
View
11 lib/camper_van/channel.rb
@@ -121,10 +121,12 @@ def privmsg(msg)
# convert ACTIONs
msg.sub! /^\01ACTION (.*)\01$/, '*\1*'
- if matched = users.values.detect {|u| msg.start_with?(u.nick + ': ')}
- msg = msg.sub(/^#{matched.nick}/, matched.name)
+ matched = users.values.detect do |user|
+ msg =~ /^#{Regexp.escape(user.nick)}($|\W+(\s|$))/
end
+ msg = msg.sub(/^#{matched.nick}/, matched.name) if matched
+
room.text(msg) { } # async, no-op callback
end
@@ -337,7 +339,10 @@ def map_message_to_irc(message)
if message.body =~ /^\*.*\*$/
client.campfire_reply :privmsg, name, channel, ":\01ACTION " + message.body[1..-2] + "\01"
else
- matched = users.values.detect {|u| message.body.start_with?(u.name + ': ')}
+ matched = users.values.detect do |user|
+ message.body =~ /^#{Regexp.escape(user.name)}(\W+(\s|$)|$)/
+ end
+
if matched
body = message.body.sub(/^#{matched.name}/, matched.nick)
else
View
43 spec/camper_van/channel_spec.rb
@@ -192,6 +192,28 @@ def stream
@channel.privmsg "bob_fred: sup dude"
@room.sent.last.must_match /Bob Fred: sup dude/
end
+
+ it "converts leading nicknames followed by punctuation" do
+ @room.users = [
+ OpenStruct.new(:id => 11, :name => "Bob Fred", :email_address => "x@y.com"),
+ OpenStruct.new(:id => 12, :name => "Joe", :email_address => "x@y.com")
+ ]
+ @channel.list_users
+
+ @channel.privmsg "bob_fred! sup!"
+ @room.sent.last.must_match /Bob Fred! sup/
+ end
+
+ it "converts just leading nicks to names" do
+ @room.users = [
+ OpenStruct.new(:id => 11, :name => "Bob Fred", :email_address => "x@y.com"),
+ OpenStruct.new(:id => 12, :name => "Joe", :email_address => "x@y.com")
+ ]
+ @channel.list_users
+
+ @channel.privmsg "bob_fred"
+ @room.sent.last.must_match /Bob Fred/
+ end
end
describe "#current_mode" do
@@ -310,6 +332,27 @@ def msg(type, attributes={})
@client.sent.last.must_match %r(PRIVMSG #test :bob_fred: hello)
end
+ it "converts just leading names to nicks" do
+ @room.users = [
+ OpenStruct.new(:id => 11, :name => "Bob Fred", :email_address => "x@y.com"),
+ OpenStruct.new(:id => 12, :name => "Joe", :email_address => "x@y.com")
+ ]
+ @channel.list_users
+ @channel.map_message_to_irc msg("Text", :body => "Bob Fred")
+ @client.sent.last.must_match %r(PRIVMSG #test bob_fred)
+ end
+
+ it "converts leading names plus punctuation to nicks" do
+ @room.users = [
+ OpenStruct.new(:id => 11, :name => "Bob Fred", :email_address => "x@y.com"),
+ OpenStruct.new(:id => 12, :name => "Joe", :email_address => "x@y.com")
+ ]
+ @channel.list_users
+
+ @channel.map_message_to_irc msg("Text", :body => "Bob Fred!!? dude!")
+ @client.sent.last.must_match %r(PRIVMSG #test :bob_fred!!\? dude)
+ end
+
it "sends an action when a user plays the crickets sound" do
@channel.map_message_to_irc msg("Sound", :body => "crickets")
@client.sent.last.must_match /\x01ACTION hears crickets chirping\x01/

0 comments on commit c681400

Please sign in to comment.