123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- /*
- * clients
- * architecture clients/serveur guinness : clients list
- * Thomas Nemeth -- le 15 juin 2001
- *
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <strings.h>
- #include "defines.h"
- #include "printlog.h"
- #include "xmem.h"
- #include "guinnessd.h"
- #include "lists.h"
- #include "tools.h"
- #include "clients.h"
-
-
- extern Elt *clients_list;
-
- void add_client (userinfos *infos) {
- char *datas;
- int size;
-
- size = strlen (infos->nom);
- size += strlen (infos->prefb);
- size += strlen (infos->cxdate);
- size += strlen (infos->host);
- size += strlen (infos->ip);
- size += 5 * strlen (SEP) + 10;
-
- datas = xmalloc (size + 1);
-
- sprintf (datas, "%s%s%s%s%s%s%s%s%s%s%d",
- infos->nom, SEP,
- infos->prefb, SEP,
- infos->cxdate, SEP,
- infos->host, SEP,
- infos->ip, SEP,
- infos->port);
-
- add_elt (&clients_list, datas);
-
- #ifdef DEBUG
- printf ("Taille des éléments :\n"
- "nom : %d [%s]\n"
- "bière : %d [%s]\n"
- "date : %d [%s]\n"
- "hôte : %d [%s]\n"
- "ip : %d [%s]\n",
- strlen (infos->nom), infos->nom,
- strlen (infos->prefb), infos->prefb,
- strlen (infos->cxdate), infos->cxdate,
- strlen (infos->host), infos->host,
- strlen (infos->ip), infos->ip);
-
- printf ("Ajout des infos : size = %d -- [%s]\n", size, datas);
- #endif
-
- free (datas);
- }
-
-
- void remove_client (userinfos *infos) {
- remove_elt (&clients_list, infos->nom);
- }
-
-
- int rename_client (userinfos *infos, const char *new_nick) {
- if (exist_elt (clients_list, new_nick)) return FALSE;
- remove_elt (&clients_list, infos->nom);
- snprintf (infos->nom, MAXSTRLEN, "%s", new_nick);
- add_client (infos);
- return TRUE;
- }
-
-
- char *list_clients () {
- char *list;
- char *lstelt;
- char *rt;
- char text[MAXSTRLEN + 1];
- int size;
- int i;
-
- size = strlen ("Client(s) connecté(s) :\n");
- for (i = 0 ; i < get_nb_elts (clients_list) ; i++) {
- lstelt = elt_number (clients_list, i);
- rt = strstr (lstelt, SEP);
- strncpy (text, lstelt, rt - lstelt);
- text[rt - lstelt] = 0;
- size += 1 + strlen (text);
- }
-
- list = xmalloc ((size + 1) * sizeof (char));
-
- strcpy (list, "Client(s) connecté(s) :\n");
- for (i = 0 ; i < get_nb_elts (clients_list) ; i++) {
- lstelt = elt_number (clients_list, i);
- rt = strstr (lstelt, SEP);
- strncpy (text, lstelt, rt - lstelt);
- text[rt - lstelt] = 0;
- strcat (list, text);
- strcat (list, "\n");
- }
-
- return list;
- }
-
- char *infos_client (int admin, const char *nick) {
- char *datas;
- char *infos;
- char *token, *use, *saveptr;
- char *nom = NULL, *biere = NULL, *date = NULL;
- char *host = NULL, *ip = NULL, *port = NULL;
- int size = 0;
-
- infos = get_elt (clients_list, nick);
- if (! infos) {
- datas = xmalloc (strlen (nick) + strlen (" : pseudo inconnu !\n"));
- sprintf (datas, "%s : pseudo inconnu !\n", nick);
- return datas;
- }
-
- use = xstrdup (infos);
-
- /* nom */
- token = strtok_r (use, SEP, &saveptr);
- if (token) nom = xstrdup (token);
-
- /* bière */
- token = strtok_r (NULL, SEP, &saveptr);
- if (token) biere = xstrdup (token);
-
- /* date */
- token = strtok_r (NULL, SEP, &saveptr);
- if (token) date = xstrdup (token);
-
- /* hôte */
- token = strtok_r (NULL, SEP, &saveptr);
- if (token) host = xstrdup (token);
-
- /* ip */
- token = strtok_r (NULL, SEP, &saveptr);
- if (token) ip = xstrdup (token);
-
- /* port */
- token = strtok_r (NULL, SEP, &saveptr);
- if (token) port = xstrdup (token);
-
- size += strlen ("Pseudo : ");
- size += strlen ("Bière : ");
- size += strlen ("Connexion : ");
- size += 1 + (nom ? strlen (nom) : 1);
- size += 1 + (biere ? strlen (biere) : 1);
- size += 1 + (date ? strlen (date) : 1);
- if (admin) {
- size += strlen ("Hôte : ");
- size += strlen ("IP : ");
- size += strlen ("Port : ");
- size += 1 + (host ? strlen (host) : 1);
- size += 1 + (ip ? strlen (ip) : 1);
- size += 1 + (port ? strlen (port) : 1);
- }
-
- datas = xmalloc (size + 1);
-
- sprintf (datas,
- "Pseudo : %s\n"
- "Bière : %s\n"
- "Connexion : %s\n",
- nom ? nom : "-",
- biere ? biere : "-",
- date ? date : "-");
- if (admin)
- sprintf (datas, "%s"
- "Hôte : %s\n"
- "IP : %s\n"
- "Port : %s\n",
- datas,
- host ? host : "-",
- ip ? ip : "-",
- port ? port : "-");
-
- if (nom) free (nom);
- if (biere) free (biere);
- if (date) free (date);
- if (host) free (host);
- if (ip) free (ip);
- if (port) free (port);
-
- return datas;
- }
|