summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/dialplan/fax.lua
blob: dc52d16391a9141ac072025289375d776db3a26a (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
-- Gemeinschaft 5 module: fax class
-- (c) AMOOMA GmbH 2012-2013
-- 

module(...,package.seeall)

FAX_SPOOL_DIRECTORY = '/var/spool/freeswitch/'
FAX_PARALLEL_MAX = 8;
Fax = {}

-- Create Fax object
function Fax.new(self, arg)
  arg = arg or {}
  object = arg.object or {}
  setmetatable(object, self)
  self.__index = self
  self.class = 'faxaccount';
  self.log = arg.log;
  self.database = arg.database;
  self.record = arg.record;
  self.fax_spool_directory = arg.fax_spool_directory or FAX_SPOOL_DIRECTORY;
  return object;
end

-- find fax account by id
function Fax.find_by_id(self, id)
  local sql_query = 'SELECT * FROM `fax_accounts` WHERE `id` = ' .. tonumber(id) .. ' LIMIT 1';
  local fax_account = nil;

  self.database:query(sql_query, function(fax_entry)
    fax_account = Fax:new(self);
    fax_account.record = fax_entry;
    fax_account.id = tonumber(fax_entry.id);
    fax_account.uuid = fax_entry.uuid;
  end)

  return fax_account;
end


-- find fax account by uuid
function Fax.find_by_uuid(self, uuid)
  local sql_query = 'SELECT * FROM `fax_accounts` WHERE `uuid` = "' .. uuid .. '" LIMIT 1';
  local fax_account = nil;

  self.database:query(sql_query, function(fax_entry)
    fax_account = Fax:new(self);
    fax_account.record = fax_entry;
    fax_account.id = tonumber(fax_entry.id);
    fax_account.uuid = fax_entry.uuid;
  end)

  return fax_account;
end


function Fax.destination_numbers(self, id)
  local sql_query = 'SELECT `number` FROM `phone_numbers` WHERE `phone_numberable_type` = "FaxDocument" AND `phone_numberable_id` = ' .. tonumber(id);
  local destination_numbers = {}

  self.database:query(sql_query, function(fax_entry)
    table.insert(destination_numbers, fax_entry.number);
  end)

  return destination_numbers;
end

function Fax.destination_number(self, id)
  local sql_query = 'SELECT `number` FROM `phone_numbers` WHERE `phone_numberable_type` = "FaxDocument" AND `phone_numberable_id`= ' .. tonumber(id) .. ' LIMIT 1';
  local destination_number = nil;

  self.database:query(sql_query, function(fax_entry)
    destination_number = fax_entry.number;
  end)

  return destination_number;
end

-- List waiting fax documents
function Fax.queued_for_sending(self, limit)
  limit = limit or FAX_PARALLEL_MAX;
  local sql_query = 'SELECT * FROM `fax_documents` WHERE `state` IN ("queued_for_sending","unsuccessful") AND `retry_counter` > 0  AND `tiff` IS NOT NULL AND `tiff` != "" ORDER BY `sent_at` ASC LIMIT ' .. limit;
  local fax_documents = {}
  self.database:query(sql_query, function(fax_entry)
    fax_entry['destination_numbers'] = Fax:destination_numbers(fax_entry.id)
    table.insert(fax_documents, fax_entry);
  end)

  return fax_documents;
end

-- Update fax document sending status
function Fax.document_update(self, id, params)
  require 'common.str'
  local params_sql = {}
  
  for name, value in pairs(params) do
    table.insert(params_sql, '`' .. name .. '`=' .. common.str.to_sql(value));
  end

  if not params['sent_at'] then
    table.insert(params_sql, '`sent_at`=NOW()');
  end

  if not params['updated_at'] then
    table.insert(params_sql, '`updated_at`=NOW()');
  end
  
  local sql_query = 'UPDATE `fax_documents` SET ' .. table.concat(params_sql, ',') .. ' WHERE `id` = ' .. tonumber(id);

  return self.database:query(sql_query);
end


function Fax.get_parameters(self, caller)
  local fax_parameters = {
    bad_rows = caller:to_i('fax_bad_rows'),
    total_pages = caller:to_i('fax_document_total_pages'),
    transferred_pages = caller:to_i('fax_document_transferred_pages'),
    ecm_requested = caller:to_b('fax_ecm_requested'),
    ecm_used = caller:to_b('fax_ecm_used'),
    filename = caller:to_s('fax_filename'),
    image_resolution = caller:to_s('fax_image_resolution'),
    image_size = caller:to_i('fax_image_size'),
    local_station_id = caller:to_s('fax_local_station_id'),
    result_code = caller:to_i('fax_result_code'),
    result_text = caller:to_s('fax_result_text'),
    remote_station_id = caller:to_s('fax_remote_station_id'),
    success = caller:to_b('fax_success'),
    transfer_rate = caller:to_i('fax_transfer_rate'),
    v17_disabled = caller:to_b('fax_v17_disabled'),
  }

  return fax_parameters;
end
  
-- Receive Fax
function Fax.receive(self, caller, file_name)
  file_name = file_name or self.fax_spool_directory .. 'fax_in_' .. caller.uuid .. '.tiff';

  caller:set_variable('fax_ident',   self.record.station_id)
  caller:set_variable('fax_verbose', 'false')

  caller:answer();
  local start_time = os.time();
  caller:execute('rxfax', file_name);
  local record = self:get_parameters(caller);
  record.transmission_time = os.time() - start_time;
  return record;
end

-- Send Fax
function Fax.send(self, caller, file_name)
  caller:set_variable('fax_ident',   self.record.station_id)
  caller:set_variable('fax_header',  self.record.name)
  caller:set_variable('fax_verbose', 'false')
  local start_time = os.time();
  caller:execute('txfax', file_name);
  local record = self:get_parameters(caller);
  record.transmission_time = os.time() - start_time;
  return record;
end

-- find fax document by id
function Fax.find_document_by_id(self, id)
  local sql_query = 'SELECT * FROM `fax_documents` WHERE `id` = ' .. tonumber(id) .. ' LIMIT 1'
  local record = nil

  self.database:query(sql_query, function(fax_entry)
    record = fax_entry;
  end);

  return record;
end

-- save fax document to database
function Fax.insert_document(self, record)
  require 'common.str'
  local sql_query = 'INSERT INTO `fax_documents` ( \
    inbound, \
    retry_counter, \
    fax_resolution_id, \
    state, \
    transmission_time, \
    sent_at, \
    document_total_pages, \
    document_transferred_pages, \
    ecm_requested, \
    ecm_used, \
    image_resolution, \
    image_size, \
    local_station_id, \
    result_code, \
    remote_station_id, \
    success, \
    transfer_rate, \
    created_at, \
    updated_at, \
    fax_account_id, \
    caller_id_number, \
    caller_id_name, \
    tiff, \
    uuid \
    ) VALUES ( \
      true, \
      0, \
      1, \
      "received", \
      ' .. common.str.to_sql(record.transmission_time) .. ', \
      NOW(), \
      ' .. common.str.to_sql(record.total_pages) .. ', \
      ' .. common.str.to_sql(record.transferred_pages) .. ', \
      ' .. common.str.to_sql(record.ecm_requested) .. ', \
      ' .. common.str.to_sql(record.ecm_used) .. ', \
      ' .. common.str.to_sql(record.image_resolution) .. ', \
      ' .. common.str.to_sql(record.image_size) .. ', \
      ' .. common.str.to_sql(record.local_station_id) .. ', \
      ' .. common.str.to_sql(record.result_code) .. ', \
      ' .. common.str.to_sql(record.remote_station_id) .. ', \
      ' .. common.str.to_sql(record.success) .. ', \
      ' .. common.str.to_sql(record.transfer_rate) .. ', \
      NOW(), \
      NOW(), \
      ' .. common.str.to_sql(self.id) .. ', \
      ' .. common.str.to_sql(record.caller_id_number) .. ', \
      ' .. common.str.to_sql(record.caller_id_name) .. ', \
      ' .. common.str.to_sql(record.filename) .. ', \
      ' .. common.str.to_sql(record.uuid) .. ' \
    )';

  return  self.database:query(sql_query); 
end

function Fax.trigger_notification(self, fax_account_id, uuid)
  local command = 'http_request.lua ' .. uuid .. ' http://127.0.0.1/trigger/fax?fax_account_id=' .. tostring(fax_account_id);

  require 'common.fapi'
  return common.fapi.FApi:new():execute('luarun', command);
end