Comment servir des pintes de Guinness "over Internet" ;) Reprise/Fork d'un très ancien code d'un pilier de f.m.b.l : http://tnemeth.free.fr/projets/guinness-server.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

clients.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * clients
  3. * architecture clients/serveur guinness : clients list
  4. * Thomas Nemeth -- le 15 juin 2001
  5. *
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include <strings.h>
  12. #include "defines.h"
  13. #include "printlog.h"
  14. #include "xmem.h"
  15. #include "guinnessd.h"
  16. #include "lists.h"
  17. #include "tools.h"
  18. #include "clients.h"
  19. extern Elt *clients_list;
  20. void add_client (userinfos *infos) {
  21. char *datas;
  22. int size;
  23. size = strlen (infos->nom);
  24. size += strlen (infos->prefb);
  25. size += strlen (infos->cxdate);
  26. size += strlen (infos->host);
  27. size += strlen (infos->ip);
  28. size += 5 * strlen (SEP) + 10;
  29. datas = xmalloc (size + 1);
  30. sprintf (datas, "%s%s%s%s%s%s%s%s%s%s%d",
  31. infos->nom, SEP,
  32. infos->prefb, SEP,
  33. infos->cxdate, SEP,
  34. infos->host, SEP,
  35. infos->ip, SEP,
  36. infos->port);
  37. add_elt (&clients_list, datas);
  38. #ifdef DEBUG
  39. printf ("Taille des elements :\n"
  40. "nom : %d [%s]\n"
  41. "biere : %d [%s]\n"
  42. "date : %d [%s]\n"
  43. "hote : %d [%s]\n"
  44. "ip : %d [%s]\n",
  45. strlen (infos->nom), infos->nom,
  46. strlen (infos->prefb), infos->prefb,
  47. strlen (infos->cxdate), infos->cxdate,
  48. strlen (infos->host), infos->host,
  49. strlen (infos->ip), infos->ip);
  50. printf ("Ajout des infos : size = %d -- [%s]\n", size, datas);
  51. #endif
  52. free (datas);
  53. }
  54. void remove_client (userinfos *infos) {
  55. remove_elt (&clients_list, infos->nom);
  56. }
  57. int rename_client (userinfos *infos, const char *new_nick) {
  58. if (exist_elt (clients_list, new_nick)) return FALSE;
  59. remove_elt (&clients_list, infos->nom);
  60. snprintf (infos->nom, MAXSTRLEN, "%s", new_nick);
  61. add_client (infos);
  62. return TRUE;
  63. }
  64. char *list_clients () {
  65. char *list;
  66. char *lstelt;
  67. char *rt;
  68. char text[MAXSTRLEN + 1];
  69. int size;
  70. int i;
  71. size = strlen ("Client(s) connecte(s) :\n");
  72. for (i = 0 ; i < get_nb_elts (clients_list) ; i++) {
  73. lstelt = elt_number (clients_list, i);
  74. rt = strstr (lstelt, SEP);
  75. strncpy (text, lstelt, rt - lstelt);
  76. text[rt - lstelt] = 0;
  77. size += 1 + strlen (text);
  78. }
  79. list = xmalloc ((size + 1) * sizeof (char));
  80. strcpy (list, "Client(s) connecte(s) :\n");
  81. for (i = 0 ; i < get_nb_elts (clients_list) ; i++) {
  82. lstelt = elt_number (clients_list, i);
  83. rt = strstr (lstelt, SEP);
  84. strncpy (text, lstelt, rt - lstelt);
  85. text[rt - lstelt] = 0;
  86. strcat (list, text);
  87. strcat (list, "\n");
  88. }
  89. return list;
  90. }
  91. char *infos_client (int admin, const char *nick) {
  92. char *datas;
  93. char *infos;
  94. char *token, *use, *saveptr;
  95. char *nom = NULL, *biere = NULL, *date = NULL;
  96. char *host = NULL, *ip = NULL, *port = NULL;
  97. int size = 0;
  98. infos = get_elt (clients_list, nick);
  99. if (! infos) {
  100. datas = xmalloc (strlen (nick) + strlen (" : pseudo inconnu !\n"));
  101. sprintf (datas, "%s : pseudo inconnu !\n", nick);
  102. return datas;
  103. }
  104. use = xstrdup (infos);
  105. /* nom */
  106. token = strtok_r (use, SEP, &saveptr);
  107. if (token) nom = xstrdup (token);
  108. /* biere */
  109. token = strtok_r (NULL, SEP, &saveptr);
  110. if (token) biere = xstrdup (token);
  111. /* date */
  112. token = strtok_r (NULL, SEP, &saveptr);
  113. if (token) date = xstrdup (token);
  114. /* hote */
  115. token = strtok_r (NULL, SEP, &saveptr);
  116. if (token) host = xstrdup (token);
  117. /* ip */
  118. token = strtok_r (NULL, SEP, &saveptr);
  119. if (token) ip = xstrdup (token);
  120. /* port */
  121. token = strtok_r (NULL, SEP, &saveptr);
  122. if (token) port = xstrdup (token);
  123. size += strlen ("Pseudo : ");
  124. size += strlen ("Biere : ");
  125. size += strlen ("Connexion : ");
  126. size += 1 + (nom ? strlen (nom) : 1);
  127. size += 1 + (biere ? strlen (biere) : 1);
  128. size += 1 + (date ? strlen (date) : 1);
  129. if (admin) {
  130. size += strlen ("Hote : ");
  131. size += strlen ("IP : ");
  132. size += strlen ("Port : ");
  133. size += 1 + (host ? strlen (host) : 1);
  134. size += 1 + (ip ? strlen (ip) : 1);
  135. size += 1 + (port ? strlen (port) : 1);
  136. }
  137. datas = xmalloc (size + 1);
  138. sprintf (datas,
  139. "Pseudo : %s\n"
  140. "Biere : %s\n"
  141. "Connexion : %s\n",
  142. nom ? nom : "-",
  143. biere ? biere : "-",
  144. date ? date : "-");
  145. if (admin)
  146. sprintf (datas, "%s"
  147. "Hote : %s\n"
  148. "IP : %s\n"
  149. "Port : %s\n",
  150. datas,
  151. host ? host : "-",
  152. ip ? ip : "-",
  153. port ? port : "-");
  154. if (nom) free (nom);
  155. if (biere) free (biere);
  156. if (date) free (date);
  157. if (host) free (host);
  158. if (ip) free (ip);
  159. if (port) free (port);
  160. return datas;
  161. }