Skip to content

Commit

Permalink
Merge pull request #57 from lunks/feature/convert-nicknames-on-any-pa…
Browse files Browse the repository at this point in the history
…rt-of-the-message

Convert any nickname on the message.
  • Loading branch information
lunks committed Aug 12, 2014
2 parents 2f77222 + 169d780 commit 2dedfc2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
6 changes: 2 additions & 4 deletions lib/camper_van/channel.rb
Expand Up @@ -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

Expand Down
36 changes: 36 additions & 0 deletions spec/camper_van/channel_spec.rb
Expand Up @@ -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"),
Expand Down

0 comments on commit 2dedfc2

Please sign in to comment.