-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlight_jabber_notify.rb
103 lines (92 loc) · 3.1 KB
/
highlight_jabber_notify.rb
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require 'rubygems'
require 'xmpp4r-simple'
"""
@authors : Cyril Mougel <[email protected]>, Martin Catty <[email protected]>
@goal : This weechat's plugin send xmpp notification on your jabber
account when you have been highlighted on a irc's channel or
when you received private message.
Just copy this script in your .weechat/ruby/autoload and set this
3 variables in your plugins.rc :
- ruby.highlight_jabber_notify.jid
- ruby.highlight_jabber_notify.password
- ruby.highlight_jabber_notify.recipient
@licence : GPL v2.
@started_at : 2008-03-03
@updated_at : 2008-03-14
@url : http://blog.noremember.org/public/highlight_jabber_notify.rb
@version : 0.1.1
"""
# Plugin initialization
def weechat_init
Weechat.register("highlight_jabber_notify", "0.1.1", "", "A plugin which send jabber's notifications on irc's highlights or private messages.")
Weechat.add_message_handler('weechat_highlight', 'highlight')
Weechat.add_message_handler('weechat_pv', 'pv')
Weechat.set_plugin_config("port", "5222") if Weechat.get_plugin_config("port").empty?
check_config ? Weechat::PLUGIN_RC_OK : Weechat::PLUGIN_RC_KO
end
# Print usage
def usage
Weechat.print %q{
You need the following informations in your config :
- jid
- password
- recipient
}
end
# Check that each required parameters have been found in the configuration file.
def check_config
%w(jid password recipient).each do |param|
(usage and return false) if Weechat.get_plugin_config(param).to_s.empty?
end
true
end
# Highlight callback
def highlight(server, args)
JabberNotification.notify("You have been highlighted on server #{server}, with the following message : #{args}")
return Weechat::PLUGIN_RC_OK
end
# Pv callback
def pv(server, args)
JabberNotification.notify("You have received a private message on server #{server}, with the following message : #{args}")
return Weechat::PLUGIN_RC_OK
end
# JabberNotification uses static variables to avoid repeated instanciation which
# are still the same.
class JabberNotification
def self.authenticate
Jabber::Simple.new(Weechat.get_plugin_config('jid'), Weechat.get_plugin_config('password'))
end
def self.jid
begin
@@im ||= self.authenticate
@@im.reconnect unless @@im.connected?
rescue Jabber::AuthenticationFailure => e
Weechat.print "Failed to authenticate : #{e.to_s}."
@@im = nil
end
@@im
end
def self.recipient
@@recipient ||= Weechat.get_plugin_config('recipient')
end
def self.notify(message)
unless self.jid.nil? # First call authenticate, next just return @@im
begin
@@im.deliver(self.recipient, message)
rescue Exception => e
Weechat.print "Failed to deliver message due to : #{e.to_s}."
retry
ensure
self.close
end
end
end
# Close the Jabber Connection
def self.close
if not @@im.nil? and @@im.connected?
# programm need to sleep, otherwise message may be lost.
sleep 3
@@im.disconnect
end
end
end