-
Notifications
You must be signed in to change notification settings - Fork 2
/
voip.module
270 lines (233 loc) · 7.71 KB
/
voip.module
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
<?php
// $Id$
/**
* @file
* Enables communication between Drupal and VoIP servers.
*/
require_once(dirname(__FILE__) . '/includes/voip_error.inc');
/**
* Make a phone call
*
* @param object $voipcall with the information about the call to be made
*
* @return boolean with TRUE in case of success or FALSE otherwise
*/
function voip_dial($voipcall) {
// make sure the user is allowed to make outbound calls
$allowed = user_access('make outbound calls');
if (!$allowed) {
global $user;
$variables['%uid'] = $user->uid;
$msg = 'User %uid does not have permissions to make outbound calls.';
watchdog('voip', $msg, $variables, WATCHDOG_WARNING);
return FALSE;
}
$voip_server = voip_default_server();
$response = $voip_server->dial($voipcall);
if (voip_error()) {
$error_message = 'Failure executing voip_dial() with %options.';
$variables['%options'] = print_r($voipcall, TRUE);
if (voip_error_message()) {
$error_message .= ' The server said: ' . voip_error_message();
}
watchdog('voip', $error_message, $variables, WATCHDOG_ERROR);
}
return $response;
}
/**
* Send a text message
*
* @param string $text with the body of the text
* @param object $voipcall with the information about the text to be made
* @param boolean $reply optional parameter indicating if this is a reply message
*
* @return boolean with TRUE in case of success or FALSE otherwise
*/
function voip_text($text, $voipcall, $reply = FALSE) {
// @todo : Convert to text perm.
// make sure the user is allowed to make outbound calls
$allowed = user_access('make outbound calls');
if (!$allowed) {
global $user;
$variables['%uid'] = $user->uid;
$msg = 'User %uid does not have permissions to make outbound calls.';
watchdog('voip', $msg, $variables, WATCHDOG_WARNING);
return FALSE;
}
$voip_server = voip_default_server();
$response = $voip_server->send_text($text, $voipcall, $reply);
if (voip_error()) {
$error_message = 'Failure executing voip_text() with %options.';
$variables['%options'] = print_r($voipcall, TRUE);
if (voip_error_message()) {
$error_message .= ' The server said: ' . voip_error_message();
}
watchdog('voip', $error_message, $variables, WATCHDOG_ERROR);
}
return $response;
}
/**
* Hangup a phone call
*
* @param object $voipcall with the information about the call to be hangup
*
* @return boolean with TRUE in case of success or FALSE otherwise
*/
function voip_hangup($voipcall) {
// make sure the user is allowed to hangup the call
global $user;
$allowed = user_access('hangup any calls')
|| (($user->uid == $voipcall->getUid()) && user_access('hangup own calls'));
if (!$allowed) {
$variables['%uid'] = $user->uid;
$msg = 'User %uid does not have permissions to hangup calls.';
watchdog('voip', $msg, $variables, WATCHDOG_WARNING);
return FALSE;
}
$voip_server = voip_default_server();
$response = $voip_server->hangup($voipcall);
if (voip_error()) {
$error_message = 'Failure executing voip_hangup() with %options.';
$variables['%options'] = print_r($voipcall, TRUE);
if (voip_error_message()) {
$error_message .= ' The server said: ' . voip_error_message();
}
watchdog('voip', $error_message, $variables, WATCHDOG_ERROR);
}
return $response;
}
/**
* Common entry-point function for text handling.
*
* @param text_content with the text body
*
* @return
*/
function voip_text_handler($text_content, $origin, $destination, $time, $network = 'sms', $server_name = '') {
$voip_server = voip_default_server();
$response = $voip_server->text_handler($text_content, $origin, $destination, $time, $network, $server_name);
if (voip_error()) {
$error_message = 'Failure executing voip_text_handler() with %options.';
$variables['%options'] = "$text_content, $origin ,$destination, $time, $network, $server_name";
if (voip_error_message()) {
$error_message .= ' The server said: ' . voip_error_message();
}
watchdog('voip', $error_message, $variables, WATCHDOG_ERROR);
}
return $response;
}
/**
* Implements hook_sms_incoming().
*/
function voip_sms_incoming($op, $number, $message, $options) {
if ($op == 'process') {
$msg = "in voipcall_sms_incoming() with number: $number and message: $message ";
watchdog('voipcall', $msg);
voip_text_handler($message, $number, '', REQUEST_TIME);
}
}
/**
* Implements hook_menu().
*/
function voip_menu() {
$items = array();
$items['admin/voip'] = array(
'title' => 'VoIP Drupal',
'description' => 'Control how your site interacts with VoIP servers.',
'position' => 'right',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer voip drupal framework'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/voip/servers'] = array(
'title' => 'VoIP server configuration',
'description' => 'Configure VoIP servers and choose the default server.',
'page callback' => 'drupal_get_form',
'page arguments' => array('voip_admin_default_form', NULL),
'access arguments' => array('administer voip drupal framework'),
'file' => 'voip.admin.inc',
);
$items['admin/voip/servers/%'] = array(
'title callback' => 'voip_admin_server_title',
'title arguments' => array(3),
'page callback' => 'drupal_get_form',
'page arguments' => array('voip_admin_server_form', 3),
'access arguments' => array('administer voip drupal framework'),
'type' => MENU_CALLBACK,
'file' => 'voip.admin.inc',
);
return $items;
}
/**
* Implements hook_permission().
*/
function voip_permission() {
return array(
'administer voip drupal framework' => array(
'title' => t('administer voip drupal framework'),
'description' => t('TODO Add a description for \'administer voip drupal framework\''),
),
'hangup any calls' => array(
'title' => t('hangup any calls'),
'description' => t('TODO Add a description for \'hangup any calls\''),
),
'hangup own calls' => array(
'title' => t('hangup own calls'),
'description' => t('TODO Add a description for \'hangup own calls\''),
),
'make outbound calls' => array(
'title' => t('make outbound calls'),
'description' => t('TODO Add a description for \'make outbound calls\''),
),
);
}
/**
* Implements hook_theme().
*/
function voip_theme() {
$items['voip_admin_default_form'] =
array('render element' => 'form');
return $items;
}
/**
* Internal functions
*/
/**
* Voip server menu title callback.
*/
function voip_admin_server_title($server_id) {
$server = VoipServer::getServer($server_id);
return sprintf('%s server', $server->getName());
}
/**
* Returns the current default voip server.
*/
function voip_default_server() {
$server_id = variable_get('voip_default_server', 'log');
$server = VoipServer::getServer($server_id);
return $server;
}
/**
* Implements hook_voipserver_info().
*
* Note - Moved here so it can be discovered by @see module_implements(). An alternative would have been
* @see hook_hook_info() but that would require changing file names which is probably a good idea but not
* on my todo list right now.
*/
function voip_voipserver_info() {
$server = new VoipLogServer();
return $server;
}
/**
* Implements hook_getvoices().
*
* Note - Moved here so it can be discovered by @see module_implements(). An alternative would have been
* @see hook_hook_info() but that would require changing file names which is probably a good idea but not
* on my todo list right now.
*/
function voip_getvoices($language, $gender, $is_local) {
$voice = new VoipVoice('log', 'man', 'en');
$voices['log'] = $voice;
return $voices;
}