summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/DHT.h2
-rw-r--r--core/Messenger.c31
-rw-r--r--core/Messenger.h4
-rw-r--r--core/network.h5
4 files changed, 33 insertions, 9 deletions
diff --git a/core/DHT.h b/core/DHT.h
index 2308abd8..cb5697ea 100644
--- a/core/DHT.h
+++ b/core/DHT.h
@@ -30,8 +30,6 @@
30extern "C" { 30extern "C" {
31#endif 31#endif
32 32
33/* Current time, unix format */
34#define unix_time() ((uint64_t)time(NULL))
35 33
36/* size of the client_id in bytes */ 34/* size of the client_id in bytes */
37#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES 35#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES
diff --git a/core/Messenger.c b/core/Messenger.c
index 44fe5f94..90dfe48d 100644
--- a/core/Messenger.c
+++ b/core/Messenger.c
@@ -51,18 +51,27 @@ static uint8_t *self_statusmessage;
51static uint16_t self_statusmessage_len; 51static uint16_t self_statusmessage_len;
52static USERSTATUS self_userstatus; 52static USERSTATUS self_userstatus;
53 53
54#define MAX_NUM_FRIENDS 256 54static Friend *friendlist;
55
56static Friend friendlist[MAX_NUM_FRIENDS];
57
58static uint32_t numfriends; 55static uint32_t numfriends;
59 56
57
60static void set_friend_status(int friendnumber, uint8_t status); 58static void set_friend_status(int friendnumber, uint8_t status);
61 59
62/* 1 if we are online 60/* 1 if we are online
63 0 if we are offline 61 0 if we are offline
64 static uint8_t online; */ 62 static uint8_t online; */
65 63
64/* set the size of the friend list to numfriends
65 return -1 if realloc fails */
66int realloc_friendlist(uint32_t num) {
67 Friend *newfriendlist = realloc(friendlist, num*sizeof(Friend));
68 if (newfriendlist == NULL)
69 return -1;
70 memset(&newfriendlist[num-1], 0, sizeof(Friend));
71 friendlist = newfriendlist;
72 return 0;
73}
74
66/* return the friend id associated to that public key. 75/* return the friend id associated to that public key.
67 return -1 if no such friend */ 76 return -1 if no such friend */
68int getfriend_id(uint8_t *client_id) 77int getfriend_id(uint8_t *client_id)
@@ -120,8 +129,11 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
120 if (getfriend_id(client_id) != -1) 129 if (getfriend_id(client_id) != -1)
121 return FAERR_ALREADYSENT; 130 return FAERR_ALREADYSENT;
122 131
132 /* resize the friend list if necessary */
133 realloc_friendlist(numfriends + 1);
134
123 uint32_t i; 135 uint32_t i;
124 for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ 136 for (i = 0; i <= numfriends; ++i) {
125 if (friendlist[i].status == NOFRIEND) { 137 if (friendlist[i].status == NOFRIEND) {
126 DHT_addfriend(client_id); 138 DHT_addfriend(client_id);
127 set_friend_status(i, FRIEND_ADDED); 139 set_friend_status(i, FRIEND_ADDED);
@@ -147,8 +159,12 @@ int m_addfriend_norequest(uint8_t * client_id)
147{ 159{
148 if (getfriend_id(client_id) != -1) 160 if (getfriend_id(client_id) != -1)
149 return -1; 161 return -1;
162
163 /* resize the friend list if necessary */
164 realloc_friendlist(numfriends + 1);
165
150 uint32_t i; 166 uint32_t i;
151 for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ 167 for (i = 0; i <= numfriends; ++i) {
152 if(friendlist[i].status == NOFRIEND) { 168 if(friendlist[i].status == NOFRIEND) {
153 DHT_addfriend(client_id); 169 DHT_addfriend(client_id);
154 set_friend_status(i, FRIEND_REQUESTED); 170 set_friend_status(i, FRIEND_REQUESTED);
@@ -160,7 +176,7 @@ int m_addfriend_norequest(uint8_t * client_id)
160 friendlist[i].userstatus = USERSTATUS_NONE; 176 friendlist[i].userstatus = USERSTATUS_NONE;
161 friendlist[i].message_id = 0; 177 friendlist[i].message_id = 0;
162 friendlist[i].receives_read_receipts = 1; /* default: YES */ 178 friendlist[i].receives_read_receipts = 1; /* default: YES */
163 numfriends++; 179 ++numfriends;
164 return i; 180 return i;
165 } 181 }
166 } 182 }
@@ -186,6 +202,7 @@ int m_delfriend(int friendnumber)
186 break; 202 break;
187 } 203 }
188 numfriends = i; 204 numfriends = i;
205 realloc_friendlist(numfriends + 1);
189 206
190 return 0; 207 return 0;
191} 208}
diff --git a/core/Messenger.h b/core/Messenger.h
index 569fcab1..3044122b 100644
--- a/core/Messenger.h
+++ b/core/Messenger.h
@@ -188,6 +188,10 @@ void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t));
188 you are not responsible for freeing newstatus */ 188 you are not responsible for freeing newstatus */
189void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t)); 189void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t));
190 190
191/* set the callback for status type changes
192 function(int friendnumber, USERSTATUS kind) */
193void m_callback_userstatus(void (*function)(int, USERSTATUS));
194
191/* set the callback for read receipts 195/* set the callback for read receipts
192 function(int friendnumber, uint32_t receipt) 196 function(int friendnumber, uint32_t receipt)
193 if you are keeping a record of returns from m_sendmessage, 197 if you are keeping a record of returns from m_sendmessage,
diff --git a/core/network.h b/core/network.h
index d246a9d1..d3c39333 100644
--- a/core/network.h
+++ b/core/network.h
@@ -66,6 +66,11 @@ extern "C" {
66 66
67#define MAX_UDP_PACKET_SIZE 65507 67#define MAX_UDP_PACKET_SIZE 65507
68 68
69
70/* Current time, unix format */
71#define unix_time() ((uint64_t)time(NULL))
72
73
69typedef union { 74typedef union {
70 uint8_t c[4]; 75 uint8_t c[4];
71 uint16_t s[2]; 76 uint16_t s[2];