Friend requests from friends are now discarded.

This commit is contained in:
irungentoo 2013-11-27 14:18:39 -05:00
parent 3fe7e08791
commit 74b13a9fcf
3 changed files with 32 additions and 1 deletions

View File

@ -1454,6 +1454,17 @@ int m_msi_packet(Messenger *m, int friendnumber, uint8_t *data, uint16_t length)
return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length);
}
/* Function to filter out some friend requests*/
static int friend_already_added(uint8_t * client_id, void * data)
{
Messenger *m = data;
if (getfriend_id(m, client_id) == -1)
return 0;
return -1;
}
/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */
static void LANdiscovery(Messenger *m)
{
@ -1503,6 +1514,8 @@ Messenger *new_messenger(uint8_t ipv6enabled)
friendreq_init(&(m->fr), m->net_crypto);
LANdiscovery_init(m->dht);
set_nospam(&(m->fr), random_int());
set_filter_function(&(m->fr), &friend_already_added, m);
networking_registerhandler(m->net, NET_PACKET_GROUP_CHATS, &handle_group, m);
return m;

View File

@ -96,6 +96,12 @@ void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uin
fr->handle_friendrequest_isset = 1;
fr->handle_friendrequest_userdata = userdata;
}
/* Set the function used to check if a friend request should be displayed to the user or not. */
void set_filter_function(Friend_Requests *fr, int (*function)(uint8_t *, void *), void *userdata)
{
fr->filter_function = function;
fr->filter_function_userdata = userdata;
}
/* Add to list of received friend requests. */
static void addto_receivedlist(Friend_Requests *fr, uint8_t *client_id)
@ -141,6 +147,10 @@ static int friendreq_handlepacket(void *object, IP_Port source, uint8_t *source_
if (memcmp(packet, &fr->nospam, sizeof(fr->nospam)) != 0)
return 1;
if (fr->filter_function)
if ((*fr->filter_function)(source_pubkey, fr->filter_function_userdata) != 0)
return 1;
addto_receivedlist(fr, source_pubkey);
(*fr->handle_friendrequest)(source_pubkey, packet + 4, length - 4, fr->handle_friendrequest_userdata);
return 0;

View File

@ -34,6 +34,8 @@ typedef struct {
uint8_t handle_friendrequest_isset;
void *handle_friendrequest_userdata;
int (*filter_function)(uint8_t *, void *);
void *filter_function_userdata;
/* NOTE: The following is just a temporary fix for the multiple friend requests received at the same time problem.
* TODO: Make this better (This will most likely tie in with the way we will handle spam.)
*/
@ -53,11 +55,17 @@ void set_nospam(Friend_Requests *fr, uint32_t num);
uint32_t get_nospam(Friend_Requests *fr);
/* Set the function that will be executed when a friend request for us is received.
* Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length)
* Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length, void * userdata)
*/
void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *),
void *userdata);
/* Set the function used to check if a friend request should be displayed to the user or not.
* Function format is int function(uint8_t * public_key, void * userdata)
* It must return 0 if the request is ok (anything else if it is bad.)
*/
void set_filter_function(Friend_Requests *fr, int (*function)(uint8_t *, void *), void *userdata);
/* Sets up friendreq packet handlers. */
void friendreq_init(Friend_Requests *fr, Net_Crypto *c);