Added new convenient adc_msg_* functions for accessing named parameters.

This commit is contained in:
Jan Vidar Krey 2009-08-31 19:42:34 +02:00
parent af7b52b708
commit fe9b48f14c
4 changed files with 53 additions and 28 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);
} }
@ -306,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;