Class: Jah::XmppAgent
- Inherits:
-
Object
- Object
- Jah::XmppAgent
- Defined in:
- lib/jah/agents/xmpp.rb
Overview
include Blather::DSL
Constant Summary collapse
- ROSTER =
[]
- PUBSUB =
{:pubs => [], :subs => []}
- PEERS =
[]
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #alert_peers(msg) ⇒ Object
- #all_pubs ⇒ Object
-
#beautify(txt) ⇒ Object
TODO: need to write raw.…
- #create_pub(name) ⇒ Object
- #destroy_pub(name) ⇒ Object
- #execute_pub(code) ⇒ Object
-
#execute_ruby(code) ⇒ Object
Naive.…
- #execute_sh(code) ⇒ Object
- #fetch_pubs ⇒ Object
-
#initialize ⇒ XmppAgent
constructor
(jid, key, server, port=5222, debug=false, report=0).
- #list_pub(name) ⇒ Object
- #my_pubs ⇒ Object
- #process_items(items) ⇒ Object
- #process_message(to, msg, type = :chat) ⇒ Object
- #pub_node ⇒ Object
- #publish_pub(name, *text) ⇒ Object
- #run ⇒ Object
- #setup ⇒ Object
- #sub_pub(pub) ⇒ Object
- #unsub_pub(pub) ⇒ Object
Constructor Details
#initialize ⇒ XmppAgent
(jid, key, server, port=5222, debug=false, report=0)
13 14 15 16 17 18 19 |
# File 'lib/jah/agents/xmpp.rb', line 13 def initialize #(jid, key, server, port=5222, debug=false, report=0) Blather.logger.level = Logger::DEBUG if Opt.debug @report = Opt.report @client = Blather::Client.setup Opt.jid, Opt.key, Opt.host, Opt.port setup self end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
10 11 12 |
# File 'lib/jah/agents/xmpp.rb', line 10 def client @client end |
Instance Method Details
#alert_peers(msg) ⇒ Object
156 157 158 159 160 |
# File 'lib/jah/agents/xmpp.rb', line 156 def alert_peers(msg) PEERS.each do |peer| client.write Blather::Stanza::Message.new(peer, msg) end end |
#all_pubs ⇒ Object
128 129 130 131 132 |
# File 'lib/jah/agents/xmpp.rb', line 128 def all_pubs call = Blather::Stanza::DiscoItems.new call.to = pub_node client.write call end |
#beautify(txt) ⇒ Object
TODO: need to write raw.…
170 171 172 173 174 175 176 177 178 |
# File 'lib/jah/agents/xmpp.rb', line 170 def beautify(txt) #txt.gsub!(/\*(.*)\*/, "<span style=\"font-weight: bold;\">\\1</span>") #txt.gsub!(/\/(.*)\//, "<em>\\1</em>") # Italic #txt.gsub!(/\_(.*)\_/, "<span style=\"font-decoration: underline;\">\\1</span>") #txt.gsub!(/\-(.*)\-/, "<span style=\"font-decoration: line-through;\">\\1</span>") # <span style="font-size: large;">ok?</span> # <span style="color: #FF0000;">ok?</span> txt end |
#create_pub(name) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/jah/agents/xmpp.rb', line 106 def create_pub(name) client.write Blather::Stanza::PubSub::Create.new(:set, pub_node, name) { |n| yield n if block_given? } PUBSUB[:pubs] << name "Done." end |
#destroy_pub(name) ⇒ Object
122 123 124 125 126 |
# File 'lib/jah/agents/xmpp.rb', line 122 def destroy_pub(name) client.write Blather::Stanza::PubSubOwner::Delete.new(:set, pub_node, '/' + name) PUBSUB[:pubs] -= [name] "Done." end |
#execute_pub(code) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/jah/agents/xmpp.rb', line 87 def execute_pub(code) comm = code.split(" ") case comm[1] when "all" then all_pubs when "mine" then my_pubs when "list" then list_pub(comm[2]) when "fetch" then fetch_pubs || "Done." when "create" then create_pub(comm[2]) when /^sub\w*/ then sub_pub(comm[2]) when /^unsub\w*/ then unsub_pub(comm[2]) when "destroy" then destroy_pub(comm[2]) end end |
#execute_ruby(code) ⇒ Object
Naive.… to be improved
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/jah/agents/xmpp.rb', line 61 def execute_ruby(code) keywords = / class | module | def | do | while | for / if code =~ keywords return "Unmatched ends" if code.scan(keywords).length != code.scan(/end(\s|$)/).length end return "Unmatched.." if code.scan(/\{|\(|\[/).length != code.scan(/\}|\)|\]/).length return "Unmatched quotes.." if code.scan(/\"|\'/).length % 2 != 0 begin eval(code[1..-1]) || "nil" rescue => e "Fail => #{e}" end end |
#execute_sh(code) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/jah/agents/xmpp.rb', line 75 def execute_sh(code) return I18n.t(:no_sudo) if code =~ /sudo/ res = `#{code}`.chomp ex = $?.exitstatus if ex != 0 res << I18n.t(:exit_code, :ex => ex.to_s) else res = "\n" + res unless res.split("\n").length < 2 end res end |
#fetch_pubs ⇒ Object
151 152 153 154 |
# File 'lib/jah/agents/xmpp.rb', line 151 def fetch_pubs client.write Blather::Stanza::PubSub::Affiliations.new(:get, pub_node) client.write Blather::Stanza::PubSub::Subscriptions.new(:get, pub_node) end |
#list_pub(name) ⇒ Object
118 119 120 |
# File 'lib/jah/agents/xmpp.rb', line 118 def list_pub(name) client.write Blather::Stanza::PubSub::Items.request(pub_node, name) end |
#my_pubs ⇒ Object
134 135 136 137 138 139 |
# File 'lib/jah/agents/xmpp.rb', line 134 def my_pubs out = "--- Pubsubs\n" out << "Owner: #{PUBSUB[:pubs].join(', ')}\n" unless PUBSUB[:pubs].empty? out << "Subscribed: #{PUBSUB[:subs].join(', ')}\n" unless PUBSUB[:subs].empty? out == "" ? "No PubSubs." : out end |
#process_items(items) ⇒ Object
162 163 164 165 166 |
# File 'lib/jah/agents/xmpp.rb', line 162 def process_items(items) items.map do |item| "\nItem: #{item.payload.strip}" end end |
#process_message(to, msg, type = :chat) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/jah/agents/xmpp.rb', line 21 def (to, msg, type = :chat) args = msg.split(" ") if comm = Jah::Command.find(msg) puts "Commmand => #{comm} | #{msg}" body = comm[2].send(comm[0], *args[1..-1]) elsif pub = PUBSUB[:pubs].find { |x| msg =~ /^#{x}:/ } publish_pub(pub, *args[1..-1]) body = "Publishing...." else keywords = %w{ start stop restart monitor unmonitor } kind = case msg when /#{keywords.join("|")}/ then :god when /^\!/ then :ruby when /^pub\s/ then :pub else :sh end body = case kind when :sh puts "Executing *sh command => #{msg}" "$> #{execute_sh(msg)}" when :pub PEERS << to.stripped unless PEERS.find { |p| p == [to.stripped] } puts "Executing pubsub command => #{msg}" "P> #{execute_pub(msg)}" when :ruby puts "Executing ruby command => #{msg}" "=> #{execute_ruby(msg)}" when :god puts "Executing god command => #{msg}" "G> #{execute_sh('god ' + msg)}" else "dunno what to do...." end end #beautify(body) [ Blather::Stanza::Message.new(to, body, type)] rescue => e "Something is wrong.. #{e}\n#{e.backtrace.join("\n")}" end |
#pub_node ⇒ Object
102 103 104 |
# File 'lib/jah/agents/xmpp.rb', line 102 def pub_node @pub_node ||= "pubsub." + client.jid.domain end |
#publish_pub(name, *text) ⇒ Object
113 114 115 116 |
# File 'lib/jah/agents/xmpp.rb', line 113 def publish_pub(name, *text) client.write Blather::Stanza::PubSub::Publish.new(pub_node, name, :set, text.join) text.join end |
#run ⇒ Object
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/jah/agents/xmpp.rb', line 293 def run puts "Starting Jah Client...#{client.jid.stripped}" EM.run do client.run if @report != 0 puts "will report..." # TODO # EM.add_periodic_timer(@report) do # puts " I'm ok.. " # client.write Blather::Stanza::Message.new( # "[email protected]", Jah::Command.quick_results) # end end end end |
#setup ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/jah/agents/xmpp.rb', line 188 def setup # return if client && client.setup? client.register_handler(:ready) do puts "Connected!" ROSTER << [client.roster.items.keys, Opt.groups].flatten.uniq ROSTER.flatten! ROSTER.select { |j| j =~ /\@conference\./ }.each do |c| presence = Blather::Stanza::Presence.new presence.to = "#{c}/#{Opt.hostname}" client.write presence end fetch_pubs end client.register_handler :subscription, :request? do |s| if ROSTER.include?(s.from.stripped.to_s) puts "[REQUEST] Approve #{s}" client.write s.approve! else puts "[REQUEST] Refuse #{s}" client.write s.refuse! end end # client.register_handler :message, :chat?, :body => 'exit' do |m| # client.write Blather::Stanza::Message.new(m.from, 'Exiting...') # client.close # end #client.register_handler :roster, [], #client.register_handler :message, :error?, :body do |m| #client.register_handler :message, :headline?, :body do |m| #client.register_handler :message, :normal?, :body do |m| client.register_handler :pubsub_affiliations, :affiliations do |m| puts "[PUB] => #{m.inspect}" m.each do |af| puts "[PUB ITEM] => #{af.inspect}" PUBSUB[:pubs] = af[1].map { |p| p.gsub(/\//, '') } end end client.register_handler :pubsub_subscriptions, :subscriptions do |m| puts "[SUB] => #{m.inspect}" m.each do |af| puts "[SUB ITEM] => #{af.inspect}" PUBSUB[:subs] = af[1].map { |p| p[:node].gsub(/\//, '') } end end client.register_handler :pubsub_event, :items do |m| puts "[PUBSUB EV] => #{m.inspect}" alert_peers "PubSub: #{m.node} #{process_items(m.items)}" end client.register_handler :pubsub_items, :items do |m| puts "[PUBSUB ITEMS] => #{m.inspect}" alert_peers "PubSub: #{m.node} #{process_items(m.items)}" end client.register_handler :disco_items do |r| puts "[ITEM] => #{r}" # Pub.delete_all # PubItem.delete_all for item in r.items puts "[IT] => #{item.name} on #{item.node.class}" # next if item.name =~ /^home$/ if item.node =~ /\// puts "[PUBSUB] => #{item.name} on #{item.node}" alert_peers item.name else if item.jid.to_s =~ /conference\./ puts "[GROUP] => #{item.name} on #{item.node}" else puts "[USER] => #{item.jid} name #{item.name}" end end end end client.register_handler :message, :groupchat? do |m| if m.body =~ Regexp.new(Opt.hostname) body = m.body.split(":")[-1].strip else body = m.body end if m.body =~ /^!|^>|^\\|#{Opt.hostname}/ && m.to_s !~ /x.*:delay/ #delay.nil? puts "[GROUP] => #{m.inspect}" for msg in (m.from.stripped, body, :groupchat) client.write msg end end end client.register_handler :message, :chat?, :body do |m| if ROSTER.include?(m.from.stripped.to_s) puts "[PVT] => #{m.inspect}" for msg in (m.from, m.body) client.write msg end end end end |
#sub_pub(pub) ⇒ Object
141 142 143 144 |
# File 'lib/jah/agents/xmpp.rb', line 141 def sub_pub(pub) client.write Blather::Stanza::PubSub::Subscribe.new(:set, pub_node, '/' + pub, client.jid.stripped) PUBSUB[:subs] << pub end |
#unsub_pub(pub) ⇒ Object
146 147 148 149 |
# File 'lib/jah/agents/xmpp.rb', line 146 def unsub_pub(pub) client.write Blather::Stanza::PubSub::Unsubscribe.new(:set, pub_node, '/' + pub, client.jid.stripped) PUBSUB[:subs] -= [pub] end |