summaryrefslogtreecommitdiff
path: root/app/controllers/config_polycom_controller.rb
blob: 9d44e514785f5535a7ef12bc076c05c4669d08d7 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
class ConfigPolycomController < ApplicationController
	MAX_SIP_ACCOUNTS_COUNT = 11
	MAX_SOFTKEYS_COUNT = 12
  MAX_DIRECTORY_ENTRIES = 20
  SIP_DEFAULT_PORT = 5060
 
	skip_authorization_check
	
	before_filter { |controller|
    if ! params[:mac_address].blank? then
      @mac_address = params[:mac_address].upcase.gsub(/[^0-9A-F]/,'')
      @phone = Phone.where({ :mac_address => @mac_address }).first
    elsif ! params[:phone].blank? then
      @phone = Phone.where({ :id => params[:phone].to_i }).first
    end      

    if ! @phone
      render(
        :status => 404,
        :layout => false,
        :content_type => 'text/plain',
        :text => "<!-- Phone not found -->",
      )
      return false
    end

    if ! params[:sip_account].blank?
      @sip_account = @phone.sip_accounts.where({ :id => params[:sip_account].to_i }).first
      if ! @sip_account
        render(
          :status => 404,
          :layout => false,
          :content_type => 'text/plain',
          :text => "<!-- SipAccount not found -->",
        )
        return false
      end
    end

    if ! params[:type].blank?
      @type = params[:type].to_s.strip.downcase
    end
	}
	
  def config_files
    if params[:mac_address].blank? then
      render(
        :status => 404,
        :layout => false,
        :content_type => 'text/plain',
        :text => "<!-- MAC not specified -->",
      )
    end

    respond_to { |format|
      format.any {
        self.formats = [ :xml ]
        render
      }
    }
  end
	
	def settings
		if ! request.env['HTTP_USER_AGENT'].index('polycom')
			Rails.logger.info "---> User-Agent indicates not a Polycom phone (#{request.env['HTTP_USER_AGENT'].inspect})"
		else
			Rails.logger.info "---> Phone #{@mac_address.inspect}, IP address #{request.remote_ip.inspect}"
			@phone.update_attributes({ :ip_address => request.remote_ip })
		end

    xml_applications_url = "#{request.protocol}#{request.host_with_port}/config_polycom/#{@phone.id}/0"

    @settings = {
      'device.sntp.serverName' => 'pool.ntp.org',
      'device.sntp.gmtOffset' => 3600,
      'up.welcomeSoundOnWarmBootEnabled' => 0,
      'up.welcomeSoundEnabled' => 0,
      'bg.hiRes.color.selection' => '2,1',
      'msg.mwi.1.callBackMode' => 'contact', 
      'msg.mwi.1.callBack' => 'f-vmcheck',
      'feature.enhancedFeatureKeys.enabled' => 1,
      'softkey.feature.basicCallManagement.redundant' => 0,
      'softkey.feature.buddies' => 0,
      'softkey.feature.callers' => 0,
      'softkey.feature.directories' => 0,
      'softkey.feature.endcall' => 1,
      'softkey.feature.forward' => 0,
      'softkey.feature.mystatus' => 0,
      'softkey.feature.newcall' => 0,
      'call.directedCallPickupMethod' => 'legacy',
      'call.directedCallPickupString' => 'f-ia-',
      'call.advancedMissedCalls.enabled' => 0,
      'lineKey.reassignment.enabled' => 1,
      'lineKey.1.category' => 'Line',
      'lineKey.1.index' => 1,
    }

    for key_index in 2..42
      @settings["lineKey.#{key_index}.category"] = 'Unassigned'
    end

    for ring_class in 1..17
      @settings["se.rt.custom#{ring_class}.name"] = "Ringer#{ring_class-1}"
      @settings["se.rt.custom#{ring_class}.ringer"] = "ringer#{ring_class}"
    end
    @settings["se.rt.custom1.type"] = 'visual'

    for ring_class in 1..17
      @settings["voIpProt.SIP.alertInfo.#{ring_class}.class"] = "custom#{ring_class}"
      @settings["voIpProt.SIP.alertInfo.#{ring_class}.value"] = "Ringer#{ring_class-1}"
    end

    softkey_index = 1
    blf_index = 0

    @phone.sip_accounts.each do |sip_account|
      sip_account_index = 0
      if (sip_account.sip_accountable_type == @phone.phoneable_type) and (sip_account.sip_accountable_id == @phone.phoneable_id)
        sip_account_index += 1
        if sip_account_index == 1
          xml_applications_url = "#{request.protocol}#{request.host_with_port}/config_polycom/#{@phone.id}/#{sip_account.id}"
          @settings['voIpProt.SIP.outboundProxy.address'] = sip_account.host
          @settings['voIpProt.SIP.outboundProxy.port'] = SIP_DEFAULT_PORT
        end

        @settings["reg.#{sip_account_index}.address"] = "#{sip_account.auth_name}@#{sip_account.host}"
        @settings["reg.#{sip_account_index}.auth.password"] = sip_account.password
        @settings["reg.#{sip_account_index}.auth.userId"] = sip_account.auth_name
        @settings["reg.#{sip_account_index}.displayName"] = 'Call'
        @settings["reg.#{sip_account_index}.label"] = sip_account.caller_name
        @settings["voIpProt.server.#{sip_account_index}.address"] = sip_account.host
        @settings["voIpProt.server.#{sip_account_index}.port"] = SIP_DEFAULT_PORT
        @settings["call.missedCallTracking.#{sip_account_index}.enabled"] = 0 

        sip_account.softkeys.order(:position).each do |softkey|
          softkey_index += 1
          if softkey.softkey_function
            softkey_function = softkey.softkey_function.name
          end
          case softkey_function
          when 'blf'
            blf_index += 1
            @settings["lineKey.#{softkey_index}.category"] = 'BLF'
            @settings["attendant.resourceList.#{blf_index}.address"] = "#{softkey.number}@#{sip_account.host}"
            @settings["attendant.resourceList.#{blf_index}.label"] = softkey.label
          end
        end
      end
    end

    @settings['mb.idleDisplay.home'] = "#{xml_applications_url}/idle_screen.xml"
    @settings['mb.idleDisplay.refresh'] = 60

    @settings['efk.efklist.1.mname'] = "directory"
    @settings['efk.efklist.1.status'] = 1
    @settings['efk.efklist.1.action.string'] = "#{xml_applications_url}/phone_book.xml"
    @settings['efk.efklist.2.mname'] = "callhistory"
    @settings['efk.efklist.2.status'] = 1
    @settings['efk.efklist.2.action.string'] = "#{xml_applications_url}/call_history.xml"
    @settings['efk.efklist.3.mname'] = "applications"
    @settings['efk.efklist.3.status'] = 1
    @settings['efk.efklist.3.action.string'] = "#{xml_applications_url}/applications.xml"

    @settings['softkey.1.action'] = "#{xml_applications_url}/phone_book.xml"
    @settings['softkey.1.enable'] = 1
    @settings['softkey.1.insert'] =  1
    @settings['softkey.1.label'] = 'Directory'
    @settings['softkey.1.precede'] = 1
    @settings['softkey.1.use.active'] = 1
    @settings['softkey.1.use.alerting'] = 0
    @settings['softkey.1.use.dialtone'] = 1
    @settings['softkey.1.use.hold'] = 1
    @settings['softkey.1.use.idle'] = 1
    @settings['softkey.1.use.proceeding'] = 0
    @settings['softkey.1.use.setup'] = 0
    @settings['softkey.2.action'] = "#{xml_applications_url}/call_history.xml"
    @settings['softkey.2.enable'] = 1
    @settings['softkey.2.insert'] =  2
    @settings['softkey.2.label'] = 'Call History'
    @settings['softkey.2.precede'] = 1
    @settings['softkey.2.use.active'] = 1
    @settings['softkey.2.use.alerting'] = 0
    @settings['softkey.2.use.dialtone'] = 1
    @settings['softkey.2.use.hold'] = 1
    @settings['softkey.2.use.idle'] = 1
    @settings['softkey.2.use.proceeding'] = 0
    @settings['softkey.2.use.setup'] = 0

		respond_to { |format|
			format.any {
				self.formats = [ :xml ]
				render
			}
		}
	end


  def settings_directory
    respond_to { |format|
      format.any {
        self.formats = [ :xml ]
        render
      }
    }
  end


  def call_history

    if ! @sip_account
      render(
        :status => 404,
        :layout => false,
        :content_type => 'text/plain',
        :text => "<!-- SipAccount not found -->",
      )
      return
    end

    if ['dialed', 'missed', 'received'].include? @type
      @phone_xml_object = { 
        :name => "call_history",
        :title => @type.titleize,
        :entries => []
      }

      calls = @sip_account.call_histories.where(:entry_type => @type).order('start_stamp DESC').limit(MAX_DIRECTORY_ENTRIES)

      calls.each do |call|
        display_name = call.display_name
        phone_number = call.display_number
        phone_book_entry = call.phone_book_entry_by_number(phone_number)
        if display_name.blank?
          display_name = phone_book_entry.to_s
        end
        
        @phone_xml_object[:entries].push({
            :selected => false, 
            :number => phone_number,
            :date => call.start_stamp.strftime('%d.%m %H:%M'),
            :text => display_name,
            :url => "tel:#{phone_number}",
            })     
      end
    else
      base_url = "#{request.protocol}#{request.host_with_port}#{request.fullpath.split("?")[0]}"
      @phone_xml_object = { 
        :name => 'call_history_menu',
        :title => 'Call History Lists',
        :entries => [
          {:text => 'Missed Calls', :url => "#{base_url}?&type=missed",   :selected => false},
          {:text => 'Received Calls',  :url => "#{base_url}?&type=received", :selected => false},
          {:text => 'Placed Calls', :url => "#{base_url}?&type=dialed",   :selected => false},
        ]
      }
    end

    respond_to { |format|
      format.any {
        self.formats = [ :xml ]
        render :action => "_#{@phone_xml_object[:name]}", :content_type => Mime::HTML
      }
    }

  end

  
  def phone_book

    if ! @sip_account
      render(
        :status => 404,
        :layout => false,
        :content_type => 'text/plain',
        :text => "<!-- SipAccount not found -->",
      )
      return
    end

    @phone_xml_object = { 
      :name => 'phone_book',
      :title => "Phone Book".strip,
      :entries => [],
      :softkeys => [],
    }

    phone_books = Array.new()
    phone_books = phone_books + @sip_account.sip_accountable.try(:phone_books).all
    if @sip_account.sip_accountable.class == User
      phone_books = phone_books + @sip_account.sip_accountable.try(:current_tenant).try(:phone_books).all
    end

    phone_book_ids = Array.new()
    phone_books.each do |phone_book|
      phone_book_ids << phone_book.id
    end

    PhoneBookEntry.where(:phone_book_id => phone_book_ids).order(:last_name).order(:first_name).limit(MAX_DIRECTORY_ENTRIES).each do |phone_book_entry|
      phone_numbers_count = 0
      phone_book_entry.phone_numbers.each do |phone_number|
        phone_numbers_count += 1
        if phone_numbers_count > 1
          entry_name = ''
        else
          entry_name = phone_book_entry.to_s
        end

        @phone_xml_object[:entries] << { :text => entry_name, :type => phone_number.name, :number => phone_number.number, :url => "tel:#{phone_number.number}" }
      end
    end

    respond_to { |format|
      format.any {
        self.formats = [ :xml ]
        render :action => "_#{@phone_xml_object[:name]}", :content_type => Mime::HTML
      }
    }

  end


  def idle_screen
    respond_to { |format|
      format.any {
        self.formats = [ :xml ]
        render :action => "idle_screen", :content_type => Mime::HTML
      }
    }
  end
end