diff --git a/include/dcc.h b/include/dcc.h index 7ddfd1b..7f097aa 100644 --- a/include/dcc.h +++ b/include/dcc.h @@ -145,7 +145,7 @@ int BX_add_dcc_bind(char *, char *, void *, void *, void *, void *, void *); - SocketList *BX_find_dcc(char *, char *, char *, int, int, int, int); + SocketList *BX_find_dcc(const char *, const char *, const char *, int, int, int, int); void BX_erase_dcc_info(int, int, char *, ...); DCC_int *BX_dcc_create(char *, char *, char *, unsigned long, int, int, unsigned long, void (*func)(int)); int close_dcc_number(int); diff --git a/include/modval.h b/include/modval.h index fb2ae5a..8cea487 100644 --- a/include/modval.h +++ b/include/modval.h @@ -611,7 +611,7 @@ extern Function_ptr *global; /* dcc.c */ #define dcc_create (*(DCC_int *(*)(char *, char *, char *, unsigned long, int, int, unsigned long, void (*)(int)))global[DCC_CREATE_FUNC]) -#define find_dcc (*(SocketList *(*)(char *, char *, char *, int, int, int, int))global[FIND_DCC_FUNC]) +#define find_dcc (*(SocketList *(*)(const char *, const char *, const char *, int, int, int, int))global[FIND_DCC_FUNC]) #define erase_dcc_info (*(void (*)(int, int, char *, ...))global[ERASE_DCC_INFO]) #define add_dcc_bind (*(int (*)(char *, char *, void *, void *, void *, void *, void *))global[ADD_DCC_BIND]) #define remove_dcc_bind (*(int (*)(char *, int ))global[REMOVE_DCC_BIND]) diff --git a/source/dcc.c b/source/dcc.c index 7e3f3ec..8d0a665 100644 --- a/source/dcc.c +++ b/source/dcc.c @@ -362,51 +362,55 @@ static time_t last_reject = 0; /* dcc_renumber_active();*/ } +/* dcc_match() + * + * Returns non-zero if the passed DCC socket matches the supplied parameters. + * NULL for string parameters and -1 for int parameters means Don't-Care. + */ +static int dcc_match(const SocketList *s, const char *nick, const char *desc, const char *other, int type, int active, int num) +{ + const DCC_int *n = s->info; + + if (type != -1 && type != (s->flags & DCC_TYPES)) + return 0; + if (num != -1 && num != n->dccnum) + return 0; + if (nick && my_stricmp(nick, s->server)) + return 0; + if (desc && n->filename && my_stricmp(desc, n->filename)) + { + const char *last = strrchr(n->filename, '/'); + if (!last || my_stricmp(desc, last + 1)) + return 0; + } + if (other && n->othername && my_stricmp(other, n->othername)) + return 0; + if (active == 0 && (s->flags & DCC_ACTIVE)) + return 0; + if (active == 1 && !(s->flags & DCC_ACTIVE)) + return 0; + + return 1; +} + /* * finds an active dcc connection. one that either is already started * or one that has been initiated on our side. */ -SocketList *BX_find_dcc(char *nick, char *desc, char *other, int type, int create, int active, int num) +SocketList *BX_find_dcc(const char *nick, const char *desc, const char *other, int type, int create, int active, int num) { -int i; -SocketList *s; -DCC_int *n; -char *othername = NULL; + int i; + SocketList *s; for (i = 0; i < get_max_fd()+1; i++) { if (!check_dcc_socket(i)) continue; s = get_socket(i); - n = (DCC_int *) s->info; - if ( (type != -1) && !((s->flags & DCC_TYPES) == type) ) - continue; - if ((num != -1) && !(n->dccnum == num)) - continue; - if (nick && my_stricmp(nick, s->server)) - continue; - if (desc && n->filename && my_stricmp(desc, n->filename)) - { - char *last; - if (!(last = strrchr(n->filename, '/'))) - continue; - last++; - if (last && my_stricmp(desc, last)) - { - if (!othername || !n->othername) - continue; - if (my_stricmp(othername, n->othername)) - continue; - } - } - if (other && n->othername && my_stricmp(other, n->othername)) - continue; - if (active == 0 && (s->flags & DCC_ACTIVE)) - continue; - if (active == 1 && !(s->flags & DCC_ACTIVE)) - continue; - return s; + + if (dcc_match(s, nick, desc, other, type, active, num)) + return s; } return NULL; } @@ -414,44 +418,28 @@ char *othername = NULL; /* * finds a pending dcc which the other end has initiated. */ -DCC_List *find_dcc_pending(char *nick, char *desc, char *othername, int type, int remove, int num) +static DCC_List *find_dcc_pending(const char *nick, const char *desc, const char *othername, int type, int remove, int num) { - unsigned long dcc_type; SocketList *s; - DCC_int *n; DCC_List *new_i; DCC_List *last_i = NULL; for (new_i = pending_dcc; new_i; last_i = new_i, new_i = new_i->next) { s = &new_i->sock; - n = (DCC_int *)s->info; - dcc_type = s->flags & DCC_TYPES; - if ((type != -1) && !(dcc_type == type)) - continue; - if ((num != -1) && !(n->dccnum == num)) - continue; - if (nick && my_stricmp(nick, new_i->sock.server)) - continue; - if ((desc && my_stricmp(desc, n->filename))) + + if (dcc_match(s, nick, desc, othername, type, -1, num)) { - char *last; - if (!(last = strrchr(n->filename, '/'))) - continue; - last++; - if (last && my_stricmp(desc, last)) - continue; + if (remove) + { + if (last_i) + last_i->next = new_i->next; + else + pending_dcc = new_i->next; + } + + return new_i; } - if (othername && n->othername && my_stricmp(othername, n->othername)) - continue; - if (remove) - { - if (last_i) - last_i->next = new_i->next; - else - pending_dcc = new_i->next; - } - return new_i; } return NULL; }