Simplify send_text() by separating out special targets into another function

Also rename the double-negative flag 'not_done' to the more description 'done_forward'.
This commit is contained in:
Kevin Easton
2017-07-05 17:36:52 +10:00
parent ac84793dd6
commit 2c8671f3dd

View File

@@ -3752,8 +3752,91 @@ static int recursion = 0;
from_server = old_from_server; from_server = old_from_server;
} }
static int try_send_special_target(char *target, const char *text, char *command, int hook)
{
/*
* It is legal to send an empty line to a process, but not legal
* to send an empty line anywhere else.
*/
switch (*target)
{
case '%':
{
int process_idx = get_process_index(&target);
if (process_idx == -1)
say("Invalid process specification");
else
text_to_process(process_idx, text, 1);
}
break;
#ifdef WANT_FTP
case '-':
if (text && *text)
dcc_ftpcommand(target + 1, (char *)text);
break;
#endif
case '"':
if (text && *text)
send_to_server("%s", text);
break;
case '/':
if (text && *text)
{
char *line = m_opendup(target, space, text, NULL);
parse_command(line, 0, empty_string);
new_free(&line);
}
break;
case '=':
if (text && *text)
{
const char *key;
int old_server;
char *line;
if (!is_number(target + 1) &&
!dcc_activechat(target + 1) &&
!dcc_activebot(target+1) &&
!dcc_activeraw(target+1))
{
yell("No DCC CHAT connection open to %s", target + 1);
break;
}
if ((key = is_crypted(target)))
line = sed_encrypt_msg(text, key);
else
line = m_strdup(text);
old_server = from_server;
from_server = -1;
if (dcc_activechat(target+1))
{
dcc_chat_transmit(target + 1, line, line, command, hook);
if (hook)
addtabkey(target, "msg", 0);
}
else if (dcc_activebot(target + 1))
dcc_bot_transmit(target + 1, line, command);
else
dcc_raw_transmit(target + 1, line, command);
add_last_type(&last_sent_dcc[0], MAX_LAST_MSG, NULL, NULL, target+1, text);
from_server = old_server;
new_free(&line);
}
break;
default:
return 0;
}
return 1;
}
struct target_type struct target_type
{ {
@@ -3790,15 +3873,13 @@ void BX_send_text(const char *nick_list, const char *text, char *command, int h
static int sent_text_recursion = 0; static int sent_text_recursion = 0;
int i, int i,
old_server, done_forward = 0,
not_done = 1,
is_current = 0, is_current = 0,
old_window_display = window_display; old_window_display = window_display;
char *current_nick, char *current_nick,
*next_nick, *next_nick,
*free_nick, *free_nick,
*line; *line;
const char *key = NULL;
struct target_type target[4] = struct target_type target[4] =
{ {
@@ -3827,72 +3908,25 @@ void BX_send_text(const char *nick_list, const char *text, char *command, int h
while ((current_nick = next_nick)) while ((current_nick = next_nick))
{ {
const char *key;
if ((next_nick = strchr(current_nick, ','))) if ((next_nick = strchr(current_nick, ',')))
*next_nick++ = 0; *next_nick++ = 0;
if (!*current_nick) if (!*current_nick)
continue; continue;
if (*current_nick == '%') if (try_send_special_target(current_nick, text, command, hook))
{ continue;
if ((i = get_process_index(&current_nick)) == -1)
say("Invalid process specification");
else
text_to_process(i, text, 1);
}
/* /*
* This test has to be here because it is legal to * This test has to be here because it is legal to
* send an empty line to a process, but not legal * send an empty line to a process, but not legal
* to send an empty line anywhere else. * to send an empty line anywhere else.
*/ */
else if (!text || !*text) if (!text || !*text)
;
#ifdef WANT_FTP
else if (*current_nick == '-')
dcc_ftpcommand(current_nick+1, (char *)text);
#endif
else if (*current_nick == '"')
send_to_server("%s", text);
else if (*current_nick == '/')
{
line = m_opendup(current_nick, space, text, NULL);
parse_command(line, 0, empty_string);
new_free(&line);
}
else if (*current_nick == '=')
{
if (!is_number(current_nick + 1) &&
!dcc_activechat(current_nick + 1) &&
!dcc_activebot(current_nick+1) &&
!dcc_activeraw(current_nick+1))
{
yell("No DCC CHAT connection open to %s", current_nick + 1);
continue; continue;
}
if ((key = is_crypted(current_nick)))
line = sed_encrypt_msg(text, key);
else
line = m_strdup(text);
old_server = from_server;
from_server = -1;
if (dcc_activechat(current_nick+1))
{
dcc_chat_transmit(current_nick + 1, line, line, command, hook);
if (hook)
addtabkey(current_nick, "msg", 0);
}
else if (dcc_activebot(current_nick + 1))
dcc_bot_transmit(current_nick + 1, line, command);
else
dcc_raw_transmit(current_nick + 1, line, command);
add_last_type(&last_sent_dcc[0], MAX_LAST_MSG, NULL, NULL, current_nick+1, text);
from_server = old_server;
new_free(&line);
}
else
{
if (doing_notice) if (doing_notice)
{ {
say("You cannot send a message from within ON NOTICE"); say("You cannot send a message from within ON NOTICE");
@@ -3925,7 +3959,6 @@ void BX_send_text(const char *nick_list, const char *text, char *command, int h
malloc_strcat(&target[i].nick_list, current_nick); malloc_strcat(&target[i].nick_list, current_nick);
} }
} }
}
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
{ {
@@ -3943,10 +3976,10 @@ void BX_send_text(const char *nick_list, const char *text, char *command, int h
/* do we forward this?*/ /* do we forward this?*/
if (forwardnick && not_done) if (forwardnick && !done_forward)
{ {
send_to_server("NOTICE %s :-> *%s* %s", forwardnick, nick_list, text); send_to_server("NOTICE %s :-> *%s* %s", forwardnick, nick_list, text);
not_done = 0; done_forward = 1;
} }
/* log it if logging on */ /* log it if logging on */