-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.c
188 lines (162 loc) · 4.68 KB
/
process.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* Main processing code for Services.
*
* Services is copyright (c) 1996-1999 Andrew Church.
* E-mail: <achurch@dragonfire.net>
* Services is copyright (c) 1999-2000 Andrew Kempe.
* E-mail: <theshadow@shadowfire.org>
* This program is free but copyrighted software; see the file COPYING for
* details.
*/
#include "services.h"
#include "messages.h"
/*************************************************************************/
/*************************************************************************/
/* Use ignore code? */
int allow_ignore = 1;
/* People to ignore (hashed by first character of nick). */
IgnoreData *ignore[256];
/*************************************************************************/
/* add_ignore: Add someone to the ignorance list for the next `delta'
* seconds.
*/
void add_ignore(const char *nick, time_t delta)
{
IgnoreData *ign;
char who[NICKMAX];
time_t now = time(NULL);
IgnoreData **whichlist = &ignore[tolower(nick[0])];
strscpy(who, nick, NICKMAX);
for (ign = *whichlist; ign; ign = ign->next) {
if (stricmp(ign->who, who) == 0)
break;
}
if (ign) {
if (ign->time > now)
ign->time += delta;
else
ign->time = now + delta;
} else {
ign = smalloc(sizeof(*ign));
strscpy(ign->who, who, sizeof(ign->who));
ign->time = now + delta;
ign->next = *whichlist;
*whichlist = ign;
}
}
/*************************************************************************/
/* get_ignore: Retrieve an ignorance record for a nick. If the nick isn't
* being ignored, return NULL and flush the record from the
* in-core list if it exists (i.e. ignore timed out).
*/
IgnoreData *get_ignore(const char *nick)
{
IgnoreData *ign, *prev;
time_t now = time(NULL);
IgnoreData **whichlist = &ignore[tolower(nick[0])];
for (ign = *whichlist, prev = NULL; ign; prev = ign, ign = ign->next) {
if (stricmp(ign->who, nick) == 0)
break;
}
if (ign && ign->time <= now) {
if (prev)
prev->next = ign->next;
else
*whichlist = ign->next;
free(ign);
ign = NULL;
}
return ign;
}
/*************************************************************************/
/*************************************************************************/
/* split_buf: Split a buffer into arguments and store the arguments in an
* argument vector pointed to by argv (which will be malloc'd
* as necessary); return the argument count. If colon_special
* is non-zero, then treat a parameter with a leading ':' as
* the last parameter of the line, per the IRC RFC. Destroys
* the buffer by side effect.
*/
int split_buf(char *buf, char ***argv, int colon_special)
{
int argvsize = 8;
int argc;
char *s;
*argv = smalloc(sizeof(char *) * argvsize);
argc = 0;
while (*buf) {
if (argc == argvsize) {
argvsize += 8;
*argv = srealloc(*argv, sizeof(char *) * argvsize);
}
if (*buf == ':') {
(*argv)[argc++] = buf+1;
buf = "";
} else {
s = strpbrk(buf, " ");
if (s) {
*s++ = 0;
while (isspace((int)*s))
s++;
} else {
s = buf + strlen(buf);
}
(*argv)[argc++] = buf;
buf = s;
}
}
return argc;
}
/*************************************************************************/
/* process: Main processing routine. Takes the string in inbuf (global
* variable) and does something appropriate with it. */
void process()
{
char source[64];
char cmd[64];
char buf[512]; /* Longest legal IRC command line */
char *s;
int ac; /* Parameters for the command */
char **av;
Message *m;
/* If debugging, log the buffer. */
if (debug)
log("debug: Received: %s", inbuf);
/* First make a copy of the buffer so we have the original in case we
* crash - in that case, we want to know what we crashed on. */
strscpy(buf, inbuf, sizeof(buf));
/* Split the buffer into pieces. */
if (*buf == ':') {
s = strpbrk(buf, " ");
if (!s)
return;
*s = 0;
while (isspace((int)*++s))
;
strscpy(source, buf+1, sizeof(source));
memmove(buf, s, strlen(s)+1);
} else {
*source = 0;
}
if (!*buf)
return;
s = strpbrk(buf, " ");
if (s) {
*s = 0;
while (isspace((int)*++s))
;
} else
s = buf + strlen(buf);
strscpy(cmd, buf, sizeof(cmd));
ac = split_buf(s, &av, 1);
/* Do something with the message. */
m = find_message(cmd);
if (m) {
if (m->func)
m->func(source, ac, av);
} else {
log("unknown message from server (%s)", inbuf);
}
/* Free argument list we created */
free(av);
}
/*************************************************************************/