gtalk_service.rb
2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class NotificationServices::GtalkService < NotificationService
LABEL = "gtalk"
FIELDS += [
[:subdomain, {
placeholder: "username@example.com",
label: "Username"
}],
[:api_token, {
placeholder: "password",
label: "Password"
}],
[:user_id, {
placeholder: "touser@example.com, anotheruser@example.com",
label: "Send To User(s)"
}, :room_id],
[:room_id, {
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? && 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
service_url || "http://www.google.com/talk/"
end
def create_notification(problem)
# build the xmpp client
client = nil
Timeout.timeout(5) do
client = Jabber::Client.new(Jabber::JID.new(subdomain))
client.connect(service)
client.auth(api_token)
# has to look like this to be formatted properly in the client
message = """#{problem.app.name}\n" \
"#{Errbit::Config.protocol}://#{Errbit::Config.host}/apps/#{problem.app.id}\n" \
"#{notification_description problem}"""
# post the issue to the xmpp room(s)
send_to_users(client, message) unless user_id.blank?
send_to_muc(client, message) unless room_id.blank?
end
ensure
client.close unless client.nil?
end
private
def send_to_users(client, message)
user_id.tr(' ', ",").tr(';', ",").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