mirror of
https://github.com/irssi/irssi.git
synced 2024-12-04 14:46:39 -05:00
Merge pull request #29 from sebth/master
Properly split long IRC messages
This commit is contained in:
commit
99b629ab20
1
AUTHORS
1
AUTHORS
@ -80,3 +80,4 @@ Other patches (grep for "patch" in ChangeLog) by:
|
||||
Svante Kvarnström
|
||||
Ailin Nemui (Nei)
|
||||
Tom Feist (shabble)
|
||||
Sebastian Thorarensen
|
||||
|
5
NEWS
5
NEWS
@ -4,6 +4,11 @@ v0.8.17-head 2014-xx-xx The Irssi team <staff@irssi.org>
|
||||
+ Performance enhancement of the nicklist as well as the window_item_find function. See Github PR #24.
|
||||
+ Disallow unloading of static modules.
|
||||
+ Allow UTF-8 characters in /bind. See Github PR #18.
|
||||
+ Split overlong outgoing messages instead of silently truncating them.
|
||||
Adds two new options: 'split_line_end' and 'split_line_start'.
|
||||
'split_line_end' contains a string added to the end of line fragments.
|
||||
'split_line_start' contains a string added to the beginning of line
|
||||
fragments. See Github PR #29.
|
||||
- Fixed various compiler warnings.
|
||||
- Fixed format_get_text Perl API. See Github PR #23.
|
||||
- Fixed gui_printtext_after and term_refresh_*() visibility. See Github PR #22.
|
||||
|
@ -378,12 +378,35 @@ 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;
|
||||
char **tmp = NULL;
|
||||
char *singlemsg[] = { msg, NULL };
|
||||
char *m;
|
||||
int n = 0;
|
||||
|
||||
/*
|
||||
* If split_message is NULL, the server doesn't need to split
|
||||
* long messages.
|
||||
*/
|
||||
if (server->split_message != NULL)
|
||||
splitmsgs = tmp = server->split_message(server, target,
|
||||
msg);
|
||||
else
|
||||
splitmsgs = singlemsg;
|
||||
|
||||
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(tmp);
|
||||
} 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);
|
||||
|
@ -966,3 +966,21 @@ char *ascii_strdown(char *str)
|
||||
*s = g_ascii_tolower (*s);
|
||||
return str;
|
||||
}
|
||||
|
||||
char **strsplit_len(const char *str, int len)
|
||||
{
|
||||
char **ret;
|
||||
size_t total_len = strlen(str);
|
||||
int n = total_len / len;
|
||||
int i;
|
||||
|
||||
if (total_len % 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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -182,6 +182,86 @@ 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;
|
||||
|
||||
g_return_val_if_fail(str != NULL, 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 != NULL && *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");
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -44,6 +44,9 @@
|
||||
static void cmd_me(const char *data, IRC_SERVER_REC *server, WI_ITEM_REC *item)
|
||||
{
|
||||
const char *target;
|
||||
char *subdata;
|
||||
char **splitdata;
|
||||
int n = 0;
|
||||
|
||||
CMD_IRC_SERVER(server);
|
||||
if (!IS_IRC_ITEM(item))
|
||||
@ -53,10 +56,13 @@ static void cmd_me(const char *data, IRC_SERVER_REC *server, WI_ITEM_REC *item)
|
||||
cmd_return_error(CMDERR_NOT_CONNECTED);
|
||||
|
||||
target = window_item_get_target(item);
|
||||
irc_server_send_action(server, target, data);
|
||||
|
||||
signal_emit("message irc own_action", 3, server, data,
|
||||
item->visible_name);
|
||||
splitdata = irc_server_split_action(server, target, data);
|
||||
while ((subdata = splitdata[n++])) {
|
||||
irc_server_send_action(server, target, subdata);
|
||||
signal_emit("message irc own_action", 3, server, subdata,
|
||||
item->visible_name);
|
||||
}
|
||||
g_strfreev(splitdata);
|
||||
}
|
||||
|
||||
/* SYNTAX: ACTION [-<server tag>] <target> <message> */
|
||||
@ -64,6 +70,9 @@ static void cmd_action(const char *data, IRC_SERVER_REC *server)
|
||||
{
|
||||
GHashTable *optlist;
|
||||
const char *target, *text;
|
||||
char *subtext;
|
||||
char **splittexts;
|
||||
int n = 0;
|
||||
void *free_arg;
|
||||
|
||||
CMD_IRC_SERVER(server);
|
||||
@ -79,10 +88,14 @@ static void cmd_action(const char *data, IRC_SERVER_REC *server)
|
||||
if (server == NULL || !server->connected)
|
||||
cmd_param_error(CMDERR_NOT_CONNECTED);
|
||||
|
||||
irc_server_send_action(server, target, text);
|
||||
|
||||
signal_emit("message irc own_action", 3, server, text, target);
|
||||
splittexts = irc_server_split_action(server, target, text);
|
||||
while ((subtext = splittexts[n++])) {
|
||||
irc_server_send_action(server, target, subtext);
|
||||
signal_emit("message irc own_action", 3, server, subtext,
|
||||
target);
|
||||
}
|
||||
|
||||
g_strfreev(splittexts);
|
||||
cmd_params_free(free_arg);
|
||||
}
|
||||
|
||||
|
@ -51,6 +51,12 @@
|
||||
#define DEFAULT_CMDS_MAX_AT_ONCE 5
|
||||
#define DEFAULT_MAX_QUERY_CHANS 1 /* more and more IRC networks are using stupid ircds.. */
|
||||
|
||||
/*
|
||||
* 63 is the maximum hostname length defined by the protocol. 10 is a common
|
||||
* username limit on many networks. 1 is for the `@'.
|
||||
*/
|
||||
#define MAX_USERHOST_LEN (63 + 10 + 1)
|
||||
|
||||
void irc_servers_reconnect_init(void);
|
||||
void irc_servers_reconnect_deinit(void);
|
||||
|
||||
@ -74,6 +80,72 @@ static int ischannel_func(SERVER_REC *server, const char *data)
|
||||
return ischannel(*data);
|
||||
}
|
||||
|
||||
static char **split_line(const SERVER_REC *server, const char *line,
|
||||
const char *target, int len)
|
||||
{
|
||||
const char *start = settings_get_str("split_line_start");
|
||||
const char *end = settings_get_str("split_line_end");
|
||||
char *recoded_start = recode_out(server, start, target);
|
||||
char *recoded_end = recode_out(server, end, target);
|
||||
char **lines;
|
||||
int i;
|
||||
|
||||
/*
|
||||
* Having the same length limit on all lines will make the first line
|
||||
* shorter than necessary if `split_line_start' is set, but it makes
|
||||
* the code much simpler. It's worth it.
|
||||
*/
|
||||
len -= strlen(recoded_start) + strlen(recoded_end);
|
||||
if (len <= 0) {
|
||||
/* There is no room for anything. */
|
||||
g_free(recoded_start);
|
||||
g_free(recoded_end);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
lines = recode_split(server, line, target, len);
|
||||
for (i = 0; lines[i] != NULL; i++) {
|
||||
if (i != 0 && *start != '\0') {
|
||||
/* Not the first line. */
|
||||
char *tmp = lines[i];
|
||||
lines[i] = g_strconcat(start, tmp, NULL);
|
||||
g_free(tmp);
|
||||
}
|
||||
if (lines[i + 1] != NULL && *end != '\0') {
|
||||
/* Not the last line. */
|
||||
char *tmp = lines[i];
|
||||
|
||||
if (lines[i + 2] == NULL) {
|
||||
/* Next to last line. Check if we have room
|
||||
* to append the last line to the current line,
|
||||
* to avoid an unnecessary line break.
|
||||
*/
|
||||
char *recoded_l = recode_out(server,
|
||||
lines[i+1],
|
||||
target);
|
||||
if (strlen(recoded_l) <= strlen(recoded_end)) {
|
||||
lines[i] = g_strconcat(tmp, lines[i+1],
|
||||
NULL);
|
||||
g_free_and_null(lines[i+1]);
|
||||
lines = g_renew(char *, lines, i + 2);
|
||||
|
||||
g_free(recoded_l);
|
||||
g_free(tmp);
|
||||
break;
|
||||
}
|
||||
g_free(recoded_l);
|
||||
}
|
||||
|
||||
lines[i] = g_strconcat(tmp, end, NULL);
|
||||
g_free(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
g_free(recoded_start);
|
||||
g_free(recoded_end);
|
||||
return lines;
|
||||
}
|
||||
|
||||
static void send_message(SERVER_REC *server, const char *target,
|
||||
const char *msg, int target_type)
|
||||
{
|
||||
@ -102,6 +174,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 = MAX_USERHOST_LEN;
|
||||
|
||||
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 split_line(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 +384,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;
|
||||
@ -358,6 +455,23 @@ void irc_server_send_action(IRC_SERVER_REC *server, const char *target, const ch
|
||||
g_free(recoded);
|
||||
}
|
||||
|
||||
char **irc_server_split_action(IRC_SERVER_REC *server, const char *target,
|
||||
const char *data)
|
||||
{
|
||||
int userhostlen = MAX_USERHOST_LEN;
|
||||
|
||||
g_return_val_if_fail(server != NULL, NULL);
|
||||
g_return_val_if_fail(target != NULL, NULL);
|
||||
g_return_val_if_fail(data != NULL, NULL);
|
||||
|
||||
if (server->userhost != NULL)
|
||||
userhostlen = strlen(server->userhost);
|
||||
|
||||
return split_line(SERVER(server), data, target,
|
||||
510 - strlen(":! PRIVMSG :\001ACTION \001") -
|
||||
strlen(server->nick) - userhostlen - strlen(target));
|
||||
}
|
||||
|
||||
void irc_server_send_away(IRC_SERVER_REC *server, const char *reason)
|
||||
{
|
||||
char *recoded = NULL;
|
||||
@ -866,6 +980,8 @@ void irc_server_init_isupport(IRC_SERVER_REC *server)
|
||||
void irc_servers_init(void)
|
||||
{
|
||||
settings_add_str("misc", "usermode", DEFAULT_USER_MODE);
|
||||
settings_add_str("misc", "split_line_start", "");
|
||||
settings_add_str("misc", "split_line_end", "");
|
||||
settings_add_time("flood", "cmd_queue_speed", DEFAULT_CMD_QUEUE_SPEED);
|
||||
settings_add_int("flood", "cmds_max_at_once", DEFAULT_CMDS_MAX_AT_ONCE);
|
||||
|
||||
|
@ -117,7 +117,10 @@ void irc_server_purge_output(IRC_SERVER_REC *server, const char *target);
|
||||
char *irc_server_get_channels(IRC_SERVER_REC *server);
|
||||
|
||||
/* INTERNAL: */
|
||||
void irc_server_send_action(IRC_SERVER_REC *server, const char *target, const char *data);
|
||||
void irc_server_send_action(IRC_SERVER_REC *server, const char *target,
|
||||
const char *data);
|
||||
char **irc_server_split_action(IRC_SERVER_REC *server, const char *target,
|
||||
const char *data);
|
||||
void irc_server_send_away(IRC_SERVER_REC *server, const char *reason);
|
||||
void irc_server_send_data(IRC_SERVER_REC *server, const char *data, int len);
|
||||
void irc_server_init_isupport(IRC_SERVER_REC *server);
|
||||
|
Loading…
Reference in New Issue
Block a user