Asterisk data retrieval API.
[asterisk/asterisk.git] / apps / app_zapateller.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
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 Playback the special information tone to get rid of telemarketers
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \ingroup applications
26  */
27  
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/lock.h"
33 #include "asterisk/file.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/pbx.h"
36 #include "asterisk/module.h"
37 #include "asterisk/translate.h"
38 #include "asterisk/app.h"
39
40 /*** DOCUMENTATION
41         <application name="Zapateller" language="en_US">
42                 <synopsis>
43                         Block telemarketers with SIT.
44                 </synopsis>
45                 <syntax>
46                         <parameter name="options" required="true">
47                                 <para>Comma delimited list of options.</para>
48                                 <optionlist>
49                                         <option name="answer">
50                                                 <para>Causes the line to be answered before playing the tone.</para>
51                                         </option>
52                                         <option name="nocallerid">
53                                                 <para>Causes Zapateller to only play the tone if there is no
54                                                 callerid information available.</para>
55                                         </option>
56                                 </optionlist>
57                         </parameter>
58                 </syntax>
59                 <description>
60                         <para>Generates special information tone to block telemarketers from calling you.</para>
61                         <para>This application will set the following channel variable upon completion:</para>
62                         <variablelist>
63                                 <variable name="ZAPATELLERSTATUS">
64                                         <para>This will contain the last action accomplished by the
65                                         Zapateller application. Possible values include:</para>
66                                         <value name="NOTHING" />
67                                         <value name="ANSWERED" />
68                                         <value name="ZAPPED" />
69                                 </variable>
70                         </variablelist>
71                 </description>
72         </application>
73  ***/
74
75 static char *app = "Zapateller";
76
77 static int zapateller_exec(struct ast_channel *chan, const char *data)
78 {
79         int res = 0;
80         int i, answer = 0, nocallerid = 0;
81         char *parse = ast_strdupa((char *)data);
82         AST_DECLARE_APP_ARGS(args,
83                 AST_APP_ARG(options)[2];
84         );
85
86         AST_STANDARD_APP_ARGS(args, parse);
87
88         for (i = 0; i < args.argc; i++) {
89                 if (!strcasecmp(args.options[i], "answer"))
90                         answer = 1;
91                 else if (!strcasecmp(args.options[i], "nocallerid"))
92                         nocallerid = 1;
93         }
94
95         pbx_builtin_setvar_helper(chan, "ZAPATELLERSTATUS", "NOTHING");
96         ast_stopstream(chan);
97         if (chan->_state != AST_STATE_UP) {
98                 if (answer) {
99                         res = ast_answer(chan);
100                         pbx_builtin_setvar_helper(chan, "ZAPATELLERSTATUS", "ANSWERED");
101                 }
102                 if (!res)
103                         res = ast_safe_sleep(chan, 500);
104         }
105
106         if (!ast_strlen_zero(chan->cid.cid_num) && nocallerid)
107                 return res;
108
109         if (!res) 
110                 res = ast_tonepair(chan, 950, 0, 330, 0);
111         if (!res) 
112                 res = ast_tonepair(chan, 1400, 0, 330, 0);
113         if (!res) 
114                 res = ast_tonepair(chan, 1800, 0, 330, 0);
115         if (!res) 
116                 res = ast_tonepair(chan, 0, 0, 1000, 0);
117         
118         pbx_builtin_setvar_helper(chan, "ZAPATELLERSTATUS", "ZAPPED");
119         return res;
120 }
121
122 static int unload_module(void)
123 {
124         return ast_unregister_application(app);
125 }
126
127 static int load_module(void)
128 {
129         return ((ast_register_application_xml(app, zapateller_exec)) ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SUCCESS);
130 }
131
132 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Block Telemarketers with Special Information Tone");