Class: Sendxmpp::Message
- Inherits:
-
Object
- Object
- Sendxmpp::Message
- Includes:
- Jabber, Config
- Defined in:
- lib/sendxmpp/message.rb
Overview
Public: Message class
requires an initialized Config class.
Instance Attribute Summary collapse
-
#batch ⇒ Object
readonly
Public: Getter for the batch status.
-
#client ⇒ Object
Public: Getter / Setter for the jabber client.
Class Method Summary collapse
-
.batch(&block) ⇒ Object
Public: Batch relay.
-
.message_to_room(room) ⇒ Object
Public: Send a message to a chatroom.
-
.message_to_user(user) ⇒ Object
Public: Send a message to a user.
-
.myself ⇒ Object
Public: Singleton object getter.
Instance Method Summary collapse
-
#initialize ⇒ Message
constructor
Public: Initializer.
-
#message(options) ⇒ Object
Public: Send a message.
-
#process_batch(&block) ⇒ Object
Public: Enables batch procession for this session.
-
#receipients ⇒ Object
Public: Getter for receipients.
-
#send_batch ⇒ Object
Public: Send the batch out.
-
#send_message(user) ⇒ Object
Public: Send a message to a single user.
-
#send_muc_message(room, password = nil) ⇒ Object
Public: Send a message to a MUC (Multi User Chat).
-
#split_password(user) ⇒ Object
Public: Split a special formatted JID into the following parts: JID - normal XMPP JID Password - XMPP room password.
Methods included from Config
Constructor Details
#initialize ⇒ Message
Public: Initializer
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/sendxmpp/message.rb', line 28 def initialize @batch=false if config.jid.nil? || config.jid.empty? raise ArgumentError, <<-RAISE \rJID is not configured. Please use the configuration file or specify the \r-j option. RAISE end jid = JID.new(config.jid) Log.logger.debug("Initializing a new Jabber client instance.") self.client = Client.new(jid) Log.logger.debug("Initialized a new Jabber client instance.") Log.logger.debug("Connecting to Jabber server.") client.connect(config.server, config.port) Log.logger.debug("Connected to Jabber server.") Log.logger.debug("Authenticating with Jabber server.") client.auth(config.password) Log.logger.debug("Authenticating with Jabber server.") end |
Instance Attribute Details
#batch ⇒ Object (readonly)
Public: Getter for the batch status
13 14 15 |
# File 'lib/sendxmpp/message.rb', line 13 def batch @batch end |
#client ⇒ Object
Public: Getter / Setter for the jabber client
16 17 18 |
# File 'lib/sendxmpp/message.rb', line 16 def client @client end |
Class Method Details
.batch(&block) ⇒ Object
Public: Batch relay
57 58 59 |
# File 'lib/sendxmpp/message.rb', line 57 def self.batch(&block) myself.process_batch(&block) end |
.message_to_room(room) ⇒ Object
Public: Send a message to a chatroom
room - Room to send message to
73 74 75 |
# File 'lib/sendxmpp/message.rb', line 73 def self.(room) myself.(type: :group, user: room) end |
.message_to_user(user) ⇒ Object
Public: Send a message to a user
user - Receipient
65 66 67 |
# File 'lib/sendxmpp/message.rb', line 65 def self.(user) myself.(type: :user, user: user) end |
.myself ⇒ Object
Public: Singleton object getter
52 53 54 |
# File 'lib/sendxmpp/message.rb', line 52 def self.myself @self ||= self.new end |
Instance Method Details
#message(options) ⇒ Object
Public: Send a message
options - Message hash options
Examples
(type: :user, user: "[email protected]")
(type: :group, user: "[email protected]:password")
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/sendxmpp/message.rb', line 97 def () return if config..empty? if batch == true receipients << else if [:type] == :user (config., [:user]) else group, password = split_password([:user]) (config., group, password) end end end |
#process_batch(&block) ⇒ Object
Public: Enables batch procession for this session
block - Block to execute as a batch
Returns false
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/sendxmpp/message.rb', line 116 def process_batch(&block) Log.logger.info("Batch procession started.") unless block_given? raise ArgumentError, "Do not use this function without a block" end @batch=true yield send_batch @batch=false end |
#receipients ⇒ Object
Public: Getter for receipients
Returns an Array
21 22 23 |
# File 'lib/sendxmpp/message.rb', line 21 def receipients @receipients ||= [] end |
#send_batch ⇒ Object
Public: Send the batch out
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/sendxmpp/message.rb', line 159 def send_batch receipients.flatten.each do |rcpt| user, password = split_password(rcpt[:user]) if rcpt[:type] == :user (user) elsif rcpt[:type] == :group (user, password) end Log.logger.debug("Removing item %p from queue" % rcpt) receipients.delete(rcpt) end end |
#send_message(user) ⇒ Object
Public: Send a message to a single user
user - JID of the receipient
Returns nothing
132 133 134 135 136 137 |
# File 'lib/sendxmpp/message.rb', line 132 def (user) Log.logger.debug("sending message to user %s" % user) m = Jabber::Message.new(user, config.) client.send(m) Log.logger.debug("sent message") end |
#send_muc_message(room, password = nil) ⇒ Object
Public: Send a message to a MUC (Multi User Chat)
room - Room to send the message to password - Password for the chatroom if required
Returns nothing
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sendxmpp/message.rb', line 145 def (room, password=nil) Log.logger.debug("including file xmpp4r/muc") require 'xmpp4r/muc' m = Jabber::Message.new(room, config.) muc = MUC::MUCClient.new(client) if !muc.active? Log.logger.info("Joining room %s" % room) muc.join(JID.new(room + '/' + config.resource), password) Log.logger.info("Joined room %s" % room) end muc.send m end |
#split_password(user) ⇒ Object
Public: Split a special formatted JID into the following
parts:
JID - normal XMPP JID
Password - XMPP room password
user - JID to split up
Returns an Array of 2 elements
85 86 87 |
# File 'lib/sendxmpp/message.rb', line 85 def split_password(user) user.split(":") end |