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 50 51 |
# File 'lib/sendxmpp/message.rb', line 28 def initialize @batch=false if config.jid.nil? || config.jid.empty? Log.logger.error("JID is not defined.") Log.logger.error("Please use the -j option or the directive jid=... in .sendxmpprbrc") exit 1 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.") rescue Jabber::ClientAuthenticationFailure => e Log.logger.error("Authentication error for jid %s" % config.jid) exit 1 rescue SocketError => e Log.logger.error("There was an error connecting to the server: %s" % e.) exit 1 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
59 60 61 |
# File 'lib/sendxmpp/message.rb', line 59 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
75 76 77 |
# File 'lib/sendxmpp/message.rb', line 75 def self.(room) myself.(type: :group, user: room) end |
.message_to_user(user) ⇒ Object
Public: Send a message to a user
user - Receipient
67 68 69 |
# File 'lib/sendxmpp/message.rb', line 67 def self.(user) myself.(type: :user, user: user) end |
.myself ⇒ Object
Public: Singleton object getter
54 55 56 |
# File 'lib/sendxmpp/message.rb', line 54 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")
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/sendxmpp/message.rb', line 99 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
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/sendxmpp/message.rb', line 118 def process_batch(&block) Log.logger.info("Batch procession started.") unless block_given? Log.logger.error("Please specify a block to use this function.") exit 1 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
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/sendxmpp/message.rb', line 162 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
135 136 137 138 139 140 |
# File 'lib/sendxmpp/message.rb', line 135 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
148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/sendxmpp/message.rb', line 148 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
87 88 89 |
# File 'lib/sendxmpp/message.rb', line 87 def split_password(user) user.split(":") end |