diff --git a/lib/camper_van/channel.rb b/lib/camper_van/channel.rb index 48a77a8..5638b2c 100644 --- a/lib/camper_van/channel.rb +++ b/lib/camper_van/channel.rb @@ -117,12 +117,10 @@ def privmsg(msg) # convert ACTIONs msg.sub! /^\01ACTION (.*)\01$/, '*\1*' - matched = users.values.detect do |user| - msg =~ /^#{Regexp.escape(user.nick)}($|\W+(\s|$))/ + users.values.each do |user| + msg.sub!(/\b#{user.nick}\b/, user.name) end - msg = msg.sub(/^#{matched.nick}/, matched.name) if matched - room.text(msg) { } # async, no-op callback end diff --git a/spec/camper_van/channel_spec.rb b/spec/camper_van/channel_spec.rb index c0df0ac..34a426a 100644 --- a/spec/camper_van/channel_spec.rb +++ b/spec/camper_van/channel_spec.rb @@ -254,6 +254,42 @@ def close # em-http defines this @room.sent.last.must_match /Bob Fred: sup dude/ end + it "converts names on any part" do + @room.users = [ + OpenStruct.new(:id => 11, :name => "JD Wolk", :email_address => "x@y.com"), + OpenStruct.new(:id => 12, :name => "Joe", :email_address => "x@y.com") + ] + @channel.list_users + + @channel.privmsg "sup dude jd_wolk" + @room.sent.last.must_match /sup dude JD Wolk/ + end + + it "does not convert names in words" do + @room.users = [ + OpenStruct.new(:id => 11, :name => "JD Wolk", :email_address => "x@y.com"), + OpenStruct.new(:id => 12, :name => "Nathan", :email_address => "x@y.com") + ] + @channel.list_users + + @channel.privmsg "sup dude jd_wolk and jonathan and nathan" + @room.sent.last.must_match /sup dude JD Wolk and jonathan and Nathan/ + + end + + it "converts various nicknames" do + @room.users = [ + OpenStruct.new(:id => 11, :name => "JD Wolk", :email_address => "x@y.com"), + OpenStruct.new(:id => 12, :name => "Pedro Nascimento", :email_address => "x@y.com"), + OpenStruct.new(:id => 13, :name => "Joe", :email_address => "x@y.com") + ] + @channel.list_users + + @channel.privmsg "sup dude jd_wolk and pedro_nascimento!" + @room.sent.last.must_match /sup dude JD Wolk and Pedro Nascimento!/ + + end + it "converts leading nicknames followed by punctuation" do @room.users = [ OpenStruct.new(:id => 11, :name => "Bob Fred", :email_address => "x@y.com"),