Fix some issues regarding header extraction.

This commit is contained in:
Jan Vidar Krey 2009-08-31 19:45:47 +02:00
parent 1e380ecf0f
commit 993d9ae33b
4 changed files with 73 additions and 41 deletions

View File

@ -521,7 +521,7 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
struct adc_message* adc_msg_create(const char* line) struct adc_message* adc_msg_create(const char* line)
{ {
return adc_msg_parse_verify(NULL, line, strlen(line)); return adc_msg_parse(line, strlen(line));
} }
@ -737,6 +737,27 @@ int adc_msg_add_named_argument(struct adc_message* cmd, const char prefix[2], co
return ret; return ret;
} }
int adc_msg_add_named_argument_string(struct adc_message* cmd, const char prefix[2], const char* string)
{
char* escaped = adc_msg_escape(string);
int ret = adc_msg_add_named_argument(cmd, prefix, escaped);
hub_free(escaped);
return ret;
}
int adc_msg_add_named_argument_int(struct adc_message* cmd, const char prefix[2], int num)
{
char* s = uhub_itoa(num);
int ret = adc_msg_add_named_argument(cmd, prefix, s);
return ret;
}
int adc_msg_add_named_argument_uint64(struct adc_message* cmd, const char prefix[2], uint64_t num)
{
char* s = uhub_ulltoa(num);
int ret = adc_msg_add_named_argument(cmd, prefix, s);
return ret;
}
int adc_msg_add_argument(struct adc_message* cmd, const char* string) int adc_msg_add_argument(struct adc_message* cmd, const char* string)
{ {

View File

@ -173,6 +173,22 @@ extern int adc_msg_add_argument(struct adc_message* cmd, const char* string);
*/ */
extern int adc_msg_add_named_argument(struct adc_message* cmd, const char prefix[2], const char* string); extern int adc_msg_add_named_argument(struct adc_message* cmd, const char prefix[2], const char* string);
/**
* Append a string as a named argument.
* The string will automatcally be escaped, if you do not wish to escape th string use adc_msg_add_named_argument() instead.
*
* @arg prefix a 2 character argument prefix
* @arg string must NOT be escaped
* @return 0 if successful, or -1 if an error occured (out of memory).
*/
extern int adc_msg_add_named_argument_string(struct adc_message* cmd, const char prefix[2], const char* string);
/**
* Append an integer as a named argument.
*/
extern int adc_msg_add_named_argument_int(struct adc_message* cmd, const char prefix[2], int integer);
extern int adc_msg_add_named_argument_uint64(struct adc_message* cmd, const char prefix[2], uint64_t num);
/** /**
* Convert a ADC command escaped string to a regular string. * Convert a ADC command escaped string to a regular string.
* @return string or NULL if out of memory * @return string or NULL if out of memory

View File

@ -64,14 +64,11 @@ static void adc_cid_pid(struct ADC_client* client)
base32_encode((unsigned char*) tiger_res1, TIGERSIZE, pid); base32_encode((unsigned char*) tiger_res1, TIGERSIZE, pid);
tiger((uint64_t*) tiger_res1, TIGERSIZE, tiger_res2); tiger((uint64_t*) tiger_res1, TIGERSIZE, tiger_res2);
base32_encode((unsigned char*) tiger_res2, TIGERSIZE, cid); base32_encode((unsigned char*) tiger_res2, TIGERSIZE, cid);
cid[ADC_CID_SIZE] = 0; cid[ADC_CID_SIZE] = 0;
pid[ADC_CID_SIZE] = 0; pid[ADC_CID_SIZE] = 0;
strcat(client->info, " PD"); adc_msg_add_named_argument(client->info, ADC_INF_FLAG_PRIVATE_ID, pid);
strcat(client->info, pid); adc_msg_add_named_argument(client->info, ADC_INF_FLAG_CLIENT_ID, cid);
strcat(client->info, " ID");
strcat(client->info, cid);
} }
@ -118,14 +115,23 @@ static void event_callback(struct net_connection* con, int events, void *arg)
} }
} }
#define EXTRACT(MSG, NAME, TARGET) \ #define UNESCAPE_ARG(TMP, TARGET) \
if (TMP) \
TARGET = adc_msg_unescape(TMP); \
else \
TARGET = NULL; \
hub_free(TMP);
#define EXTRACT_NAMED_ARG(MSG, NAME, TARGET) \
do { \ do { \
char* tmp = adc_msg_get_named_argument(MSG, NAME); \ char* tmp = adc_msg_get_named_argument(MSG, NAME); \
if (tmp) \ UNESCAPE_ARG(tmp, TARGET); \
TARGET = adc_msg_unescape(tmp); \ } while (0)
else \
TARGET = NULL; \ #define EXTRACT_POS_ARG(MSG, POS, TARGET) \
hub_free(tmp); \ do { \
char* tmp = adc_msg_get_argument(MSG, POS); \
UNESCAPE_ARG(tmp, TARGET); \
} while (0) } while (0)
@ -170,13 +176,11 @@ static void ADC_client_on_recv_line(struct ADC_client* client, const char* line,
case ADC_CMD_IMSG: case ADC_CMD_IMSG:
{ {
struct ADC_chat_message chat; struct ADC_chat_message chat;
struct ADC_client_callback_data data;
chat.from_sid = msg->source; chat.from_sid = msg->source;
chat.to_sid = msg->target; chat.to_sid = msg->target;
char* message = adc_msg_get_argument(msg, 0);
chat.message = adc_msg_unescape(message);
hub_free(message);
struct ADC_client_callback_data data;
data.chat = &chat; data.chat = &chat;
EXTRACT_POS_ARG(msg, 0, chat.message);
client->callback(client, ADC_CLIENT_MESSAGE, &data); client->callback(client, ADC_CLIENT_MESSAGE, &data);
hub_free(chat.message); hub_free(chat.message);
break; break;
@ -185,9 +189,9 @@ static void ADC_client_on_recv_line(struct ADC_client* client, const char* line,
case ADC_CMD_IINF: case ADC_CMD_IINF:
{ {
struct ADC_hub_info hubinfo; struct ADC_hub_info hubinfo;
EXTRACT(msg, "NI", hubinfo.name); EXTRACT_NAMED_ARG(msg, "NI", hubinfo.name);
EXTRACT(msg, "DE", hubinfo.description); EXTRACT_NAMED_ARG(msg, "DE", hubinfo.description);
EXTRACT(msg, "VE", hubinfo.version); EXTRACT_NAMED_ARG(msg, "VE", hubinfo.version);
struct ADC_client_callback_data data; struct ADC_client_callback_data data;
data.hubinfo = &hubinfo; data.hubinfo = &hubinfo;
@ -299,30 +303,21 @@ void ADC_client_send(struct ADC_client* client, char* msg)
void ADC_client_send_info(struct ADC_client* client) void ADC_client_send_info(struct ADC_client* client)
{ {
client->info[0] = 0; char binf[11];
strcat(client->info, "BINF "); snprintf(binf, 11, "BINF %s\n", sid_to_string(client->sid));
strcat(client->info, sid_to_string(client->sid)); client->info = adc_msg_create(binf);
strcat(client->info, " NI");
strcat(client->info, client->nick); /* FIXME: no escaping */ adc_msg_add_named_argument_string(client->info, ADC_INF_FLAG_NICK, client->nick);
strcat(client->info, "_");
strcat(client->info, uhub_itoa(client->sid));
strcat(client->info, " VE" VERSION);
if (client->desc) if (client->desc)
{ {
strcat(client->info, " DE"); adc_msg_add_named_argument_string(client->info, ADC_INF_FLAG_DESCRIPTION, client->desc);
strcat(client->info, client->desc); /* FIXME: no escaping */
} }
strcat(client->info, " I40.0.0.0");
strcat(client->info, " EMuhub@extatic.org"); adc_msg_add_named_argument_string(client->info, ADC_INF_FLAG_USER_AGENT, PRODUCT "/" VERSION);
strcat(client->info, " SL3");
strcat(client->info, " HN1");
strcat(client->info, " HR1");
strcat(client->info, " HO1");
adc_cid_pid(client); adc_cid_pid(client);
strcat(client->info, "\n"); ADC_client_send(client, client->info->cache);
ADC_client_send(client, client->info);
} }
int ADC_client_create(struct ADC_client* client, const char* nickname, const char* description) int ADC_client_create(struct ADC_client* client, const char* nickname, const char* description)

View File

@ -92,7 +92,7 @@ struct ADC_client
{ {
sid_t sid; sid_t sid;
enum ADC_client_state state; enum ADC_client_state state;
char info[ADC_BUFSIZE]; struct adc_message* info;
char recvbuf[ADC_BUFSIZE]; char recvbuf[ADC_BUFSIZE];
char sendbuf[ADC_BUFSIZE]; char sendbuf[ADC_BUFSIZE];
adc_client_cb callback; adc_client_cb callback;