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->peers != NULL)
110 fnetdelpeer(fn->peers);
111 if(fn->mynick != NULL)
121 struct fnetnode *findfnetnode(int id)
125 for(fn = fnetnodes; (fn != NULL) && (fn->id != id); fn = fn->next);
129 void linkfnetnode(struct fnetnode *fn)
134 fn->next = fnetnodes;
135 if(fnetnodes != NULL)
136 fnetnodes->prev = fn;
139 GCBCHAINDOCB(newfncb, fn);
142 void unlinkfnetnode(struct fnetnode *fn)
147 fnetnodes = fn->next;
149 fn->next->prev = fn->prev;
151 fn->prev->next = fn->next;
153 CBCHAINDOCB(fn, fnetnode_unlink, fn);
157 static void conncb(struct socket *sk, int err, struct fnetnode *data)
166 fnetsetstate(data, FNN_HS);
167 socksettos(sk, confgetint("fnet", "fntos"));
168 data->fnet->connect(data);
172 static void resolvecb(struct sockaddr *addr, int addrlen, struct fnetnode *data)
179 netcsconn(addr, addrlen, (void (*)(struct socket *, int, void *))conncb, data);
183 static struct fnetpeerdatum *finddatum(struct fnetnode *fn, wchar_t *id)
185 struct fnetpeerdatum *datum;
187 for(datum = fn->peerdata; datum != NULL; datum = datum->next)
189 if(!wcscmp(datum->id, id))
195 static struct fnetpeerdatum *adddatum(struct fnetnode *fn, wchar_t *id, int datatype)
197 struct fnetpeerdatum *new;
199 new = smalloc(sizeof(*new));
201 new->id = swcsdup(id);
202 new->datatype = datatype;
204 new->next = fn->peerdata;
205 if(fn->peerdata != NULL)
206 fn->peerdata->prev = new;
211 static struct fnetpeerdi *difindoradd(struct fnetpeer *peer, struct fnetpeerdatum *datum, int *isnew)
215 for(i = 0; i < peer->dinum; i++)
217 if(peer->peerdi[i].datum == datum)
222 peer->peerdi = srealloc(peer->peerdi, sizeof(struct fnetpeerdi) * (peer->dinum + 1));
223 memset(&peer->peerdi[peer->dinum], 0, sizeof(struct fnetpeerdi));
224 peer->peerdi[peer->dinum].datum = datum;
228 return(&peer->peerdi[peer->dinum++]);
232 return(&peer->peerdi[i]);
236 void fnetpeersetstr(struct fnetpeer *peer, wchar_t *id, wchar_t *value)
238 struct fnetpeerdatum *datum;
239 struct fnetpeerdi *di;
242 if((datum = finddatum(peer->fn, id)) == NULL)
243 datum = adddatum(peer->fn, id, FNPD_STR);
244 di = difindoradd(peer, datum, &changed);
245 if(di->data.str != NULL) {
246 changed = (changed || !wcscmp(value, di->data.str));
251 di->data.str = swcsdup(value);
253 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
256 void fnetpeersetnum(struct fnetpeer *peer, wchar_t *id, int value)
258 struct fnetpeerdatum *datum;
259 struct fnetpeerdi *di;
262 if((datum = finddatum(peer->fn, id)) == NULL)
263 datum = adddatum(peer->fn, id, FNPD_INT);
264 di = difindoradd(peer, datum, &changed);
265 changed = (changed || (di->data.num != value));
266 di->data.num = value;
268 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
271 void fnetpeersetlnum(struct fnetpeer *peer, wchar_t *id, long long value)
273 struct fnetpeerdatum *datum;
274 struct fnetpeerdi *di;
277 if((datum = finddatum(peer->fn, id)) == NULL)
278 datum = adddatum(peer->fn, id, FNPD_LL);
279 di = difindoradd(peer, datum, &changed);
280 changed = (changed || (di->data.lnum != value));
281 di->data.lnum = value;
283 CBCHAINDOCB(peer->fn, fnetpeer_chdi, peer->fn, peer, di);
286 static void putdatum(struct fnetpeer *peer, struct fnetpeerdatum *datum)
288 if(--datum->refcount > 0)
290 if(datum->next != NULL)
291 datum->next->prev = datum->prev;
292 if(datum->prev != NULL)
293 datum->prev->next = datum->next;
294 if(datum == peer->fn->peerdata)
295 peer->fn->peerdata = datum->next;
300 void fnetpeerunset(struct fnetpeer *peer, wchar_t *id)
303 struct fnetpeerdatum *datum;
305 if((datum = finddatum(peer->fn, id)) == NULL)
307 for(i = 0; i < peer->dinum; i++)
309 if(peer->peerdi[i].datum == datum)
314 if((datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
315 free(peer->peerdi[i].data.str);
317 memmove(&peer->peerdi[i], &peer->peerdi[i + 1], sizeof(struct fnetpeerdi) * (peer->dinum - i));
318 putdatum(peer, datum);
321 struct fnetpeer *fnetaddpeer(struct fnetnode *fn, wchar_t *id, wchar_t *nick)
323 struct fnetpeer *new;
325 new = smalloc(sizeof(*new));
327 new->id = swcsdup(id);
328 new->nick = swcsdup(nick);
332 new->next = fn->peers;
334 if(fn->peers != NULL)
335 fn->peers->prev = new;
338 CBCHAINDOCB(fn, fnetnode_ac, fn, L"numpeers");
339 CBCHAINDOCB(fn, fnetpeer_new, fn, new);
343 void fnetdelpeer(struct fnetpeer *peer)
347 if(peer->next != NULL)
348 peer->next->prev = peer->prev;
349 if(peer->prev != NULL)
350 peer->prev->next = peer->next;
351 if(peer->fn->peers == peer)
352 peer->fn->peers = peer->next;
353 peer->fn->numpeers--;
354 CBCHAINDOCB(peer->fn, fnetnode_ac, peer->fn, L"numpeers");
355 CBCHAINDOCB(peer->fn, fnetpeer_del, peer->fn, peer);
358 for(i = 0; i < peer->dinum; i++)
360 if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
361 free(peer->peerdi[i].data.str);
362 putdatum(peer, peer->peerdi[i].datum);
364 if(peer->peerdi != NULL)
369 struct fnetpeer *fnetfindpeer(struct fnetnode *fn, wchar_t *id)
371 struct fnetpeer *cur;
373 for(cur = fn->peers; (cur != NULL) && wcscmp(cur->id, id); cur = cur->next);
377 int fnetsetnick(struct fnetnode *fn, wchar_t *newnick)
381 if(fn->fnet->setnick != NULL)
382 ret = fn->fnet->setnick(fn, newnick);
387 if(fn->mynick != NULL)
389 fn->mynick = swcsdup(newnick);
394 int fnetsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *string)
396 if(fn->fnet->sendchat == NULL)
401 return(fn->fnet->sendchat(fn, public, to, string));
404 int fnetsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlist *ln)
406 if(fn->fnet->search == NULL)
411 return(fn->fnet->search(fn, srch, ln));
414 void fnetsetname(struct fnetnode *fn, wchar_t *newname)
418 fn->name = swcsdup(newname);
419 CBCHAINDOCB(fn, fnetnode_ac, fn, L"name");
422 void fnetsetstate(struct fnetnode *fn, int newstate)
424 fn->state = newstate;
425 CBCHAINDOCB(fn, fnetnode_ac, fn, L"state");
428 struct fnet *findfnet(wchar_t *name)
432 for(fnet = networks; fnet != NULL; fnet = fnet->next)
434 if(!wcscmp(name, fnet->name))
440 struct fnetnode *fnetinitconnect(wchar_t *name, char *addr)
445 if((fnet = findfnet(name)) == NULL)
447 errno = EPROTONOSUPPORT;
452 if(netresolve(addr, (void (*)(struct sockaddr *, int, void *))resolvecb, fn) < 0)
457 void regfnet(struct fnet *fnet)
459 fnet->next = networks;
464 * Note on the chat string: Must be in UNIX text file format - that
465 * is, LF line endings. The filenet-specific code must see to it that
466 * any other kind of format is converted into that. In the future,
467 * certain control characters and escape sequences will be parsed by
468 * the client. Make sure that any filenet-specific code strips any
469 * such that aren't supposed to be in the protocol.
471 * Note on "name": This is supposed to be an identifier for the
472 * source. If the chat is a public message, set "public" to non-zero
473 * and "name" to whatever "chat room" name is appropriate for the
474 * fnetnode, but not NULL. If there is a "default" channel in this
475 * filenet, set "name" to the empty string. If the chat is a private
476 * message, name is ignored.
478 void fnethandlechat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *chat)
480 CBCHAINDOCB(fn, fnetnode_chat, fn, public, name, peer, chat);
483 static struct configvar myvars[] =
485 {CONF_VAR_INT, "srchwait", {.num = 15}},
486 {CONF_VAR_INT, "fntos", {.num = 0}},
487 {CONF_VAR_INT, "fnptos", {.num = 0}},
491 static struct module me =