Ticket #4068: func_extstate.c

File func_extstate.c, 3.5 kB (added by p_lindheimer, 2 years ago)

my modified copy of func_extstate.c for Asterisk 1.4

Line 
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007, Digium, Inc.
5  *
6  * Modified from func_devstate.c by Russell Bryant <russell@digium.com>
7  * Adam Gundy <adam@starsilk.net>
8
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*! \file
21  *
22  * \brief Get the state of a hinted extension for dialplan control
23  *
24  * \author Adam Gundy <adam@starsilk.net>
25  *
26  * \ingroup functions
27  */
28
29 #include "asterisk.h"
30
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 191140 $")
32
33 #include "asterisk/module.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/pbx.h"
36 #include "asterisk/utils.h"
37 #include "asterisk/devicestate.h"
38
39 /*** DOCUMENTATION
40   <function name="EXTENSION_STATE" language="en_US">
41     <synopsis>
42       Get an extension's state.
43     </synopsis>
44     <syntax argsep="@">
45       <parameter name="extension" required="true" />
46       <parameter name="context">
47         <para>If it is not specified defaults to <literal>default</literal>.</para>
48       </parameter>
49     </syntax>
50     <description>
51       <para>The EXTENSION_STATE function can be used to retrieve the state from any
52       hinted extension. For example:</para>
53       <para>NoOp(1234@default has state ${EXTENSION_STATE(1234)})</para>
54       <para>NoOp(4567@home has state ${EXTENSION_STATE(4567@home)})</para>
55       <para>The possible values returned by this function are:</para>
56       <para>UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING |
57       RINGINUSE | HOLDINUSE | ONHOLD</para>
58     </description>
59   </function>
60  ***/
61
62 static const char *ast_extstate_str(int state)
63 {
64   const char *res = "UNKNOWN";
65
66   switch (state) {
67   case AST_EXTENSION_NOT_INUSE:
68     res = "NOT_INUSE";
69     break;
70   case AST_EXTENSION_INUSE:
71     res = "INUSE";
72     break;
73   case AST_EXTENSION_BUSY:
74     res = "BUSY";
75     break;
76   case AST_EXTENSION_UNAVAILABLE:
77     res = "UNAVAILABLE";
78     break;
79   case AST_EXTENSION_RINGING:
80     res = "RINGING";
81     break;
82   case AST_EXTENSION_INUSE | AST_EXTENSION_RINGING:
83     res = "RINGINUSE";
84     break;
85   case AST_EXTENSION_INUSE | AST_EXTENSION_ONHOLD:
86     res = "HOLDINUSE";
87     break;
88   case AST_EXTENSION_ONHOLD:
89     res = "ONHOLD";
90     break;
91   }
92
93   return res;
94 }
95
96 static int extstate_read(struct ast_channel *chan, const char *cmd, char *data,
97   char *buf, size_t len)
98 {
99   char *exten, *context;
100
101   if (ast_strlen_zero(data)) {
102     ast_log(LOG_WARNING, "EXTENSION_STATE requires an extension\n");
103     return -1;
104   }
105
106   context = exten = data;
107   strsep(&context, "@");
108   if (ast_strlen_zero(context))
109     context = "default";
110
111   if (ast_strlen_zero(exten)) {
112     ast_log(LOG_WARNING, "EXTENSION_STATE requires an extension\n");
113     return -1;
114   }
115
116   ast_copy_string(buf,
117     ast_extstate_str(ast_extension_state(chan, context, exten)), len);
118
119   return 0;
120 }
121
122 static struct ast_custom_function extstate_function = {
123   .name = "EXTENSION_STATE",
124   .read = extstate_read,
125 };
126
127 static int unload_module(void)
128 {
129   int res;
130
131   res = ast_custom_function_unregister(&extstate_function);
132
133   return res;
134 }
135
136 static int load_module(void)
137 {
138   int res;
139
140   res = ast_custom_function_register(&extstate_function);
141
142   return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
143 }
144
145 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Gets an extension's state in the dialplan");