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