Merge pull request #341 from stal888/userstatus-ext

An extension to user statuses.
This commit is contained in:
irungentoo 2013-08-05 17:24:04 -07:00
commit 109fe7bd0a
7 changed files with 126 additions and 48 deletions

View File

@ -35,6 +35,7 @@ typedef struct {
uint8_t *userstatus;
uint16_t userstatus_length;
uint8_t userstatus_sent;
USERSTATUS_KIND userstatus_kind;
uint16_t info_size; /* length of the info */
} Friend;
@ -45,6 +46,7 @@ static uint16_t self_name_length;
static uint8_t *self_userstatus;
static uint16_t self_userstatus_len;
static USERSTATUS_KIND self_userstatus_kind;
#define MAX_NUM_FRIENDS 256
@ -98,12 +100,12 @@ int getclient_id(int friend_id, uint8_t *client_id)
* return FAERR_NOMESSAGE if no message (message length must be >= 1 byte)
* return FAERR_OWNKEY if user's own key
* return FAERR_ALREADYSENT if friend request already sent or already a friend
* return FAERR_UNKNOWN for unknown error
* return FAERR_UNKNOWN for unknown error
*/
int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
{
if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES
- crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES
if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES
- crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES
+ crypto_box_ZEROBYTES))
return FAERR_TOOLONG;
if (length < 1)
@ -123,6 +125,7 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
friendlist[i].userstatus = calloc(1, 1);
friendlist[i].userstatus_length = 1;
friendlist[i].userstatus_kind = USERSTATUS_KIND_OFFLINE;
memcpy(friendlist[i].info, data, length);
friendlist[i].info_size = length;
@ -205,7 +208,7 @@ int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length)
return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1);
}
/* send a name packet to friendnumber
/* send a name packet to friendnumber
length is the length with the NULL terminator*/
static int m_sendname(int friendnumber, uint8_t * name, uint16_t length)
{
@ -247,14 +250,14 @@ int setname(uint8_t * name, uint16_t length)
}
/* get our nickname
put it in name
put it in name
name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
return the length of the name */
uint16_t getself_name(uint8_t *name)
{
memcpy(name, self_name, self_name_length);
return self_name_length;
}
}
/* get name of friendnumber
put it in name
@ -269,10 +272,13 @@ int getname(int friendnumber, uint8_t * name)
return 0;
}
int m_set_userstatus(uint8_t *status, uint16_t length)
int m_set_userstatus(USERSTATUS_KIND kind, uint8_t *status, uint16_t length)
{
if (length > MAX_USERSTATUS_LENGTH)
return -1;
if (kind != USERSTATUS_KIND_RETAIN) {
self_userstatus_kind = kind;
}
uint8_t *newstatus = calloc(length, 1);
memcpy(newstatus, status, length);
free(self_userstatus);
@ -285,6 +291,20 @@ int m_set_userstatus(uint8_t *status, uint16_t length)
return 0;
}
int m_set_userstatus_kind(USERSTATUS_KIND kind) {
if (kind >= USERSTATUS_KIND_INVALID) {
return -1;
}
if (kind == USERSTATUS_KIND_RETAIN) {
return 0;
}
self_userstatus_kind = kind;
uint32_t i;
for (i = 0; i < numfriends; ++i)
friendlist[i].userstatus_sent = 0;
return 0;
}
/* return the size of friendnumber's user status
guaranteed to be at most MAX_USERSTATUS_LENGTH */
int m_get_userstatus_size(int friendnumber)
@ -305,11 +325,33 @@ int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen)
return 0;
}
int m_copy_self_userstatus(uint8_t * buf, uint32_t maxlen)
{
memset(buf, 0, maxlen);
memcpy(buf, self_userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1);
return 0;
}
USERSTATUS_KIND m_get_userstatus_kind(int friendnumber) {
if (friendnumber >= numfriends || friendnumber < 0)
return USERSTATUS_KIND_INVALID;
USERSTATUS_KIND uk = friendlist[friendnumber].userstatus_kind;
if (uk >= USERSTATUS_KIND_INVALID) {
uk = USERSTATUS_KIND_ONLINE;
}
return uk;
}
USERSTATUS_KIND m_get_self_userstatus_kind(void) {
return self_userstatus_kind;
}
static int send_userstatus(int friendnumber, uint8_t * status, uint16_t length)
{
uint8_t *thepacket = malloc(length + 1);
memcpy(thepacket + 1, status, length);
uint8_t *thepacket = malloc(length + 2);
memcpy(thepacket + 2, status, length);
thepacket[0] = PACKET_ID_USERSTATUS;
thepacket[1] = self_userstatus_kind;
int written = write_cryptpacket(friendlist[friendnumber].crypt_connection_id, thepacket, length + 1);
free(thepacket);
return written;
@ -327,6 +369,11 @@ static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t le
return 0;
}
static void set_friend_userstatus_kind(int friendnumber, USERSTATUS_KIND k)
{
friendlist[friendnumber].userstatus_kind = k;
}
/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
static uint8_t friend_request_isset = 0; */
/* set the function that will be executed when a friend request is received. */
@ -353,9 +400,9 @@ void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t))
friend_namechange_isset = 1;
}
static void (*friend_statuschange)(int, uint8_t *, uint16_t);
static void (*friend_statuschange)(int, USERSTATUS_KIND, uint8_t *, uint16_t);
static uint8_t friend_statuschange_isset = 0;
void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t))
void m_callback_userstatus(void (*function)(int, USERSTATUS_KIND, uint8_t *, uint16_t))
{
friend_statuschange = function;
friend_statuschange_isset = 1;
@ -366,7 +413,7 @@ void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t))
int initMessenger(void)
{
new_keys();
m_set_userstatus((uint8_t*)"Online", sizeof("Online"));
m_set_userstatus(USERSTATUS_KIND_ONLINE, (uint8_t*)"Online", sizeof("Online"));
initNetCrypto();
IP ip;
ip.i = 0;
@ -438,12 +485,17 @@ static void doFriends(void)
break;
}
case PACKET_ID_USERSTATUS: {
uint8_t *status = calloc(MIN(len - 1, MAX_USERSTATUS_LENGTH), 1);
memcpy(status, temp + 1, MIN(len - 1, MAX_USERSTATUS_LENGTH));
if (friend_statuschange_isset)
friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH));
set_friend_userstatus(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH));
free(status);
if (len > 2) {
uint8_t *status = calloc(MIN(len - 2, MAX_USERSTATUS_LENGTH), 1);
memcpy(status, temp + 2, MIN(len - 2, MAX_USERSTATUS_LENGTH));
if (friend_statuschange_isset)
friend_statuschange(i, temp[1], status, MIN(len - 2, MAX_USERSTATUS_LENGTH));
set_friend_userstatus(i, status, MIN(len - 2, MAX_USERSTATUS_LENGTH));
free(status);
} else if (friend_statuschange_isset) {
friend_statuschange(i, temp[1], friendlist[i].userstatus, friendlist[i].userstatus_length);
}
set_friend_userstatus_kind(i, temp[1]);
break;
}
case PACKET_ID_MESSAGE: {

View File

@ -60,6 +60,22 @@ extern "C" {
/* don't assume MAX_USERSTATUS_LENGTH will stay at 128, it may be increased
to an absurdly large number later */
/* USERSTATUS_KIND
* Represents the different kinds of userstatus
* someone can have.
* More on this later... */
typedef enum {
USERSTATUS_KIND_RETAIN = (uint8_t)0, /* This is a special value that must not be returned by
* m_get_userstatus_kind. You can pass it into m_set_userstatus
* to keep the current USERSTATUS_KIND. */
USERSTATUS_KIND_ONLINE, /* Recommended representation: Green. */
USERSTATUS_KIND_AWAY, /* Recommended representation: Orange, or yellow. */
USERSTATUS_KIND_BUSY, /* Recommended representation: Red. */
USERSTATUS_KIND_OFFLINE, /* Recommended representation: Grey, semi-transparent. */
USERSTATUS_KIND_INVALID,
} USERSTATUS_KIND;
/*
* add a friend
* set the data that will be sent along with friend request
@ -70,7 +86,7 @@ extern "C" {
* return -2 if no message (message length must be >= 1 byte)
* return -3 if user's own key
* return -4 if friend request already sent or already a friend
* return -5 for unknown error
* return -5 for unknown error
*/
int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length);
@ -114,7 +130,7 @@ int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length);
int setname(uint8_t *name, uint16_t length);
/* get our nickname
put it in name
put it in name
return the length of the name*/
uint16_t getself_name(uint8_t *name);
@ -128,7 +144,8 @@ int getname(int friendnumber, uint8_t *name);
/* set our user status
you are responsible for freeing status after
returns 0 on success, -1 on failure */
int m_set_userstatus(uint8_t *status, uint16_t length);
int m_set_userstatus(USERSTATUS_KIND kind, uint8_t *status, uint16_t length);
int m_set_userstatus_kind(USERSTATUS_KIND kind);
/* return the length of friendnumber's user status,
including null
@ -136,8 +153,17 @@ int m_set_userstatus(uint8_t *status, uint16_t length);
int m_get_userstatus_size(int friendnumber);
/* copy friendnumber's userstatus into buf, truncating if size is over maxlen
get the size you need to allocate from m_get_userstatus_size */
get the size you need to allocate from m_get_userstatus_size
The self variant will copy our own userstatus. */
int m_copy_userstatus(int friendnumber, uint8_t *buf, uint32_t maxlen);
int m_copy_self_userstatus(uint8_t *buf, uint32_t maxlen);
/* Return one of USERSTATUS_KIND values, except USERSTATUS_KIND_RETAIN.
* Values unknown to your application should be represented as USERSTATUS_KIND_ONLINE.
* As above, the self variant will return our own USERSTATUS_KIND.
* If friendnumber is invalid, this shall return USERSTATUS_KIND_INVALID. */
USERSTATUS_KIND m_get_userstatus_kind(int friendnumber);
USERSTATUS_KIND m_get_self_userstatus_kind(void);
/* set the function that will be executed when a friend request is received.
function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
@ -153,9 +179,9 @@ void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t));
void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t));
/* set the callback for user status changes
function(int friendnumber, uint8_t *newstatus, uint16_t length)
function(int friendnumber, USERSTATUS_KIND kind, uint8_t *newstatus, uint16_t length)
you are not responsible for freeing newstatus */
void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t));
void m_callback_userstatus(void (*function)(int, USERSTATUS_KIND, uint8_t *, uint16_t));
/* run this at startup
returns 0 if no connection problems

View File

@ -228,7 +228,7 @@ void line_eval(char *line)
status[i-3] = line[i];
}
status[i-3] = 0;
m_set_userstatus(status, strlen((char*)status) + 1);
m_set_userstatus(USERSTATUS_KIND_ONLINE, status, strlen((char*)status) + 1);
char numstring[100];
sprintf(numstring, "[i] changed status to %s", (char*)status);
new_lines(numstring);
@ -364,7 +364,7 @@ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length)
}
}
void print_statuschange(int friendnumber, uint8_t *string, uint16_t length)
void print_statuschange(int friendnumber, USERSTATUS_KIND kind, uint8_t *string, uint16_t length)
{
char name[MAX_NAME_LENGTH];
if(getname(friendnumber, (uint8_t*)name) != -1) {
@ -392,7 +392,7 @@ void load_key(char *path)
}
Messenger_load(data, size);
} else {
} else {
//else save new keys
int size = Messenger_size();
uint8_t data[size];

View File

@ -18,7 +18,7 @@
*
* You should have received a copy of the GNU General Public License
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#include "nTox_win32.h"
@ -86,7 +86,7 @@ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length)
printf(msg);
}
void print_statuschange(int friendnumber, uint8_t *string, uint16_t length)
void print_statuschange(int friendnumber, USERSTATUS_KIND kind, uint8_t *string, uint16_t length)
{
char name[MAX_NAME_LENGTH];
getname(friendnumber, (uint8_t*)name);
@ -95,7 +95,7 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length)
printf(msg);
}
void load_key()
void load_key()
{
FILE *data_file = NULL;
data_file = fopen("data","r");
@ -130,7 +130,7 @@ void add_friend()
int i;
char temp_id[128];
for (i = 0; i < 128; i++)
for (i = 0; i < 128; i++)
temp_id[i] = line[i+3];
int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
@ -141,7 +141,7 @@ void add_friend()
printf(numstring);
++maxnumfriends;
}
else if (num == -1)
else if (num == -1)
printf("\n[i] Message is too long.\n\n");
else if (num == -2)
@ -180,7 +180,7 @@ void list_friends()
char name[MAX_NAME_LENGTH];
getname(i, (uint8_t*)name);
if (m_friendstatus(i) == 4)
if (m_friendstatus(i) == 4)
printf("[%d] %s\n", i, (uint8_t*)name);
}
@ -213,7 +213,7 @@ void message_friend()
for (i = 0; i < len; i++) {
if (line[i+3] != ' ')
if (line[i+3] != ' ')
numstring[i] = line[i+3];
else {
@ -243,7 +243,7 @@ void change_nickname()
for (i = 3; i < len; i++) {
if (line[i] == 0 || line[i] == '\n')
if (line[i] == 0 || line[i] == '\n')
break;
name[i-3] = line[i];
@ -268,7 +268,7 @@ void change_status(int savetofile)
size_t len = strlen(line);
for (i = 3; i < len; i++) {
if (line[i] == 0 || line[i] == '\n')
if (line[i] == 0 || line[i] == '\n')
break;
status[i-3] = line[i];
@ -350,7 +350,7 @@ void line_eval(char* line)
accept_friend_request(line);
}
/* EXIT */
else if (inpt_command == 'q') {
else if (inpt_command == 'q') {
strcpy(line, "---Offline");
change_status(0);
exit(EXIT_SUCCESS);
@ -398,7 +398,7 @@ int main(int argc, char *argv[])
nameloaded = 1;
printf("%s\n", name);
fclose(name_file);
}
}
FILE* status_file = NULL;
status_file = fopen("statusfile.txt", "r");
@ -424,7 +424,7 @@ int main(int argc, char *argv[])
{
if(self_public_key[i] < (PUB_KEY_BYTES/2))
strcpy(idstring1[i],"0");
else
else
strcpy(idstring1[i], "");
sprintf(idstring2[i], "%hhX",self_public_key[i]);
}
@ -442,7 +442,7 @@ int main(int argc, char *argv[])
int resolved_address = resolve_addr(argv[1]);
if (resolved_address != 0)
bootstrap_ip_port.ip.i = resolved_address;
else
else
exit(1);
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));

