summaryrefslogtreecommitdiff
path: root/vchat-user.c
diff options
context:
space:
mode:
authorerdgeist <>2003-02-12 17:48:37 +0000
committererdgeist <>2003-02-12 17:48:37 +0000
commitdea6bf757aa9a875eab35b2b650412e7605f1308 (patch)
tree14ed8374c3a3862529313088375693a7de70d3a7 /vchat-user.c
CVS moved to erdgeist.org
Diffstat (limited to 'vchat-user.c')
-rwxr-xr-xvchat-user.c374
1 files changed, 374 insertions, 0 deletions
diff --git a/vchat-user.c b/vchat-user.c
new file mode 100755
index 0000000..4f4990c
--- /dev/null
+++ b/vchat-user.c
@@ -0,0 +1,374 @@
1/*
2 * vchat-client - alpha version
3 * vchat-user.c - functions working with the userlist
4 *
5 * Copyright (C) 2001 Andreas Kotes <count@flatline.de>
6 *
7 * This program is free software. It can be redistributed and/or modified,
8 * provided that this copyright notice is kept intact. This program is
9 * distributed in the hope that it will be useful, but without any warranty;
10 * without even the implied warranty of merchantability or fitness for a
11 * particular purpose. In no event shall the copyright holder be liable for
12 * any direct, indirect, incidental or special damages arising in any way out
13 * of the use of this software.
14 *
15 */
16
17/* general includes */
18#include <stdlib.h>
19#include <stdio.h>
20#include <string.h>
21#include <sys/types.h>
22#include <regex.h>
23#include "vchat.h"
24
25/* version of this module */
26unsigned char *vchat_us_version = "$Id$";
27
28/* externally used variables */
29/* current nick */
30unsigned char *nick = NULL;
31/* current channel */
32int chan = 0;
33/* userlist */
34user *nicks = NULL;
35
36/* add user to userlist */
37void
38ul_add (unsigned char *name, int ignored)
39{
40 user *tmp = NULL;
41
42 /* is it this client? return */
43 if (nick && !strcasecmp (nick, name))
44 return;
45
46 /* no list? create one */
47 if (!nicks)
48 {
49 nicks = malloc (sizeof (user));
50 memset(nicks,0,sizeof(user));
51 nicks->nick = strdup(name);
52 nicks->chan = 0; /* users default in channel 0 */
53 nicks->chan_valid = 0;
54 nicks->next = NULL;
55 }
56 else
57 {
58 /* travel list until end */
59 tmp = nicks;
60 while (tmp)
61 {
62 /* it is this user? return */
63 if (!strcmp (name, tmp->nick))
64 return;
65 /* is there a next user? */
66 if (tmp->next)
67 tmp = tmp->next;
68 else
69 {
70 /* create one */
71 tmp->next = malloc (sizeof (user));
72 tmp = tmp->next;
73 memset(tmp,0,sizeof(user));
74 tmp->nick = strdup(name);
75 tmp->chan = 0;
76 tmp->chan_valid = 0;
77 tmp->next = NULL;
78 tmp = NULL;
79 }
80 }
81 }
82}
83
84/* delete user from userlist */
85void
86ul_del (unsigned char *name, int ignored)
87{
88 user *tmp = NULL, *ltmp = NULL;
89
90 /* is it this client? return */
91 if (nick && !strcmp (nick, name))
92 return;
93 /* no list? return */
94 if (!nicks)
95 return;
96 /* the user on top of list? */
97 if (!strcmp (name, nicks->nick))
98 {
99 /* remove user and copy next in list */
100 tmp = nicks->next;
101 free (nicks);
102 nicks = tmp;
103 return;
104 }
105 /* travel through list, skip first entry */
106 ltmp = nicks;
107 tmp = nicks->next;
108 while (tmp)
109 {
110 /* is it this user? */
111 if (!strcmp (name, tmp->nick))
112 {
113 /* hook next to last, discard this */
114 ltmp->next = tmp->next;
115 free (tmp);
116 return;
117 }
118 /* advance in list */
119 ltmp = tmp;
120 tmp = tmp->next;
121 }
122}
123
124/* let user join a channel */
125void
126ul_join (unsigned char *name, int channel)
127{
128 /* is it this client? handle and return */
129 if (nick && !strcmp (nick, name))
130 {
131 ownjoin (channel);
132 return;
133 } else ul_moveuser(name,channel);
134}
135
136user *
137ul_finduser (unsigned char *name) {
138 user *tmp = nicks;
139 /* search user */
140 while (tmp)
141 {
142 /* is it this user? */
143 if (!strcmp (name, tmp->nick))
144 {
145 return tmp;
146 }
147 /* advance in list */
148 tmp = tmp->next;
149 }
150 return NULL;
151}
152
153unsigned char *
154ul_matchuser( unsigned char *regex) {
155 user *tmp = nicks;
156 unsigned char *dest = tmpstr;
157 regex_t preg;
158
159 *dest = 0;
160 if( !regcomp( &preg, regex, REG_ICASE | REG_EXTENDED | REG_NEWLINE)) {
161 while( tmp ) {
162 /* does the username match? */
163 if( !regexec( &preg, tmp->nick, 0, NULL, 0)) /* append username to list */
164 dest += snprintf ( dest, 256, " %s", tmp->nick);
165 tmp = tmp->next;
166 }
167 }
168 regfree( &preg );
169 return tmpstr;
170}
171
172void
173ul_msgto (unsigned char *name) {
174 user *tmp = ul_finduser(name);
175
176 if (tmp)
177 tmp->messaged |= 1;
178}
179
180void
181ul_msgfrom (unsigned char *name) {
182 user *tmp = ul_finduser(name);
183
184 if (tmp)
185 tmp->messaged |= 2;
186}
187
188/* set channel of user */
189void
190ul_moveuser (unsigned char *name, int channel) {
191 user *tmp = ul_finduser(name);
192
193 if (tmp) {
194 /* store channel information and mark it valid */
195 tmp->chan = channel;
196 tmp->chan_valid = 1;
197 }
198}
199
200/* let user leave a channel */
201void
202ul_leave (unsigned char *name, int channel)
203{
204 user *tmp = ul_finduser(name);
205 /* is it this client? handle and return */
206 if (nick && !strcmp (nick, name))
207 {
208 ownleave (channel);
209 return;
210 }
211
212 if (tmp)
213 {
214 /* mark channel information invalid */
215 tmp->chan_valid = 0;
216 return;
217 }
218}
219
220/* let user change nick */
221void
222ul_nickchange (unsigned char *oldnick, unsigned char *newnick)
223{
224 user *tmp = ul_finduser(oldnick);
225 /* is it this client? handle and return */
226 if (nick && !strcmp (nick, oldnick))
227 {
228 ownnickchange (newnick);
229 return;
230 }
231 if (tmp)
232 {
233 /* exchange nickname */
234 free (tmp->nick);
235 tmp->nick = strdup (newnick);
236 return;
237 }
238}
239
240/* clear userlist */
241void
242ul_clear (void)
243{
244 user *tmp = nicks, *tmp2;
245 /* walk list */
246 while (tmp)
247 {
248 /* store next, delete this */
249 tmp2 = tmp->next;
250 free (tmp->nick);
251 free (tmp);
252 /* advance */
253 tmp = tmp2;
254 }
255 /* mark list empty */
256 nicks = NULL;
257}
258
259int ulnc_casenick(user *tmp, const unsigned char *text, int len, int value) {
260 return (!strncmp(tmp->nick, text, len));
261}
262
263int ulnc_ncasenick(user *tmp, const unsigned char *text, int len, int value) {
264 return (!strncasecmp(tmp->nick, text, len));
265}
266
267unsigned char *
268ulnc_complete (const unsigned char *text, int state, int value, int (*checkfn)(user *,const unsigned char *,int,int)) {
269 static int len;
270 static user *tmp;
271 unsigned char *name;
272
273 /* first round? reset pointers! */
274 if (!state)
275 {
276 tmp = nicks;
277 len = strlen (text);
278 }
279
280 /* walk list .. */
281 while (tmp)
282 {
283 /* we found a match? */
284 if (checkfn(tmp,text,len,value))
285 {
286 /* copy nick, advance pointer for next call, return nick */
287 name = tmp->nick;
288 tmp = tmp->next;
289 return name;
290 }
291 else
292 {
293 tmp = tmp->next;
294 }
295 }
296 return NULL;
297}
298
299/* nick completion functions for readline in vchat-ui.c */
300unsigned char *
301ul_nickcomp (const unsigned char *text, int state)
302{
303 int ncasemode;
304 unsigned char *name = NULL;
305 if (!state) ncasemode = 0;
306 if (!ncasemode) {
307 name = ulnc_complete(text,state,0,ulnc_casenick);
308 if (!state && !name) ncasemode = 1;
309 }
310 if (ncasemode)
311 name = ulnc_complete(text,state,0,ulnc_ncasenick);
312 if (name)
313 return strdup(name);
314 else
315 return NULL;
316}
317
318int ulnc_casenickc(user *tmp, const unsigned char *text, int len, int value) {
319 return (!strncmp(tmp->nick, text, len) && (tmp->chan_valid) && (tmp->chan == value));
320}
321
322int ulnc_ncasenickc(user *tmp, const unsigned char *text, int len, int value) {
323 return (!strncasecmp(tmp->nick, text, len) && (tmp->chan_valid) && (tmp->chan == value));
324}
325
326/* nick completion for channel, used by vchat-ui.c */
327unsigned char *
328ul_cnickcomp (const unsigned char *text, int state)
329{
330 int ncasemode;
331 static unsigned char *name = NULL;
332
333 if (!state) ncasemode = 0;
334 if (!ncasemode) {
335 name = ulnc_complete(text,state,chan,ulnc_casenickc);
336 if (!state && !name) ncasemode = 1;
337 }
338 if (ncasemode)
339 name = ulnc_complete(text,state,chan,ulnc_ncasenickc);
340 if (name) {
341 snprintf(tmpstr,TMPSTRSIZE,"%s:",name);
342 return strdup(tmpstr);
343 } else
344 return NULL;
345}
346
347int ulnc_casenickm(user *tmp, const unsigned char *text, int len, int value) {
348 return (!strncmp(tmp->nick, text, len) && (tmp->messaged));
349}
350
351int ulnc_ncasenickm(user *tmp, const unsigned char *text, int len, int value) {
352 return (!strncasecmp(tmp->nick, text, len) && (tmp->messaged));
353}
354
355/* nick completion for channel, used by vchat-ui.c */
356unsigned char *
357ul_mnickcomp (const unsigned char *text, int state)
358{
359 int ncasemode;
360 static unsigned char *name = NULL;
361
362 if (!state) ncasemode = 0;
363 if (!ncasemode) {
364 name = ulnc_complete(text,state,chan,ulnc_casenickm);
365 if (!state && !name) ncasemode = 1;
366 }
367 if (ncasemode)
368 name = ulnc_complete(text,state,chan,ulnc_ncasenickm);
369 if (name) {
370 snprintf(tmpstr,TMPSTRSIZE,".m %s",name);
371 return strdup(tmpstr);
372 } else
373 return NULL;
374}