Messenger stores / loads nickname

new toxic command to show nickname (mynick)
This commit is contained in:
Michael Rose 2013-08-17 11:59:28 +02:00
parent f2c336e4f5
commit d1d24c123d
3 changed files with 51 additions and 5 deletions

View File

@ -850,8 +850,15 @@ void doMessenger(Messenger *m)
/* returns the size of the messenger data (for saving) */ /* returns the size of the messenger data (for saving) */
uint32_t Messenger_size(Messenger *m) uint32_t Messenger_size(Messenger *m)
{ {
return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
+ sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * m->numfriends; + sizeof(uint32_t) // nospam
+ sizeof(uint32_t) // DHT size
+ DHT_size() // DHT itself
+ sizeof(uint32_t) // Friendlist size
+ sizeof(Friend) * m->numfriends // Friendlist itself
+ sizeof(uint16_t) // Own nickname length
+ m->name_length // Own nickname
;
} }
/* save the messenger in data of size Messenger_size() */ /* save the messenger in data of size Messenger_size() */
@ -871,6 +878,11 @@ void Messenger_save(Messenger *m, uint8_t *data)
memcpy(data, &size, sizeof(size)); memcpy(data, &size, sizeof(size));
data += sizeof(size); data += sizeof(size);
memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends); memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends);
data += size;
uint16_t small_size = m->name_length;
memcpy(data, &small_size, sizeof(small_size));
data += sizeof(small_size);
memcpy(data, m->name, small_size);
} }
/* load the messenger from data of size length. */ /* load the messenger from data of size length. */
@ -905,7 +917,7 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
memcpy(&size, data, sizeof(size)); memcpy(&size, data, sizeof(size));
data += sizeof(size); data += sizeof(size);
if (length != size || length % sizeof(Friend) != 0) if (length < size || size % sizeof(Friend) != 0)
return -1; return -1;
Friend *temp = malloc(size); Friend *temp = malloc(size);
@ -931,6 +943,23 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
} }
} }
data += size;
length -= size;
uint16_t small_size;
if (length < sizeof(small_size))
return -1;
memcpy(&small_size, data, sizeof(small_size));
data += sizeof(small_size);
length -= sizeof(small_size);
if (length != small_size)
return -1;
setname(m, data, small_size);
free(temp); free(temp);
return 0; return 0;
} }

View File

@ -13,6 +13,9 @@
#include "windows.h" #include "windows.h"
#include "prompt.h" #include "prompt.h"
extern char *DATA_FILE;
extern int store_data(Messenger *m, char *path);
uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX
uint8_t num_requests = 0; // XXX uint8_t num_requests = 0; // XXX
@ -29,11 +32,12 @@ void cmd_help(ToxWindow *, Messenger *m, char **);
void cmd_msg(ToxWindow *, Messenger *m, char **); void cmd_msg(ToxWindow *, Messenger *m, char **);
void cmd_myid(ToxWindow *, Messenger *m, char **); void cmd_myid(ToxWindow *, Messenger *m, char **);
void cmd_nick(ToxWindow *, Messenger *m, char **); void cmd_nick(ToxWindow *, Messenger *m, char **);
void cmd_mynick(ToxWindow *, Messenger *m, char **);
void cmd_quit(ToxWindow *, Messenger *m, char **); void cmd_quit(ToxWindow *, Messenger *m, char **);
void cmd_status(ToxWindow *, Messenger *m, char **); void cmd_status(ToxWindow *, Messenger *m, char **);
void cmd_statusmsg(ToxWindow *, Messenger *m, char **); void cmd_statusmsg(ToxWindow *, Messenger *m, char **);
#define NUM_COMMANDS 13 #define NUM_COMMANDS 14
static struct { static struct {
char *name; char *name;
@ -49,6 +53,7 @@ static struct {
{ "msg", 2, cmd_msg }, { "msg", 2, cmd_msg },
{ "myid", 0, cmd_myid }, { "myid", 0, cmd_myid },
{ "nick", 1, cmd_nick }, { "nick", 1, cmd_nick },
{ "mynick", 0, cmd_mynick },
{ "q", 0, cmd_quit }, { "q", 0, cmd_quit },
{ "quit", 0, cmd_quit }, { "quit", 0, cmd_quit },
{ "status", 2, cmd_status }, { "status", 2, cmd_status },
@ -222,6 +227,7 @@ void cmd_help(ToxWindow *self, Messenger *m, char **args)
wprintw(self->window, " status <type> <message> : Set your status\n"); wprintw(self->window, " status <type> <message> : Set your status\n");
wprintw(self->window, " statusmsg <message> : Set your status\n"); wprintw(self->window, " statusmsg <message> : Set your status\n");
wprintw(self->window, " nick <nickname> : Set your nickname\n"); wprintw(self->window, " nick <nickname> : Set your nickname\n");
wprintw(self->window, " mynick : Print your current nickname\n");
wprintw(self->window, " accept <number> : Accept friend request\n"); wprintw(self->window, " accept <number> : Accept friend request\n");
wprintw(self->window, " myid : Print your ID\n"); wprintw(self->window, " myid : Print your ID\n");
wprintw(self->window, " quit/exit : Exit program\n"); wprintw(self->window, " quit/exit : Exit program\n");
@ -267,6 +273,17 @@ void cmd_nick(ToxWindow *self, Messenger *m, char **args)
char *nick = args[1]; char *nick = args[1];
setname(m, (uint8_t *) nick, strlen(nick) + 1); setname(m, (uint8_t *) nick, strlen(nick) + 1);
wprintw(self->window, "Nickname set to: %s\n", nick); wprintw(self->window, "Nickname set to: %s\n", nick);
if (store_data(m, DATA_FILE)) {
wprintw(self->window, "\nCould not store Messenger data\n");
}
}
void cmd_mynick(ToxWindow *self, Messenger *m, char **args)
{
uint8_t *nick = malloc(m->name_length);
getself_name(m, nick, m->name_length);
wprintw(self->window, "Current nickname: %s\n", nick);
free(nick);
} }
void cmd_status(ToxWindow *self, Messenger *m, char **args) void cmd_status(ToxWindow *self, Messenger *m, char **args)

View File

@ -83,7 +83,7 @@ void on_friendadded(Messenger *m, int friendnumber)
{ {
friendlist_onFriendAdded(m, friendnumber); friendlist_onFriendAdded(m, friendnumber);
if (store_data(m, DATA_FILE) != 0) { if (store_data(m, DATA_FILE)) {
wprintw(prompt->window, "\nCould not store Messenger data\n"); wprintw(prompt->window, "\nCould not store Messenger data\n");
} }
} }