| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  |  * Copyright (C) 1999-2006, Digium, Inc. | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  | /*! \file
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  |  * \brief Caller ID related dialplan functions | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  *  | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2005-11-08 04:48:00 +00:00
										 |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "asterisk/module.h"
 | 
					
						
							|  |  |  | #include "asterisk/channel.h"
 | 
					
						
							|  |  |  | #include "asterisk/pbx.h"
 | 
					
						
							|  |  |  | #include "asterisk/logger.h"
 | 
					
						
							|  |  |  | #include "asterisk/utils.h"
 | 
					
						
							|  |  |  | #include "asterisk/app.h"
 | 
					
						
							|  |  |  | #include "asterisk/options.h"
 | 
					
						
							| 
									
										
										
										
											2005-07-25 18:34:40 +00:00
										 |  |  | #include "asterisk/callerid.h"
 | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | static int callerid_read(struct ast_channel *chan, char *cmd, char *data, | 
					
						
							|  |  |  | 			 char *buf, size_t len) | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-12-23 21:03:25 +00:00
										 |  |  | 	char *opt = data; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-21 10:51:31 +00:00
										 |  |  | 	/* XXX we are not always clearing the buffer. Is this correct ? */ | 
					
						
							| 
									
										
										
										
											2005-12-23 21:03:25 +00:00
										 |  |  | 	if (strchr(opt, '|')) { | 
					
						
							|  |  |  | 		char name[80], num[80]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		data = strsep(&opt, "|"); | 
					
						
							|  |  |  | 		ast_callerid_split(opt, name, sizeof(name), num, sizeof(num)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!strncasecmp("all", data, 3)) { | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 			snprintf(buf, len, "\"%s\" <%s>", name, num); | 
					
						
							| 
									
										
										
										
											2005-12-23 21:03:25 +00:00
										 |  |  | 		} else if (!strncasecmp("name", data, 4)) { | 
					
						
							|  |  |  | 			ast_copy_string(buf, name, len); | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 		} else if (!strncasecmp("num", data, 3) || | 
					
						
							|  |  |  | 			   !strncasecmp("number", data, 6)) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-23 21:03:25 +00:00
										 |  |  | 			ast_copy_string(buf, num, len); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			ast_log(LOG_ERROR, "Unknown callerid data type.\n"); | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2005-12-23 21:03:25 +00:00
										 |  |  | 		if (!strncasecmp("all", data, 3)) { | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 			snprintf(buf, len, "\"%s\" <%s>", | 
					
						
							| 
									
										
										
										
											2006-04-21 10:51:31 +00:00
										 |  |  | 				 S_OR(chan->cid.cid_name, ""), | 
					
						
							|  |  |  | 				 S_OR(chan->cid.cid_num, "")); | 
					
						
							| 
									
										
										
										
											2005-12-23 21:03:25 +00:00
										 |  |  | 		} else if (!strncasecmp("name", data, 4)) { | 
					
						
							|  |  |  | 			if (chan->cid.cid_name) { | 
					
						
							|  |  |  | 				ast_copy_string(buf, chan->cid.cid_name, len); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 		} else if (!strncasecmp("num", data, 3) | 
					
						
							|  |  |  | 				   || !strncasecmp("number", data, 6)) { | 
					
						
							| 
									
										
										
										
											2005-12-23 21:03:25 +00:00
										 |  |  | 			if (chan->cid.cid_num) { | 
					
						
							|  |  |  | 				ast_copy_string(buf, chan->cid.cid_num, len); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else if (!strncasecmp("ani", data, 3)) { | 
					
						
							|  |  |  | 			if (chan->cid.cid_ani) { | 
					
						
							|  |  |  | 				ast_copy_string(buf, chan->cid.cid_ani, len); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else if (!strncasecmp("dnid", data, 4)) { | 
					
						
							|  |  |  | 			if (chan->cid.cid_dnid) { | 
					
						
							|  |  |  | 				ast_copy_string(buf, chan->cid.cid_dnid, len); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else if (!strncasecmp("rdnis", data, 5)) { | 
					
						
							|  |  |  | 			if (chan->cid.cid_rdnis) { | 
					
						
							|  |  |  | 				ast_copy_string(buf, chan->cid.cid_rdnis, len); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			ast_log(LOG_ERROR, "Unknown callerid data type.\n"); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | static int callerid_write(struct ast_channel *chan, char *cmd, char *data, | 
					
						
							|  |  |  | 			  const char *value) | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (!value) | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-07-25 17:26:59 +00:00
										 |  |  | 	if (!strncasecmp("all", data, 3)) { | 
					
						
							|  |  |  | 		char name[256]; | 
					
						
							|  |  |  | 		char num[256]; | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-07-25 17:26:59 +00:00
										 |  |  | 		if (!ast_callerid_split(value, name, sizeof(name), num, sizeof(num))) | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 			ast_set_callerid(chan, num, name, num); | 
					
						
							|  |  |  | 	} else if (!strncasecmp("name", data, 4)) { | 
					
						
							|  |  |  | 		ast_set_callerid(chan, NULL, value, NULL); | 
					
						
							|  |  |  | 	} else if (!strncasecmp("num", data, 3) || | 
					
						
							|  |  |  | 		   !strncasecmp("number", data, 6)) { | 
					
						
							|  |  |  | 		ast_set_callerid(chan, value, NULL, NULL); | 
					
						
							|  |  |  | 	} else if (!strncasecmp("ani", data, 3)) { | 
					
						
							|  |  |  | 		ast_set_callerid(chan, NULL, NULL, value); | 
					
						
							|  |  |  | 	} else if (!strncasecmp("dnid", data, 4)) { | 
					
						
							|  |  |  | 		/* do we need to lock chan here? */ | 
					
						
							|  |  |  | 		if (chan->cid.cid_dnid) | 
					
						
							|  |  |  | 			free(chan->cid.cid_dnid); | 
					
						
							| 
									
										
										
										
											2006-04-21 10:51:31 +00:00
										 |  |  | 		chan->cid.cid_dnid = ast_strdup(value); | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 	} else if (!strncasecmp("rdnis", data, 5)) { | 
					
						
							|  |  |  | 		/* do we need to lock chan here? */ | 
					
						
							|  |  |  | 		if (chan->cid.cid_rdnis) | 
					
						
							|  |  |  | 			free(chan->cid.cid_rdnis); | 
					
						
							| 
									
										
										
										
											2006-04-21 10:51:31 +00:00
										 |  |  | 		chan->cid.cid_rdnis = ast_strdup(value); | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Unknown callerid data type.\n"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-11 03:14:05 +00:00
										 |  |  | static struct ast_custom_function callerid_function = { | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  | 	.name = "CALLERID", | 
					
						
							|  |  |  | 	.synopsis = "Gets or sets Caller*ID data on the channel.", | 
					
						
							| 
									
										
										
										
											2005-12-23 21:03:25 +00:00
										 |  |  | 	.syntax = "CALLERID(datatype[,<optional-CID>])", | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 	.desc = | 
					
						
							|  |  |  | 		"Gets or sets Caller*ID data on the channel.  The allowable datatypes\n" | 
					
						
							|  |  |  | 		"are \"all\", \"name\", \"num\", \"ANI\", \"DNID\", \"RDNIS\".\n" | 
					
						
							|  |  |  | 		"Uses channel callerid by default or optional callerid, if specified.\n", | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  | 	.read = callerid_read, | 
					
						
							|  |  |  | 	.write = callerid_write, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *tdesc = "Caller ID related dialplan function"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-14 14:08:19 +00:00
										 |  |  | static int unload_module(void *mod) | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 	return ast_custom_function_unregister(&callerid_function); | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-14 14:08:19 +00:00
										 |  |  | static int load_module(void *mod) | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 	return ast_custom_function_register(&callerid_function); | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-14 14:08:19 +00:00
										 |  |  | static const char *description(void) | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	return tdesc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-14 14:08:19 +00:00
										 |  |  | static const char *key(void) | 
					
						
							| 
									
										
										
										
											2005-05-15 17:45:30 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	return ASTERISK_GPL_KEY; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2006-04-14 14:08:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | STD_MOD(MOD_1 | NO_USECOUNT, NULL, NULL, NULL); |