class Sendxmpp::CLI

Class CLI

Input via STDIN or -m option:

Examples

sendxmpprb -m "server down notification" user mosny@jabber.org
echo "server down"|sendxmpprb user mosny@jabber.org
...

Public Class Methods

new(*args) click to toggle source

Public: Initialize a new Object from CLI

args - Arguments, passed to Thor

Raises IniFile::Error on invalid configuration Raises ArgumentError if the main hash key was not found

Calls superclass method
# File lib/sendxmpp/cli.rb, line 38
def initialize(*args)
  super
  local_conf = options.dup
  local_conf.delete_if{|k,v|v.nil?||(v.kind_of?(String) && v.empty?)}
  update_config(local_conf)
  if File.exists?(options[:config])
    conf = IniFile.load(options[:config])["sendxmpp"]
    if conf.nil? || conf.empty?
      raise ArgumentError, "No [sendxmpp] section in ini file found!"
    end
  conf.merge!(local_conf)
  update_config(conf)
  end

  Log.logger.debug("finished loading configuration.")
  $stdout.sync = true
end

Public Instance Methods

chat(*jids) click to toggle source

Public: Send a message to a single/multiple multi user chatrooms.

Messages will be sent to each chat seperately

jids - Array of MUC(Multi user chat) jids.

Examples

sendxmpp chat edv@conference.jabber.org -m "test"
echo "test" | sendxmpp chat edv@conference.jabber.org:password
sendxmpp chat edv@conference.jabber.org < /var/log/system.log

chat(["edv@conference.jabber.org"])
chat(["edv@conference.jabber.org", "staff@conference.jabber.org"])

Returns 0 or 1 exit codes

# File lib/sendxmpp/cli.rb, line 105
def chat(*jids)
  Log.logger.debug("Received call for chat method")
  fetch_stdin
  unless jids.kind_of?(Array)
    Log.logger.error("Throwing ArgumentError because Jids is not an array.")
    raise ArgumentError, "Jids needs to be an Array got #{jids.class}"
  end

  check_messagequeue

  Message.batch do
    jids.each do |jid|
      Message.message_to_room(jid)
    end
  end

end
check_messagequeue() click to toggle source

Public: Check for presence of a message. The message is needed, because otherwise

there would be nothing to do.

Exits on failure

# File lib/sendxmpp/cli.rb, line 158
def check_messagequeue
  if !config.message
    Log.logger.error("No message to send. Exiting.")
    Log.logger.error("See https://github.com/nirnanaaa/sendxmpprb/wiki/Sending-messages for available message formats.")
    exit 1
  end
end
fetch_stdin() click to toggle source

Public: Fetch stdin input

Before reading, stdin will be checked for presence.

THIS METHOD ONLY WORKS ON UNIX BASED SYSTEMS, AS FCNTL IS NOT AVAILABLE ON WINDOWS

Returns nothing

# File lib/sendxmpp/cli.rb, line 186
def fetch_stdin
  require 'fcntl'
  if !config.message
    Log.logger.info("messages empty. using stdin")
    if STDIN.fcntl(Fcntl::F_GETFL, 0) == 0
      Log.logger.info("stdin not empty. Reading from it")
      while $stdin.gets
        config.message ||= ""
        config.message << $_
      end
    else
      return
    end
  end
end
fromconf() click to toggle source

Public: Will read the receipients from configuration file and send the message to the users

Examples

sendxmpp fromconf -m "test"
echo "test" |sendxmpp fromconf
sendxmpp fromconf < /var/log/system.log

Returns an exit code 0 or 1

# File lib/sendxmpp/cli.rb, line 132
def fromconf
  Log.logger.debug("Received call for fromconf method")
  if !config.receipients
    Log.logger.error("No receipient(s) specified.")
    Log.logger.error("Please read https://github.com/nirnanaaa/sendxmpprb/wiki/Configuration on how to specify receipients.")
    exit 1
  end
  fetch_stdin
  check_messagequeue
  users, muc = parse_receipients
  Message.batch do 
    users.each do |jid|
      Message.message_to_user(jid)
    end
    muc.each do |jid|
      Message.message_to_room(jid)
    end
  end

end
parse_receipients() click to toggle source

Public: read receipients from configuration

Returns an array of types

# File lib/sendxmpp/cli.rb, line 169
def parse_receipients
  types = [[],[]]
  config.receipients.split(",").each do |r|
    type, jid = r.split("->")
    type == "muc" ? types.last << jid : types.first << jid
  end
  types
end
user(*jids) click to toggle source

Public: Send a message to multiple users.

Message will be sent to each user seperately

jids - Receipient(s) can be one or more

Examples

sendxmpp user mosny@jabber.org -m "test"
echo "test" | sendxmpp user mosny@jabber.org
sendxmpp user mosny@jabber.org < /var/log/system.log

user(["mosny@jabber.org"])
user(["mosny@jabber.org", "someone@jabber.org"])

Returns 0 or 1 exit codes

# File lib/sendxmpp/cli.rb, line 72
def user(*jids)
  Log.logger.debug("Received call for user method")
  fetch_stdin
  unless jids.kind_of?(Array)
    Log.logger.error("Throwing ArgumentError because Jids is not an array.")
    raise ArgumentError, "Jids needs to be an Array got #{jids.class}"
  end

  check_messagequeue

  Message.batch do
    jids.each do |jid|
      Message.message_to_user(jid)
    end
  end
end