diff options
-rw-r--r-- | src/core/chat-commands.c | 21 | ||||
-rw-r--r-- | src/core/misc.c | 17 | ||||
-rw-r--r-- | src/core/misc.h | 3 | ||||
-rw-r--r-- | src/core/recode.c | 81 | ||||
-rw-r--r-- | src/core/recode.h | 2 | ||||
-rw-r--r-- | src/core/server-rec.h | 3 | ||||
-rw-r--r-- | src/irc/core/irc-servers.c | 25 |
7 files changed, 147 insertions, 5 deletions
diff --git a/src/core/chat-commands.c b/src/core/chat-commands.c index 535bf9f8..e00e6b1a 100644 --- a/src/core/chat-commands.c +++ b/src/core/chat-commands.c @@ -378,12 +378,23 @@ static void cmd_msg(const char *data, SERVER_REC *server, WI_ITEM_REC *item) } } if (target != NULL) { - signal_emit("server sendmsg", 4, server, target, msg, - GINT_TO_POINTER(target_type)); + char **splitmsgs = server->split_message(server, target, msg); + char *m; + int n = 0; + + while ((m = splitmsgs[n++])) { + signal_emit("server sendmsg", 4, server, target, m, + GINT_TO_POINTER(target_type)); + signal_emit(target_type == SEND_TARGET_CHANNEL ? + "message own_public" : + "message own_private", 4, server, m, + target, origtarget); + } + g_strfreev(splitmsgs); + } else { + signal_emit("message own_private", 4, server, msg, target, + origtarget); } - signal_emit(target != NULL && target_type == SEND_TARGET_CHANNEL ? - "message own_public" : "message own_private", 4, - server, msg, target, origtarget); if (free_ret && target != NULL) g_free(target); cmd_params_free(free_arg); diff --git a/src/core/misc.c b/src/core/misc.c index 8d821ecc..19789f64 100644 --- a/src/core/misc.c +++ b/src/core/misc.c @@ -966,3 +966,20 @@ char *ascii_strdown(char *str) *s = g_ascii_tolower (*s); return str; } + +char **strsplit_len(const char *str, int len) +{ + char **ret; + int n = strlen(str) / len; + int i; + + if (strlen(str) % len) + n++; + + ret = g_new(char *, n + 1); + for (i = 0; i < n; i++, str += len) + ret[i] = g_strndup(str, len); + ret[n] = NULL; + + return ret; +} diff --git a/src/core/misc.h b/src/core/misc.h index 2cb8e66a..8fb5078f 100644 --- a/src/core/misc.h +++ b/src/core/misc.h @@ -115,4 +115,7 @@ uoff_t str_to_uofft(const char *str); /* find `item' from a space separated `list' */ int find_substr(const char *list, const char *item); +/* split `str' into `len' sized substrings */ +char **strsplit_len(const char *str, int len); + #endif diff --git a/src/core/recode.c b/src/core/recode.c index ca69b73e..2f126d6e 100644 --- a/src/core/recode.c +++ b/src/core/recode.c @@ -182,6 +182,87 @@ char *recode_out(const SERVER_REC *server, const char *str, const char *target) return recoded; } +char **recode_split(const SERVER_REC *server, const char *str, + const char *target, int len) +{ + GIConv cd = (GIConv)-1; + const char *from = translit_charset; + const char *to = translit_charset; + char *translit_to = NULL; + const char *inbuf = str; + const char *previnbuf = inbuf; + char *tmp = NULL; + char *outbuf; + gsize inbytesleft = strlen(inbuf); + gsize outbytesleft = len; + int n = 0; + char **ret; + + if (!str) + return NULL; + + if (settings_get_bool("recode")) { + to = find_conversion(server, target); + if (to == NULL) + /* default outgoing charset if set */ + to = settings_get_str("recode_out_default_charset"); + if (to && *to != '\0') { + if (settings_get_bool("recode_transliterate") && + !is_translit(to)) + to = translit_to = g_strconcat(to, + "//TRANSLIT", + NULL); + } else { + to = from; + } + } + + cd = g_iconv_open(to, from); + if (cd == (GIConv)-1) { + /* Fall back to splitting by byte. */ + ret = strsplit_len(str, len); + goto out; + } + + tmp = g_malloc(outbytesleft); + outbuf = tmp; + ret = g_new(char *, 1); + while (g_iconv(cd, (char **)&inbuf, &inbytesleft, &outbuf, + &outbytesleft) == -1) { + if (errno != E2BIG) { + /* + * Conversion failed. Fall back to splitting + * by byte. + */ + ret[n] = NULL; + g_strfreev(ret); + ret = strsplit_len(str, len); + goto out; + } + + /* Outbuf overflowed, split the input string. */ + ret[n++] = g_strndup(previnbuf, inbuf - previnbuf); + ret = g_renew(char *, ret, n + 1); + previnbuf = inbuf; + + /* Reset outbuf for the next substring. */ + outbuf = tmp; + outbytesleft = len; + } + /* Copy the last substring into the array. */ + ret[n++] = g_strndup(previnbuf, inbuf - previnbuf); + ret = g_renew(char *, ret, n + 1); + ret[n] = NULL; + +out: + if (cd != (GIConv)-1) + g_iconv_close(cd); + g_free(translit_to); + g_free(tmp); + + return ret; +} + void recode_update_charset(void) { const char *charset = settings_get_str("term_charset"); diff --git a/src/core/recode.h b/src/core/recode.h index c8f867cf..b70ec630 100644 --- a/src/core/recode.h +++ b/src/core/recode.h @@ -3,6 +3,8 @@ char *recode_in (const SERVER_REC *server, const char *str, const char *target); char *recode_out (const SERVER_REC *server, const char *str, const char *target); +char **recode_split(const SERVER_REC *server, const char *str, + const char *target, int len); gboolean is_valid_charset(const char *charset); gboolean is_utf8(void); void recode_update_charset(void); diff --git a/src/core/server-rec.h b/src/core/server-rec.h index 726d1c14..69e990c6 100644 --- a/src/core/server-rec.h +++ b/src/core/server-rec.h @@ -61,6 +61,9 @@ const char *(*get_nick_flags)(SERVER_REC *server); /* send public or private message to server */ void (*send_message)(SERVER_REC *server, const char *target, const char *msg, int target_type); +/* split message in case it is too long for the server to receive */ +char **(*split_message)(SERVER_REC *server, const char *target, + const char *msg); /* -- Default implementations are used if NULL -- */ CHANNEL_REC *(*channel_find_func)(SERVER_REC *server, const char *name); diff --git a/src/irc/core/irc-servers.c b/src/irc/core/irc-servers.c index e5f86c20..a52fa816 100644 --- a/src/irc/core/irc-servers.c +++ b/src/irc/core/irc-servers.c @@ -102,6 +102,30 @@ static void send_message(SERVER_REC *server, const char *target, g_free(recoded); } +static char **split_message(SERVER_REC *server, const char *target, + const char *msg) +{ + IRC_SERVER_REC *ircserver = IRC_SERVER(server); + int userhostlen = 63; /* Maximum length defined by protocol. */ + + g_return_val_if_fail(ircserver != NULL, NULL); + g_return_val_if_fail(target != NULL, NULL); + g_return_val_if_fail(msg != NULL, NULL); + + /* + * If we have joined a channel, userhost will be set, so we can + * calculate the exact maximum length. + */ + if (ircserver->userhost != NULL) + userhostlen = strlen(ircserver->userhost); + + /* length calculation shamelessly stolen from splitlong.pl */ + return recode_split(SERVER(server), msg, target, + 510 - strlen(":! PRIVMSG :") - + strlen(ircserver->nick) - userhostlen - + strlen(target)); +} + static void server_init(IRC_SERVER_REC *server) { IRC_SERVER_CONNECT_REC *conn; @@ -288,6 +312,7 @@ static void sig_connected(IRC_SERVER_REC *server) server->isnickflag = isnickflag_func; server->ischannel = ischannel_func; + server->split_message = split_message; server->send_message = send_message; server->query_find_func = (QUERY_REC *(*)(SERVER_REC *, const char *)) irc_query_find; |