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);
61 void killfnetnode(struct fnetnode *fn)
63 fnetsetstate(fn, FNN_DEAD);
67 if(fn->sk->data == fn)
74 void getfnetnode(struct fnetnode *fn)
78 fprintf(stderr, "getfnetnode on id %i at %p, refcount=%i\n", fn->id, fn, fn->refcount);
82 void putfnetnode(struct fnetnode *fn)
87 fprintf(stderr, "putfnetnode on id %i at %p, refcount=%i\n", fn->id, fn, fn->refcount - 1);
91 for(cur = fnetnodes; cur != NULL; cur = cur->next)
94 flog(LOG_CRIT, "BUG: fnetnode reached refcount 0 while still in list - id %i", fn->id);
96 CBCHAINDOCB(fn, fnetnode_destroy, fn);
97 CBCHAINFREE(fn, fnetnode_ac);
98 CBCHAINFREE(fn, fnetnode_chat);
99 CBCHAINFREE(fn, fnetnode_unlink);
100 CBCHAINFREE(fn, fnetnode_destroy);
101 if(fn->fnet->destroy != NULL)
102 fn->fnet->destroy(fn);
103 while(fn->peers != NULL)
104 fnetdelpeer(fn->peers);
105 if(fn->mynick != NULL)
115 struct fnetnode *findfnetnode(int id)
119 for(fn = fnetnodes; (fn != NULL) && (fn->id != id); fn = fn->next);
123 void linkfnetnode(struct fnetnode *fn)
128 fn->next = fnetnodes;
129 if(fnetnodes != NULL)
130 fnetnodes->prev = fn;
133 GCBCHAINDOCB(newfncb, fn);
136 void unlinkfnetnode(struct fnetnode *fn)
141 fnetnodes = fn->next;
143 fn->next->prev = fn->prev;
145 fn->prev->next = fn->next;
147 CBCHAINDOCB(fn, fnetnode_unlink, fn);
151 static void conncb(struct socket *sk, int err, struct fnetnode *data)
160 fnetsetstate(data, FNN_HS);
161 socksettos(sk, confgetint("fnet", "fntos"));
162 data->fnet->connect(data);
166 static void resolvecb(struct sockaddr *addr, int addrlen, struct fnetnode *data)
173 netcsconn(addr, addrlen, (void (*)(struct socket *, int, void *))conncb, data);
177 static struct fnetpeerdatum *finddatum(struct fnetnode *fn, wchar_t *id)
179 struct fnetpeerdatum *datum;
181 for(datum = fn->peerdata; datum != NULL; datum = datum->next)
183 if(!wcscmp(datum->id, id))
189 static struct fnetpeerdatum *adddatum(struct fnetnode *fn, wchar_t *id, int datatype)
191 struct fnetpeerdatum *new;
193 new = smalloc(sizeof(*new));
195 new->id = swcsdup(id);
196 new->datatype = datatype;
198 new->next = fn->peerdata;
199 if(fn->peerdata != NULL)
200 fn->peerdata->prev = new;
205 static struct fnetpeerdi *difindoradd(struct fnetpeer *peer, struct fnetpeerdatum *datum)
209 for(i = 0; i < peer->dinum; i++)
211 if(peer->peerdi[i].datum == datum)
216 peer->peerdi = srealloc(peer->peerdi, sizeof(struct fnetpeerdi) * (peer->dinum + 1));
217 memset(&peer->peerdi[peer->dinum], 0, sizeof(struct fnetpeerdi));
218 peer->peerdi[peer->dinum].datum = datum;
220 return(&peer->peerdi[peer->dinum++]);
222 return(&peer->peerdi[i]);
226 void fnetpeersetstr(struct fnetpeer *peer, wchar_t *id, wchar_t *value)
228 struct fnetpeerdatum *datum;
229 struct fnetpeerdi *di;
231 if((datum = finddatum(peer->fn, id)) == NULL)
232 datum = adddatum(peer->fn, id, FNPD_STR);
233 di = difindoradd(peer, datum);
234 if(di->data.str != NULL)
236 di->data.str = swcsdup(value);
239 void fnetpeersetnum(struct fnetpeer *peer, wchar_t *id, int value)
241 struct fnetpeerdatum *datum;
242 struct fnetpeerdi *di;
244 if((datum = finddatum(peer->fn, id)) == NULL)
245 datum = adddatum(peer->fn, id, FNPD_INT);
246 di = difindoradd(peer, datum);
247 di->data.num = value;
250 void fnetpeersetlnum(struct fnetpeer *peer, wchar_t *id, long long value)
252 struct fnetpeerdatum *datum;
253 struct fnetpeerdi *di;
255 if((datum = finddatum(peer->fn, id)) == NULL)
256 datum = adddatum(peer->fn, id, FNPD_LL);
257 di = difindoradd(peer, datum);
258 di->data.lnum = value;
261 static void putdatum(struct fnetpeer *peer, struct fnetpeerdatum *datum)
263 if(--datum->refcount > 0)
265 if(datum->next != NULL)
266 datum->next->prev = datum->prev;
267 if(datum->prev != NULL)
268 datum->prev->next = datum->next;
269 if(datum == peer->fn->peerdata)
270 peer->fn->peerdata = datum->next;
275 void fnetpeerunset(struct fnetpeer *peer, wchar_t *id)
278 struct fnetpeerdatum *datum;
280 if((datum = finddatum(peer->fn, id)) == NULL)
282 for(i = 0; i < peer->dinum; i++)
284 if(peer->peerdi[i].datum == datum)
289 if((datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
290 free(peer->peerdi[i].data.str);
292 memmove(&peer->peerdi[i], &peer->peerdi[i + 1], sizeof(struct fnetpeerdi) * (peer->dinum - i));
293 putdatum(peer, datum);
296 struct fnetpeer *fnetaddpeer(struct fnetnode *fn, wchar_t *id, wchar_t *nick)
298 struct fnetpeer *new;
300 new = smalloc(sizeof(*new));
302 new->id = swcsdup(id);
303 new->nick = swcsdup(nick);
307 new->next = fn->peers;
309 if(fn->peers != NULL)
310 fn->peers->prev = new;
313 CBCHAINDOCB(fn, fnetnode_ac, fn, L"numpeers");
317 void fnetdelpeer(struct fnetpeer *peer)
321 if(peer->next != NULL)
322 peer->next->prev = peer->prev;
323 if(peer->prev != NULL)
324 peer->prev->next = peer->next;
325 if(peer->fn->peers == peer)
326 peer->fn->peers = peer->next;
327 peer->fn->numpeers--;
328 CBCHAINDOCB(peer->fn, fnetnode_ac, peer->fn, L"numpeers");
331 for(i = 0; i < peer->dinum; i++)
333 if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
334 free(peer->peerdi[i].data.str);
335 putdatum(peer, peer->peerdi[i].datum);
337 if(peer->peerdi != NULL)
342 struct fnetpeer *fnetfindpeer(struct fnetnode *fn, wchar_t *id)
344 struct fnetpeer *cur;
346 for(cur = fn->peers; (cur != NULL) && wcscmp(cur->id, id); cur = cur->next);
350 int fnetsetnick(struct fnetnode *fn, wchar_t *newnick)
354 if(fn->fnet->setnick != NULL)
355 ret = fn->fnet->setnick(fn, newnick);
360 if(fn->mynick != NULL)
362 fn->mynick = swcsdup(newnick);
367 int fnetsendchat(struct fnetnode *fn, int public, wchar_t *to, wchar_t *string)
369 if(fn->fnet->sendchat == NULL)
374 return(fn->fnet->sendchat(fn, public, to, string));
377 int fnetsearch(struct fnetnode *fn, struct search *srch, struct srchfnnlist *ln)
379 if(fn->fnet->search == NULL)
384 return(fn->fnet->search(fn, srch, ln));
387 void fnetsetname(struct fnetnode *fn, wchar_t *newname)
391 fn->name = swcsdup(newname);
392 CBCHAINDOCB(fn, fnetnode_ac, fn, L"name");
395 void fnetsetstate(struct fnetnode *fn, int newstate)
397 fn->state = newstate;
398 CBCHAINDOCB(fn, fnetnode_ac, fn, L"state");
401 struct fnet *findfnet(wchar_t *name)
405 for(fnet = networks; fnet != NULL; fnet = fnet->next)
407 if(!wcscmp(name, fnet->name))
413 struct fnetnode *fnetinitconnect(wchar_t *name, char *addr)
418 if((fnet = findfnet(name)) == NULL)
420 errno = EPROTONOSUPPORT;
425 if(netresolve(addr, (void (*)(struct sockaddr *, int, void *))resolvecb, fn) < 0)
430 void regfnet(struct fnet *fnet)
432 fnet->next = networks;
437 * Note on the chat string: Must be in UNIX text file format - that
438 * is, LF line endings. The filenet-specific code must see to it that
439 * any other kind of format is converted into that. In the future,
440 * certain control characters and escape sequences will be parsed by
441 * the client. Make sure that any filenet-specific code strips any
442 * such that aren't supposed to be in the protocol.
444 * Note on "name": This is supposed to be an identifier for the
445 * source. If the chat is a public message, set "public" to non-zero
446 * and "name" to whatever "chat room" name is appropriate for the
447 * fnetnode, but not NULL. If there is a "default" channel in this
448 * filenet, set "name" to the empty string. If the chat is a private
449 * message, name is ignored.
451 void fnethandlechat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *chat)
453 CBCHAINDOCB(fn, fnetnode_chat, fn, public, name, peer, chat);
456 static struct configvar myvars[] =
458 {CONF_VAR_INT, "srchwait", {.num = 15}},
459 {CONF_VAR_INT, "fntos", {.num = 0}},
460 {CONF_VAR_INT, "fnptos", {.num = 0}},
464 static struct module me =