be3ac65ef115a04644af61460e565988df6e358a
[asterisk/asterisk.git] / apps / app_saycounted.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2008, Trinity College Computing Center
5  * Written by David Chappell
6  *
7  * See http://www.asterisk.org for more information about
8  * the Asterisk project. Please do not directly contact
9  * any of the maintainers of this project for assistance;
10  * the project provides a web site, mailing lists and IRC
11  * channels for your use.
12  *
13  * This program is free software, distributed under the terms of
14  * the GNU General Public License Version 2. See the LICENSE file
15  * at the top of the source tree.
16  */
17
18 /*! \file
19  *
20  * \brief Applications to decline words according to current language
21  *
22  * \author David Chappell <David.Chappell@trincoll.edu>
23  *
24  * \ingroup applications
25  */
26
27 /*** MODULEINFO
28         <defaultenabled>no</defaultenabled>
29         <support_level>extended</support_level>
30  ***/
31
32 /*** DOCUMENTATION
33         <application name="SayCountedNoun" language="en_US">
34                 <synopsis>
35                         Say a noun in declined form in order to count things
36                 </synopsis>
37                 <syntax>
38                         <parameter name="number" required="true">
39                                 <para>The number of things</para>
40                         </parameter>
41                         <parameter name="filename" required="true">
42                                 <para>File name stem for the noun that is the the name of the things</para>
43                         </parameter>
44                 </syntax>
45                 <description>
46                         <para>Selects and plays the proper singular or plural form of a noun
47                         when saying things such as "five calls".  English has simple rules
48                         for deciding when to say "call" and when to say "calls", but other
49                         languages have complicated rules which would be extremely difficult
50                         to implement in the Asterisk dialplan language.</para>
51                         <para>The correct sound file is selected by examining the
52                         <replaceable>number</replaceable> and adding the appropriate suffix
53                         to <replaceable>filename</replaceable>. If the channel language is
54                         English, then the suffix will be either empty or "s". If the channel
55                         language is Russian or some other Slavic language, then the suffix
56                         will be empty for nominative, "x1" for genative singular, and "x2"
57                         for genative plural.</para>
58                         <para>Note that combining <replaceable>filename</replaceable> with
59                         a suffix will not necessarily produce a correctly spelled plural
60                         form. For example, SayCountedNoun(2,man) will play the sound file
61                         "mans" rather than "men". This behavior is intentional. Since the
62                         file name is never seen by the end user, there is no need to
63                         implement complicated spelling rules.  We simply record the word
64                         "men" in the sound file named "mans".</para>
65                 </description>
66                 <see-also>
67                         <ref type="application">SayCountedAdj</ref>
68                         <ref type="application">SayNumber</ref>
69                 </see-also>
70         </application>
71         <application name="SayCountedAdj" language="en_US">
72                 <synopsis>
73                         Say a adjective in declined form in order to count things
74                 </synopsis>
75                 <syntax>
76                         <parameter name="number" required="true">
77                                 <para>The number of things</para>
78                         </parameter>
79                         <parameter name="filename" required="true">
80                                 <para>File name stem for the adjective</para>
81                         </parameter>
82                         <parameter name="gender">
83                                 <para>The gender of the noun modified, one of 'm', 'f', 'n', or 'c'</para>
84                         </parameter>
85                 </syntax>
86                 <description>
87                         <para>Selects and plays the proper form of an adjective according to
88                         the gender and of the noun which it modifies and the number of
89                         objects named by the noun-verb combination which have been counted.
90                         Used when saying things such as "5 new messages".  The various
91                         singular and plural forms of the adjective are selected by adding
92                         suffixes to <replaceable>filename</replaceable>.</para>
93                         <para>If the channel language is English, then no suffix will ever
94                         be added (since, in English, adjectives are not declined). If the
95                         channel language is Russian or some other slavic language, then the
96                         suffix will the specified <replaceable>gender</replaceable> for
97                         nominative, and "x" for genative plural. (The genative singular is
98                         not used when counting things.) For example, SayCountedAdj(1,new,f)
99                         will play sound file "newa" (containing the word "novaya"), but
100                         SayCountedAdj(5,new,f) will play sound file "newx" (containing the
101                         word "novikh").</para>
102                 </description>
103                 <see-also>
104                         <ref type="application">SayCountedNoun</ref>
105                         <ref type="application">SayNumber</ref>
106                 </see-also>
107         </application>
108  ***/
109
110 #include "asterisk.h"
111
112 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
113
114 #include "asterisk/logger.h"
115 #include "asterisk/module.h"
116 #include "asterisk/app.h"
117 #include "asterisk/say.h"
118
119 static int saycountednoun_exec(struct ast_channel *chan, const char *data)
120 {
121         char *parse;
122         int number;
123         AST_DECLARE_APP_ARGS(args,
124                 AST_APP_ARG(number);
125                 AST_APP_ARG(noun);
126         );
127
128         if (ast_strlen_zero(data)) {
129                 ast_log(LOG_WARNING, "SayCountedNoun requires two arguments (<number>,<noun>)\n");
130                 return -1;
131         }
132
133         parse = ast_strdupa(data);
134         AST_STANDARD_APP_ARGS(args, parse);
135
136         if (args.argc != 2) {
137                 ast_log(LOG_WARNING, "SayCountedNoun requires two arguments\n");
138                 return -1;
139         }
140
141         if (sscanf(args.number, "%d", &number) != 1) {
142                 ast_log(LOG_WARNING, "First argument must be a number between 0 and 2,147,483,647.\n");
143                 return -1;
144         }
145
146         return ast_say_counted_noun(chan, number, args.noun);
147 }
148
149 static int saycountedadj_exec(struct ast_channel *chan, const char *data)
150 {
151         char *parse;
152         int number;
153         AST_DECLARE_APP_ARGS(args,
154                 AST_APP_ARG(number);
155                 AST_APP_ARG(adjective);
156                 AST_APP_ARG(gender);
157         );
158
159         if (ast_strlen_zero(data)) {
160                 ast_log(LOG_WARNING, "SayCountedAdj requires two or three arguments (<number>,<adjective>[,<gender>])\n");
161                 return -1;
162         }
163
164         parse = ast_strdupa(data);
165         AST_STANDARD_APP_ARGS(args, parse);
166
167         if (args.argc < 2) {
168                 ast_log(LOG_WARNING, "SayCountedAdj requires at least two arguments\n");
169                 return -1;
170         }
171
172         if (sscanf(args.number, "%d", &number) != 1) {
173                 ast_log(LOG_WARNING, "First argument must be a number between 0 and 2,147,483,647.\n");
174                 return -1;
175         }
176
177         if (!ast_strlen_zero(args.gender)) {
178                 if (strchr("cCfFmMnN", args.gender[0])) {
179                         ast_log(LOG_WARNING, "SayCountedAdj gender option must be one of 'f', 'm', 'c', or 'n'.\n");
180                         return -1;
181                 }
182         }
183
184         return ast_say_counted_adjective(chan, number, args.adjective, args.gender);
185 }
186
187 static int load_module(void)
188 {
189         int res;
190         res = ast_register_application_xml("SayCountedNoun", saycountednoun_exec);
191         res |= ast_register_application_xml("SayCountedAdj", saycountedadj_exec);
192         return res;
193 }
194
195 static int unload_module(void)
196 {
197         int res;
198         res = ast_unregister_application("SayCountedNoun");
199         res |= ast_unregister_application("SayCountedAdj");
200         return res;
201 }
202
203 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Decline words according to channel language");