Applications no longer need to call ast_module_user_add and ast_module_user_remove...
[asterisk/asterisk.git] / apps / app_waitforring.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 Wait for Ring Application
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 <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37
38 #include "asterisk/file.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/module.h"
43 #include "asterisk/options.h"
44 #include "asterisk/lock.h"
45
46 static char *synopsis = "Wait for Ring Application";
47
48 static char *desc = "  WaitForRing(timeout)\n"
49 "Returns 0 after waiting at least timeout seconds. and\n"
50 "only after the next ring has completed.  Returns 0 on\n"
51 "success or -1 on hangup\n";
52
53 static char *app = "WaitForRing";
54
55
56 static int waitforring_exec(struct ast_channel *chan, void *data)
57 {
58         struct ast_frame *f;
59         int res = 0;
60         double s;
61         int ms;
62
63         if (!data || (sscanf(data, "%lg", &s) != 1)) {
64                 ast_log(LOG_WARNING, "WaitForRing requires an argument (minimum seconds)\n");
65                 return 0;
66         }
67
68         ms = s*1000.0;
69         while(ms > 0) {
70                 ms = ast_waitfor(chan, ms);
71                 if (ms < 0) {
72                         res = ms;
73                         break;
74                 }
75                 if (ms > 0) {
76                         f = ast_read(chan);
77                         if (!f) {
78                                 res = -1;
79                                 break;
80                         }
81                         if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
82                                 if (option_verbose > 2)
83                                         ast_verbose(VERBOSE_PREFIX_3 "Got a ring but still waiting for timeout\n");
84                         }
85                         ast_frfree(f);
86                 }
87         }
88         /* Now we're really ready for the ring */
89         if (!res) {
90                 ms = 99999999;
91                 while(ms > 0) {
92                         ms = ast_waitfor(chan, ms);
93                         if (ms < 0) {
94                                 res = ms;
95                                 break;
96                         }
97                         if (ms > 0) {
98                                 f = ast_read(chan);
99                                 if (!f) {
100                                         res = -1;
101                                         break;
102                                 }
103                                 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
104                                         if (option_verbose > 2)
105                                                 ast_verbose(VERBOSE_PREFIX_3 "Got a ring after the timeout\n");
106                                         ast_frfree(f);
107                                         break;
108                                 }
109                                 ast_frfree(f);
110                         }
111                 }
112         }
113
114         return res;
115 }
116
117 static int unload_module(void)
118 {
119         return ast_unregister_application(app);
120 }
121
122 static int load_module(void)
123 {
124         return ast_register_application(app, waitforring_exec, synopsis, desc);
125 }
126
127 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Waits until first ring after time");