View File

@ -162,7 +162,7 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd)
return;
}
msg++;
m_set_userstatus((uint8_t*) msg, strlen(msg)+1);
m_set_userstatus(USERSTATUS_KIND_RETAIN, (uint8_t*) msg, strlen(msg)+1);
wprintw(ctx->history, "Status set to: %s\n", msg);
}
else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {

View File

@ -69,7 +69,7 @@ void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) {
}
}
void on_statuschange(int friendnumber, uint8_t* string, uint16_t length) {
void on_statuschange(int friendnumber, USERSTATUS_KIND kind, uint8_t* string, uint16_t length) {
size_t i;
wprintw(prompt->window, "\n(statuschange) %d: %s!\n", friendnumber, string);
@ -201,7 +201,7 @@ static void load_data(char *path) {
Messenger_load(buf, len);
}
else {
else {
len = Messenger_size();
buf = malloc(len);
@ -289,7 +289,7 @@ int main(int argc, char* argv[]) {
for(i = 0; i < argc; i++) {
if (argv[i] == NULL){
break;
break;
} else if(argv[i][0] == '-') {
if(argv[i][1] == 'f') {
if(argv[i + 1] != NULL)

View File

@ -91,7 +91,7 @@ static void execute(ToxWindow* self, char* cmd) {
dht.ip.i = resolved_address;
unsigned char *binary_string = hex_string_to_bin(key);
DHT_bootstrap(dht, binary_string);
free(binary_string);
free(binary_string);
}
else if(!strncmp(cmd, "add ", strlen("add "))) {
uint8_t id_bin[32];
@ -137,7 +137,7 @@ static void execute(ToxWindow* self, char* cmd) {
num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1);
switch (num) {
case -1:
case -1:
wprintw(self->window, "Message is too long.\n");
break;
case -2:
@ -151,7 +151,7 @@ static void execute(ToxWindow* self, char* cmd) {
break;
case -5:
wprintw(self->window, "Undefined error when adding friend.\n");
break;
break;
default:
wprintw(self->window, "Friend added as %d.\n", num);
on_friendadded(num);
@ -174,7 +174,7 @@ static void execute(ToxWindow* self, char* cmd) {
}
msg++;
m_set_userstatus((uint8_t*) msg, strlen(msg)+1);
m_set_userstatus(USERSTATUS_KIND_RETAIN, (uint8_t*) msg, strlen(msg)+1);
wprintw(self->window, "Status set to: %s\n", msg);
}
else if(!strncmp(cmd, "nick ", strlen("nick "))) {