Added SILC Thread Queue API
[crypto.git] / apps / irssi / src / core / masks.c
1 /*
2  masks.c : irssi
3
4     Copyright (C) 1999-2000 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22 #include "network.h"
23 #include "misc.h"
24
25 #include "servers.h"
26
27 /* Returns TRUE if mask contains '!' ie. address should be checked too.
28    Also checks if mask contained any wildcards. */
29 static int check_address(const char *mask, int *wildcards)
30 {
31         int ret;
32
33         *wildcards = FALSE;
34         ret = FALSE;
35         while (*mask != '\0') {
36                 if (*mask == '!') {
37                         if (*wildcards) return TRUE;
38                         ret = TRUE;
39                 }
40
41                 if (*mask == '?' || *mask == '*') {
42                         *wildcards = TRUE;
43                         if (ret) return TRUE;
44                 }
45                 mask++;
46         }
47
48         return ret;
49 }
50
51 static int check_mask(SERVER_REC *server, const char *mask,
52                       const char *str, int wildcards)
53 {
54         if (server != NULL && server->mask_match_func != NULL) {
55                 /* use server specified mask match function */
56                 return server->mask_match_func(mask, str);
57         }
58
59         return wildcards ? match_wildcards(mask, str) :
60                 g_strcasecmp(mask, str) == 0;
61 }
62
63 int mask_match(SERVER_REC *server, const char *mask,
64                const char *nick, const char *user, const char *host)
65 {
66         char *str;
67         int ret, wildcards;
68
69         g_return_val_if_fail(server == NULL || IS_SERVER(server), FALSE);
70         g_return_val_if_fail(mask != NULL && nick != NULL &&
71                              nick != NULL && host != NULL, FALSE);
72
73         str = !check_address(mask, &wildcards) ? (char *) nick :
74                 g_strdup_printf("%s!%s@%s", nick, user, host);
75         ret = check_mask(server, mask, str, wildcards);
76         if (str != nick) g_free(str);
77
78         return ret;
79 }
80
81 int mask_match_address(SERVER_REC *server, const char *mask,
82                        const char *nick, const char *address)
83 {
84         char *str;
85         int ret, wildcards;
86
87         g_return_val_if_fail(server == NULL || IS_SERVER(server), FALSE);
88         g_return_val_if_fail(mask != NULL && nick != NULL, FALSE);
89         if (address == NULL) address = "";
90
91         str = !check_address(mask, &wildcards) ? (char *) nick :
92                 g_strdup_printf("%s!%s", nick, address);
93         ret = check_mask(server, mask, str, wildcards);
94         if (str != nick) g_free(str);
95
96         return ret;
97 }
98
99 int masks_match(SERVER_REC *server, const char *masks,
100                 const char *nick, const char *address)
101 {
102         int (*mask_match_func)(const char *, const char *);
103         char **list, **tmp, *mask;
104         int found;
105
106         g_return_val_if_fail(server == NULL || IS_SERVER(server), FALSE);
107         g_return_val_if_fail(masks != NULL &&
108                              nick != NULL && address != NULL, FALSE);
109
110         if (*masks == '\0')
111                 return FALSE;
112
113         mask_match_func = server != NULL && server->mask_match_func != NULL ?
114                 server->mask_match_func : match_wildcards;
115
116         found = FALSE;
117         mask = g_strdup_printf("%s!%s", nick, address);
118         list = g_strsplit(masks, " ", -1);
119         for (tmp = list; *tmp != NULL; tmp++) {
120                 if (g_strcasecmp(*tmp, nick) == 0) {
121                         found = TRUE;
122                         break;
123                 }
124
125                 if (mask_match_func(*tmp, mask)) {
126                         found = TRUE;
127                         break;
128                 }
129         }
130         g_strfreev(list);
131         g_free(mask);
132
133         return found;
134 }