Skip to content

Commit 74a9486

Browse files
k4bek4beprogval
authored andcommitted
fixes JOIN tag breakage and memory leak
1 parent 22ff1dd commit 74a9486

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

files/metadata2.c

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (C) 2020-2021 k4be
1+
/* Copyright (C) 2020-2021, 2023 k4be
22
** Copyright (C) 2023 Valentin Lorentz
33
** License: GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
44
*/
@@ -164,8 +164,8 @@ struct metadata_moddata_user {
164164
struct metadata_unsynced *us;
165165
};
166166

167-
struct metadata_unsynced { /* we're listing users (nicknames) that should be synced but were not */
168-
char *name;
167+
struct metadata_unsynced { /* we're listing users (UIDs) that should be synced but were not */
168+
char *id;
169169
char *key;
170170
struct metadata_unsynced *next;
171171
};
@@ -548,11 +548,24 @@ int metadata_notify_or_queue(Client *client, const char *who, const char *key, c
548548
metadata_send_change(client, who, key, value, changer);
549549
} else
550550
{ /* store for the SYNC */
551+
Client *who_client;
552+
const char *uid_or_channel;
553+
if (*who == '#') {
554+
uid_or_channel = who;
555+
} else {
556+
who_client = find_client(who, NULL); /* FIXME the caller should already have this figured out */
557+
if (!who_client) {
558+
unreal_log(ULOG_DEBUG, "metadata", "METADATA_DEBUG", changer, "metadata_notify_or_queue called with nonexistent client!");
559+
return 0; /* shouldn't happen */
560+
}
561+
uid_or_channel = who_client->id;
562+
}
563+
551564
trylater = 1;
552565
while (*us)
553566
us = &(*us)->next; /* find last list element */
554567
*us = safe_alloc(sizeof(struct metadata_unsynced));
555-
(*us)->name = strdup(who);
568+
(*us)->id = strdup(uid_or_channel);
556569
(*us)->key = strdup(key);
557570
(*us)->next = NULL;
558571
}
@@ -690,7 +703,7 @@ void metadata_user_free(ModData *md)
690703
metadata_free_list(metadata, NULL, NULL);
691704
while (us)
692705
{
693-
safe_free(us->name);
706+
safe_free(us->id);
694707
safe_free(us->key);
695708
prev_us = us;
696709
us = us->next;
@@ -1375,23 +1388,41 @@ int metadata_join(Client *client, Channel *channel, MessageTag *mtags)
13751388

13761389
void metadata_sync(Client *client)
13771390
{
1378-
Client *acptr;
1391+
Client *acptr = NULL;
13791392
Channel *channel = NULL;
1393+
int do_send = 0;
1394+
char *who;
13801395

13811396
struct metadata_moddata_user *my_moddata = USER_METADATA(client);
13821397
if(!my_moddata)
13831398
return; /* nothing queued */
13841399
struct metadata_unsynced *us = my_moddata->us;
13851400
struct metadata_unsynced *prev_us;
1386-
1401+
13871402
while (us)
13881403
{
13891404
if (!IsSendable(client))
13901405
break;
1391-
acptr = hash_find_nickatserver(us->name, NULL);
1392-
if (acptr && has_common_channels(acptr, client))
1393-
{ /* if not, the user has vanished since or one of us parted the channel */
1394-
struct metadata_moddata_user *moddata = USER_METADATA(acptr);
1406+
if (*us->id == '#') {
1407+
channel = find_channel(us->id);
1408+
if (channel && IsMember(client, channel)) {
1409+
do_send = 1;
1410+
who = us->id;
1411+
}
1412+
} else {
1413+
acptr = find_client(us->id, NULL);
1414+
if (acptr && has_common_channels(acptr, client)) { /* if not, the user has vanished since or one of us parted the channel */
1415+
do_send = 1;
1416+
who = acptr->name;
1417+
}
1418+
}
1419+
1420+
if (do_send) {
1421+
struct metadata_moddata_user *moddata;
1422+
if (acptr)
1423+
moddata = USER_METADATA(acptr);
1424+
else
1425+
moddata = CHANNEL_METADATA(channel);
13951426
if (moddata)
13961427
{
13971428
struct metadata *metadata = moddata->metadata;
@@ -1401,7 +1432,7 @@ void metadata_sync(Client *client)
14011432
{ /* has it */
14021433
const char *value = metadata_get_user_key_value(acptr, us->key);
14031434
if(value)
1404-
metadata_send_change(client, us->name, us->key, value, NULL);
1435+
metadata_send_change(client, us->id, us->key, value, NULL);
14051436
}
14061437
metadata = metadata->next;
14071438
}
@@ -1410,7 +1441,7 @@ void metadata_sync(Client *client)
14101441
/* now remove the processed entry */
14111442
prev_us = us;
14121443
us = us->next;
1413-
safe_free(prev_us->name);
1444+
safe_free(prev_us->id);
14141445
safe_free(prev_us);
14151446
my_moddata->us = us; /* we're always removing the first list item */
14161447
}

0 commit comments

Comments
 (0)