From 02d4148bd74e23c9cc4b1d81c1116d7e306a8cdb Mon Sep 17 00:00:00 2001 From: damau Date: Thu, 31 Jan 2013 10:26:57 +0000 Subject: [PATCH] added support for any jabber and ability to send to rooms --- app/models/notification_services/gtalk_service.rb | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/app/models/notification_services/gtalk_service.rb b/app/models/notification_services/gtalk_service.rb index f8ed281..4a4c41b 100644 --- a/app/models/notification_services/gtalk_service.rb +++ b/app/models/notification_services/gtalk_service.rb @@ -9,31 +9,64 @@ class NotificationServices::GtalkService < NotificationService :placeholder => "password", :label => "Password" }], + [:user_id, { + :placeholder => "touser@example.com, anotheruser@example.com", + :label => "Send To User(s)" + }, :room_id], [:room_id, { - :placeholder => "touser@example.com, anotheruser@example.com", - :label => "Send To User(s)" + :placeholder => "toroom@conference.example.com", + :label => "Send To Room (one only)" + }, :user_id], + [ :service, { + :placeholder => "talk.google.com", + :label => "Jabber Service" }], + [ :service_url, { + :placeholder => "http://www.google.com/talk/", + :label => "Link To Jabber Service" + }] ] def check_params - if Fields.detect {|f| self[f[0]].blank? } - errors.add :base, 'You must specify your Username, Password and To User(s)' + if Fields.detect { |f| self[f[0]].blank? && self[f[2]].blank? } + errors.add :base, + """You must specify your Username, Password, service, service_url + and either rooms or users to send to or both""" end end def url - "http://www.google.com/talk/" + "http://www.google.com/talk/" || service_url end def create_notification(problem) # build the xmpp client client = Jabber::Client.new(Jabber::JID.new(subdomain)) - client.connect("talk.google.com") + client.connect(service) client.auth(api_token) + #has to look like this to be formatted properly in the client + message = """#{problem.app.name.to_s} +http://#{Errbit::Config.host}/apps/#{problem.app.id.to_s} +#{notification_description problem}""" + # post the issue to the xmpp room(s) - room_id.gsub(/ /i, ",").gsub(/;/i, ",").split(",").map(&:strip).reject(&:empty?).each do |room| - client.send(Jabber::Message.new(room, "[errbit] http://#{Errbit::Config.host}/apps/#{problem.app.id.to_s} #{notification_description problem}")) + send_to_users(client, message) unless user_id.blank? + send_to_muc(client, message) unless room_id.blank? + end + + private + + def send_to_users client, message + user_id.gsub(/ /i, ",").gsub(/;/i, ",").split(",").map(&:strip).reject(&:empty?).each do |user| + client.send(Jabber::Message.new(user, message)) end end + + def send_to_muc client, message + #TODO: set this so that it can send to multiple rooms like users, nb multiple room joins in one send fail randomly so leave as one room for the moment + muc = Jabber::MUC::SimpleMUCClient.new(client) + muc.join(room_id + "/errbit") + muc.send(Jabber::Message.new(room_id, message)) + end end -- libgit2 0.21.2