Merge pull request #508 from Misdre/split-long-action-notice

Long ctcp action/notice split
This commit is contained in:
TingPing 2013-04-07 12:40:29 -07:00
commit c766b24920
1 changed files with 123 additions and 47 deletions

View File

@ -2576,10 +2576,57 @@ cmd_load (struct session *sess, char *tbuf, char *word[], char *word_eol[])
return FALSE; return FALSE;
} }
char *
split_up_text(struct session *sess, char *text, int cmd_length, char *split_text)
{
unsigned int max;
/* maximum allowed text */
/* :nickname!username@host.com cmd_length */
max = 512; /* rfc 2812 */
max -= 3; /* :, !, @ */
max -= cmd_length;
max -= strlen (sess->server->nick);
max -= strlen (sess->channel);
if (sess->me && sess->me->hostname)
max -= strlen (sess->me->hostname);
else
{
max -= 9; /* username */
max -= 65; /* max possible hostname and '@' */
}
if (strlen (text) > max)
{
unsigned int i = 0;
int size;
/* traverse the utf8 string and find the nearest cut point that
doesn't split 1 char in half */
while (1)
{
size = g_utf8_skip[((unsigned char *)text)[i]];
if ((i + size) >= max)
break;
i += size;
}
max = i;
split_text = g_strdup_printf ("%.*s", max, text);
return split_text;
}
return NULL;
}
static int static int
cmd_me (struct session *sess, char *tbuf, char *word[], char *word_eol[]) cmd_me (struct session *sess, char *tbuf, char *word[], char *word_eol[])
{ {
char *act = word_eol[2]; char *act = word_eol[2];
char *split_text = NULL;
int cmd_length = 22; /* " PRIVMSG ", " ", :, \001ACTION, " ", \001, \r, \n */
int offset = 0;
if (!(*act)) if (!(*act))
return FALSE; return FALSE;
@ -2601,9 +2648,21 @@ cmd_me (struct session *sess, char *tbuf, char *word[], char *word_eol[])
/* DCC CHAT failed, try through server */ /* DCC CHAT failed, try through server */
if (sess->server->connected) if (sess->server->connected)
{ {
sess->server->p_action (sess->server, sess->channel, act); while ((split_text = split_up_text (sess, act + offset, cmd_length, split_text)))
{
sess->server->p_action (sess->server, sess->channel, split_text);
/* print it to screen */ /* print it to screen */
inbound_action (sess, sess->channel, sess->server->nick, "", act, TRUE, FALSE); inbound_action (sess, sess->channel, sess->server->nick, "", split_text, TRUE, FALSE);
if (*split_text)
offset += strlen(split_text);
g_free(split_text);
}
sess->server->p_action (sess->server, sess->channel, act + offset);
/* print it to screen */
inbound_action (sess, sess->channel, sess->server->nick, "", act + offset, TRUE, FALSE);
} else } else
{ {
notc_msg (sess); notc_msg (sess);
@ -2664,6 +2723,10 @@ cmd_msg (struct session *sess, char *tbuf, char *word[], char *word_eol[])
char *msg = word_eol[3]; char *msg = word_eol[3];
struct session *newsess; struct session *newsess;
char *split_text = NULL;
int cmd_length = 13; /* " PRIVMSG ", " ", :, \r, \n */
int offset = 0;
if (*nick) if (*nick)
{ {
if (*msg) if (*msg)
@ -2692,14 +2755,37 @@ cmd_msg (struct session *sess, char *tbuf, char *word[], char *word_eol[])
notc_msg (sess); notc_msg (sess);
return TRUE; return TRUE;
} }
sess->server->p_message (sess->server, nick, msg);
while ((split_text = split_up_text (sess, msg + offset, cmd_length, split_text)))
{
sess->server->p_message (sess->server, nick, split_text);
if (*split_text)
offset += strlen(split_text);
g_free(split_text);
}
sess->server->p_message (sess->server, nick, msg + offset);
offset = 0;
} }
newsess = find_dialog (sess->server, nick); newsess = find_dialog (sess->server, nick);
if (!newsess) if (!newsess)
newsess = find_channel (sess->server, nick); newsess = find_channel (sess->server, nick);
if (newsess) if (newsess)
{
while ((split_text = split_up_text (sess, msg + offset, cmd_length, split_text)))
{
inbound_chanmsg (newsess->server, NULL, newsess->channel, inbound_chanmsg (newsess->server, NULL, newsess->channel,
newsess->server->nick, msg, TRUE, FALSE); newsess->server->nick, split_text, TRUE, FALSE);
if (*split_text)
offset += strlen(split_text);
g_free(split_text);
}
inbound_chanmsg (newsess->server, NULL, newsess->channel,
newsess->server->nick, msg + offset, TRUE, FALSE);
}
else else
{ {
/* mask out passwords */ /* mask out passwords */
@ -2769,10 +2855,27 @@ cmd_nick (struct session *sess, char *tbuf, char *word[], char *word_eol[])
static int static int
cmd_notice (struct session *sess, char *tbuf, char *word[], char *word_eol[]) cmd_notice (struct session *sess, char *tbuf, char *word[], char *word_eol[])
{ {
char *text = word_eol[3];
char *split_text = NULL;
int cmd_length = 12; /* " NOTICE ", " ", :, \r, \n */
int offset = 0;
if (*word[2] && *word_eol[3]) if (*word[2] && *word_eol[3])
{ {
sess->server->p_notice (sess->server, word[2], word_eol[3]); while ((split_text = split_up_text (sess, text + offset, cmd_length, split_text)))
EMIT_SIGNAL (XP_TE_NOTICESEND, sess, word[2], word_eol[3], NULL, NULL, 0); {
sess->server->p_notice (sess->server, word[2], split_text);
EMIT_SIGNAL (XP_TE_NOTICESEND, sess, word[2], split_text, NULL, NULL, 0);
if (*split_text)
offset += strlen(split_text);
g_free(split_text);
}
sess->server->p_notice (sess->server, word[2], text + offset);
EMIT_SIGNAL (XP_TE_NOTICESEND, sess, word[2], text + offset, NULL, NULL, 0);
return TRUE; return TRUE;
} }
return FALSE; return FALSE;
@ -4257,52 +4360,25 @@ handle_say (session *sess, char *text, int check_spch)
if (sess->server->connected) if (sess->server->connected)
{ {
unsigned int max; char *split_text = NULL;
unsigned char t = 0; int cmd_length = 13; /* " PRIVMSG ", " ", :, \r, \n */
int offset = 0;
/* maximum allowed message text */ while ((split_text = split_up_text (sess, text + offset, cmd_length, split_text)))
/* :nickname!username@host.com PRIVMSG #channel :text\r\n */
max = 512;
max -= 16; /* :, !, @, " PRIVMSG ", " ", :, \r, \n */
max -= strlen (sess->server->nick);
max -= strlen (sess->channel);
if (sess->me && sess->me->hostname)
max -= strlen (sess->me->hostname);
else
{ {
max -= 9; /* username */ inbound_chanmsg (sess->server, sess, sess->channel, sess->server->nick,
max -= 65; /* max possible hostname and '@' */ split_text, TRUE, FALSE);
} sess->server->p_message (sess->server, sess->channel, split_text);
if (strlen (text) > max) if (*split_text)
{ offset += strlen(split_text);
unsigned int i = 0;
int size;
/* traverse the utf8 string and find the nearest cut point that g_free(split_text);
doesn't split 1 char in half */
while (1)
{
size = g_utf8_skip[((unsigned char *)text)[i]];
if ((i + size) >= max)
break;
i += size;
}
max = i;
t = text[max];
text[max] = 0; /* insert a NULL terminator to shorten it */
} }
inbound_chanmsg (sess->server, sess, sess->channel, sess->server->nick, inbound_chanmsg (sess->server, sess, sess->channel, sess->server->nick,
text, TRUE, FALSE); text + offset, TRUE, FALSE);
sess->server->p_message (sess->server, sess->channel, text); sess->server->p_message (sess->server, sess->channel, text + offset);
if (t)
{
text[max] = t;
handle_say (sess, text + max, FALSE);
}
} else } else
{ {
notc_msg (sess); notc_msg (sess);