add ability to set or read both callerid name and num in one call to the function
[asterisk/asterisk.git] / funcs / func_callerid.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Caller ID related dialplan functions
5  * 
6  * Copyright (C) 2005, Digium, Inc.
7  *
8  * This program is free software, distributed under the terms of
9  * the GNU General Public License
10  */
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15
16 #include "asterisk.h"
17
18 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
19
20 #ifndef BUILTIN_FUNC
21 #include "asterisk/module.h"
22 #endif /* BUILTIN_FUNC */
23 #include "asterisk/channel.h"
24 #include "asterisk/pbx.h"
25 #include "asterisk/logger.h"
26 #include "asterisk/utils.h"
27 #include "asterisk/app.h"
28 #include "asterisk/options.h"
29
30 static char *callerid_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
31 {
32         if (!strncasecmp("all", data, 3)) {
33                 snprintf(buf, len, "\"%s\" <%s>", chan->cid.cid_name ? chan->cid.cid_name : "", chan->cid.cid_num ? chan->cid.cid_num : "");    
34         } else if (!strncasecmp("name", data, 4)) {
35                 if (chan->cid.cid_name) {
36                         ast_copy_string(buf, chan->cid.cid_name, len);
37                 }
38         } else if (!strncasecmp("num", data, 3) || !strncasecmp("number", data, 6)) {
39                 if (chan->cid.cid_num) {
40                         ast_copy_string(buf, chan->cid.cid_num, len);
41                 }
42         } else if (!strncasecmp("ani", data, 3)) {
43                 if (chan->cid.cid_ani) {
44                         ast_copy_string(buf, chan->cid.cid_ani, len);
45                 }
46         } else if (!strncasecmp("dnid", data, 4)) {
47                 if (chan->cid.cid_dnid) {
48                         ast_copy_string(buf, chan->cid.cid_dnid, len);
49                 }
50         } else if (!strncasecmp("rdnis", data, 5)) {
51                 if (chan->cid.cid_rdnis) {
52                         ast_copy_string(buf, chan->cid.cid_rdnis, len);
53                 }
54         } else {
55                 ast_log(LOG_ERROR, "Unknown callerid data type.\n");
56         }
57
58         return buf;
59 }
60
61 static void callerid_write(struct ast_channel *chan, char *cmd, char *data, const char *value) 
62 {
63         if (!value)
64                 return;
65         
66         if (!strncasecmp("all", data, 3)) {
67                 char name[256];
68                 char num[256];
69                 if (!ast_callerid_split(value, name, sizeof(name), num, sizeof(num)))
70                         ast_set_callerid(chan, num, name, num); 
71         } else if (!strncasecmp("name", data, 4)) {
72                 ast_set_callerid(chan, NULL, value, NULL);
73         } else if (!strncasecmp("num", data, 3) || !strncasecmp("number", data, 6)) {
74                 ast_set_callerid(chan, value, NULL, NULL);
75         } else if (!strncasecmp("ani", data, 3)) {
76                 ast_set_callerid(chan, NULL, NULL, value);
77         } else if (!strncasecmp("dnid", data, 4)) {
78                 /* do we need to lock chan here? */
79                 if (chan->cid.cid_dnid)
80                         free(chan->cid.cid_dnid);
81                 chan->cid.cid_dnid = ast_strlen_zero(value) ? NULL : strdup(value);
82         } else if (!strncasecmp("rdnis", data, 5)) {
83                 /* do we need to lock chan here? */
84                 if (chan->cid.cid_rdnis)
85                         free(chan->cid.cid_rdnis);
86                 chan->cid.cid_rdnis = ast_strlen_zero(value) ? NULL : strdup(value);
87         } else {
88                 ast_log(LOG_ERROR, "Unknown callerid data type.\n");
89         }
90 }
91
92 #ifndef BUILTIN_FUNC
93 static
94 #endif /* BUILTIN_FUNC */
95 struct ast_custom_function callerid_function = {
96         .name = "CALLERID",
97         .synopsis = "Gets or sets Caller*ID data on the channel.",
98         .syntax = "CALLERID(datatype)",
99         .desc = "Gets or sets Caller*ID data on the channel.  The allowable datatypes\n"
100         "are \"all\", \"name\", \"num\", \"ANI\", \"DNID\", \"RDNIS\".\n",
101         .read = callerid_read,
102         .write = callerid_write,
103 };
104
105 #ifndef BUILTIN_FUNC
106 static char *tdesc = "Caller ID related dialplan function";
107
108 int unload_module(void)
109 {
110         return ast_custom_function_unregister(&callerid_function);
111 }
112
113 int load_module(void)
114 {
115         return ast_custom_function_register(&callerid_function);
116 }
117
118 char *description(void)
119 {
120         return tdesc;
121 }
122
123 int usecount(void)
124 {
125         return 0;
126 }
127
128 char *key()
129 {
130         return ASTERISK_GPL_KEY;
131 }
132 #endif /* BUILTIN_FUNC */
133
134 /*
135 Local Variables:
136 mode: C
137 c-file-style: "linux"
138 indent-tabs-mode: nil
139 End:
140 */