7014e431b500efa311546f63bbe2cfb613befd93
[runtime.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         ret = FALSE;
34         while (*mask != '\0') {
35                 if (*mask == '!') {
36                         if (*wildcards) return TRUE;
37                         ret = TRUE;
38                 }
39
40                 if (*mask == '?' || *mask == '*') {
41                         *wildcards = TRUE;
42                         if (ret) return TRUE;
43                 }
44                 mask++;
45         }
46
47         return ret;
48 }
49
50 static int check_mask(SERVER_REC *server, const char *mask,
51                       const char *str, int wildcards)
52 {
53         if (server != NULL && server->mask_match_func != NULL) {
54                 /* use server specified mask match function */
55                 return server->mask_match_func(mask, str);
56         }
57
58         return wildcards ? match_wildcards(mask, str) :
59                 g_strcasecmp(mask, str) == 0;
60 }
61
62 int mask_match(SERVER_REC *server, const char *mask,
63                const char *nick, const char *user, const char *host)
64 {
65         char *str;
66         int ret, wildcards;
67
68         g_return_val_if_fail(server == NULL || IS_SERVER(server), FALSE);
69         g_return_val_if_fail(mask != NULL && nick != NULL &&
70                              nick != NULL && host != NULL, FALSE);
71
72         str = !check_address(mask, &wildcards) ? (char *) nick :
73                 g_strdup_printf("%s!%s@%s", nick, user, host);
74         ret = check_mask(server, mask, str, wildcards);
75         if (str != nick) g_free(str);
76
77         return ret;
78 }
79
80 int mask_match_address(SERVER_REC *server, const char *mask,
81                        const char *nick, const char *address)
82 {
83         char *str;
84         int ret, wildcards;
85
86         g_return_val_if_fail(server == NULL || IS_SERVER(server), FALSE);
87         g_return_val_if_fail(mask != NULL && nick != NULL, FALSE);
88         if (address == NULL) address = "";
89
90         str = !check_address(mask, &wildcards) ? (char *) nick :
91                 g_strdup_printf("%s!%s", nick, address);
92         ret = check_mask(server, mask, str, wildcards);
93         if (str != nick) g_free(str);
94
95         return ret;
96 }
97
98 int masks_match(SERVER_REC *server, const char *masks,
99                 const char *nick, const char *address)
100 {
101         int (*mask_match_func)(const char *, const char *);
102         char **list, **tmp, *mask;
103         int found;
104
105         g_return_val_if_fail(server == NULL || IS_SERVER(server), FALSE);
106         g_return_val_if_fail(masks != NULL &&
107                              nick != NULL && address != NULL, FALSE);
108
109         if (*masks == '\0')
110                 return FALSE;
111
112         mask_match_func = server != NULL && server->mask_match_func != NULL ?
113                 server->mask_match_func : match_wildcards;
114
115         found = FALSE;
116         mask = g_strdup_printf("%s!%s", nick, address);
117         list = g_strsplit(masks, " ", -1);
118         for (tmp = list; *tmp != NULL; tmp++) {
119                 if (g_strcasecmp(*tmp, nick) == 0) {
120                         found = TRUE;
121                         break;
122                 }
123
124                 if (mask_match_func(*tmp, mask)) {
125                         found = TRUE;
126                         break;
127                 }
128         }
129         g_strfreev(list);
130         g_free(mask);
131
132         return found;
133 }