Advertisement
Guest User

sendxmpp.rb

a guest
Mar 16th, 2012
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 4.13 KB | None | 0 0
  1. #!/usr/bin/env /usr/bin/ruby
  2. #
  3. #   * ----------------------------------------------------------------------------
  4. #   * "THE BEER-WARE LICENSE" (Revision 42):
  5. #   * <kim@maisspace.org> wrote this file. As long as you retain this notice you
  6. #   * can do whatever you want with this stuff. If we meet some day, and you think
  7. #   * this stuff is worth it, you can buy me a beer in return Kim Jahn
  8. #   * ----------------------------------------------------------------------------
  9. #
  10.  
  11. require 'xmpp4r'
  12. require 'optparse'
  13.  
  14. usage = 'Usage: sendxmpp.rb [options] receiver1@jabber.example.com receiver2@jabber.example.com'
  15.  
  16. options = {}
  17.  
  18. optparse = OptionParser.new do|opts|
  19.  
  20.   opts.banner = usage
  21.  
  22. # not yet implemented
  23. #  options['chatroom'] = ""
  24. #  opts.on( '-c', '--chatroom CHATROOM', 'chatroom' ) do|chatroom|
  25. #    options['chatroom'] = chatroom
  26. #  end
  27.  
  28.   options['debug'] = false
  29.   opts.on( '-d', '--debug', 'debug' ) do
  30.     options['debug'] = true
  31.   end
  32.  
  33.   options['file'] = ENV['HOME']+'/.sendxmpp'
  34.   opts.on( '-f', '--file FILE', 'Use FILE for configuration instead of ~/.sendxmpp' ) do|file|
  35.     options['file'] = file
  36.   end
  37.  
  38.   opts.on( '-h', '--help', 'Display this screen' ) do
  39.     puts opts
  40.     exit
  41.   end
  42.  
  43. # not yet implemented
  44. #  options['interactive'] = false
  45. #  opts.on( '-i', '--interactive', 'interactive' ) do
  46. #    options['interactive'] = true
  47. #  end
  48.  
  49. # not yet implemented
  50. #  options['jserver'] = ""
  51. #  opts.on( '-j', '--jserver SERVER', 'jserver' ) do|jserver|
  52. #    options['jserver'] = jserver
  53. #  end
  54.   options['message'] = false
  55.   opts.on( '-m', '--message MESSAGE', 'use MESSAGE instead of stdin' ) do|message|
  56.     options['message'] = message
  57.   end
  58.  
  59.   options['password'] = nil
  60.   opts.on( '-p', '--password PASSWORD', 'use PASSWORD instead of the one in the configuration file' ) do|password|
  61.     options['password'] = password
  62.   end
  63.  
  64.   options["resource"] = "sendxmpp"
  65.   opts.on( "-r", "--resource RESOURCE", "use RESOURCE instead of the on in the configuration file (default: sendxmpp)" ) do|resource|
  66.     options["resource"] = resource
  67.   end
  68.  
  69. # not yet implemented
  70. #  options['subject'] = false
  71. #  opts.on( '-s', '--subject SUBJECT', 'subject' ) do|subject|
  72. #    options['subject'] = subject
  73. #  end
  74.  
  75. # not yet implemented
  76. #  options['tls'] = false
  77. #  opts.on( '-t', '--tls', 'tls' ) do
  78. #    options['tls'] = true
  79. #  end
  80.  
  81.   options['username'] = nil
  82.   opts.on( '-u', '--username USERNAME', 'use USERNAME instead of the one in the configuration file' ) do|username|
  83.     options['username'] = username
  84.   end
  85.  
  86. # not yet implemented
  87. #  options['verbose'] = false
  88. #  opts.on( '-v', '--verbose', 'Output more information' ) do
  89. #    options['verbose'] = true
  90. #  end
  91.                
  92. end
  93.  
  94. optparse.parse!
  95.  
  96. if File.exists?(options['file'])
  97.  
  98.   config = {}
  99.  
  100.   File.foreach(options['file']) do |line|
  101.     line.strip!
  102.     if (line[0] != ?# and line =~ /\S/ )
  103.       i = line.index('=')
  104.       if (i)
  105.         config[line[0..i - 1].strip] = line[i + 1..-1].strip
  106.       else
  107.         config[line] = ''
  108.       end
  109.     end
  110.   end
  111.  
  112.   if options['debug'] == true
  113.     puts config
  114.   end
  115.  
  116.   if config['username'] != nil && options['username'] == nil
  117.     options['username'] = config['username']
  118.   end
  119.  
  120.   if config['password'] != nil && options['password'] == nil
  121.     options['password'] = config['password']
  122.   end
  123.  
  124.   if config['resource'] != nil && options['resource'] == nil
  125.     options['resource'] = config['resource']
  126.   end
  127.  
  128.  
  129. end
  130.  
  131. unless ARGV.length >= 1
  132.   puts usage
  133.   exit
  134. end
  135.  
  136. if options['username'] == nil
  137.   puts 'Dude, you need to set the username'
  138.   exit
  139. end
  140.  
  141. if options['password'] == nil
  142.   puts 'Dude, you need to set the password in the configuration file'
  143.   exit
  144. end
  145.  
  146. if options['debug'] == true
  147.   puts options
  148. end
  149.  
  150. if options['message']
  151.   text = options['message']
  152. else
  153.   text = STDIN.gets
  154. end
  155.  
  156. ARGV.each do |receiver|
  157.  
  158.   client = Jabber::Client.new(Jabber::JID.new(options['username']+"/"+options['resource']))
  159.  
  160.   client.connect
  161.  
  162.   client.auth(options['password'])
  163.  
  164.   msg = Jabber::Message.new(receiver,text)
  165.  
  166.   msg.type = :chat
  167.  
  168.   client.send(msg)
  169.  
  170. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement