summaryrefslogtreecommitdiff
path: root/core/Messenger.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/Messenger.c')
-rw-r--r--core/Messenger.c31
1 files changed, 24 insertions, 7 deletions
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}