Refactor common channel iteration

This commit is contained in:
Ed Kellett 2020-10-18 13:20:15 +01:00
parent 5e413b1372
commit e8a8d7a440
6 changed files with 72 additions and 65 deletions

View file

@ -27,5 +27,5 @@ static void
h_huc_doing_whois_channel_visibility(void *data_) h_huc_doing_whois_channel_visibility(void *data_)
{ {
hook_data_channel_visibility *data = data_; hook_data_channel_visibility *data = data_;
data->approved = data->approved && (!IsInvisible(data->targms->client_p) || data->clientms != NULL); data->approved = data->approved && (!IsInvisible(data->target) || data->clientms != NULL);
} }

View file

@ -200,6 +200,15 @@ typedef int (*ExtbanFunc)(const char *data, struct Client *client_p,
#define EXTBAN_NOMATCH 0 /* valid mask, no match */ #define EXTBAN_NOMATCH 0 /* valid mask, no match */
#define EXTBAN_MATCH 1 /* matches */ #define EXTBAN_MATCH 1 /* matches */
int iter_comm_channels_step(rb_dlink_node *pos1, rb_dlink_node *pos2,
struct membership **ms1, struct membership **ms2,
struct Channel **chptr);
#define ITER_COMM_CHANNELS(pos1, pos2, head1, head2, ms1, ms2, chptr) for ((pos1) = (head1), (pos2) = (head2); \
iter_comm_channels_step((pos1), (pos2), &(ms1), &(ms2), &(chptr)); \
(ms1) && ((pos1) = (pos1)->next), (ms2) && ((pos2) = (pos2)->next))
extern rb_dlink_list global_channel_list; extern rb_dlink_list global_channel_list;
void init_channels(void); void init_channels(void);

View file

@ -110,9 +110,10 @@ typedef struct
typedef struct typedef struct
{ {
struct Client *client; struct Client *client;
struct Client *target;
struct Channel *chptr; struct Channel *chptr;
struct membership *clientms; struct membership *clientms;
struct membership *targms; struct membership *targetms;
int approved; int approved;
} hook_data_channel_visibility; } hook_data_channel_visibility;

View file

