summaryrefslogtreecommitdiff
path: root/core/Messenger.c
diff options
context:
space:
mode:
authorMaxim Biro <nurupo.contributions@gmail.com>2013-08-09 15:58:08 -0400
committerMaxim Biro <nurupo.contributions@gmail.com>2013-08-09 16:17:56 -0400
commit19bbed475dd044163f7bbeaf533abaa850380633 (patch)
treed06e6fe097447833133cba40874b0df22717d7cb /core/Messenger.c
parent44902d15a720a0fb10550f03d00324db82b9531c (diff)
Extracted repeated code into a function
Diffstat (limited to 'core/Messenger.c')
-rw-r--r--core/Messenger.c46
1 files changed, 17 insertions, 29 deletions
diff --git a/core/Messenger.c b/core/Messenger.c
index 00f54cfa..5589e752 100644
--- a/core/Messenger.c
+++ b/core/Messenger.c
@@ -56,6 +56,7 @@ static uint32_t numfriends;
56 56
57 57
58static void set_friend_status(int friendnumber, uint8_t status); 58static void set_friend_status(int friendnumber, uint8_t status);
59static int write_cryptpacket_id(int crypt_connection_id, uint8_t packet_id, uint8_t *data, uint32_t length);
59 60
60/* 1 if we are online 61/* 1 if we are online
61 0 if we are offline 62 0 if we are offline
@@ -240,11 +241,10 @@ uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message
240 /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ 241 /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */
241 return 0; 242 return 0;
242 uint8_t temp[MAX_DATA_SIZE]; 243 uint8_t temp[MAX_DATA_SIZE];
243 temp[0] = PACKET_ID_MESSAGE;
244 theid = htonl(theid); 244 theid = htonl(theid);
245 memcpy(temp + 1, &theid, sizeof(theid)); 245 memcpy(temp, &theid, sizeof(theid));
246 memcpy(temp + 1 + sizeof(theid), message, length); 246 memcpy(temp + sizeof(theid), message, length);
247 return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1 + sizeof(theid)); 247 return write_cryptpacket_id(friendlist[friendnumber].crypt_connection_id, PACKET_ID_MESSAGE, temp, length + sizeof(theid));
248} 248}
249 249
250/* send an action to an online friend 250/* send an action to an online friend
@@ -256,10 +256,7 @@ int m_sendaction(int friendnumber, uint8_t *action, uint32_t length)
256 return 0; 256 return 0;
257 if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != FRIEND_ONLINE) 257 if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != FRIEND_ONLINE)
258 return 0; 258 return 0;
259 uint8_t temp[MAX_DATA_SIZE]; 259 return write_cryptpacket_id(friendlist[friendnumber].crypt_connection_id, PACKET_ID_ACTION, action, length);
260 temp[0] = PACKET_ID_ACTION;
261 memcpy(temp + 1, action, length);
262 return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1);
263} 260}
264 261
265/* send a name packet to friendnumber 262/* send a name packet to friendnumber
@@ -268,10 +265,7 @@ static int m_sendname(int friendnumber, uint8_t * name, uint16_t length)
268{ 265{
269 if(length > MAX_NAME_LENGTH || length == 0) 266 if(length > MAX_NAME_LENGTH || length == 0)
270 return 0; 267 return 0;
271 uint8_t temp[MAX_NAME_LENGTH + 1]; 268 return write_cryptpacket_id(friendlist[friendnumber].crypt_connection_id, PACKET_ID_NICKNAME, name, length);
272 memcpy(temp + 1, name, length);
273 temp[0] = PACKET_ID_NICKNAME;
274 return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1);
275} 269}
276 270
277/* set the name of a friend 271/* set the name of a friend
@@ -399,22 +393,12 @@ USERSTATUS m_get_self_userstatus(void)
399 393
400static int send_statusmessage(int friendnumber, uint8_t * status, uint16_t length) 394static int send_statusmessage(int friendnumber, uint8_t * status, uint16_t length)
401{ 395{
402 uint8_t *thepacket = malloc(length + 1); 396 return write_cryptpacket_id(friendlist[friendnumber].crypt_connection_id, PACKET_ID_STATUSMESSAGE, status, length);
403 memcpy(thepacket + 1, status, length);
404 thepacket[0] = PACKET_ID_STATUSMESSAGE;
405 int written = write_cryptpacket(friendlist[friendnumber].crypt_connection_id, thepacket, length + 1);
406 free(thepacket);
407 return written;
408} 397}
409 398
410static int send_userstatus(int friendnumber, USERSTATUS status) 399static int send_userstatus(int friendnumber, USERSTATUS status)
411{ 400{
412 uint8_t *thepacket = malloc(1 + 1); 401 return write_cryptpacket_id(friendlist[friendnumber].crypt_connection_id, PACKET_ID_USERSTATUS, (uint8_t*)&status, sizeof(USERSTATUS));
413 memcpy(thepacket + 1, &status, 1);
414 thepacket[0] = PACKET_ID_USERSTATUS;
415 int written = write_cryptpacket(friendlist[friendnumber].crypt_connection_id, thepacket, 1 + 1);
416 free(thepacket);
417 return written;
418} 402}
419 403
420static int set_friend_statusmessage(int friendnumber, uint8_t * status, uint16_t length) 404static int set_friend_statusmessage(int friendnumber, uint8_t * status, uint16_t length)
@@ -517,6 +501,14 @@ static void set_friend_status(int friendnumber, uint8_t status)
517 friendlist[friendnumber].status = status; 501 friendlist[friendnumber].status = status;
518} 502}
519 503
504static int write_cryptpacket_id(int crypt_connection_id, uint8_t packet_id, uint8_t *data, uint32_t length)
505{
506 uint8_t packet[length + 1];
507 packet[0] = packet_id;
508 memcpy(packet + 1, data, length);
509 return write_cryptpacket(crypt_connection_id, packet, length + 1);
510}
511
520#define PORT 33445 512#define PORT 33445
521/* run this at startup */ 513/* run this at startup */
522int initMessenger(void) 514int initMessenger(void)
@@ -619,11 +611,7 @@ static void doFriends(void)
619 } 611 }
620 case PACKET_ID_MESSAGE: { 612 case PACKET_ID_MESSAGE: {
621 if (friendlist[i].receives_read_receipts) { 613 if (friendlist[i].receives_read_receipts) {
622 uint8_t *thepacket = malloc(5); 614 write_cryptpacket_id(friendlist[i].crypt_connection_id, PACKET_ID_RECEIPT, temp + 1, 4);
623 thepacket[0] = PACKET_ID_RECEIPT;
624 memcpy(thepacket + 1, temp + 1, 4);
625 write_cryptpacket(friendlist[i].crypt_connection_id, thepacket, 5);
626 free(thepacket);
627 } 615 }
628 if (friend_message_isset) 616 if (friend_message_isset)
629 (*friend_message)(i, temp + 5, len - 5); 617 (*friend_message)(i, temp + 5, len - 5);