00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <asterisk.h>
00011 #include <stdio.h>
00012 #include <asterisk/file.h>
00013 #include <asterisk/logger.h>
00014 #include <asterisk/channel.h>
00015 #include <asterisk/pbx.h>
00016 #include <asterisk/module.h>
00017 #include <asterisk/lock.h>
00018 #include <stdlib.h>
00019 #include <unistd.h>
00020 #include <string.h>
00021
00022 static char *app = "SayCountPL";
00023
00024 static char *synopsis = "Say the counting word the fits to a number";
00025
00026 static char *descrip =
00027 "Polish grammar has some funny rules for counting words. for example 1 zloty, 2 zlote, 5 zlotych. This application will take the words for 1, 2-4 and 5 and\n"
00028 "decide based on grammar rules which one to use with the number you pass to it.\n\n"
00029 "Example: saycountpl(zloty,zlote,zlotych,122) will give: zlote\n";
00030
00031 static int saywords(struct ast_channel *chan, char *word1, char *word2, char *word5, int num)
00032 {
00033
00034
00035 int md;
00036 int d=0;
00037
00038 if (num >0) {
00039 md = num % 1000;
00040 if (md == 1) {
00041 ast_streamfile(chan, word1, chan->language);
00042 d = ast_waitstream(chan,"");
00043 }
00044 else {
00045 if (((md % 10) >= 2) && ( (md % 10) <= 4 ) && ( ( md % 100) < 10 || (md % 100) > 20)) {
00046 ast_streamfile(chan, word2, chan->language);
00047 d = ast_waitstream(chan,"");
00048 }
00049 else {
00050 ast_streamfile(chan, word5, chan->language);
00051 d = ast_waitstream(chan,"");
00052 }
00053 }
00054 }
00055
00056 return d;
00057
00058 }
00059
00060
00061 static int sayword_exec(struct ast_channel *chan, void *data)
00062 {
00063 int res=0;
00064
00065 char *word1, *word2, *word5, *num;
00066 char *s;
00067
00068 int inum;
00069
00070 struct ast_module_user *u;
00071
00072 if (!data) {
00073 ast_log(LOG_WARNING, "You didn't pass any arguments - I need 4 arguments, word-1,word-2,word-5,number\n");
00074 return -1;
00075 }
00076 u = ast_module_user_add(chan);
00077
00078
00079 s = ast_strdupa((void *) data);
00080
00081 word1 = strsep(&s, "|");
00082 word2 = strsep(&s, "|");
00083 word5 = strsep(&s, "|");
00084 num = strsep(&s, "|");
00085
00086
00087
00088 if (!word1 || !word2 || !word5 || !num) {
00089 ast_log(LOG_WARNING, "Saycountpl requires the arguments word-1|word-2|word-3|number\n");
00090 ast_module_user_remove(u);
00091 return -1;
00092 }
00093
00094 if (sscanf(num, "%d", &inum) != 1) {
00095 ast_log(LOG_WARNING, "'%s' is not a valid number\n", num);
00096 ast_module_user_remove(u);
00097 return -1;
00098 }
00099
00100
00101
00102 res = saywords(chan,word1,word2,word5,inum);
00103
00104
00105 ast_module_user_remove(u);
00106
00107 return res;
00108 }
00109
00110 static int unload_module(void)
00111 {
00112 ast_module_user_hangup_all();
00113 return ast_unregister_application(app);
00114 }
00115
00116 static int load_module(void)
00117 {
00118 return ast_register_application(app, sayword_exec, synopsis, descrip);
00119 }
00120
00121 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Say polish counting words");