# Jabber::Simple does some insane kind of queueing if it thinks
# we are not in their buddy list (which is always) so messages
# never get sent before we disconnect. This forces the library
# to assume the recipient is a buddy.
class Jabber::Simple
  def subscribed_to?(x); true; end
end

service :jabber do |data, payload|
  repository = payload['repository']['name']
  branch     = payload['ref'].split('/').last
  im         = Jabber::Simple.new(jabber_user, jabber_password)


  # Accept any friend request
  im.accept_subscriptions = true

  #Split multiple addresses into array
  recipients  = data['user'].split(',')

  #Send message to each member in array 
  recipients.each do |recipient|
    # Ask recipient to be our buddy if need be
    im.add(recipient)


    payload['commits'].each do |commit|
      sha1 = commit['id']
      im.deliver recipient, <<EOM
#{repository}: #{commit['author']['name']} #{branch} SHA1-#{sha1[0..6]}"

#{commit['message']}
#{commit['url']}
EOM
  end
    end

  im.disconnect
end

# Jabber::Simple does some insane kind of queueing if it thinks
# we are not in their buddy list (which is always) so messages
# never get sent before we disconnect. This forces the library
# to assume the recipient is a buddy.
class Jabber::Simple
  def subscribed_to?(x); true; end
end

service :jabber do |data, payload|
  repository = payload['repository']['name']
  branch     = payload['ref'].split('/').last
  im         = Jabber::Simple.new(jabber_user, jabber_password)


  # Accept any friend request
  im.accept_subscriptions = true

  #Split multiple addresses into array, removing duplicates
  recipients  = data['user'].split(',').uniq

  #Send message to each member in array (Limit to 25 members to prevent overloading something, if this is not and issue, just remove the [0..24] from recipients 
  recipients[0..24].each do |recipient|
    # Ask recipient to be our buddy if need be
    im.add(recipient)


    payload['commits'].each do |commit|
      sha1 = commit['id']
      im.deliver recipient, <<EOM
#{repository}: #{commit['author']['name']} #{branch} SHA1-#{sha1[0..6]}"

#{commit['message']}
#{commit['url']}
EOM
  end
    end

  im.disconnect
end

Add limitation so it only sends to 25 people, to prevent overloading the server. Also run uniq on the array so it removes duplicate names (to prevent possible spamming of a user).
Add limitation so it only sends to 25 people, to prevent overloading the server.  Also run uniq on the array so it removes duplicate names (to prevent possible spamming of a user).

Signed-off-by: PJ Hyett <2e83d8358152ba11d38d54d2bc18c2dcbf7dc0dd@gmail.com>