mirror of
https://github.com/irssi/irssi.git
synced 2026-08-13 13:42:13 +02:00
Properly split long IRC messages
This commit adds handling of long IRC messages to the core. In contrast to the `splitlong.pl' plugin, multi-byte encoded and recoded messages are properly split. To allow for this, a new function has been added to the server struct: `split_message'. `split_message' returns a string array with the message splitted to substrings of a length that the server can handle. If a protocol module doesn't have any limit, it can simply return a singleton array with a copy of the message. The `MSG' chat command now calls `split_message' before `send_message', and emits `message own_public' / `message own_private' with each substring, so that the string splitting will be visible in the UI. `split_message' in the IRC module uses `recode_split' which in turn uses iconv to properly split multi-byte encoded (and recoded) messages.
This commit is contained in:
parent
43baf71efd
commit
e6147fb8f2
7 changed files with 147 additions and 5 deletions
|
|
@ -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");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue