make app_queue 1.2 jump compliant (issue #5580)
[asterisk/asterisk.git] / apps / app_sendtext.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 App to transmit a text message
22  * 
23  * Requires support of sending text messages from channel driver
24  *
25  * \ingroup applications
26  */
27  
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/lock.h"
37 #include "asterisk/file.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/module.h"
42 #include "asterisk/translate.h"
43 #include "asterisk/image.h"
44 #include "asterisk/options.h"
45 #include "asterisk/app.h"
46
47 static const char *tdesc = "Send Text Applications";
48
49 static const char *app = "SendText";
50
51 static const char *synopsis = "Send a Text Message";
52
53 static const char *descrip = 
54 "  SendText(text[|options]): Sends text to current channel (callee).\n"
55 "Result of transmission will be stored in the SENDTEXTSTATUS\n"
56 "channel variable:\n"
57 "      SUCCESS      Transmission succeeded\n"
58 "      FAILURE      Transmission failed\n"
59 "      UNSUPPORTED  Text transmission not supported by channel\n"
60 "\n"
61 "At this moment, text is supposed to be 7 bit ASCII in most channels.\n"
62 "The option string many contain the following character:\n"
63 "'j' -- jump to n+101 priority if the channel doesn't support\n"
64 "       text transport\n";
65
66 STANDARD_LOCAL_USER;
67
68 LOCAL_USER_DECL;
69
70 static int sendtext_exec(struct ast_channel *chan, void *data)
71 {
72         int res = 0;
73         struct localuser *u;
74         char *status = "UNSUPPORTED";
75         char *parse = NULL;
76         int priority_jump = 0;
77         AST_DECLARE_APP_ARGS(args,
78                 AST_APP_ARG(text);
79                 AST_APP_ARG(options);
80         );
81                 
82         LOCAL_USER_ADD(u);      
83
84         if (ast_strlen_zero(data)) {
85                 ast_log(LOG_WARNING, "SendText requires an argument (text[|options])\n");
86                 LOCAL_USER_REMOVE(u);
87                 return -1;
88         } else {
89                 parse = ast_strdupa(data);
90                 if (!parse) {
91                         ast_log(LOG_ERROR, "Out of memory!\n");
92                         LOCAL_USER_REMOVE(u);
93                         return -1;
94                 }
95         }
96         
97         AST_STANDARD_APP_ARGS(args, parse);
98
99         if (args.options) {
100                 if (strchr(args.options, 'j'))
101                         priority_jump = 1;
102         }
103
104         ast_mutex_lock(&chan->lock);
105         if (!chan->tech->send_text) {
106                 ast_mutex_unlock(&chan->lock);
107                 /* Does not support transport */
108                 if (priority_jump || option_priority_jumping)
109                         ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
110                 LOCAL_USER_REMOVE(u);
111                 return 0;
112         }
113         status = "FAILURE";
114         ast_mutex_unlock(&chan->lock);
115         res = ast_sendtext(chan, args.text);
116         if (!res)
117                 status = "SUCCESS";
118         pbx_builtin_setvar_helper(chan, "SENDTEXTSTATUS", status);
119         LOCAL_USER_REMOVE(u);
120         return 0;
121 }
122
123 int unload_module(void)
124 {
125         int res;
126         
127         res = ast_unregister_application(app);
128         
129         STANDARD_HANGUP_LOCALUSERS;
130
131         return res;     
132 }
133
134 int load_module(void)
135 {
136         return ast_register_application(app, sendtext_exec, synopsis, descrip);
137 }
138
139 char *description(void)
140 {
141         return (char *) tdesc;
142 }
143
144 int usecount(void)
145 {
146         int res;
147
148         STANDARD_USECOUNT(res);
149
150         return res;
151 }
152
153 char *key()
154 {
155         return ASTERISK_GPL_KEY;
156 }