Minor formatting change to test a mantis change ...
[asterisk-bristuff.git] / funcs / func_extstate.c
blob8866d89b9cf0c69c0297472c29847111c1e5974f
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007, Digium, Inc.
6 * Modified from func_devstate.c by Russell Bryant <russell@digium.com>
7 * Adam Gundy <adam@starsilk.net>
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.
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.
20 /*! \file
22 * \brief Get the state of a hinted extension for dialplan control
24 * \author Adam Gundy <adam@starsilk.net>
26 * \ingroup functions
29 #include "asterisk.h"
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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"
39 static const char *ast_extstate_str(int state)
41 const char *res = "UNKNOWN";
43 switch (state) {
44 case AST_EXTENSION_NOT_INUSE:
45 res = "NOT_INUSE";
46 break;
47 case AST_EXTENSION_INUSE:
48 res = "INUSE";
49 break;
50 case AST_EXTENSION_BUSY:
51 res = "BUSY";
52 break;
53 case AST_EXTENSION_UNAVAILABLE:
54 res = "UNAVAILABLE";
55 break;
56 case AST_EXTENSION_RINGING:
57 res = "RINGING";
58 break;
59 case AST_EXTENSION_INUSE | AST_EXTENSION_RINGING:
60 res = "RINGINUSE";
61 break;
62 case AST_EXTENSION_INUSE | AST_EXTENSION_ONHOLD:
63 res = "HOLDINUSE";
64 break;
65 case AST_EXTENSION_ONHOLD:
66 res = "ONHOLD";
67 break;
70 return res;
73 static int extstate_read(struct ast_channel *chan, const char *cmd, char *data,
74 char *buf, size_t len)
76 char *exten, *context;
78 if (ast_strlen_zero(data)) {
79 ast_log(LOG_WARNING, "EXTENSION_STATE requires an extension\n");
80 return -1;
83 context = exten = data;
84 strsep(&context, "@");
85 if (ast_strlen_zero(context))
86 context = "default";
88 if (ast_strlen_zero(exten)) {
89 ast_log(LOG_WARNING, "EXTENSION_STATE requires an extension\n");
90 return -1;
93 ast_copy_string(buf,
94 ast_extstate_str(ast_extension_state(chan, context, exten)), len);
96 return 0;
99 static struct ast_custom_function extstate_function = {
100 .name = "EXTENSION_STATE",
101 .synopsis = "Get an extension's state",
102 .syntax = "EXTENSION_STATE(extension[@context])",
103 .desc =
104 " The EXTENSION_STATE function can be used to retrieve the state from any\n"
105 "hinted extension. For example:\n"
106 " NoOp(1234@default has state ${EXTENSION_STATE(1234)})\n"
107 " NoOp(4567@home has state ${EXTENSION_STATE(4567@home)})\n"
108 "\n"
109 " The possible values returned by this function are:\n"
110 "UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n"
111 "RINGINUSE | HOLDINUSE | ONHOLD\n",
112 .read = extstate_read,
115 static int unload_module(void)
117 int res;
119 res = ast_custom_function_unregister(&extstate_function);
121 return res;
124 static int load_module(void)
126 int res;
128 res = ast_custom_function_register(&extstate_function);
130 return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
133 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Gets an extension's state in the dialplan");