diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-07-08 15:30:19 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-07-08 22:02:52 +0000 |
commit | 76e1d05da631e8eec8b370fb4f2d775ed8e2aaa7 (patch) | |
tree | 24c79c1010d7c24dec908ee9623616291dd5fef3 /toxcore/friend_requests.c | |
parent | c0c309873775824e2cb4d029e469c8dd12903976 (diff) |
Use named function types for friend_requests callbacks.
Also:
* `#define` must be scoped. If it's used outside a scope, it must be
defined outside that scope (global `#define` does not need a matching
`#undef`).
Diffstat (limited to 'toxcore/friend_requests.c')
-rw-r--r-- | toxcore/friend_requests.c | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index 3123e4ff..c4f25127 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c | |||
@@ -32,22 +32,26 @@ | |||
32 | 32 | ||
33 | #include "util.h" | 33 | #include "util.h" |
34 | 34 | ||
35 | /* NOTE: The following is just a temporary fix for the multiple friend requests received at the same time problem. | ||
36 | * TODO(irungentoo): Make this better (This will most likely tie in with the way we will handle spam.) | ||
37 | */ | ||
38 | #define MAX_RECEIVED_STORED 32 | ||
39 | |||
40 | struct Received_Requests { | ||
41 | uint8_t requests[MAX_RECEIVED_STORED][CRYPTO_PUBLIC_KEY_SIZE]; | ||
42 | uint16_t requests_index; | ||
43 | }; | ||
44 | |||
35 | struct Friend_Requests { | 45 | struct Friend_Requests { |
36 | uint32_t nospam; | 46 | uint32_t nospam; |
37 | void (*handle_friendrequest)(void *, const uint8_t *, const uint8_t *, size_t, void *); | 47 | fr_friend_request_cb *handle_friendrequest; |
38 | uint8_t handle_friendrequest_isset; | 48 | uint8_t handle_friendrequest_isset; |
39 | void *handle_friendrequest_object; | 49 | void *handle_friendrequest_object; |
40 | 50 | ||
41 | int (*filter_function)(const uint8_t *, void *); | 51 | filter_function_cb *filter_function; |
42 | void *filter_function_userdata; | 52 | void *filter_function_userdata; |
43 | /* NOTE: The following is just a temporary fix for the multiple friend requests received at the same time problem. | ||
44 | * TODO(irungentoo): Make this better (This will most likely tie in with the way we will handle spam.) | ||
45 | */ | ||
46 | |||
47 | #define MAX_RECEIVED_STORED 32 | ||
48 | 53 | ||
49 | uint8_t received_requests[MAX_RECEIVED_STORED][CRYPTO_PUBLIC_KEY_SIZE]; | 54 | struct Received_Requests received; |
50 | uint16_t received_requests_index; | ||
51 | }; | 55 | }; |
52 | 56 | ||
53 | /* Set and get the nospam variable used to prevent one type of friend request spam. */ | 57 | /* Set and get the nospam variable used to prevent one type of friend request spam. */ |
@@ -63,8 +67,7 @@ uint32_t get_nospam(const Friend_Requests *fr) | |||
63 | 67 | ||
64 | 68 | ||
65 | /* Set the function that will be executed when a friend request is received. */ | 69 | /* Set the function that will be executed when a friend request is received. */ |
66 | void callback_friendrequest(Friend_Requests *fr, void (*function)(void *, const uint8_t *, const uint8_t *, size_t, | 70 | void callback_friendrequest(Friend_Requests *fr, fr_friend_request_cb *function, void *object) |
67 | void *), void *object) | ||
68 | { | 71 | { |
69 | fr->handle_friendrequest = function; | 72 | fr->handle_friendrequest = function; |
70 | fr->handle_friendrequest_isset = 1; | 73 | fr->handle_friendrequest_isset = 1; |
@@ -72,7 +75,7 @@ void callback_friendrequest(Friend_Requests *fr, void (*function)(void *, const | |||
72 | } | 75 | } |
73 | 76 | ||
74 | /* Set the function used to check if a friend request should be displayed to the user or not. */ | 77 | /* Set the function used to check if a friend request should be displayed to the user or not. */ |
75 | void set_filter_function(Friend_Requests *fr, int (*function)(const uint8_t *, void *), void *userdata) | 78 | void set_filter_function(Friend_Requests *fr, filter_function_cb *function, void *userdata) |
76 | { | 79 | { |
77 | fr->filter_function = function; | 80 | fr->filter_function = function; |
78 | fr->filter_function_userdata = userdata; | 81 | fr->filter_function_userdata = userdata; |
@@ -81,12 +84,12 @@ void set_filter_function(Friend_Requests *fr, int (*function)(const uint8_t *, v | |||
81 | /* Add to list of received friend requests. */ | 84 | /* Add to list of received friend requests. */ |
82 | static void addto_receivedlist(Friend_Requests *fr, const uint8_t *real_pk) | 85 | static void addto_receivedlist(Friend_Requests *fr, const uint8_t *real_pk) |
83 | { | 86 | { |
84 | if (fr->received_requests_index >= MAX_RECEIVED_STORED) { | 87 | if (fr->received.requests_index >= MAX_RECEIVED_STORED) { |
85 | fr->received_requests_index = 0; | 88 | fr->received.requests_index = 0; |
86 | } | 89 | } |
87 | 90 | ||
88 | id_copy(fr->received_requests[fr->received_requests_index], real_pk); | 91 | id_copy(fr->received.requests[fr->received.requests_index], real_pk); |
89 | ++fr->received_requests_index; | 92 | ++fr->received.requests_index; |
90 | } | 93 | } |
91 | 94 | ||
92 | /* Check if a friend request was already received. | 95 | /* Check if a friend request was already received. |
@@ -97,7 +100,7 @@ static void addto_receivedlist(Friend_Requests *fr, const uint8_t *real_pk) | |||
97 | static bool request_received(const Friend_Requests *fr, const uint8_t *real_pk) | 100 | static bool request_received(const Friend_Requests *fr, const uint8_t *real_pk) |
98 | { | 101 | { |
99 | for (uint32_t i = 0; i < MAX_RECEIVED_STORED; ++i) { | 102 | for (uint32_t i = 0; i < MAX_RECEIVED_STORED; ++i) { |
100 | if (id_equal(fr->received_requests[i], real_pk)) { | 103 | if (id_equal(fr->received.requests[i], real_pk)) { |
101 | return true; | 104 | return true; |
102 | } | 105 | } |
103 | } | 106 | } |
@@ -105,7 +108,7 @@ static bool request_received(const Friend_Requests *fr, const uint8_t *real_pk) | |||
105 | return false; | 108 | return false; |
106 | } | 109 | } |
107 | 110 | ||
108 | /* Remove real pk from received_requests list. | 111 | /* Remove real pk from received.requests list. |
109 | * | 112 | * |
110 | * return 0 if it removed it successfully. | 113 | * return 0 if it removed it successfully. |
111 | * return -1 if it didn't find it. | 114 | * return -1 if it didn't find it. |
@@ -113,8 +116,8 @@ static bool request_received(const Friend_Requests *fr, const uint8_t *real_pk) | |||
113 | int remove_request_received(Friend_Requests *fr, const uint8_t *real_pk) | 116 | int remove_request_received(Friend_Requests *fr, const uint8_t *real_pk) |
114 | { | 117 | { |
115 | for (uint32_t i = 0; i < MAX_RECEIVED_STORED; ++i) { | 118 | for (uint32_t i = 0; i < MAX_RECEIVED_STORED; ++i) { |
116 | if (id_equal(fr->received_requests[i], real_pk)) { | 119 | if (id_equal(fr->received.requests[i], real_pk)) { |
117 | crypto_memzero(fr->received_requests[i], CRYPTO_PUBLIC_KEY_SIZE); | 120 | crypto_memzero(fr->received.requests[i], CRYPTO_PUBLIC_KEY_SIZE); |
118 | return 0; | 121 | return 0; |
119 | } | 122 | } |
120 | } | 123 | } |