| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include <ctype.h> |
| 35 |
#include <iconv.h> |
| 36 |
|
| 37 |
#include "asterisk/module.h" |
| 38 |
#include "asterisk/channel.h" |
| 39 |
#include "asterisk/pbx.h" |
| 40 |
#include "asterisk/utils.h" |
| 41 |
#include "asterisk/app.h" |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
#define AST_ICONV_CAST void * |
| 78 |
|
| 79 |
static int iconv_read(struct ast_channel *chan, const char *cmd, char *arguments, char *buf, size_t len) |
| 80 |
{ |
| 81 |
AST_DECLARE_APP_ARGS(args, |
| 82 |
AST_APP_ARG(in_charset); |
| 83 |
AST_APP_ARG(out_charset); |
| 84 |
AST_APP_ARG(text); |
| 85 |
); |
| 86 |
iconv_t cd; |
| 87 |
size_t incount, outcount = len - 1; |
| 88 |
char *parse; |
| 89 |
|
| 90 |
if (ast_strlen_zero(arguments)) { |
| 91 |
ast_log(LOG_WARNING, "Syntax: ICONV(<in-charset>,<out-charset>,<text>) - missing arguments!\n"); |
| 92 |
return -1; |
| 93 |
} |
| 94 |
|
| 95 |
parse = ast_strdupa(arguments); |
| 96 |
AST_STANDARD_APP_ARGS(args, parse); |
| 97 |
|
| 98 |
if (args.argc < 3) { |
| 99 |
ast_log(LOG_WARNING, "Syntax: ICONV(<in-charset>,<out-charset>,<text>) %u\n", args.argc); |
| 100 |
return -1; |
| 101 |
} |
| 102 |
|
| 103 |
incount = strlen(args.text); |
| 104 |
|
| 105 |
ast_debug(1, "Iconv: \"%s\" %s -> %s\n", args.text, args.in_charset, args.out_charset); |
| 106 |
|
| 107 |
cd = iconv_open(args.out_charset, args.in_charset); |
| 108 |
|
| 109 |
if (cd == (iconv_t) -1) { |
| 110 |
ast_log(LOG_ERROR, "conversion from '%s' to '%s' not available. type 'iconv -l' in a shell to list the supported charsets.\n", args.in_charset, args.out_charset); |
| 111 |
return -1; |
| 112 |
} |
| 113 |
|
| 114 |
if (iconv(cd, (AST_ICONV_CAST) &args.text, &incount, &buf, &outcount) == (size_t) -1) { |
| 115 |
if (errno == E2BIG) |
| 116 |
ast_log(LOG_WARNING, "Iconv: output buffer too small.\n"); |
| 117 |
else if (errno == EILSEQ) |
| 118 |
ast_log(LOG_WARNING, "Iconv: illegal character.\n"); |
| 119 |
else if (errno == EINVAL) |
| 120 |
ast_log(LOG_WARNING, "Iconv: incomplete character sequence.\n"); |
| 121 |
else |
| 122 |
ast_log(LOG_WARNING, "Iconv: error %d: %s.\n", errno, strerror(errno)); |
| 123 |
} |
| 124 |
*buf = '\0'; |
| 125 |
iconv_close(cd); |
| 126 |
|
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
static struct ast_custom_function iconv_function = { |
| 132 |
.name = "ICONV", |
| 133 |
.read = iconv_read |
| 134 |
}; |
| 135 |
|
| 136 |
static int unload_module(void) |
| 137 |
{ |
| 138 |
return ast_custom_function_unregister(&iconv_function); |
| 139 |
} |
| 140 |
|
| 141 |
static int load_module(void) |
| 142 |
{ |
| 143 |
return ast_custom_function_register(&iconv_function); |
| 144 |
} |
| 145 |
|
| 146 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Charset conversions"); |