Merge "AST-2018-005: Fix tdata leaks when calling pjsip_endpt_send_response(2)"
[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 /*** MODULEINFO
29         <support_level>extended</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 #include "asterisk/lock.h"
35 #include "asterisk/file.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/module.h"
39 #include "asterisk/translate.h"
40 #include "asterisk/app.h"
41
42 /*** DOCUMENTATION
43         <application name="Zapateller" language="en_US">
44                 <synopsis>
45                         Block telemarketers with SIT.
46                 </synopsis>
47                 <syntax>
48                         <parameter name="options" required="true">
49                                 <para>Comma delimited list of options.</para>
50                                 <optionlist>
51                                         <option name="answer">
52                                                 <para>Causes the line to be answered before playing the tone.</para>
53                                         </option>
54                                         <option name="nocallerid">
55                                                 <para>Causes Zapateller to only play the tone if there is no
56                                                 callerid information available.</para>
57                                         </option>
58                                 </optionlist>
59                         </parameter>
60                 </syntax>
61                 <description>
62                         <para>Generates special information tone to block telemarketers from calling you.</para>
63                         <para>This application will set the following channel variable upon completion:</para>
64                         <variablelist>
65                                 <variable name="ZAPATELLERSTATUS">
66                                         <para>This will contain the last action accomplished by the
67                                         Zapateller application. Possible values include:</para>
68                                         <value name="NOTHING" />
69                                         <value name="ANSWERED" />
70                                         <value name="ZAPPED" />
71                                 </variable>
72                         </variablelist>
73                 </description>
74         </application>
75  ***/
76
77 static char *app = "Zapateller";
78
79 static int zapateller_exec(struct ast_channel *chan, const char *data)
80 {
81         int res = 0;
82         int i, answer = 0, nocallerid = 0;
83         char *parse = ast_strdupa((char *)data);
84         AST_DECLARE_APP_ARGS(args,
85                 AST_APP_ARG(options)[2];
86         );
87
88         AST_STANDARD_APP_ARGS(args, parse);
89
90         for (i = 0; i < args.argc; i++) {
91                 if (!strcasecmp(args.options[i], "answer"))
92                         answer = 1;
93                 else if (!strcasecmp(args.options[i], "nocallerid"))
94                         nocallerid = 1;
95         }
96
97         pbx_builtin_setvar_helper(chan, "ZAPATELLERSTATUS", "NOTHING");
98         ast_stopstream(chan);
99         if (ast_channel_state(chan) != AST_STATE_UP) {
100                 if (answer) {
101                         res = ast_answer(chan);
102                         pbx_builtin_setvar_helper(chan, "ZAPATELLERSTATUS", "ANSWERED");
103                 }
104                 if (!res)
105                         res = ast_safe_sleep(chan, 500);
106         }
107
108         if (nocallerid  /* Zap caller if no caller id. */
109                 && ast_channel_caller(chan)->id.number.valid
110                 && !ast_strlen_zero(ast_channel_caller(chan)->id.number.str)) {
111                 /* We have caller id. */
112                 return res;
113         }
114
115         if (!res)
116                 res = ast_tonepair(chan, 950, 0, 330, 0);
117         if (!res)
118                 res = ast_tonepair(chan, 1400, 0, 330, 0);
119         if (!res)
120                 res = ast_tonepair(chan, 1800, 0, 330, 0);
121         if (!res)
122                 res = ast_tonepair(chan, 0, 0, 1000, 0);
123
124         pbx_builtin_setvar_helper(chan, "ZAPATELLERSTATUS", "ZAPPED");
125         return res;
126 }
127
128 static int unload_module(void)
129 {
130         return ast_unregister_application(app);
131 }
132
133 static int load_module(void)
134 {
135         return ((ast_register_application_xml(app, zapateller_exec)) ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS);
136 }
137
138 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Block Telemarketers with Special Information Tone");