summaryrefslogtreecommitdiff
path: root/lib/freeswitch_event.rb
blob: 68d9df2a0d52a3c77f84f392e8e6d8e39792ad57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
class FreeswitchEventSocket
  DEFAULT_HOST = "127.0.0.1"
  DEFAULT_PORT = 8021
  DEFAULT_PASSWORD = "ClueCon"
  DEFAULT_SOCKET_TIMEOUT = 20
  @socket = nil
  
  def auth(password)
    self.command("auth #{password}")
    result = self.result()

    if result && result["Reply-Text"] == "+OK accepted"
      return true
    end
    return false
  end

  def connect(password = DEFAULT_PASSWORD, event_host = DEFAULT_HOST, event_port = DEFAULT_PORT)
    begin
      @socket = TCPSocket.open(event_host, event_port)
    rescue
      return false
    end

    if not @socket
      return false
    end

    result = self.result()
    if result && result["Content-Type"] == "auth/request" then
      if not self.auth(password)
        return false
      end
    end

    return true
  end

  def command(command)
    @socket.puts("#{command}\n\n")
  end

  def close()
    @socket.close
  end

  def read(read_bytes=1024)
    return @socket.recv(read_bytes)
  end

  def result()
    reply = self.read()
    if reply.class == String
      message = Hash.new()
      header, body = reply.split("\n\n", 2)
      if ! body.blank?
        message['_BODY'] = body
      end
      header.split("\n").each do |line|
        key, value = line.split(/\: */, 2)
        message[key] = value
      end
      return message
    end
    return nil
  end
end

class FreeswitchEvent
  def initialize(event_type)
    @event_type = event_type
    @event_header = Array.new()
    @event_body = nil
    @event_header.push("sendevent #{@event_type}")
  end

  def add_header(name, value)
    @event_header.push("#{name}: #{value}")
  end

  def add_body(body)
    @event_body = body
  end

  def fire()
    if @event_body && @event_body.length > 0 
      self.add_header("content-length",  @event_body.length);
    end

    event = FreeswitchEventSocket.new()
    if event && event.connect()
      event.command(@event_header.join("\n"))
      event.command( @event_body)
      result = event.result()
      event.close()

      if result && result["Reply-Text"] == "+OK"
        return true
      end
    end
    return false
  end
end

class FreeswitchAPI
  def self.execute(command, arguments, bgapi = false)

    event = FreeswitchEventSocket.new()
    if event && event.connect()
      api = bgapi ? 'bgapi' : 'api'
      event.command( "#{api} #{command} #{arguments}")
      result = event.result()
      if result && result["Content-Type"] == 'api/response' && result["_BODY"].blank?
        result = event.result()
      end
      event.close()

      if result 
        if result.has_key?('Reply-Text') && result['Reply-Text'] =~ /^\+OK/
          if result.has_key?('Job-UUID') && ! result['Job-UUID'].blank?
            return result['Job-UUID'];
          else
            return true;
          end
        elsif result.has_key?('_BODY') && result['_BODY'] =~ /^\+OK/
          return true;
        end
      end
    end

    return false
  end

  def self.channel_variable_get(channel_uuid, variable_name)
    result = nil
    event = FreeswitchEventSocket.new()
    if event && event.connect()
      event.command( "api uuid_getvar #{channel_uuid} #{variable_name}")
      event_result = event.result()
      if event_result && event_result["Content-Type"] == 'api/response' && event_result["Content-Length"].to_i > 0
        result = event.read(event_result["Content-Length"].to_i)
      end
      event.close()
    end

    return result
  end

end