Change sed_encrypt_msg() so it doesn't modify the passed message in-place

This means the calling code in commands.c no longer needs to take a copy before calling the function.

Also remove code testing result of of ctcp_quote_it() and ctcp_unquote_it() - these functions never fail.
This commit is contained in:
Kevin Easton
2017-06-28 23:03:58 +10:00
parent 3c2028e167
commit 57827008a1
3 changed files with 22 additions and 37 deletions

View File

@@ -71,7 +71,7 @@ extern char * do_notice_ctcp (char *, char *, char *);
extern int in_ctcp (void); extern int in_ctcp (void);
extern void send_ctcp (int, char *, int, char *, ...); extern void send_ctcp (int, char *, int, char *, ...);
extern int get_ctcp_val (char *); extern int get_ctcp_val (char *);
extern char *sed_encrypt_msg(char *, const char *); extern char *sed_encrypt_msg(const char *, const char *);
extern char *sed_decrypt_msg(const char *, const char *); extern char *sed_decrypt_msg(const char *, const char *);
void BX_split_CTCP (char *, char *, char *); void BX_split_CTCP (char *, char *, char *);

View File

@@ -3758,7 +3758,7 @@ static int recursion = 0;
struct target_type struct target_type
{ {
char *nick_list; char *nick_list;
char *message; const char *message;
int hook_type; int hook_type;
char *command; char *command;
char *format; char *format;
@@ -3874,11 +3874,7 @@ struct target_type target[4] =
continue; continue;
} }
if ((key = is_crypted(current_nick))) if ((key = is_crypted(current_nick)))
{ line = sed_encrypt_msg(text, key);
char *breakage;
breakage = LOCAL_COPY(text);
line = sed_encrypt_msg(breakage, key);
}
else else
line = m_strdup(text); line = m_strdup(text);
@@ -3895,21 +3891,18 @@ struct target_type target[4] =
else else
dcc_raw_transmit(current_nick + 1, line, command); dcc_raw_transmit(current_nick + 1, line, command);
add_last_type(&last_sent_dcc[0], MAX_LAST_MSG, NULL, NULL, current_nick+1, (char *)text); add_last_type(&last_sent_dcc[0], MAX_LAST_MSG, NULL, NULL, current_nick+1, text);
from_server = old_server; from_server = old_server;
new_free(&line); new_free(&line);
} }
else else
{ {
char *copy = NULL;
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");
continue; continue;
} }
copy = LOCAL_COPY(text);
if (!(i = is_channel(current_nick)) && hook) if (!(i = is_channel(current_nick)) && hook)
addtabkey(current_nick, "msg", 0); addtabkey(current_nick, "msg", 0);
@@ -3920,8 +3913,8 @@ struct target_type target[4] =
{ {
set_display_target(current_nick, target[i].level); set_display_target(current_nick, target[i].level);
line = sed_encrypt_msg(copy, key); line = sed_encrypt_msg(text, key);
if (hook && do_hook(target[i].hook_type, "%s %s", current_nick, copy)) if (hook && do_hook(target[i].hook_type, "%s %s", current_nick, text))
put_it("%s", convert_output_format(target[i].format_encrypted, put_it("%s", convert_output_format(target[i].format_encrypted,
"%s %s %s %s", update_clock(GET_TIME), current_nick, get_server_nickname(from_server), text)); "%s %s %s %s", update_clock(GET_TIME), current_nick, get_server_nickname(from_server), text));
@@ -3935,7 +3928,7 @@ struct target_type target[4] =
malloc_strcat(&target[i].nick_list, ","); malloc_strcat(&target[i].nick_list, ",");
malloc_strcat(&target[i].nick_list, current_nick); malloc_strcat(&target[i].nick_list, current_nick);
if (!target[i].message) if (!target[i].message)
target[i].message = (char *)text; target[i].message = text;
} }
} }
} }

View File

@@ -612,29 +612,26 @@ int server;
* Encrypts a message with my_encrypt() under the given key, and encapsulates it * Encrypts a message with my_encrypt() under the given key, and encapsulates it
* as a CTCP SED message ready to transmit. * as a CTCP SED message ready to transmit.
*/ */
char *sed_encrypt_msg(char *str, const char *key) char *sed_encrypt_msg(const char *str, const char *key)
{ {
static const char sed_prefix[] = { CTCP_DELIM_CHAR, 'S', 'E', 'D', ' ', 0 }; static const char sed_prefix[] = { CTCP_DELIM_CHAR, 'S', 'E', 'D', ' ', 0 };
const size_t len = strlen(str); const size_t len = strlen(str);
char buffer[CRYPT_BUFFER_SIZE]; char *str_encrypted = m_strdup(str);
char *buffer = new_malloc(CRYPT_BUFFER_SIZE);
char *ptr; char *ptr;
my_encrypt(str, len, key); my_encrypt(str_encrypted, len, key);
ptr = ctcp_quote_it(str, len); ptr = ctcp_quote_it(str_encrypted, len);
new_free(&str_encrypted);
if (ptr)
{
/* The - 1 terms here are to ensure that the trailing CTCP_DELIM_CHAR /* The - 1 terms here are to ensure that the trailing CTCP_DELIM_CHAR
* always gets added. */ * always gets added. */
strlcpy(buffer, sed_prefix, sizeof buffer - 1); strlcpy(buffer, sed_prefix, CRYPT_BUFFER_SIZE - 1);
strlcat(buffer, ptr, sizeof buffer - 1); strlcat(buffer, ptr, CRYPT_BUFFER_SIZE - 1);
strlcat(buffer, CTCP_DELIM_STR, sizeof buffer); strlcat(buffer, CTCP_DELIM_STR, CRYPT_BUFFER_SIZE);
new_free(&ptr); new_free(&ptr);
}
else
strlcpy(buffer, str, sizeof buffer);
return (m_strdup(buffer)); return buffer;
} }
/* /*
@@ -658,13 +655,8 @@ char *sed_decrypt_msg(const char *str, const char *key)
ptr = ctcp_unquote_it(str, &len); ptr = ctcp_unquote_it(str, &len);
my_decrypt(ptr, len, key); my_decrypt(ptr, len, key);
if (ptr)
{
strlcpy(buffer, ptr, CRYPT_BUFFER_SIZE); strlcpy(buffer, ptr, CRYPT_BUFFER_SIZE);
new_free(&ptr); new_free(&ptr);
}
else
strlcat(buffer, str, CRYPT_BUFFER_SIZE);
return buffer; return buffer;
} }