webfiles.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (C) 2000,2001 Florian Sander
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. void stats_setsock(int sock, int options) /* fabian's code */
  19. {
  20. (int) allocsock(sock, options);
  21. (int) fcntl(sock, F_SETFL, O_NONBLOCK);
  22. }
  23. static void write_new_webstats()
  24. {
  25. char *url, *file, *dir, *dir2, *buf, *buf2, *type, *today;
  26. char *bak, *bak2;
  27. globstats *gs;
  28. locstats *ls;
  29. FILE *f;
  30. int fd;
  31. Context;
  32. putlog(LOG_MISC, "*", "Writing static webfiles to %s...", webdir);
  33. file = nmalloc(strlen(webdir) + strlen("/index.html") + 1);
  34. sprintf(file, "%s/index.html", webdir);
  35. fd = creat(file, 0644);
  36. if (fd == -1) {
  37. putlog(LOG_MISC, "*", "ERROR writing webfiles! (%s)", file);
  38. nfree(file);
  39. return;
  40. }
  41. nfree(file);
  42. stats_setsock(fd, SOCK_NONSOCK);
  43. send_livestats(-fd, "/");
  44. killsock(fd);
  45. for (gs = sdata; gs; gs = gs->next) {
  46. putlog(LOG_MISC, "*", "... writing stats for %s ...", gs->chan);
  47. if (strchr(gs->chan, '[') || strchr(gs->chan, ']') || strchr(gs->chan, '^')) {
  48. putlog(LOG_MISC, "*", "... illegal character ('[', ']', or '^') in channame, skipping...");
  49. continue;
  50. }
  51. dir = nmalloc(strlen(webdir) + strlen(gs->chan) + 1 + 1);
  52. sprintf(dir, "%s/%s", webdir, (gs->chan[0] == '#') ? gs->chan + 1 : gs->chan);
  53. mkdir(dir, 0755);
  54. file = nmalloc(strlen(dir) + 11 + 1);
  55. sprintf(file, "%s/index.html", dir);
  56. fd = creat(file, 0644);
  57. if (fd == -1) {
  58. putlog(LOG_MISC, "*", "ERROR writing webfiles! (%s)", file);
  59. nfree(dir);
  60. nfree(file);
  61. return;
  62. }
  63. nfree(file);
  64. url = nmalloc(strlen(gs->chan) + 2 + 1);
  65. sprintf(url, "/%s/", (gs->chan[0] == '#') ? gs->chan + 1 : gs->chan);
  66. stats_setsock(fd, SOCK_NONSOCK);
  67. send_livestats(-fd, url);
  68. nfree(url);
  69. killsock(fd);
  70. dir2 = nmalloc(strlen(dir) + 5 + 1);
  71. sprintf(dir2, "%s/misc", dir);
  72. mkdir(dir2, 0755);
  73. file = nmalloc(strlen(dir2) + 11 + 1);
  74. sprintf(file, "%s/index.html", dir2);
  75. fd = creat(file, 0644);
  76. if (fd == -1) {
  77. putlog(LOG_MISC, "*", "ERROR writing webfiles! (%s)", file);
  78. nfree(dir);
  79. nfree(file);
  80. nfree(dir2);
  81. return;
  82. }
  83. nfree(file);
  84. url = nmalloc(9 + strlen(gs->chan) + 1);
  85. sprintf(url, "/%s/misc/", (gs->chan[0] == '#') ? gs->chan + 1 : gs->chan);
  86. stats_setsock(fd, SOCK_NONSOCK);
  87. send_livestats(-fd, url);
  88. nfree(url);
  89. killsock(fd);
  90. nfree(dir2);
  91. dir2 = nmalloc(6 + strlen(dir) + 1);
  92. sprintf(dir2, "%s/top", dir);
  93. mkdir(dir2, 0755);
  94. file = nmalloc(strlen(dir2) + 11 + 1);
  95. sprintf(file, "%s/index.html", dir2);
  96. fd = creat(file, 0644);
  97. if (fd == -1) {
  98. putlog(LOG_MISC, "*", "ERROR writing webfiles! (%s)", file);
  99. nfree(dir);
  100. nfree(file);
  101. nfree(dir2);
  102. return;
  103. }
  104. nfree(file);
  105. url = nmalloc(8 + strlen(gs->chan) + 1);
  106. sprintf(url, "/%s/top/", (gs->chan[0] == '#') ? gs->chan + 1 : gs->chan);
  107. stats_setsock(fd, SOCK_NONSOCK);
  108. send_livestats(-fd, url);
  109. nfree(url);
  110. killsock(fd);
  111. nfree(dir2);
  112. buf = nmalloc(26 + 1);
  113. bak = buf;
  114. strcpy(buf, "total daily weekly monthly");
  115. while (buf[0]) {
  116. today = newsplit(&buf);
  117. dir2 = nmalloc(9 + strlen(dir) + strlen(today) + 1);
  118. sprintf(dir2, "%s/top/%s", dir, today);
  119. mkdir(dir2, 0755);
  120. nfree(dir2);
  121. buf2 = nmalloc(9 + strlen(webstats) + 1);
  122. sprintf(buf2, "%s graphs", webstats);
  123. bak2 = buf2;
  124. while (buf2[0]) {
  125. type = newsplit(&buf2);
  126. dir2 = nmalloc(12 + strlen(dir) + strlen(today) + strlen(type) + 1);
  127. sprintf(dir2, "%s/top/%s/%s", dir, today, type);
  128. mkdir(dir2, 0755);
  129. file = nmalloc(13 + strlen(dir2) + 1);
  130. sprintf(file, "%s/index.html", dir2);
  131. fd = creat(file, 0644);
  132. if (fd == -1) {
  133. putlog(LOG_MISC, "*", "ERROR writing webfiles! (%s)", file);
  134. nfree(dir);
  135. nfree(file);
  136. nfree(dir2);
  137. nfree(bak);
  138. nfree(bak2);
  139. return;
  140. }
  141. nfree(file);
  142. url = nmalloc(14 + strlen(gs->chan) + strlen(today) + strlen(type) + 1);
  143. sprintf(url, "/%s/top/%s/%s/", (gs->chan[0] == '#') ? gs->chan + 1 : gs->chan, today, type);
  144. stats_setsock(fd, SOCK_NONSOCK);
  145. send_livestats(-fd, url);
  146. killsock(fd);
  147. nfree(url);
  148. nfree(dir2);
  149. }
  150. nfree(bak2);
  151. }
  152. nfree(bak);
  153. dir2 = nmalloc(8 + strlen(dir) + 1);
  154. sprintf(dir2, "%s/users", dir);
  155. mkdir(dir2, 0755);
  156. file = nmalloc(13 + strlen(dir2) + 1);
  157. sprintf(file, "%s/index.html", dir2);
  158. fd = creat(file, 0644);
  159. if (fd == -1) {
  160. putlog(LOG_MISC, "*", "ERROR writing webfiles! (%s)", file);
  161. nfree(dir);
  162. nfree(file);
  163. nfree(dir2);
  164. return;
  165. }
  166. nfree(file);
  167. url = nmalloc(10 + strlen(gs->chan) + 1 );
  168. sprintf(url, "/%s/users/", (gs->chan[0] == '#') ? gs->chan + 1 : gs->chan);
  169. stats_setsock(fd, SOCK_NONSOCK);
  170. send_livestats(-fd, url);
  171. killsock(fd);
  172. nfree(url);
  173. nfree(dir2);
  174. dir2 = nmalloc(9 + strlen(dir) + 1);
  175. sprintf(dir2, "%s/onchan", dir);
  176. mkdir(dir2, 0755);
  177. file = nmalloc(13 + strlen(dir2) + 1);
  178. sprintf(file, "%s/index.html", dir2);
  179. fd = creat(file, 0644);
  180. if (fd == -1) {
  181. putlog(LOG_MISC, "*", "ERROR writing webfiles! (%s)", file);
  182. nfree(dir);
  183. nfree(file);
  184. nfree(dir2);
  185. return;
  186. }
  187. nfree(file);
  188. url = nmalloc(11 + strlen(gs->chan) + 1 );
  189. sprintf(url, "/%s/onchan/", (gs->chan[0] == '#') ? gs->chan + 1 : gs->chan);
  190. stats_setsock(fd, SOCK_NONSOCK);
  191. send_livestats(-fd, url);
  192. killsock(fd);
  193. nfree(url);
  194. nfree(dir2);
  195. setslglobs(gs->chan, gs->peak[S_TOTAL], countstatmembers(gs), gs->started);
  196. for (ls = gs->local; ls; ls = ls->next) { /* WARNING! DANGEROUS LOOP! If the sorting changes, you can get trapped in an endless loop! */
  197. slgloblocstats = ls;
  198. dir2 = nmalloc(11 + strlen(dir) + strlen(ls->user) + 1);
  199. sprintf(dir2, "%s/users/%s", dir, ls->user);
  200. mkdir(dir2, 0755);
  201. file = nmalloc(13 + strlen(dir2) + 1);
  202. sprintf(file, "%s/index.html", dir2);
  203. f = fopen(file, "w");
  204. chmod(file, 0644);
  205. if (!f) {
  206. putlog(LOG_MISC, "*", "ERROR writing webfiles! (%s)", file);
  207. nfree(dir);
  208. nfree(file);
  209. nfree(dir2);
  210. return;
  211. }
  212. nfree(file);
  213. fprintf(f, "<html>\n<head>\n<title>%s</title>\n%s\n</head>\n%s\n", SLSTATICTITLE, SLCSS, bodytag);
  214. fprintf(f, "%s\n", SLSTATICBODY);
  215. fprintf(f, "<br><br><hr>\n");
  216. fprintf(f, "<table width=100%% border=0>\n");
  217. fprintf(f, "<tr><td align=center><table width=100%% border=0><tr>\n");
  218. fprintf(f, "<td width=25%% align=center><font size=-1><a href=\"../\">%s</a></font></td>\n", SLOTHERUSERS);
  219. fprintf(f, "<td width=25%% align=center><font size=-1><a href=\"../../top/total/words/\">%s</a></font></td>\n", SLTOP);
  220. fprintf(f, "<td width=25%% align=center><font size=-1><a href=\"../../misc/\">%s</a></font></td>\n", SLMISCSTATS);
  221. fprintf(f, "<td width=25%% align=center><font size=-1><a href=\"../../../\">%s</a></font></td>\n", SLOTHERCHANS);
  222. fprintf(f, "</tr></table></td></tr>\n");
  223. nfree(dir2);
  224. fclose(f);
  225. }
  226. nfree(dir);
  227. }
  228. putlog(LOG_MISC, "*", "... done.");
  229. }