@ -434,6 +434,47 @@ channel_pub_or_secret(struct Channel *chptr)
return ("*"); return ("*");
} }
int
iter_comm_channels_step(rb_dlink_node *pos1, rb_dlink_node *pos2,
struct membership **ms1, struct membership **ms2,
struct Channel **chptr)
{
*ms1 = pos1 ? pos1->data : NULL;
*ms2 = pos2 ? pos2->data : NULL;
/* we're at the end */
if (*ms1 == NULL && *ms2 == NULL)
return 0;
/* one side is at the end, keep stepping the other one */
if (*ms1 == NULL || *ms2 == NULL)
{
*chptr = *ms1 != NULL ? (*ms1)->chptr : (*ms2)->chptr;
return 1;
}
/* common channel */
if ((*ms1)->chptr == (*ms2)->chptr)
{
*chptr = (*ms1)->chptr;
return 1;
}
/* null out the channel that's further ahead; we'll get to it later */
if (irccmp((*ms1)->chptr->chname, (*ms2)->chptr->chname) > 0)
{
*ms1 = NULL;
*chptr = (*ms2)->chptr;
return 1;
}
else
{
*ms2 = NULL;
*chptr = (*ms1)->chptr;
return 1;
}
}
/* channel_member_names() /* channel_member_names()
* *
* input - channel to list, client to list to, show endofnames * input - channel to list, client to list to, show endofnames

View file

@ -38,29 +38,14 @@ static int add_hashed_target(struct Client *source_p, uint32_t hashv);
struct Channel * struct Channel *
find_allowing_channel(struct Client *source_p, struct Client *target_p) find_allowing_channel(struct Client *source_p, struct Client *target_p)
{ {
rb_dlink_node *ps = source_p->user->channel.head; rb_dlink_node *ps, *pt;
rb_dlink_node *pt = target_p->user->channel.head; struct membership *ms, *mt;
struct Channel *chptr;
while (ps && pt) ITER_COMM_CHANNELS(ps, pt, source_p->user->channel.head, target_p->user->channel.head, ms, mt, chptr)
{ {
struct membership *ms = ps->data; if (ms != NULL && mt != NULL && is_chanop_voiced(ms))
struct membership *mt = pt->data; return chptr;
int d;
if (ms->chptr == mt->chptr)
{
if (is_chanop_voiced(ms))
return ms->chptr;
ps = ps->next;
pt = pt->next;
continue;
}
d = irccmp(ms->chptr->chname, mt->chptr->chname);
if (d < 0)
ps = ps->next;
else if (d > 0)
pt = pt->next;
else
assert("different channels can't have equal names" && false);
} }
return NULL; return NULL;
} }

View file

@ -224,7 +224,6 @@ static void
single_whois(struct Client *source_p, struct Client *target_p, int operspy) single_whois(struct Client *source_p, struct Client *target_p, int operspy)
{ {
char buf[BUFSIZE]; char buf[BUFSIZE];
rb_dlink_node *ptr;
int cur_len = 0; int cur_len = 0;
int mlen; int mlen;
char *t; char *t;
@ -268,56 +267,28 @@ single_whois(struct Client *source_p, struct Client *target_p, int operspy)
if (!IsService(target_p)) if (!IsService(target_p))
{ {
hook_data_channel_visibility hdata_vis; hook_data_channel_visibility hdata_vis;
rb_dlink_node *ps = source_p->user->channel.head; rb_dlink_node *ps, *pt;
rb_dlink_node *pt = target_p->user->channel.head; struct Channel *chptr;
struct membership *ms, *mt;
hdata_vis.client = source_p; hdata_vis.client = source_p;
hdata_vis.target = target_p;
while (pt) ITER_COMM_CHANNELS(ps, pt, source_p->user->channel.head, target_p->user->channel.head, ms, mt, chptr)
{ {
struct membership *mt = pt->data; if (mt == NULL)
int dir = 0;
if (ps != NULL)
{
struct membership *ms = ps->data;
if (ms->chptr == mt->chptr)
{
ps = ps->next;
pt = pt->next;
hdata_vis.chptr = mt->chptr;
hdata_vis.clientms = ms;
hdata_vis.targms = mt;
hdata_vis.approved = 1;
dir = 0;
}
else
{
dir = irccmp(ms->chptr->chname, mt->chptr->chname);
}
}
else
{
dir = 1;
}
if (dir < 0)
{
ps = ps->next;
continue; continue;
}
else if (dir > 0) hdata_vis.chptr = chptr;
{ hdata_vis.clientms = ms;
pt = pt->next; hdata_vis.targetms = mt;
hdata_vis.chptr = mt->chptr; hdata_vis.approved = ms != NULL || PubChannel(chptr);
hdata_vis.clientms = NULL;
hdata_vis.targms = mt;
hdata_vis.approved = PubChannel(mt->chptr);
}
call_hook(doing_whois_channel_visibility_hook, &hdata_vis); call_hook(doing_whois_channel_visibility_hook, &hdata_vis);
if(hdata_vis.approved || operspy) if(hdata_vis.approved || operspy)
{ {
if((cur_len + strlen(mt->chptr->chname) + 3) > (BUFSIZE - 5)) if((cur_len + strlen(chptr->chname) + strlen("!@+ ")) > (BUFSIZE - strlen("\r\n")))
{ {
sendto_one(source_p, "%s", buf); sendto_one(source_p, "%s", buf);
cur_len = mlen + extra_space; cur_len = mlen + extra_space;
@ -327,7 +298,7 @@ single_whois(struct Client *source_p, struct Client *target_p, int operspy)
tlen = sprintf(t, "%s%s%s ", tlen = sprintf(t, "%s%s%s ",
hdata_vis.approved ? "" : "!", hdata_vis.approved ? "" : "!",
find_channel_status(mt, 1), find_channel_status(mt, 1),
mt->chptr->chname); chptr->chname);
t += tlen; t += tlen;
cur_len += tlen; cur_len += tlen;
} }