Class: Sendxmpp::CLI

Inherits:
Thor
  • Object
show all
Includes:
Config
Defined in:
lib/sendxmpp/cli.rb

Overview

Class CLI

Input via STDIN or -m option:

Examples

sendxmpprb -m "server down notification" user [email protected]
echo "server down"|sendxmpprb user [email protected]
...

Instance Method Summary collapse

Methods included from Config

#config, #update_config

Constructor Details

#initialize(*args) ⇒ CLI

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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

Instance Method Details

#chat(*jids) ⇒ Object

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 [email protected] -m "test"
echo "test" | sendxmpp chat [email protected]:password
sendxmpp chat [email protected] < /var/log/system.log

chat(["[email protected]"])
chat(["[email protected]", "[email protected]"])

Returns 0 or 1 exit codes



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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

#fromconfObject

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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# 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

#user(*jids) ⇒ Object

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 [email protected] -m "test"
echo "test" | sendxmpp user [email protected]
sendxmpp user [email protected] < /var/log/system.log

user(["[email protected]"])
user(["[email protected]", "[email protected]"])

Returns 0 or 1 exit codes



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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