rendered paste body#!/usr/bin/ruby require 'socket'require 'helper'require 'drb'class TCPSocket def hola "hola wey" end def send_command(a,b=nil) if a == :finalizar_comando self.puts("ActionID: #{b}\r\n") self.puts("\r\n") else self.puts("#{a.to_s.capitalize}: #{b.to_s}\r\n") end endendclass AsteriskEventQueue attr_accessor :queue def initialize(ast) @queue = [] @current_event = nil @ast = ast end def add(str) @current_event = AsteriskEvent.new if @current_event.nil? if str == "\r\n" dispatch @current_event @current_event = nil else k, v = AsteriskEvent.get_key_and_value_from_ast(str) if k == "Event" @current_event.name = v elsif k == "ActionID" @current_event.actionid = v.to_s else @current_event.name = "Message" if k.downcase == "message" @current_event.body[k.downcase.to_sym] = v end end end def dispatch(ev) case ev.name when "QueueParams" AsteriskQueue.new(ev, @ast) when "QueueEntry" AsteriskQueueEntry.new(ev, @ast) when "QueueMember" AsteriskQueueMember.new(ev, @ast) #when "QueueCallerAbandon" when "ExtensionStatus" AsteriskExtension.new(ev, @ast) #when join / leave #agent-connect, agent-complete disconnect #queue-member-added queue-member-removed #elsif @evento[:Event] == "QueueCallerAbandon" # @qp.remove_caller << AstQueueCaller.new(@evento) else# @queue << ev #la fila general de eventos end endendclass AsteriskEvent attr_accessor :name, :body, :actionid def initialize @name = "new event" @body = {} @actionid = nil end def AsteriskEvent.get_key_and_value_from_ast(str) str.split(':').collect {|i| i.strip} end end class AsteriskQueue attr_accessor :name, :calls, :weigth, :holdtime, :completed, :queue, :abandoned, :max, :servicelevel, :servicelevelperf, :ast, :members, :entries def initialize(ev,ast) @ast = ast @name = ev.body[:queue].to_s; @calls = ev.body[:calls].to_i; @weight = ev.body[:weigth].to_i; @holdtime = ev.body[:holdtime].to_s; @completed = ev.body[:completed].to_i; @queue = ev.body[:queue].to_s; @abandoned = ev.body[:abandoned].to_i; @max = ev.body[:max].to_i; @servicelevel = ev.body[:servicelevel].to_i; @servicelevelperf = ev.body[:servicelevelperf].to_s; @entries = {} @members = {} @ast.queues[@name] = self endendclass AsteriskQueueMember attr_accessor :queue, :penalty, :callstaken, :lastcall, :paused, :exten, :location def initialize(ev,ast) @ast = ast @queue = ev.body[:queue].to_s @penalty = ev.body[:penalty].to_i @callstaken = ev.body[:callstaken].to_i @lastcall = ev.body[:lastcall].to_i @paused = ev.body[:paused].to_i @location = ev.body[:location].to_s.gsub(/Local\//, '').gsub(/@agentes\/n/, '') @exten = @ast.extensions[@location] @name = @location @membership = ev.body[:membership].to_s @ast.queues[@queue].members[@location] = self endendclass AsteriskQueueEntry attr_accessor :queue, :name, :ast, :position, :channel, :callerid, :calleridname, :wait def initialize(ev, ast) @ast = ast @name = ev.body[:queue].to_s @queue = ev.body[:queue].to_s @position = ev.body[:position].to_i @channel = ev.body[:channel].to_s #tambien seria bueno tener el canal sin id @callerid = ev.body[:callerid].to_s @calleridname = ev.body[:calleridname].to_s @wait = ev.body[:wait].to_i @ast.queues[@queue].entries[@channel] = self endendclass AsteriskExtension attr_accessor :status, :exten def initialize(ev, ast) @ast = ast @status = ev.body[:status].to_i @exten = ev.body[:exten].to_s @ast.queues.each {|queue_name, queue| queue.members.each {|member_name, member| member.exten = self if member.location == @exten } } @ast.extensions[@exten] = self endendclass Asterisk attr_accessor :host, :port, :username, :secret, :queues, :extensions attr_reader :inbound, :tracker, :actionid, :con def initialize @inbound = AsteriskEventQueue.new(self) @queues = {} @extensions = {} @actionid = 0 yield self @con = TCPSocket.new(@host, @port) self.login end def get_actionid @actionid = (@actionid.to_i + 1).to_s return @actionid end def login @con.send_command :action, :login @con.send_command :username, @username.capitalize @con.send_command :secret, @secret @con.send_command :finalizar_comando, get_actionid end def ping @con.send_command :action, :ping @con.send_command :finalizar_comando end def queue_status @con.send_command :action, :QueueStatus @con.send_command :finalizar_comando, get_actionid end def read_data @inbound.add @con.gets end def close @con.close end def serve DRb.start_service('druby://localhost:9000', self) loop { self.read_data } DRb.thread.join endendputs "---------------------------------------------------------------\n" ast = Asterisk.new { |a| a.host = "127.0.0.1"; a.port = 5038; a.username = "revisador"; a.secret = "ruby"}ast.serveast.close