From 12b19f691072b7cf292329953a9f8516dc6bf0a1 Mon Sep 17 00:00:00 2001 From: Peter Maatman Date: Tue, 30 Jul 2013 17:09:09 +0200 Subject: [PATCH 1/3] renamed getname to getfriendname and added a getname function to get the name of our client --- core/Messenger.c | 8 +++++++- core/Messenger.h | 6 +++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/core/Messenger.c b/core/Messenger.c index f77b4491..148c3350 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -234,12 +234,18 @@ int setname(uint8_t * name, uint16_t length) return 0; } +int getname(uint8_t *name) +{ + memcpy(name, self_name, sizeof(self_name)); + return 0; +} + /* get name of friendnumber put it in name name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. return 0 if success return -1 if failure */ -int getname(int friendnumber, uint8_t * name) +int getfriendname(int friendnumber, uint8_t * name) { if (friendnumber >= numfriends || friendnumber < 0) return -1; diff --git a/core/Messenger.h b/core/Messenger.h index 7263901c..08f6de2a 100644 --- a/core/Messenger.h +++ b/core/Messenger.h @@ -92,12 +92,16 @@ int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length); return -1 if failure */ int setname(uint8_t *name, uint16_t length); +/* Get our own nickname + Set's name to the our own nickname */ +int getname(uint8_t *name); + /* get name of friendnumber put it in name name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. return 0 if success return -1 if failure */ -int getname(int friendnumber, uint8_t *name); +int getfriendname(int friendnumber, uint8_t *name); /* set our user status you are responsible for freeing status after From 2c756f1c53e0c4faad8716425eafdf1b6e58f5c0 Mon Sep 17 00:00:00 2001 From: Peter Maatman Date: Tue, 30 Jul 2013 17:10:20 +0200 Subject: [PATCH 2/3] changing testing clients to reflect changes made in core/Messenger.h --- testing/Messenger_test.c | 2 +- testing/nTox.c | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 0518d284..baecea36 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -128,7 +128,7 @@ int main(int argc, char *argv[]) while(1) { uint8_t name[128]; - getname(num, name); + getfriendname(num, name); printf("%s\n", name); m_sendmessage(num, (uint8_t*)"Test", 5); diff --git a/testing/nTox.c b/testing/nTox.c index 960dfb27..c960f761 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -88,6 +88,19 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) int num = atoi(numstring); if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) { new_lines("[i] could not send message"); + } else { + char name[MAX_NAME_LENGTH]; + getname((uint8_t*)name); + char msg[100+strlen(message)+strlen(name)+1]; + time_t rawtime; + struct tm * timeinfo; + time ( &rawtime ); + timeinfo = localtime ( &rawtime ); + char* time = asctime(timeinfo); + size_t len = strlen(time); + time[len-1]='\0'; + sprintf(msg, "[%d] %s <%s> %s", num, time, name, message); // timestamp + new_lines(msg); } } else if (line[1] == 'n') { @@ -214,7 +227,7 @@ void print_request(uint8_t *public_key, uint8_t *data, uint16_t length) void print_message(int friendnumber, uint8_t * string, uint16_t length) { char name[MAX_NAME_LENGTH]; - getname(friendnumber, (uint8_t*)name); + getfriendname(friendnumber, (uint8_t*)name); char msg[100+length+strlen(name)+1]; time_t rawtime; struct tm * timeinfo; @@ -229,7 +242,7 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length) void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) { char name[MAX_NAME_LENGTH]; - getname(friendnumber, (uint8_t*)name); + getfriendname(friendnumber, (uint8_t*)name); char msg[100+length]; sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); new_lines(msg); @@ -237,7 +250,7 @@ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) { void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) { char name[MAX_NAME_LENGTH]; - getname(friendnumber, (uint8_t*)name); + getfriendname(friendnumber, (uint8_t*)name); char msg[100+length+strlen(name)+1]; sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); new_lines(msg); From dc57b779d2e3d1df836698cd119a3890ffecf86d Mon Sep 17 00:00:00 2001 From: Peter Maatman Date: Tue, 30 Jul 2013 17:32:17 +0200 Subject: [PATCH 3/3] added utility function format_message --- testing/nTox.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/testing/nTox.c b/testing/nTox.c index c960f761..3d410f19 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -50,6 +50,26 @@ void new_lines(char *line) do_refresh(); } +char *format_message(char *message, int friendnum) +{ + char name[MAX_NAME_LENGTH]; + if(friendnum != -1) { + getfriendname(friendnum, (uint8_t*)name); + } else { + getname((uint8_t*)name); + } + char *msg = malloc(100+strlen(message)+strlen(name)+1); + time_t rawtime; + struct tm * timeinfo; + time ( &rawtime ); + timeinfo = localtime ( &rawtime ); + char* time = asctime(timeinfo); + size_t len = strlen(time); + time[len-1]='\0'; + sprintf(msg, "[%d] %s <%s> %s", friendnum, time, name, message); // timestamp + return msg; +} + void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) { if (line[0] == '/') { @@ -89,18 +109,7 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) { new_lines("[i] could not send message"); } else { - char name[MAX_NAME_LENGTH]; - getname((uint8_t*)name); - char msg[100+strlen(message)+strlen(name)+1]; - time_t rawtime; - struct tm * timeinfo; - time ( &rawtime ); - timeinfo = localtime ( &rawtime ); - char* time = asctime(timeinfo); - size_t len = strlen(time); - time[len-1]='\0'; - sprintf(msg, "[%d] %s <%s> %s", num, time, name, message); // timestamp - new_lines(msg); + new_lines(format_message(message, -1)); } } else if (line[1] == 'n') { @@ -237,7 +246,7 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length) size_t len = strlen(temp); temp[len-1]='\0'; sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // timestamp - new_lines(msg); + new_lines(format_message((char*)string, friendnumber)); } void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) {