-
Notifications
You must be signed in to change notification settings - Fork 2
/
xmpp_sender.rb
163 lines (147 loc) · 5.58 KB
/
xmpp_sender.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/local/bin/ruby
require 'yaml'
require 'getoptlong'
require 'rubygems'
gem 'xmpp4r', '~>0.5'
require 'xmpp4r/client'
# Incapsulates all utility logic
#
class XmppSender
include Jabber
@@version = "1.0.1"
@@instant_variables = %w/server login password to subject body/
@@instant_variables.each{ |a| attr_accessor a.intern }
@@opts = GetoptLong.new(
[ "--config", "-c", GetoptLong::REQUIRED_ARGUMENT ],
[ "--server", "-s", GetoptLong::REQUIRED_ARGUMENT ],
[ "--login", "-l", GetoptLong::REQUIRED_ARGUMENT ],
[ "--password", "-p", GetoptLong::REQUIRED_ARGUMENT ],
[ "--to", "-t", GetoptLong::REQUIRED_ARGUMENT ],
[ "--subject", "-j", GetoptLong::REQUIRED_ARGUMENT ],
[ "--body", "-b", GetoptLong::REQUIRED_ARGUMENT ],
[ "--usage", "-u", GetoptLong::NO_ARGUMENT ],
[ "--version", "-v", GetoptLong::NO_ARGUMENT ]
)
# Sends a message composed of @subject and @body
# @return [void]
#
def send
#Jabber::debug = true
jid=JID::new "#{@login}@#{@server}"
cl = Client::new(jid)
cl.connect
cl.auth @password
m = Message::new("#{@to}@#{@server}", @body).set_type(:normal).set_id('1').set_subject(@subject)
cl.send m
end
# Sets up instance variables named in @@instance_variables with values
# passed by hash
# @param [Hash] hash contains values for instance variables
# @return [void]
#
def set_instance_variables_from hash
@@instant_variables.each do |var_name|
var_value = hash[ var_name ]
var_name = "@" + var_name
self.instance_variable_set(var_name, var_value) if self.instance_variable_get(var_name).nil?
end
end
# Prints out instance variables named in @@instance_variables
# @return [void]
#
def show_instance_variables
@@instant_variables.each do |var_name|
var_name = "@" + var_name
var_value = self.instance_variable_get var_name
puts "%12s: %s"%[var_name, var_value]
end
end
# Reads configuration parameters from config file
# @return [void]
#
def read_config
@config_file = @config || 'xmpp_sender.yml'
begin
settings = YAML.load_file( @config_file )['xmpp_sender']
rescue
$stderr.puts "Error reading config file '#{@config_file}'"
XmppSender.printusage -1
end
self.set_instance_variables_from( settings )
self.show_instance_variables
self.exit_if_nil @password, -2, "Can not authenticate due to password is nil"
self.exit_if_nil @login, -3, "Can not authenticate due to login is nil"
self.exit_if_nil @server, -4, "Can not authenticate due to server is nil"
self.exit_if_nil @to, -5, "Can not send because 'to' is nil"
self.exit_if_nil @body || @subject, -6, "Can not send because both subject and body are nil"
end
# Prints msg and exits with *error_code* if arg is nil
# @param [Object] arg argument to check for nil
# @param [Integer] exit_code the code process should return if arg is nil
# @param [String] msg message to print out to STDERR if arg is nil
# @return [void]
#
def exit_if_nil arg, exit_code, msg
if arg.nil?
$stderr.puts msg
exit exit_code
end
end
# Prints usage and exits with *error_code*
# @param [Integer] error_code process exit code
# @return [void]
#
def XmppSender.printusage(error_code)
print "xmpp_sender -- sends a message over XMPP protocol\n"
print "Usage: xmpp [POSIX or GNU style options]\n\n"
print "POSIX options GNU long options\n"
print " -c config_file_name --config config_file_name\n"
print " -s server_name --server server_name\n"
print " -l login --login login\n"
print " -p password --password password\n"
print " -t to --to to\n"
print " -b body --body body\n"
print " -j subject --subject subject\n"
print " -u --usage\n"
print " -v --version\n\n"
print "Examples: \n"
print "xmpp_sender -b Erlang\ Programming.pdf sends a message with specified body\n"
print "xmpp_sender -t VassiliyPupkin -b Erlang\ Programming.pdf sends a message with specified body to VassiliyPupkin\n\n"
print "All options could be saved to the config file in YAML format. See xmpp_sender.yml\n"
print "Command line options have precedence over the options in config file\n"
print "For licensing terms, see 'license' file\n\n"
exit(error_code)
end
# Processes command line parameters
# @return [void]
#
def get_opts
begin
@@opts.each do |opt, arg|
case opt
when "--config" then @config = arg
when "--server" then @server = arg
when "--login" then @login = arg
when "--password" then @password = arg
when "--to" then @to = arg
when "--subject" then @subject = arg
when "--body" then @body = arg
when "--help" then XmppSender.printusage 0
when "--usage" then XmppSender.printusage 0
when "--version" then puts @@version; exit 0
end
end
rescue GetoptLong::InvalidOption => e
puts "ignored #{e.message}"
end
end
end
if __FILE__ == $0
dir = /(.*\/)src$/.match( File.dirname $0 )[1] rescue dir = './'
puts "working directory: %s"%dir
Dir.chdir dir
xmpp_sender = XmppSender.new
xmpp_sender.get_opts
xmpp_sender.read_config
xmpp_sender.send
end