2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <sys/socket.h>
33 static struct fnet *networks = NULL;
34 struct fnetnode *fnetnodes = NULL;
36 GCBCHAIN(newfncb, struct fnetnode *);
38 static struct fnetnode *newfn(struct fnet *fnet)
43 new = smalloc(sizeof(*new));
44 memset(new, 0, sizeof(*new));
48 new->mynick = swcsdup(confgetstr("cli", "defnick"));
49 new->srchwait = confgetint("fnet", "srchwait");
51 CBCHAININIT(new, fnetnode_ac);
52 CBCHAININIT(new, fnetnode_chat);
53 CBCHAININIT(new, fnetnode_unlink);
54 CBCHAININIT(new, fnetnode_destroy);
55 CBCHAININIT(new, fnetpeer_new);
56 CBCHAININIT(new, fnetpeer_del);
57 CBCHAININIT(new, fnetpeer_chdi);
64 void killfnetnode(struct fnetnode *fn)
66 fnetsetstate(fn, FNN_DEAD);
70 if(fn->sk->data == fn)
77 void getfnetnode(struct fnetnode *fn)
81 fprintf(stderr, "getfnetnode on id %i at %p, refcount=%i\n", fn->id, fn, fn->refcount);
85 void putfnetnode(struct fnetnode *fn)
90 fprintf(stderr, "putfnetnode on id %i at %p, refcount=%i\n", fn->id, fn, fn->refcount - 1);
94 for(cur = fnetnodes; cur != NULL; cur = cur->next)
97 flog(LOG_CRIT, "BUG: fnetnode reached refcount 0 while still in list - id %i", fn->id);
99 CBCHAINDOCB(fn, fnetnode_destroy, fn);
100 CBCHAINFREE(fn, fnetnode_ac);
101 CBCHAINFREE(fn, fnetnode_chat);
102 CBCHAINFREE(fn, fnetnode_unlink);
103 CBCHAINFREE(fn, fnetnode_destroy);
104 CBCHAINFREE(fn, fnetpeer_new);
105 CBCHAINFREE(fn, fnetpeer_del);
106 CBCHAINFREE(fn, fnetpeer_chdi);
107 if(fn->fnet->destroy != NULL)
108 fn->fnet->destroy(fn);
109 while(fn->args != NULL)
110 freewcspair(fn->args, &fn->args);
111 while(fn->peers != NULL)
112 fnetdelpeer(fn->peers);
113 if(fn->mynick != NULL)
123 struct fnetnode *findfnetnode(int id)
127 for(fn = fnetnodes; (fn != NULL) && (fn->id != id); fn = fn->next);
131 void linkfnetnode(struct fnetnode *fn)
136 fn->next = fnetnodes;
137 if(fnetnodes != NULL)
138 fnetnodes->prev = fn;
141 GCBCHAINDOCB(newfncb, fn);
144 void unlinkfnetnode(struct fnetnode *fn)
149 fnetnodes = fn->next;
151 fn->next->prev = fn->prev;
153 fn->prev->next = fn->next;
155 CBCHAINDOCB(fn, fnetnode_unlink, fn);
159 static void conncb(struct socket *sk, int err, struct fnetnode *data)
168 fnetsetstate(data, FNN_HS);
169 socksettos(sk, confgetint("fnet", "fntos"));
170 data->fnet->connect(data);
174 static void resolvecb(struct sockaddr *addr, int addrlen, struct fnetnode *data)
181 netcsconn(addr, addrlen, (void (*)(struct socket *, int, void *))conncb, data);
185 static struct fnetpeerdatum *finddatum(struct fnetnode *fn, wchar_t *id)
187 struct fnetpeerdatum *datum;
189 for(datum = fn->peerdata; datum != NULL; datum = datum->next)
191 if(!wcscmp(datum->id, id))
197 static struct fnetpeerdatum *adddatum(struct fnetnode *fn, wchar_t *id, int datatype)
199 struct fnetpeerdatum *new;
201 new = smalloc(sizeof(*new));
203 new->id = swcsdup(id);
204 new->datatype = datatype;
206 new->next = fn->peerdata;
207 if(fn->peerdata != NULL)
208 fn->peerdata->prev = new;
213 static struct fnetpeerdi *difindoradd(struct fnetpeer *peer, struct fnetpeerdatum *datum, int *isnew)
217 for(i = 0; i < peer->dinum; i++)
219 if(peer->peerdi[i].datum == datum)
224 peer->peerdi = srealloc(peer->peerdi, sizeof(struct fnetpeerdi) * (peer->dinum + 1));
225 memset(&peer->peerdi[peer->dinum], 0, sizeof(struct fnetpeerdi));
226 peer->peerdi[peer->dinum].datum = datum;
230 return(&peer->peerdi[peer->dinum++]);
234 return(&peer->peerdi[i]);
238 void fnetpeersetstr(struct fnetpeer *peer, wchar_t *id, wchar_t *value)
240 struct fnetpeerdatum *datum;
241 struct fnetpeerdi *di;
244 if((datum = finddatum(peer->fn, id)) == NULL)
245 datum = adddatum(peer->fn, id, FNPD_STR);
246 di = difindoradd(peer, datum, &changed);
247 if(di->data.str != NULL) {
248 changed = (changed || wcscmp(value, di->data.str));
253 di->data.str = swcsdup(value);
255 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
258 void fnetpeersetnum(struct fnetpeer *peer, wchar_t *id, int value)
260 struct fnetpeerdatum *datum;
261 struct fnetpeerdi *di;
264 if((datum = finddatum(peer->fn, id)) == NULL)
265 datum = adddatum(peer->fn, id, FNPD_INT);
266 di = difindoradd(peer, datum, &changed);
267 changed = (changed || (di->data.num != value));
268 di->data.num = value;
270 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
273 void fnetpeersetlnum(struct fnetpeer *peer, wchar_t *id, long long value)
275 struct fnetpeerdatum *datum;
276 struct fnetpeerdi *di;
279 if((datum = finddatum(peer->fn, id)) == NULL)
280 datum = adddatum(peer->fn, id, FNPD_LL);
281 di = difindoradd(peer, datum, &changed);
282 changed = (changed || (di->data.lnum != value));
283 di->data.lnum = value;
285 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
288 static void putdatum(struct fnetpeer *peer, struct fnetpeerdatum *datum)
290 if(--datum->refcount > 0)
292 if(datum->next != NULL)
293 datum->next->prev = datum->prev;
294 if(datum->prev != NULL)
295 datum->prev->next = datum->next;
296 if(datum == peer->fn->peerdata)
297 peer->fn->peerdata = datum->next;
302 void fnetpeerunset(struct fnetpeer *peer, wchar_t *id)
305 struct fnetpeerdatum *datum;
307 if((datum = finddatum(peer->fn, id)) == NULL)
309 for(i = 0; i < peer->dinum; i++)
311 if(peer->peerdi[i].datum == datum)
316 if((datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
317 free(peer->peerdi[i].data.str);
319 memmove(&peer->peerdi[i], &peer->peerdi[i + 1], sizeof(struct fnetpeerdi) * (peer->dinum - i));
320 putdatum(peer, datum);
323 struct fnetpeer *fnetaddpeer(struct fnetnode *fn, wchar_t *id, wchar_t *nick)
325 struct fnetpeer *new;
327 new = smalloc(sizeof(*new));
329 new->id = swcsdup(id);
330 new->nick = swcsdup(nick);
334 new->next = fn->peers;
336 if(fn->peers != NULL)
337 fn->peers->prev = new;
340 CBCHAINDOCB(fn, fnetnode_ac, fn, L"numpeers");
341 CBCHAINDOCB(fn, fnetpeer_new, fn, new);
345 void fnetdelpeer(struct fnetpeer *peer)
349 if(peer->next != NULL)
350 peer->next->prev = peer->prev;
351 if(peer->prev != NULL)
352 peer->prev->next = peer->next;
353 if(peer->fn->peers == peer)
354 peer->fn->peers = peer->next;
355 peer->fn->numpeers--;
356 CBCHAINDOCB(peer->fn, fnetnode_ac, peer->fn, L"numpeers");
357 CBCHAINDOCB(peer->fn, fnetpeer_del, peer->fn, peer);
360 for(i = 0; i < peer->dinum; i++)
362 if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
363 free(peer->peerdi[i].data.str);
364 putdatum(peer, peer->peerdi[i].datum);
366 if(peer->peerdi != NULL)
371 struct fnetpeer *fnetfindpeer(struct fnetnode *fn, wchar_t *id)
373 struct fnetpeer *cur;
375 for(cur = fn->peers; (cur != NULL) && wcscmp(cur->id, id); cur = cur->next);
379 int fnetsetnick(struct fnetnode *fn, wchar_t *newnick)
383 if(fn->fnet->setnick != NULL)
384 ret = fn->fnet->setnick(fn, newnick);
389 if(fn->mynick != NULL)
391 fn->mynick = swcsdup(newnick);
396 int fnetsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *string)
398 if(fn->fnet->sendchat == NULL)
403 return(fn->fnet->sendchat(fn, public, to, string));
406 int fnetsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlist *ln)
408 if(fn->fnet->search == NULL)
413 return(fn->fnet->search(fn, srch, ln));
416 void fnetsetname(struct fnetnode *fn, wchar_t *newname)
420 fn->name = swcsdup(newname);
421 CBCHAINDOCB(fn, fnetnode_ac, fn, L"name");
424 void fnetsetstate(struct fnetnode *fn, int newstate)
426 fn->state = newstate;
427 CBCHAINDOCB(fn, fnetnode_ac, fn, L"state");
430 struct fnet *findfnet(wchar_t *name)
434 for(fnet = networks; fnet != NULL; fnet = fnet->next)
436 if(!wcscmp(name, fnet->name))
442 struct fnetnode *fnetinitconnect(wchar_t *name, char *addr, struct wcspair *args)
448 if((fnet = findfnet(name)) == NULL)
450 errno = EPROTONOSUPPORT;
455 for(arg = fn->args; arg != NULL; arg = arg->next)
457 if(!wcscmp(arg->key, L"nick"))
458 fnetsetnick(fn, arg->val);
461 if(netresolve(addr, (void (*)(struct sockaddr *, int, void *))resolvecb, fn) < 0)
466 void regfnet(struct fnet *fnet)
468 fnet->next = networks;
473 * Note on the chat string: Must be in UNIX text file format - that
474 * is, LF line endings. The filenet-specific code must see to it that
475 * any other kind of format is converted into that. In the future,
476 * certain control characters and escape sequences will be parsed by
477 * the client. Make sure that any filenet-specific code strips any
478 * such that aren't supposed to be in the protocol.
480 * Note on "name": This is supposed to be an identifier for the
481 * source. If the chat is a public message, set "public" to non-zero
482 * and "name" to whatever "chat room" name is appropriate for the
483 * fnetnode, but not NULL. If there is a "default" channel in this
484 * filenet, set "name" to the empty string. If the chat is a private
485 * message, name is ignored.
487 void fnethandlechat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *chat)
489 CBCHAINDOCB(fn, fnetnode_chat, fn, public, name, peer, chat);
492 static struct configvar myvars[] =
494 {CONF_VAR_INT, "srchwait", {.num = 15}},
495 {CONF_VAR_INT, "fntos", {.num = 0}},
496 {CONF_VAR_INT, "fnptos", {.num = 0}},
500 static struct module me =