Silly svn. These files didn't get merged over in the merge of the issue8824 branch.
[asterisk/asterisk.git] / funcs / func_connectedline.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007, Gareth Palmer
5  *
6  * Gareth Palmer <gareth@acsdata.co.nz>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Connected Line dialplan function
22  * 
23  * \ingroup functions
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <sys/types.h>
34
35 #include "asterisk/module.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/utils.h"
40 #include "asterisk/app.h"
41 #include "asterisk/options.h"
42 #include "asterisk/callerid.h"
43
44 /*** DOCUMENTATION
45         <function name="CONNECTEDLINE" language="en_US">
46                 <synopsis>
47                         Gets or sets Connected Line data on the channel.
48                 </synopsis>
49                 <syntax>
50                         <parameter name="datatype" required="true">
51                                 <para>The allowable datatypes are:</para>
52                                 <enumlist>
53                                         <enum name = "all" />
54                                         <enum name = "num" />
55                                         <enum name = "name" />
56                                         <enum name = "ton" />
57                                         <enum name = "pres" />
58                                         <enum name = "source" />
59                                 </enumlist>
60                         </parameter>
61                         <parameter name="i">
62                                 <para>If set, this will prevent the channel from sending out protocol
63                                 messages because of the value being set</para>
64                         </parameter>
65                 </syntax>
66                 <description>
67                         <para>Gets or sets Connected Line data on the channel. Possible values
68                         for the <replaceable>source</replaceable> datatype are:</para>
69                         <enumlist>
70                                 <enum name="answer"><para>Normal Call Answering</para></enum>
71                                 <enum name="transfer_alerting"><para>Call Transfer(Alerting)</para></enum>
72                                 <enum name="transfer_active"><para>Call Transfer(Active)</para></enum>
73                         </enumlist>
74                 </description>
75         </function>
76  ***/
77
78 static int connectedline_read(struct ast_channel *chan, const char *cmd, char *data,
79                               char *buf, size_t len)
80 {
81         /* Ensure that the buffer is empty */
82         *buf = 0;
83
84         if (!chan)
85                 return -1;
86
87         ast_channel_lock(chan);
88
89         if (!strncasecmp("all", data, 3)) {
90                 snprintf(buf, len, "\"%s\" <%s>",
91                          S_OR(chan->connected.id.name, ""),
92                          S_OR(chan->connected.id.number, ""));
93         } else if (!strncasecmp("name", data, 4)) {
94                 if (chan->connected.id.name) {
95                         ast_copy_string(buf, chan->connected.id.name, len);
96                 }
97         } else if (!strncasecmp("num", data, 3)) {
98                 if (chan->connected.id.number) {
99                         ast_copy_string(buf, chan->connected.id.number, len);
100                 }
101         } else if (!strncasecmp("ton", data, 3)) {
102                 snprintf(buf, len, "%d", chan->connected.id.number_type);
103         } else if (!strncasecmp("pres", data, 4)) {
104                 ast_copy_string(buf, ast_named_caller_presentation(chan->connected.id.number_presentation), len);
105         } else if (!strncasecmp("source", data, 6)) {
106                 ast_copy_string(buf, ast_connected_line_source_name(chan->connected.source), len);
107         } else {
108                 ast_log(LOG_ERROR, "Unknown connectedline data type '%s'.\n", data);
109         }
110
111         ast_channel_unlock(chan);
112
113         return 0;
114 }
115
116 static int connectedline_write(struct ast_channel *chan, const char *cmd, char *data,
117                                const char *value)
118 {
119         struct ast_party_connected_line connected;
120         char *val;
121         char *option;
122         void (*set_it)(struct ast_channel *chan, const struct ast_party_connected_line *connected);
123
124         if (!value || !chan) {
125                 return -1;
126         }
127
128         /* Determine if the update indication inhibit option is present */
129         option = strchr(data, ',');
130         if (option) {
131                 option = ast_skip_blanks(option + 1);
132                 switch (*option) {
133                 case 'i':
134                         set_it = ast_channel_set_connected_line;
135                         break;
136                 
137                 default:
138                         ast_log(LOG_ERROR, "Unknown connectedline option '%s'.\n", option);
139                         return 0;
140                 }
141         }
142         else {
143                 set_it = ast_channel_update_connected_line;
144         }
145
146         ast_channel_lock(chan);
147         ast_party_connected_line_set_init(&connected, &chan->connected);
148         ast_channel_unlock(chan);
149
150         value = ast_skip_blanks(value);
151
152         if (!strncasecmp("all", data, 3)) {
153                 char name[256];
154                 char num[256];
155
156                 ast_callerid_split(value, name, sizeof(name), num, sizeof(num));
157                 connected.id.name = name;
158                 connected.id.number = num;
159                 set_it(chan, &connected);
160         } else if (!strncasecmp("name", data, 4)) {
161                 connected.id.name = ast_strdupa(value);
162                 ast_trim_blanks(connected.id.name);
163                 set_it(chan, &connected);
164         } else if (!strncasecmp("num", data, 3)) { 
165                 connected.id.number = ast_strdupa(value);
166                 ast_trim_blanks(connected.id.number);
167                 set_it(chan, &connected);
168         } else if (!strncasecmp("ton", data, 3)) {
169                 val = ast_strdupa(value);
170                 ast_trim_blanks(val);
171
172                 if (('0' <= val[0]) && (val[0] <= '9')) {
173                         connected.id.number_type = atoi(val);
174                         set_it(chan, &connected);
175                 } else {
176                         ast_log(LOG_ERROR, "Unknown connectedline type of number '%s', value unchanged\n", val);
177                 }
178         } else if (!strncasecmp("pres", data, 4)) {
179                 int pres;
180
181                 val = ast_strdupa(value);
182                 ast_trim_blanks(val);
183
184                 if (('0' <= val[0]) && (val[0] <= '9')) {
185                         pres = atoi(val);
186                 } else {
187                         pres = ast_parse_caller_presentation(val);
188                 }
189
190                 if (pres < 0) {
191                         ast_log(LOG_ERROR, "Unknown connectedline number presentation '%s', value unchanged\n", val);
192                 } else {
193                         connected.id.number_presentation = pres;
194                         set_it(chan, &connected);
195                 }
196         } else if (!strncasecmp("source", data, 6)) {
197                 int source;
198
199                 val = ast_strdupa(value);
200                 ast_trim_blanks(val);
201
202                 if (('0' <= val[0]) && (val[0] <= '9')) {
203                         source = atoi(val);
204                 } else {
205                         source = ast_connected_line_source_parse(val);
206                 }
207
208                 if (source < 0) {
209                         ast_log(LOG_ERROR, "Unknown connectedline source '%s', value unchanged\n", val);
210                 } else {
211                         connected.source = source;
212                         set_it(chan, &connected);
213                 }
214         } else {
215                 ast_log(LOG_ERROR, "Unknown connectedline data type '%s'.\n", data);
216         }
217
218         return 0;
219 }
220
221 static struct ast_custom_function connectedline_function = {
222         .name = "CONNECTEDLINE",
223         .read = connectedline_read,
224         .write = connectedline_write,
225 };
226
227 static int unload_module(void)
228 {
229         return ast_custom_function_unregister(&connectedline_function);
230 }
231
232 static int load_module(void)
233 {
234         return ast_custom_function_register(&connectedline_function)
235                 ? AST_MODULE_LOAD_DECLINE
236                 : AST_MODULE_LOAD_SUCCESS;
237 }
238
239 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Connected Line dialplan function");