fishlim: Improve string handling
This commit is contained in:
parent
46061f4f3c
commit
c54a0c6db9
|
@ -22,8 +22,7 @@
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <glib.h>
|
||||||
#include <string.h>
|
|
||||||
#include "irc.h"
|
#include "irc.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -65,7 +64,6 @@ bool irc_parse_message(const char *words[],
|
||||||
*/
|
*/
|
||||||
char *irc_prefix_get_nick(const char *prefix) {
|
char *irc_prefix_get_nick(const char *prefix) {
|
||||||
const char *end;
|
const char *end;
|
||||||
char *nick;
|
|
||||||
size_t length;
|
size_t length;
|
||||||
|
|
||||||
if (!prefix) return NULL;
|
if (!prefix) return NULL;
|
||||||
|
@ -76,13 +74,7 @@ char *irc_prefix_get_nick(const char *prefix) {
|
||||||
|
|
||||||
// Allocate string
|
// Allocate string
|
||||||
length = end - prefix;
|
length = end - prefix;
|
||||||
nick = malloc(length+1);
|
return g_strndup (prefix, length);
|
||||||
if (!nick) return NULL;
|
|
||||||
|
|
||||||
// Copy to string
|
|
||||||
memcpy(nick, prefix, length);
|
|
||||||
nick[length] = '\0';
|
|
||||||
return nick;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -55,24 +55,6 @@ gchar *get_config_filename() {
|
||||||
return g_build_filename(hexchat_get_info(ph, "configdir"), "addon_fishlim.conf", NULL);
|
return g_build_filename(hexchat_get_info(ph, "configdir"), "addon_fishlim.conf", NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Appends data to a string. Returns true if there was sufficient memory.
|
|
||||||
* Frees *s and returns false if an error occurs.
|
|
||||||
*/
|
|
||||||
static bool append(char **s, size_t *length, const char *data) {
|
|
||||||
size_t datalen = strlen(data);
|
|
||||||
char *extended = realloc(*s, *length + datalen + 1);
|
|
||||||
if (!extended) {
|
|
||||||
free(*s);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
memcpy(extended + *length, data, datalen + 1);
|
|
||||||
*s = extended;
|
|
||||||
*length += datalen;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*static int handle_debug(char *word[], char *word_eol[], void *userdata) {
|
/*static int handle_debug(char *word[], char *word_eol[], void *userdata) {
|
||||||
hexchat_printf(ph, "debug incoming: ");
|
hexchat_printf(ph, "debug incoming: ");
|
||||||
for (size_t i = 1; word[i] != NULL && word[i][0] != '\0'; i++) {
|
for (size_t i = 1; word[i] != NULL && word[i][0] != '\0'; i++) {
|
||||||
|
@ -114,12 +96,11 @@ static int handle_incoming(char *word[], char *word_eol[], void *userdata) {
|
||||||
const char *peice;
|
const char *peice;
|
||||||
char *sender_nick;
|
char *sender_nick;
|
||||||
char *decrypted;
|
char *decrypted;
|
||||||
char *message;
|
|
||||||
size_t w;
|
size_t w;
|
||||||
size_t ew;
|
size_t ew;
|
||||||
size_t uw;
|
size_t uw;
|
||||||
size_t length;
|
|
||||||
char prefix_char = 0;
|
char prefix_char = 0;
|
||||||
|
GString *message;
|
||||||
|
|
||||||
if (!irc_parse_message((const char **)word, &prefix, &command, &w))
|
if (!irc_parse_message((const char **)word, &prefix, &command, &w))
|
||||||
return HEXCHAT_EAT_NONE;
|
return HEXCHAT_EAT_NONE;
|
||||||
|
@ -149,12 +130,12 @@ static int handle_incoming(char *word[], char *word_eol[], void *userdata) {
|
||||||
if (!decrypted) goto decrypt_error;
|
if (!decrypted) goto decrypt_error;
|
||||||
|
|
||||||
// Build unecrypted message
|
// Build unecrypted message
|
||||||
message = NULL;
|
message = g_string_sized_new (100); /* TODO: more accurate estimation of size */
|
||||||
length = 0;
|
g_string_append (message, "RECV");
|
||||||
if (!append(&message, &length, "RECV")) goto decrypt_error;
|
|
||||||
|
|
||||||
for (uw = 1; uw < HEXCHAT_MAX_WORDS; uw++) {
|
for (uw = 1; uw < HEXCHAT_MAX_WORDS; uw++) {
|
||||||
if (word[uw][0] != '\0' && !append(&message, &length, " ")) goto decrypt_error;
|
if (word[uw][0] != '\0')
|
||||||
|
g_string_append_c (message, ' ');
|
||||||
|
|
||||||
if (uw == ew) {
|
if (uw == ew) {
|
||||||
// Add the encrypted data
|
// Add the encrypted data
|
||||||
|
@ -163,12 +144,11 @@ static int handle_incoming(char *word[], char *word_eol[], void *userdata) {
|
||||||
|
|
||||||
if (ew == w+1) {
|
if (ew == w+1) {
|
||||||
// Prefix with colon, which gets stripped out otherwise
|
// Prefix with colon, which gets stripped out otherwise
|
||||||
if (!append(&message, &length, ":")) goto decrypt_error;
|
g_string_append_c (message, ':');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prefix_char) {
|
if (prefix_char) {
|
||||||
char prefix_str[2] = { prefix_char, '\0' };
|
g_string_append_c (message, prefix_char);
|
||||||
if (!append(&message, &length, prefix_str)) goto decrypt_error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@ -176,16 +156,16 @@ static int handle_incoming(char *word[], char *word_eol[], void *userdata) {
|
||||||
peice = word[uw];
|
peice = word[uw];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!append(&message, &length, peice)) goto decrypt_error;
|
g_string_append (message, peice);
|
||||||
}
|
}
|
||||||
free(decrypted);
|
free(decrypted);
|
||||||
|
|
||||||
// Simulate unencrypted message
|
// Simulate unencrypted message
|
||||||
//hexchat_printf(ph, "simulating: %s\n", message);
|
//hexchat_printf(ph, "simulating: %s\n", message->str);
|
||||||
hexchat_command(ph, message);
|
hexchat_command(ph, message->str);
|
||||||
|
|
||||||
free(message);
|
g_string_free (message, TRUE);
|
||||||
free(sender_nick);
|
g_free(sender_nick);
|
||||||
return HEXCHAT_EAT_HEXCHAT;
|
return HEXCHAT_EAT_HEXCHAT;
|
||||||
|
|
||||||
decrypt_error:
|
decrypt_error:
|
||||||
|
|
Loading…
Reference in New Issue