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)
115 if(fn->pubid != NULL)
125 struct fnetnode *findfnetnode(int id)
129 for(fn = fnetnodes; (fn != NULL) && (fn->id != id); fn = fn->next);
133 void linkfnetnode(struct fnetnode *fn)
138 fn->next = fnetnodes;
139 if(fnetnodes != NULL)
140 fnetnodes->prev = fn;
143 GCBCHAINDOCB(newfncb, fn);
146 void unlinkfnetnode(struct fnetnode *fn)
151 fnetnodes = fn->next;
153 fn->next->prev = fn->prev;
155 fn->prev->next = fn->next;
157 CBCHAINDOCB(fn, fnetnode_unlink, fn);
161 static void conncb(struct socket *sk, int err, struct fnetnode *data)
170 fnetsetstate(data, FNN_HS);
171 socksettos(sk, confgetint("fnet", "fntos"));
172 data->fnet->connect(data);
176 static void resolvecb(struct sockaddr *addr, int addrlen, struct fnetnode *data)
183 netcsconn(addr, addrlen, (void (*)(struct socket *, int, void *))conncb, data);
187 static struct fnetpeerdatum *finddatum(struct fnetnode *fn, wchar_t *id)
189 struct fnetpeerdatum *datum;
191 for(datum = fn->peerdata; datum != NULL; datum = datum->next)
193 if(!wcscmp(datum->id, id))
199 static struct fnetpeerdatum *adddatum(struct fnetnode *fn, wchar_t *id, int datatype)
201 struct fnetpeerdatum *new;
203 new = smalloc(sizeof(*new));
205 new->id = swcsdup(id);
206 new->datatype = datatype;
208 new->next = fn->peerdata;
209 if(fn->peerdata != NULL)
210 fn->peerdata->prev = new;
215 static struct fnetpeerdi *difindoradd(struct fnetpeer *peer, struct fnetpeerdatum *datum, int *isnew)
219 for(i = 0; i < peer->dinum; i++)
221 if(peer->peerdi[i].datum == datum)
226 peer->peerdi = srealloc(peer->peerdi, sizeof(struct fnetpeerdi) * (peer->dinum + 1));
227 memset(&peer->peerdi[peer->dinum], 0, sizeof(struct fnetpeerdi));
228 peer->peerdi[peer->dinum].datum = datum;
232 return(&peer->peerdi[peer->dinum++]);
236 return(&peer->peerdi[i]);
240 void fnetpeersetstr(struct fnetpeer *peer, wchar_t *id, wchar_t *value)
242 struct fnetpeerdatum *datum;
243 struct fnetpeerdi *di;
246 if((datum = finddatum(peer->fn, id)) == NULL)
247 datum = adddatum(peer->fn, id, FNPD_STR);
248 di = difindoradd(peer, datum, &changed);
249 if(di->data.str != NULL) {
250 changed = (changed || wcscmp(value, di->data.str));
255 di->data.str = swcsdup(value);
257 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
260 void fnetpeersetnum(struct fnetpeer *peer, wchar_t *id, int value)
262 struct fnetpeerdatum *datum;
263 struct fnetpeerdi *di;
266 if((datum = finddatum(peer->fn, id)) == NULL)
267 datum = adddatum(peer->fn, id, FNPD_INT);
268 di = difindoradd(peer, datum, &changed);
269 changed = (changed || (di->data.num != value));
270 di->data.num = value;
272 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
275 void fnetpeersetlnum(struct fnetpeer *peer, wchar_t *id, long long value)
277 struct fnetpeerdatum *datum;
278 struct fnetpeerdi *di;
281 if((datum = finddatum(peer->fn, id)) == NULL)
282 datum = adddatum(peer->fn, id, FNPD_LL);
283 di = difindoradd(peer, datum, &changed);
284 changed = (changed || (di->data.lnum != value));
285 di->data.lnum = value;
287 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
290 static void putdatum(struct fnetpeer *peer, struct fnetpeerdatum *datum)
292 if(--datum->refcount > 0)
294 if(datum->next != NULL)
295 datum->next->prev = datum->prev;
296 if(datum->prev != NULL)
297 datum->prev->next = datum->next;
298 if(datum == peer->fn->peerdata)
299 peer->fn->peerdata = datum->next;
304 void fnetpeerunset(struct fnetpeer *peer, wchar_t *id)
307 struct fnetpeerdatum *datum;
309 if((datum = finddatum(peer->fn, id)) == NULL)
311 for(i = 0; i < peer->dinum; i++)
313 if(peer->peerdi[i].datum == datum)
318 if((datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
319 free(peer->peerdi[i].data.str);
321 memmove(&peer->peerdi[i], &peer->peerdi[i + 1], sizeof(struct fnetpeerdi) * (peer->dinum - i));
322 putdatum(peer, datum);
325 struct fnetpeer *fnetaddpeer(struct fnetnode *fn, wchar_t *id, wchar_t *nick)
327 struct fnetpeer *new;
329 new = smalloc(sizeof(*new));
331 new->id = swcsdup(id);
332 new->nick = swcsdup(nick);
336 new->next = fn->peers;
338 if(fn->peers != NULL)
339 fn->peers->prev = new;
342 CBCHAINDOCB(fn, fnetnode_ac, fn, L"numpeers");
343 CBCHAINDOCB(fn, fnetpeer_new, fn, new);
347 void fnetdelpeer(struct fnetpeer *peer)
351 if(peer->next != NULL)
352 peer->next->prev = peer->prev;
353 if(peer->prev != NULL)
354 peer->prev->next = peer->next;
355 if(peer->fn->peers == peer)
356 peer->fn->peers = peer->next;
357 peer->fn->numpeers--;
358 CBCHAINDOCB(peer->fn, fnetnode_ac, peer->fn, L"numpeers");
359 CBCHAINDOCB(peer->fn, fnetpeer_del, peer->fn, peer);
362 for(i = 0; i < peer->dinum; i++)
364 if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
365 free(peer->peerdi[i].data.str);
366 putdatum(peer, peer->peerdi[i].datum);
368 if(peer->peerdi != NULL)
373 struct fnetpeer *fnetfindpeer(struct fnetnode *fn, wchar_t *id)
375 struct fnetpeer *cur;
377 for(cur = fn->peers; (cur != NULL) && wcscmp(cur->id, id); cur = cur->next);
381 int fnetsetnick(struct fnetnode *fn, wchar_t *newnick)
385 if(fn->fnet->setnick != NULL)
386 ret = fn->fnet->setnick(fn, newnick);
391 if(fn->mynick != NULL)
393 fn->mynick = swcsdup(newnick);
398 int fnetsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *string)
400 if(fn->fnet->sendchat == NULL)
405 return(fn->fnet->sendchat(fn, public, to, string));
408 int fnetsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlist *ln)
410 if(fn->fnet->search == NULL)
415 return(fn->fnet->search(fn, srch, ln));
418 void fnetsetname(struct fnetnode *fn, wchar_t *newname)
422 fn->name = swcsdup(newname);
423 CBCHAINDOCB(fn, fnetnode_ac, fn, L"name");
426 void fnetsetstate(struct fnetnode *fn, int newstate)
428 fn->state = newstate;
429 CBCHAINDOCB(fn, fnetnode_ac, fn, L"state");
432 struct fnet *findfnet(wchar_t *name)
436 for(fnet = networks; fnet != NULL; fnet = fnet->next)
438 if(!wcscmp(name, fnet->name))
444 struct fnetnode *fnetinitconnect(wchar_t *name, char *addr, struct wcspair *args)
450 if((fnet = findfnet(name)) == NULL)
452 errno = EPROTONOSUPPORT;
456 fn->pubid = icmbstowcs(addr, NULL);
457 if(fn->pubid == NULL)
458 fn->pubid = swcsdup(L"");
460 for(arg = fn->args; arg != NULL; arg = arg->next)
462 if(!wcscmp(arg->key, L"nick"))
463 fnetsetnick(fn, arg->val);
466 if(netresolve(addr, (void (*)(struct sockaddr *, int, void *))resolvecb, fn) < 0)
471 void regfnet(struct fnet *fnet)
473 fnet->next = networks;
478 * Note on the chat string: Must be in UNIX text file format - that
479 * is, LF line endings. The filenet-specific code must see to it that
480 * any other kind of format is converted into that. In the future,
481 * certain control characters and escape sequences will be parsed by
482 * the client. Make sure that any filenet-specific code strips any
483 * such that aren't supposed to be in the protocol.
485 * Note on "name": This is supposed to be an identifier for the
486 * source. If the chat is a public message, set "public" to non-zero
487 * and "name" to whatever "chat room" name is appropriate for the
488 * fnetnode, but not NULL. If there is a "default" channel in this
489 * filenet, set "name" to the empty string. If the chat is a private
490 * message, name is ignored.
492 void fnethandlechat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *chat)
494 CBCHAINDOCB(fn, fnetnode_chat, fn, public, name, peer, chat);
497 static struct configvar myvars[] =
499 {CONF_VAR_INT, "srchwait", {.num = 15}},
500 {CONF_VAR_INT, "fntos", {.num = 0}},
501 {CONF_VAR_INT, "fnptos", {.num = 0}},
505 static struct module me =