4
0

datahandling.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  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. static void incrstats(char *user, char *chan, int type, int value, int set)
  19. {
  20. globstats *gs, *gs2;
  21. locstats *ls, *ls2;
  22. int i, ii;
  23. if (type >= TOTAL_TYPES)
  24. return;
  25. if (!user) {
  26. debug0("Stats.mod: incrstats(..) Ups, user is NULL!");
  27. return;
  28. }
  29. if (!chan) {
  30. debug0("Stats.mod: incrstats(..) Ups, chan is NULL!");
  31. return;
  32. }
  33. for (gs = sdata; gs; gs = gs->next) {
  34. if (!strcasecmp(chan, gs->chan))
  35. break;
  36. }
  37. if (!gs) {
  38. gs2 = sdata;
  39. while (gs2 && gs2->next)
  40. gs2 = gs2->next;
  41. gs = nmalloc(sizeof(globstats));
  42. gs->started = now;
  43. gs->peak[S_TOTAL] = gs->peak[S_DAILY] = gs->peak[S_WEEKLY] = gs->peak[S_MONTHLY] = 0;
  44. for (i = 0; i < 24; i++) {
  45. gs->users[S_USERSUM][i] = 0;
  46. gs->users[S_USERCOUNTS][i] = -1;
  47. }
  48. for (i = 0; i < (TOTAL_TYPES + TOTAL_SPECIAL_TYPES); i++)
  49. gs->slocal[S_TOTAL][i] = gs->slocal[S_DAILY][i] = gs->slocal[S_WEEKLY][i] = gs->slocal[S_MONTHLY][i] = NULL;
  50. gs->next = NULL;
  51. gs->local = NULL;
  52. gs->topics = NULL;
  53. gs->hosts = NULL;
  54. gs->urls = NULL;
  55. gs->log = gs->lastlog = NULL;
  56. gs->log_length = 0;
  57. gs->kicks = NULL;
  58. gs->words = NULL;
  59. gs->chan = nmalloc(strlen(chan) + 1);
  60. strcpy(gs->chan, chan);
  61. if (gs2)
  62. gs2->next = gs;
  63. else
  64. sdata = gs;
  65. }
  66. for (ls = gs->local; ls; ls = ls->next) {
  67. if (!strcasecmp(ls->user, user))
  68. break;
  69. }
  70. if (type == T_GSTARTED) {
  71. gs->started = value;
  72. return;
  73. }
  74. if (type == T_PEAK) {
  75. gs->peak[set] = value;
  76. return;
  77. }
  78. if (!ls) {
  79. ls2 = gs->local;
  80. while (ls2 && ls2->next)
  81. ls2 = ls2->next;
  82. ls = nmalloc(sizeof(locstats));
  83. ls->started = now;
  84. ls->next = NULL;
  85. ls->words = NULL;
  86. ls->tree = NULL;
  87. ls->quotes = NULL;
  88. ls->quotefr = 0;
  89. ls->flag = 0;
  90. for (i = 0; i < TOTAL_TYPES; i++) {
  91. ls->values[S_TOTAL][i] = 0;
  92. ls->values[S_TODAY][i] = 0;
  93. ls->values[S_WEEKLY][i] = 0;
  94. ls->values[S_MONTHLY][i] = 0;
  95. }
  96. ls->user = nmalloc(strlen(user) + 1);
  97. strcpy(ls->user, user);
  98. // we'll initialize this later, if it's needed
  99. ls->u = NULL;
  100. if (ls2)
  101. ls2->next = ls;
  102. else
  103. gs->local = ls;
  104. for (i = 0; i < (TOTAL_TYPES + TOTAL_SPECIAL_TYPES); i++)
  105. ls->snext[S_TOTAL][i] = ls->snext[S_DAILY][i] = ls->snext[S_WEEKLY][i] = ls->snext[S_MONTHLY][i] = NULL;
  106. for (i = 0; i < 4; i++) {
  107. for (ii = 0; ii < (TOTAL_TYPES + TOTAL_SPECIAL_TYPES); ii++) {
  108. ls2 = gs->slocal[i][ii];
  109. while (ls2 && ls2->snext[i][ii])
  110. ls2 = ls2->snext[i][ii];
  111. if (ls2)
  112. ls2->snext[i][ii] = ls;
  113. else
  114. gs->slocal[i][ii] = ls;
  115. }
  116. }
  117. }
  118. if (type == T_LSTARTED)
  119. ls->started = value;
  120. else {
  121. if (set > 0)
  122. ls->values[set - 1][type] = value;
  123. else if (set < 0)
  124. ls->values[(set * (-1)) - 1][type] += value;
  125. else {
  126. ls->values[S_TOTAL][type] += value;
  127. ls->values[S_TODAY][type] += value;
  128. ls->values[S_WEEKLY][type] += value;
  129. ls->values[S_MONTHLY][type] += value;
  130. }
  131. }
  132. }
  133. static void nincrstats(locstats *ls, int type, int value)
  134. {
  135. ls->values[S_TOTAL][type] += value;
  136. ls->values[S_TODAY][type] += value;
  137. ls->values[S_WEEKLY][type] += value;
  138. ls->values[S_MONTHLY][type] += value;
  139. }
  140. static locstats *initstats(char *chan, char *user)
  141. {
  142. globstats *gs, *gs2;
  143. locstats *ls, *ls2;
  144. int i, ii;
  145. gs = sdata;
  146. while (gs) {
  147. if (!rfc_casecmp(gs->chan, chan))
  148. break;
  149. gs = gs->next;
  150. }
  151. if (!gs) {
  152. gs2 = sdata;
  153. while (gs2 && gs2->next)
  154. gs2 = gs2->next;
  155. gs = nmalloc(sizeof(globstats));
  156. gs->started = now;
  157. gs->peak[S_TOTAL] = gs->peak[S_DAILY] = gs->peak[S_WEEKLY] = gs->peak[S_MONTHLY] = 0;
  158. for (i = 0; i < 24; i++) {
  159. gs->users[S_USERSUM][i] = 0;
  160. gs->users[S_USERCOUNTS][i] = -1;
  161. }
  162. gs->next = NULL;
  163. gs->local = NULL;
  164. gs->words = NULL;
  165. gs->topics = NULL;
  166. gs->hosts = NULL;
  167. gs->urls = NULL;
  168. gs->log = gs->lastlog = NULL;
  169. gs->log_length = 0;
  170. gs->kicks = NULL;
  171. gs->chan = nmalloc(strlen(chan) + 1);
  172. strcpy(gs->chan, chan);
  173. for (i = 0; i < (TOTAL_TYPES + TOTAL_SPECIAL_TYPES); i++)
  174. gs->slocal[S_TOTAL][i] = gs->slocal[S_DAILY][i] = gs->slocal[S_WEEKLY][i] = gs->slocal[S_MONTHLY][i] = NULL;
  175. if (gs2)
  176. gs2->next = gs;
  177. else
  178. sdata = gs;
  179. }
  180. for (ls = gs->local; ls; ls = ls->next) {
  181. if (!rfc_casecmp(ls->user, user))
  182. return ls;
  183. }
  184. if (!ls) {
  185. ls2 = gs->local;
  186. while (ls2 && ls2->next)
  187. ls2 = ls2->next;
  188. ls = nmalloc(sizeof(locstats));
  189. ls->started = now;
  190. ls->next = NULL;
  191. ls->words = NULL;
  192. ls->tree = NULL;
  193. ls->quotes = NULL;
  194. ls->quotefr = 0;
  195. ls->flag = 0;
  196. for (i = 0; i < TOTAL_TYPES; i++) {
  197. ls->values[S_TOTAL][i] = 0;
  198. ls->values[S_TODAY][i] = 0;
  199. ls->values[S_WEEKLY][i] = 0;
  200. ls->values[S_MONTHLY][i] = 0;
  201. }
  202. ls->user = nmalloc(strlen(user) + 1);
  203. strcpy(ls->user, user);
  204. // we'll initialize this later, if it's needed
  205. ls->u = NULL;
  206. if (ls2)
  207. ls2->next = ls;
  208. else
  209. gs->local = ls;
  210. for (i = 0; i < (TOTAL_TYPES + TOTAL_SPECIAL_TYPES); i++)
  211. ls->snext[S_TOTAL][i] = ls->snext[S_DAILY][i] = ls->snext[S_WEEKLY][i] = ls->snext[S_MONTHLY][i] = NULL;
  212. for (i = 0; i < 4; i++) {
  213. for (ii = 0; ii < (TOTAL_TYPES + TOTAL_SPECIAL_TYPES); ii++) {
  214. ls2 = gs->slocal[i][ii];
  215. while (ls2 && ls2->snext[i][ii])
  216. ls2 = ls2->snext[i][ii];
  217. if (ls2)
  218. ls2->snext[i][ii] = ls;
  219. else
  220. gs->slocal[i][ii] = ls;
  221. }
  222. }
  223. }
  224. return ls;
  225. }
  226. static int getstats(char *user, char *chan, char *type, int today)
  227. {
  228. struct stats_global *gs = sdata;
  229. struct stats_local *ls;
  230. int itype;
  231. while (gs) {
  232. ls = gs->local;
  233. if (!strcasecmp(gs->chan, chan)) {
  234. while (ls) {
  235. if (!strcasecmp(ls->user, user)) {
  236. itype = typetoi(type);
  237. if (itype >= 0)
  238. return ls->values[today][itype];
  239. }
  240. ls = ls->next;
  241. }
  242. }
  243. gs = gs->next;
  244. }
  245. return 0;
  246. }
  247. static void sortstats(struct stats_global *gs, int itype, int today)
  248. {
  249. int again = 1;
  250. struct stats_local *last, *p, *c, *n;
  251. int a, b, pitype;
  252. Context;
  253. again = 1;
  254. last = NULL;
  255. if (itype < 0) {
  256. // switch to the special sorting function
  257. switch (itype) {
  258. case T_WPL:
  259. sortstats_wpl(gs, today);
  260. break;
  261. case T_VOCABLES:
  262. sortstats_vocables(gs, today);
  263. break;
  264. case T_WORD:
  265. sortstats_word(gs, today);
  266. break;
  267. case T_IDLE:
  268. sortstats_idle(gs, today);
  269. break;
  270. default:
  271. debug1("Missing sorting algorithm for \"%d\"!!!", itype);
  272. }
  273. return;
  274. }
  275. // if (itype < 0) pitype = (TOTAL_TYPES - 1) + (itype * -1);
  276. // not needed here
  277. pitype = itype;
  278. while ((gs->slocal[today][pitype] != last) && (again)) {
  279. p = NULL;
  280. c = gs->slocal[today][pitype];
  281. n = c->snext[today][pitype];
  282. again = 0;
  283. while (n != last) {
  284. if (!c || !n)
  285. a = b = 0;
  286. else {
  287. a = c->values[today][itype];
  288. b = n->values[today][itype];
  289. }
  290. if (a < b) {
  291. again = 1;
  292. c->snext[today][pitype] = n->snext[today][pitype];
  293. n->snext[today][pitype] = c;
  294. if (p == NULL)
  295. gs->slocal[today][pitype] = n;
  296. else
  297. p->snext[today][pitype] = n;
  298. }
  299. p = c;
  300. c = n;
  301. n = n->snext[today][pitype];
  302. }
  303. last = c;
  304. }
  305. Context;
  306. return;
  307. }
  308. static void sortstats_wpl(struct stats_global *gs, int today)
  309. {
  310. int again = 1;
  311. struct stats_local *last, *p, *c, *n;
  312. int a, b, pitype;
  313. Context;
  314. again = 1;
  315. last = NULL;
  316. pitype = (T_WPL * (-1)) + TOTAL_TYPES - 1;
  317. while ((gs->slocal[today][pitype] != last) && (again)) {
  318. p = NULL;
  319. c = gs->slocal[today][pitype];
  320. n = c->snext[today][pitype];
  321. again = 0;
  322. while (n != last) {
  323. if (!c || !n)
  324. a = b = 0;
  325. else {
  326. if (c->values[today][T_LINES])
  327. a = (int) (((float) c->values[today][T_WORDS] / (float) c->values[today][T_LINES]) * 100.0);
  328. else
  329. a = 0;
  330. if (n->values[today][T_LINES])
  331. b = (int) (((float) n->values[today][T_WORDS] / (float) n->values[today][T_LINES]) * 100.0);
  332. else
  333. b = 0;
  334. }
  335. if (a < b) {
  336. again = 1;
  337. c->snext[today][pitype] = n->snext[today][pitype];
  338. n->snext[today][pitype] = c;
  339. if (p == NULL)
  340. gs->slocal[today][pitype] = n;
  341. else
  342. p->snext[today][pitype] = n;
  343. }
  344. p = c;
  345. c = n;
  346. n = n->snext[today][pitype];
  347. }
  348. last = c;
  349. }
  350. Context;
  351. return;
  352. }
  353. static void sortstats_vocables(struct stats_global *gs, int today)
  354. {
  355. int again = 1;
  356. struct stats_local *last, *p, *c, *n;
  357. int a, b, pitype;
  358. Context;
  359. again = 1;
  360. last = NULL;
  361. countvocables(gs);
  362. pitype = (T_VOCABLES * (-1)) + TOTAL_TYPES - 1;
  363. while ((gs->slocal[today][pitype] != last) && (again)) {
  364. p = NULL;
  365. c = gs->slocal[today][pitype];
  366. n = c->snext[today][pitype];
  367. again = 0;
  368. while (n != last) {
  369. if (!c || !n)
  370. a = b = 0;
  371. else {
  372. a = c->vocables;
  373. b = n->vocables;
  374. }
  375. if (a < b) {
  376. again = 1;
  377. c->snext[today][pitype] = n->snext[today][pitype];
  378. n->snext[today][pitype] = c;
  379. if (p == NULL)
  380. gs->slocal[today][pitype] = n;
  381. else
  382. p->snext[today][pitype] = n;
  383. }
  384. p = c;
  385. c = n;
  386. n = n->snext[today][pitype];
  387. }
  388. last = c;
  389. }
  390. Context;
  391. return;
  392. }
  393. static void sortstats_word(struct stats_global *gs, int today)
  394. {
  395. int again = 1;
  396. struct stats_local *last, *p, *c, *n;
  397. int a, b, pitype;
  398. Context;
  399. again = 1;
  400. last = NULL;
  401. pitype = (T_WORD * (-1)) + TOTAL_TYPES - 1;
  402. while ((gs->slocal[today][pitype] != last) && (again)) {
  403. p = NULL;
  404. c = gs->slocal[today][pitype];
  405. n = c->snext[today][pitype];
  406. again = 0;
  407. while (n != last) {
  408. if (!c || !n)
  409. a = b = 0;
  410. else {
  411. if (c->word)
  412. a = c->word->nr;
  413. else
  414. a = 0;
  415. if (n->word)
  416. b = n->word->nr;
  417. else
  418. b = 0;
  419. }
  420. if (a < b) {
  421. again = 1;
  422. c->snext[today][pitype] = n->snext[today][pitype];
  423. n->snext[today][pitype] = c;
  424. if (p == NULL)
  425. gs->slocal[today][pitype] = n;
  426. else
  427. p->snext[today][pitype] = n;
  428. }
  429. p = c;
  430. c = n;
  431. n = n->snext[today][pitype];
  432. }
  433. last = c;
  434. }
  435. Context;
  436. return;
  437. }
  438. // sort stats by idle-factor (minutes/lines)
  439. static void sortstats_idle(struct stats_global *gs, int today)
  440. {
  441. int again = 1;
  442. struct stats_local *last, *p, *c, *n;
  443. int a, b, pitype;
  444. Context;
  445. again = 1;
  446. last = NULL;
  447. pitype = (T_IDLE * (-1)) + TOTAL_TYPES - 1;
  448. while ((gs->slocal[today][pitype] != last) && (again)) {
  449. p = NULL;
  450. c = gs->slocal[today][pitype];
  451. n = c->snext[today][pitype];
  452. again = 0;
  453. while (n != last) {
  454. if (!c || !n)
  455. a = b = 0;
  456. else {
  457. if (c->values[today][T_LINES])
  458. a = (int) (((float) c->values[today][T_MINUTES] / (float) c->values[today][T_LINES]) * 100.0);
  459. else
  460. a = 0;
  461. if (n->values[today][T_LINES])
  462. b = (int) (((float) n->values[today][T_MINUTES] / (float) n->values[today][T_LINES]) * 100.0);
  463. else
  464. b = 0;
  465. }
  466. if (a < b) {
  467. again = 1;
  468. c->snext[today][pitype] = n->snext[today][pitype];
  469. n->snext[today][pitype] = c;
  470. if (p == NULL)
  471. gs->slocal[today][pitype] = n;
  472. else
  473. p->snext[today][pitype] = n;
  474. }
  475. p = c;
  476. c = n;
  477. n = n->snext[today][pitype];
  478. }
  479. last = c;
  480. }
  481. Context;
  482. return;
  483. }
  484. static void countvocables(globstats *gs)
  485. {
  486. locstats *ls;
  487. wordstats *ws;
  488. for (ls = gs->local; ls; ls = ls->next) {
  489. ls->vocables = 0;
  490. for (ws = ls->words; ws; ws = ws->next)
  491. ls->vocables++;
  492. }
  493. }
  494. static void sortwordstats(locstats *ls, globstats *gs)
  495. {
  496. int again = 1;
  497. wordstats *last, *p, *c, *n, *tmp;
  498. int a, b;
  499. Context;
  500. again = 1;
  501. last = NULL;
  502. if (ls)
  503. tmp = ls->words;
  504. else
  505. tmp = gs->words;
  506. while ((tmp != last) && (again)) {
  507. p = NULL;
  508. if (ls)
  509. c = ls->words;
  510. else
  511. c = gs->words;
  512. n = c->next;
  513. again = 0;
  514. while (n != last) {
  515. if (!c || !n)
  516. a = b = 0;
  517. else {
  518. a = c->nr;
  519. b = n->nr;
  520. }
  521. if (a < b) {
  522. again = 1;
  523. c->next = n->next;
  524. n->next = c;
  525. if (p == NULL) {
  526. if (ls)
  527. ls->words = n;
  528. else
  529. gs->words = n;
  530. tmp = n;
  531. } else
  532. p->next = n;
  533. }
  534. p = c;
  535. c = n;
  536. n = n->next;
  537. }
  538. last = c;
  539. }
  540. Context;
  541. return;
  542. }
  543. static void sorthosts(struct stats_global *gs)
  544. {
  545. int again = 1;
  546. hoststr *last, *p, *c, *n;
  547. int a, b;
  548. Context;
  549. again = 1;
  550. last = NULL;
  551. while ((gs->hosts != last) && (again)) {
  552. p = NULL;
  553. c = gs->hosts;
  554. n = c->next;
  555. again = 0;
  556. while (n != last) {
  557. if (!c || !n)
  558. a = b = 0;
  559. else {
  560. a = c->nr;
  561. b = n->nr;
  562. }
  563. if (a < b) {
  564. again = 1;
  565. c->next = n->next;
  566. n->next = c;
  567. if (p == NULL)
  568. gs->hosts = n;
  569. else
  570. p->next = n;
  571. }
  572. p = c;
  573. c = n;
  574. n = n->next;
  575. }
  576. last = c;
  577. }
  578. Context;
  579. return;
  580. }
  581. // typetoi(): returns the index of a stat-type
  582. static int typetoi(char *type)
  583. {
  584. if (!strcasecmp(type, "lstarted"))
  585. return T_LSTARTED;
  586. else if (!strcasecmp(type, "gstarted"))
  587. return T_GSTARTED;
  588. else if (!strcasecmp(type, "words"))
  589. return T_WORDS;
  590. else if (!strcasecmp(type, "letters"))
  591. return T_LETTERS;
  592. else if (!strcasecmp(type, "minutes"))
  593. return T_MINUTES;
  594. else if (!strcasecmp(type, "topics"))
  595. return T_TOPICS;
  596. else if (!strcasecmp(type, "lines"))
  597. return T_LINES;
  598. else if (!strcasecmp(type, "actions"))
  599. return T_ACTIONS;
  600. else if (!strcasecmp(type, "kicks"))
  601. return T_KICKS;
  602. else if (!strcasecmp(type, "modes"))
  603. return T_MODES;
  604. else if (!strcasecmp(type, "bans"))
  605. return T_BANS;
  606. else if (!strcasecmp(type, "nicks"))
  607. return T_NICKS;
  608. else if (!strcasecmp(type, "joins"))
  609. return T_JOINS;
  610. else if (!strcasecmp(type, "smileys"))
  611. return T_SMILEYS;
  612. else if (!strcasecmp(type, "questions"))
  613. return T_QUESTIONS;
  614. else if (!strcasecmp(type, "wpl"))
  615. return T_WPL;
  616. else if (!strcasecmp(type, "w/l"))
  617. return T_WPL;
  618. else if (!strcasecmp(type, "word"))
  619. return T_WORD;
  620. else if (!strcasecmp(type, "vocables"))
  621. return T_VOCABLES;
  622. else if (!strcasecmp(type, "started"))
  623. return T_LSTARTED;
  624. else if (!strcasecmp(type, "quote"))
  625. return T_QUOTE;
  626. else if (!strcasecmp(type, "idle"))
  627. return T_IDLE;
  628. else {
  629. debug1("Stats.mod: Unknown stat type: %s", type);
  630. return T_ERROR;
  631. }
  632. }
  633. static locstats *findlocstats(char *chan, char *user)
  634. {
  635. globstats *gl;
  636. locstats *ll;
  637. for (gl = sdata; gl; gl = gl->next) {
  638. if (!rfc_casecmp(gl->chan, chan))
  639. break;
  640. }
  641. if (!gl)
  642. return NULL;
  643. for (ll = gl->local; ll; ll = ll->next) {
  644. if (!rfc_casecmp(ll->user, user))
  645. return ll;
  646. }
  647. return NULL;
  648. }
  649. static globstats *findglobstats(char *chan)
  650. {
  651. globstats *gl;
  652. for (gl = sdata; gl; gl = gl->next) {
  653. if (!rfc_casecmp(gl->chan, chan))
  654. break;
  655. }
  656. return gl;
  657. }
  658. static void write_stats()
  659. {
  660. char s[125];
  661. FILE *f;
  662. struct stats_global *gs;
  663. struct stats_local *ls;
  664. struct stats_userlist *u;
  665. struct stats_hostlist *h;
  666. int i;
  667. Context;
  668. if (!statsfile[0])
  669. return;
  670. sprintf(s, "%s~new", statsfile);
  671. f = fopen(s, "w");
  672. chmod(s, statsfilemode);
  673. if (f == NULL) {
  674. putlog(LOG_MISC, "*", "ERROR writing stats file.");
  675. return;
  676. }
  677. fprintf(f, "@ # Statistics from %s.\n", botnetnick);
  678. fprintf(f, "@ filever 1\n");
  679. fprintf(f, "@ month %d\n", getmonth());
  680. for (gs = sdata; gs; gs = gs->next) {
  681. fprintf(f, "%s ! %d\n", gs->chan, (int) gs->started);
  682. fprintf(f, "%s @ %d\n", gs->chan, gs->peak[S_TOTAL]);
  683. fprintf(f, "@ peaks %s %d %d %d %d\n", gs->chan, gs->peak[S_TOTAL],
  684. gs->peak[S_DAILY], gs->peak[S_WEEKLY], gs->peak[S_MONTHLY]);
  685. for (ls = gs->local; ls; ls = ls->next) {
  686. fprintf(f, "%s %s %d", gs->chan, ls->user, (int) ls->started);
  687. for (i = 0; i < TOTAL_TYPES; i++)
  688. fprintf(f, " %ld", ls->values[S_TOTAL][i]);
  689. fprintf(f, "\n");
  690. fprintf(f, "@ daily %s %s", gs->chan, ls->user);
  691. for (i = 0; i < TOTAL_TYPES; i++)
  692. fprintf(f, " %ld", ls->values[S_DAILY][i]);
  693. fprintf(f, "\n");
  694. fprintf(f, "@ weekly %s %s", gs->chan, ls->user);
  695. for (i = 0; i < TOTAL_TYPES; i++)
  696. fprintf(f, " %ld", ls->values[S_WEEKLY][i]);
  697. fprintf(f, "\n");
  698. fprintf(f, "@ monthly %s %s", gs->chan, ls->user);
  699. for (i = 0; i < TOTAL_TYPES; i++)
  700. fprintf(f, " %ld", ls->values[S_MONTHLY][i]);
  701. fprintf(f, "\n");
  702. i = 0;
  703. }
  704. }
  705. for (u = suserlist; u; u = u->next) {
  706. fprintf(f, "@ user %s %d %d", u->user, u->list, u->addhosts);
  707. for (h = u->hosts; h; h = h->next) {
  708. fprintf(f, " %s %lu", h->mask, h->lastused);
  709. }
  710. fprintf(f, "\n");
  711. if (u->email || u->homepage) {
  712. fprintf(f, "@ uxtra %s", u->user);
  713. if (u->email)
  714. fprintf(f, " e %s", u->email);
  715. if (u->homepage)
  716. fprintf(f, " h %s", u->homepage);
  717. fprintf(f, "\n");
  718. }
  719. }
  720. fclose(f);
  721. unlink(statsfile);
  722. movefile(s, statsfile);
  723. Context;
  724. return;
  725. }
  726. static void read_stats()
  727. {
  728. FILE *f;
  729. char buf[SAVESTATSLENGTH + 1];
  730. char *s, *chan, *user, *cmd, *host, *tmp;
  731. int i, version, range, month, list, addhosts;
  732. struct stats_userlist *u;
  733. time_t lastused;
  734. locstats *ls;
  735. globstats *gs;
  736. Context;
  737. ls = NULL;
  738. gs = NULL;
  739. version = 0;
  740. month = 0;
  741. f = fopen(statsfile, "r");
  742. if (f == NULL) {
  743. putlog(LOG_MISC, "*", "ERROR reading stats file");
  744. return;
  745. }
  746. free_stats();
  747. while (!feof(f)) {
  748. buf[0] = 0;
  749. s = buf;
  750. fgets(s, SAVESTATSLENGTH - 1, f);
  751. s[SAVESTATSLENGTH - 1] = 0;
  752. if (buf[0] == 0)
  753. continue;
  754. if (s[strlen(s) - 1] == '\n')
  755. s[strlen(s) - 1] = 0;
  756. chan = newsplit(&s);
  757. if (!strcmp(chan, "@")) {
  758. cmd = newsplit(&s);
  759. if (!strcmp(cmd, "filever"))
  760. version = atoi(newsplit(&s));
  761. else if (!strcmp(cmd, "month"))
  762. month = atoi(newsplit(&s));
  763. else if (!strcmp(cmd, "peaks")) {
  764. chan = newsplit(&s);
  765. incrstats("*", chan, T_PEAK, atoi(newsplit(&s)), S_TOTAL);
  766. incrstats("*", chan, T_PEAK, atoi(newsplit(&s)), S_DAILY);
  767. incrstats("*", chan, T_PEAK, atoi(newsplit(&s)), S_WEEKLY);
  768. incrstats("*", chan, T_PEAK, atoi(newsplit(&s)), S_MONTHLY);
  769. } else if (!strcmp(cmd, "daily") || !strcmp(cmd, "weekly")
  770. || !strcmp(cmd, "monthly")) {
  771. if (!strcmp(cmd, "daily"))
  772. range = S_DAILY;
  773. else if (!strcmp(cmd, "weekly"))
  774. range = S_WEEKLY;
  775. else if (!strcmp(cmd, "monthly"))
  776. range = S_MONTHLY;
  777. else {
  778. debug2("Error while reading statsfile: range uninitialized! (%s %s)", cmd, s);
  779. continue;
  780. }
  781. if ((range == S_MONTHLY) && (month != lastmonth))
  782. continue;
  783. chan = newsplit(&s);
  784. user = newsplit(&s);
  785. // Check if pointers still point to the correct data and
  786. // update them, if not.
  787. if ((gs && strcmp(gs->chan, chan)) || !gs) {
  788. gs = findglobstats(chan);
  789. ls = findlocstats(chan, user);
  790. } else {
  791. if ((ls && strcmp(ls->user, user)) || !ls)
  792. ls = findlocstats(chan, user);
  793. }
  794. if (!ls)
  795. ls = initstats(chan, user);
  796. for (i = 0; i < TOTAL_TYPES; i++)
  797. ls->values[range][i] = atoi(newsplit(&s));
  798. } else if (!strcmp(cmd, "user")) {
  799. user = newsplit(&s);
  800. list = atoi(newsplit(&s));
  801. addhosts = atoi(newsplit(&s));
  802. u = addsuser(user, list, addhosts);
  803. while (s[0]) {
  804. host = newsplit(&s);
  805. lastused = (time_t) atoi(newsplit(&s));
  806. saddhost(u, host, lastused);
  807. }
  808. } else if (!strcmp(cmd, "uxtra")) {
  809. user = newsplit(&s);
  810. u = findsuser_by_name(user);
  811. while (u && s[0]) {
  812. tmp = newsplit(&s);
  813. if (!strcmp(tmp, "e"))
  814. setemail(u, newsplit(&s));
  815. else
  816. sethomepage(u, newsplit(&s));
  817. }
  818. }
  819. } else {
  820. // old style data
  821. // left-over from v1.0. I should change it, but I don't want
  822. // to break compatibility
  823. user = newsplit(&s);
  824. if (!strcmp(user, "!"))
  825. incrstats(user, chan, T_GSTARTED, atoi(newsplit(&s)), 1);
  826. else if (!strcmp(user, "@"))
  827. incrstats(user, chan, T_PEAK, atoi(newsplit(&s)), S_TOTAL);
  828. else {
  829. incrstats(user, chan, T_LSTARTED, atoi(newsplit(&s)), 1);
  830. // initstats also returns the current 'ls' if it also exists,
  831. // so better don't even use findlocstats() before to save
  832. // some CPU-time
  833. ls = initstats(chan, user);
  834. for (i = 0; i < TOTAL_TYPES; i++)
  835. ls->values[S_TOTAL][i] = atoi(newsplit(&s));
  836. }
  837. }
  838. }
  839. fclose(f);
  840. Context;
  841. return;
  842. }
  843. static void reset_tstats()
  844. {
  845. globstats *gs;
  846. locstats *ls;
  847. int i;
  848. Context;
  849. putlog(LOG_MISC, "*", "Stats.mod: Resetting today's statistics...");
  850. for (gs = sdata; gs; gs = gs->next) {
  851. gs->peak[S_TODAY] = 0;
  852. free_wordstats(gs->words);
  853. gs->words = NULL;
  854. free_topics(gs->topics);
  855. gs->topics = NULL;
  856. free_urls(gs->urls);
  857. gs->urls = NULL;
  858. free_hosts(gs->hosts);
  859. gs->hosts = NULL;
  860. free_kicks(gs->kicks);
  861. gs->kicks = NULL;
  862. for (ls = gs->local; ls; ls = ls->next) {
  863. free_wordstats(ls->words);
  864. ls->words = NULL;
  865. ls->tree = NULL;
  866. free_quotes(ls->quotes);
  867. ls->quotes = NULL;
  868. for (i = 0; i < TOTAL_TYPES; i++)
  869. ls->values[S_TODAY][i] = 0;
  870. }
  871. }
  872. Context;
  873. }
  874. static void reset_mwstats(int range)
  875. {
  876. globstats *gs;
  877. locstats *ls;
  878. int i;
  879. Context;
  880. putlog(LOG_MISC, "*", "Stats.mod: Resetting %s statistics...", (range == S_WEEKLY) ? "weekly" : "monthly");
  881. for (gs = sdata; gs; gs = gs->next) {
  882. gs->peak[range] = 0;
  883. for (ls = gs->local; ls; ls = ls->next) {
  884. for (i = 0; i < TOTAL_TYPES; i++)
  885. ls->values[range][i] = 0;
  886. }
  887. }
  888. Context;
  889. }
  890. static void sort_stats_alphabetically(globstats *gs)
  891. {
  892. locstats *as, *bs, *l, *last;
  893. int a, b, again = 1;
  894. char *astr, *bstr, n[2];
  895. Context;
  896. n[0] = n[1] = 0;
  897. last = NULL;
  898. while ((gs->local != last) && again) {
  899. again = 0;
  900. l = NULL;
  901. as = gs->local;
  902. bs = gs->local->next;
  903. while(bs) {
  904. if (!as)
  905. astr = n;
  906. else
  907. astr = as->user;
  908. if (!bs)
  909. bstr = n;
  910. else
  911. bstr = bs->user;
  912. a = (int) tolower(astr[0]);
  913. b = (int) tolower(bstr[0]);
  914. while ((a == b) && a && b) {
  915. astr++;
  916. bstr++;
  917. a = (int) tolower(astr[0]);
  918. b = (int) tolower(bstr[0]);
  919. }
  920. if (a > b) {
  921. if (!l)
  922. gs->local = bs;
  923. else
  924. l->next = bs;
  925. as->next = bs->next;
  926. bs->next = as;
  927. again = 1;
  928. if (l == NULL)
  929. gs->local = bs;
  930. else
  931. l = bs;
  932. }
  933. l = as;
  934. as = bs;
  935. bs = bs->next;
  936. }
  937. last = as;
  938. }
  939. Context;
  940. }
  941. static void resetlocstats(locstats *ls)
  942. {
  943. int i;
  944. if (!ls) {
  945. debug0("ERROR! resetlocstats called with NULL pointer!");
  946. return;
  947. }
  948. for (i = 0; i < TOTAL_TYPES; i++) {
  949. ls->values[S_TOTAL][i] = 0;
  950. ls->values[S_TODAY][i] = 0;
  951. ls->values[S_WEEKLY][i] = 0;
  952. ls->values[S_MONTHLY][i] = 0;
  953. }
  954. return;
  955. }
  956. static void calcwordstats(char *hand, globstats *gs, char *rest, locstats *stats)
  957. {
  958. locstats *ls;
  959. char *word;
  960. int i;
  961. Context;
  962. if (!log_wordstats)
  963. return;
  964. if (!gs) {
  965. debug1("Can't calculate wordstats for %s, no globstats.", hand);
  966. return;
  967. }
  968. if (stats)
  969. ls = stats;
  970. else {
  971. for (ls = gs->local; ls; ls = ls->next)
  972. if (!rfc_casecmp(hand, ls->user))
  973. break;
  974. }
  975. if (!ls) {
  976. debug2("Can't calculate wordstats for %s in %s, no locstats.", hand, gs->chan);
  977. return;
  978. }
  979. for (i = 0; i < strlen(rest); i++)
  980. if (strchr("!?.,\"<>&\\", rest[i]))
  981. rest[i] = ' ';
  982. while (rest[0]) {
  983. word = newsplit(&rest);
  984. strlower(word);
  985. incrwordstats(ls, word, 1, 0);
  986. }
  987. }
  988. // add another entry to the tree
  989. static void incrwordstats(locstats *ls, char *word, int value, int set)
  990. {
  991. wordstats *ne, *te, *le;
  992. wordstats *ll;
  993. int cmp;
  994. Context;
  995. if ((word[0] == ' ') || !word[0])
  996. return;
  997. if (min_word_length && (strlen(word) < min_word_length))
  998. return; /* only log words that are longer than min_word_length chars */
  999. if (!ls) {
  1000. return;
  1001. }
  1002. // at first, check if it already exists and only needs to be updated
  1003. te = ls->tree;
  1004. le = NULL;
  1005. while (te) {
  1006. if (!(cmp = strcasecmp(te->word, word)))
  1007. break;
  1008. le = te;
  1009. if (cmp < 0)
  1010. te = te->left;
  1011. else
  1012. te = te->right;
  1013. }
  1014. if (!te) { // nothing to update, so let's append a new node
  1015. ne = nmalloc(sizeof(struct stats_words));
  1016. ne->word = nmalloc(strlen(word) + 1);
  1017. strcpy(ne->word, word);
  1018. ne->nr = 0;
  1019. ne->left = ne->right = ne->next = NULL;
  1020. if (!le) // no last entry -> new entry is going to be the crown
  1021. ls->tree = ne;
  1022. else {
  1023. if (strcasecmp(le->word, word) < 0) // -1 -> left child
  1024. le->left = ne;
  1025. else // 1 -> right child
  1026. le->right = ne;
  1027. }
  1028. // now let's add it also to the linked list (needed for sorting)
  1029. ll = ls->words;
  1030. while (ll && ll->next)
  1031. ll = ll->next;
  1032. if (ll)
  1033. ll->next = ne;
  1034. else
  1035. ls->words = ne;
  1036. te = ne;
  1037. }
  1038. // now let's set the value
  1039. if (set)
  1040. te->nr = value;
  1041. else
  1042. te->nr += value;
  1043. }
  1044. static void nincrwordstats(globstats *gs, char *word, int value)
  1045. {
  1046. wordstats *l, *ll;
  1047. for (l = gs->words; l; l = l->next)
  1048. if (!strcmp(word, l->word))
  1049. break;
  1050. if (!l) {
  1051. l = gs->words;
  1052. while (l && l->next)
  1053. l = l->next;
  1054. ll = nmalloc(sizeof(wordstats));
  1055. ll->word = nmalloc(strlen(word) + 1);
  1056. strcpy(ll->word, word);
  1057. ll->nr = 0;
  1058. ll->next = NULL;
  1059. if (l)
  1060. l->next = ll;
  1061. else
  1062. gs->words = ll;
  1063. l = ll;
  1064. }
  1065. l->nr += value;
  1066. }
  1067. static void do_globwordstats(globstats *gs)
  1068. {
  1069. wordstats *l;
  1070. locstats *ls;
  1071. for (l = gs->words; l; l = l->next)
  1072. l->nr = 0;
  1073. for (ls = gs->local; ls; ls = ls->next)
  1074. for (l = ls->words; l; l = l->next)
  1075. nincrwordstats(gs, l->word, l->nr);
  1076. sortwordstats(NULL, gs);
  1077. }
  1078. static void addquote(char *user, globstats *gs, char *quote, locstats *stats)
  1079. {
  1080. quotestr *l, *nl;
  1081. locstats *ls;
  1082. if (!quote_freq)
  1083. return;
  1084. if (!gs) {
  1085. debug1("Can't add quote to %s, no globstats.", user);
  1086. return;
  1087. }
  1088. if (stats)
  1089. ls = stats;
  1090. else {
  1091. for (ls = gs->local; ls; ls = ls->next)
  1092. if (!rfc_casecmp(user, ls->user))
  1093. break;
  1094. }
  1095. if (!ls) {
  1096. debug2("Can't add quote to %s in %s, no locstats.", user, gs->chan);
  1097. return;
  1098. }
  1099. ls->quotefr--;
  1100. if (ls->quotefr > 0)
  1101. return;
  1102. ls->quotefr = quote_freq;
  1103. l = ls->quotes;
  1104. while (l && l->next)
  1105. l = l->next;
  1106. nl = nmalloc(sizeof(quotestr));
  1107. nl->next = NULL;
  1108. nl->quote = nmalloc(strlen(quote) + 1);
  1109. strcpy(nl->quote, quote);
  1110. if (l)
  1111. l->next = nl;
  1112. else
  1113. ls->quotes = nl;
  1114. }
  1115. static void addtopic(char *channel, char *topic, char *by)
  1116. {
  1117. topicstr *e, *ne;
  1118. globstats *gs;
  1119. Context;
  1120. gs = findglobstats(channel);
  1121. if (!gs)
  1122. return;
  1123. for (e = gs->topics; e; e = e->next)
  1124. if (!strcasecmp(topic, e->topic))
  1125. return;
  1126. e = gs->topics;
  1127. while (e && e->next)
  1128. e = e->next;
  1129. ne = nmalloc(sizeof(topicstr));
  1130. ne->topic = nmalloc(strlen(topic) + 1);
  1131. strcpy(ne->topic, topic);
  1132. ne->by = nmalloc(strlen(by) + 1);
  1133. strcpy(ne->by, by);
  1134. ne->when = now;
  1135. ne->next = NULL;
  1136. if (e)
  1137. e->next = ne;
  1138. else
  1139. gs->topics = ne;
  1140. }
  1141. static void addhost(char *host, globstats *gs)
  1142. {
  1143. hoststr *e, *ne;
  1144. char *s;
  1145. if (!gs || !host)
  1146. return;
  1147. s = strchr(host, '@');
  1148. if (s)
  1149. host = s + 1;
  1150. if (strcmp(host, "[IP]"))
  1151. strlower(host);
  1152. for (e = gs->hosts; e; e = e->next) {
  1153. if (!strcmp(host, e->host)) {
  1154. e->nr++;
  1155. return;
  1156. }
  1157. }
  1158. e = gs->hosts;
  1159. while (e && e->next)
  1160. e = e->next;
  1161. ne = nmalloc(sizeof(hoststr));
  1162. ne->host = nmalloc(strlen(host) + 1);
  1163. strcpy(ne->host, host);
  1164. ne->nr = 1;
  1165. ne->next = NULL;
  1166. if (e)
  1167. e->next = ne;
  1168. else
  1169. gs->hosts = ne;
  1170. return;
  1171. }
  1172. static void setword(globstats *gs, char *word)
  1173. {
  1174. locstats *l;
  1175. wordstats *w;
  1176. for (l = gs->local; l; l = l->next) {
  1177. l->word = NULL;
  1178. for (w = l->words; w; w = w->next) {
  1179. if (!strcmp(w->word, word)) {
  1180. l->word = w;
  1181. break;
  1182. }
  1183. }
  1184. }
  1185. }
  1186. static int track_stat_user(char *oldnick, char *newnick)
  1187. {
  1188. globstats *gs;
  1189. locstats *ls;
  1190. struct stats_userlist *u;
  1191. int found = 0;
  1192. Context;
  1193. for (gs = sdata; gs; gs = gs->next) {
  1194. for (ls = gs->local; ls; ls = ls->next) {
  1195. if (!rfc_casecmp(oldnick, ls->user) && strcmp(newnick, ls->user)) {
  1196. nfree(ls->user);
  1197. ls->user = nmalloc(strlen(newnick) + 1);
  1198. strcpy(ls->user, newnick);
  1199. // ls->u should still be valid...
  1200. found = 1;
  1201. debug3("Transferred stats from %s to %s in %s", oldnick, newnick, gs->chan);
  1202. }
  1203. }
  1204. }
  1205. for (u = suserlist; u; u = u->next) {
  1206. if (!rfc_casecmp(oldnick, u->user) && strcmp(newnick, u->user)) {
  1207. nfree(u->user);
  1208. u->user = nmalloc(strlen(newnick) + 1);
  1209. strcpy(u->user, newnick);
  1210. found = 1;
  1211. debug2("Changed user name from %s to %s in my local database.", oldnick, newnick);
  1212. }
  1213. }
  1214. if (found)
  1215. return 1;
  1216. return 0;
  1217. }
  1218. static void check_for_url(char *user, char *chan, char *text)
  1219. {
  1220. char *p, *url;
  1221. char *tmp, *tmp2, *t;
  1222. struct stats_url *e, *ne;
  1223. globstats *gs;
  1224. int weiter;
  1225. if (log_urls < 1)
  1226. return;
  1227. gs = findglobstats(chan);
  1228. if (!gs)
  1229. return;
  1230. url = p = tmp = tmp2 = t = NULL;
  1231. if ((p = strstr(text, "http://")))
  1232. url = newsplit(&p);
  1233. else if ((p = strstr(text, "ftp://")))
  1234. url = newsplit(&p);
  1235. else if (strstr(text, "www.") || strstr(text, ".com") || strstr(text, "ftp.")) {
  1236. tmp = nmalloc(strlen(text) + 1);
  1237. strcpy(tmp, text);
  1238. t = tmp;
  1239. while (t[0]) {
  1240. p = newsplit(&t);
  1241. if (strstr(p, "www.") || strstr(p, ".com") || strstr(text, "ftp.")) {
  1242. url = p;
  1243. break;
  1244. }
  1245. }
  1246. }
  1247. if (!url)
  1248. return;
  1249. if (strchr(url, '@')) { /* probably an email address or something similar */
  1250. if (tmp)
  1251. nfree(tmp);
  1252. return;
  1253. }
  1254. if (strncasecmp(url, "http://", 7) && strncasecmp(url, "ftp://", 6)) {
  1255. if (!strncasecmp(url, "ftp.", 4)) {
  1256. tmp2 = nmalloc(strlen(url) + 6 + 1);
  1257. strcpy(tmp2, "ftp://");
  1258. strcpy(tmp2 + 6, url);
  1259. } else {
  1260. tmp2 = nmalloc(strlen(url) + 7 + 1);
  1261. strcpy(tmp2, "http://");
  1262. strcpy(tmp2 + 7, url);
  1263. }
  1264. url = tmp2;
  1265. }
  1266. for (e = gs->urls; e; e = e->next) {
  1267. if (!strcmp(e->url, url)) {
  1268. nfree(e->by);
  1269. e->by = nmalloc(strlen(user) + 1);
  1270. strcpy(e->by, user);
  1271. e->when = now;
  1272. if (tmp)
  1273. nfree(tmp);
  1274. if (tmp2)
  1275. nfree(tmp2);
  1276. return;
  1277. }
  1278. }
  1279. weiter = 1;
  1280. for (p = url; (p != url) && weiter; p--) {
  1281. if (strchr(".!?,#", p[0]))
  1282. p[0] = 0;
  1283. else
  1284. weiter = 0;
  1285. }
  1286. for (e = gs->urls; e && e->next; e = e->next);
  1287. ne = nmalloc(sizeof(struct stats_url));
  1288. ne->url = nmalloc(strlen(url) + 1);
  1289. strcpy(ne->url, url);
  1290. ne->by = nmalloc(strlen(user) + 1);
  1291. strcpy(ne->by, user);
  1292. ne->when = now;
  1293. ne->next = NULL;
  1294. if (e)
  1295. e->next = ne;
  1296. else
  1297. gs->urls = ne;
  1298. if (tmp)
  1299. nfree(tmp);
  1300. if (tmp2)
  1301. nfree(tmp2);
  1302. debug2("Logged URL: \"%s\" mentioned by %s.", ne->url, ne->by);
  1303. }
  1304. static void add_chanlog(globstats *gs, char *nick, char *text, int type)
  1305. {
  1306. char ts[20];
  1307. time_t tt, ttbuf;
  1308. quotestr *newlog, *l;
  1309. if (!gs || (kick_context < 1))
  1310. return;
  1311. ttbuf = tt = now;
  1312. strftime(ts, 19, "[%H:%M:%S]", localtime(&tt));
  1313. newlog = nmalloc(sizeof(quotestr));
  1314. newlog->next = NULL;
  1315. if (type == SL_PRIVMSG) {
  1316. newlog->quote = nmalloc(strlen(nick) + strlen(ts) + strlen(text) + 11);
  1317. sprintf(newlog->quote, "%s &lt;%s&gt; %s", ts, nick, text);
  1318. } else if (type == SL_KICK) {
  1319. newlog->quote = nmalloc(strlen(text) + strlen(ts) + 2);
  1320. sprintf(newlog->quote, "%s %s", ts, text);
  1321. } else if (type == SL_MODE) {
  1322. newlog->quote = nmalloc(strlen(ts) + strlen(text) + 2);
  1323. sprintf(newlog->quote, "%s %s", ts, text);
  1324. } else if (type == SL_NICK) {
  1325. newlog->quote = nmalloc(strlen(ts) + strlen(nick) + strlen(text) + 19);
  1326. sprintf(newlog->quote, "%s %s changed nick to %s", ts, nick, text);
  1327. } else if (type == SL_PART) {
  1328. newlog->quote = nmalloc(strlen(ts) + strlen(nick) + strlen(gs->chan) + 12);
  1329. sprintf(newlog->quote, "%s %s has left %s", ts, nick, gs->chan);
  1330. } else if (type == SL_JOIN) {
  1331. newlog->quote = nmalloc(strlen(ts) + strlen(nick) + strlen(gs->chan) + 14);
  1332. sprintf(newlog->quote, "%s %s has joined %s", ts, nick, gs->chan);
  1333. } else if (type == SL_QUIT) {
  1334. newlog->quote = nmalloc(strlen(ts) + strlen(nick) + strlen(text) + 18);
  1335. sprintf(newlog->quote, "%s %s has quit IRC (%s)", ts, nick, text);
  1336. } else {
  1337. debug1("Unknown log-type: %d !!!", type);
  1338. newlog->quote = NULL;
  1339. }
  1340. if (gs->lastlog)
  1341. gs->lastlog->next = newlog;
  1342. else
  1343. gs->log = newlog;
  1344. gs->lastlog = newlog;
  1345. gs->log_length++;
  1346. while ((gs->log_length > kick_context) && (gs->log_length > 0)) {
  1347. l = gs->log->next;
  1348. nfree(gs->log->quote);
  1349. if (gs->lastlog == gs->log)
  1350. gs->lastlog = NULL;
  1351. nfree(gs->log);
  1352. gs->log = l;
  1353. gs->log_length--;
  1354. }
  1355. ctime(&ttbuf); /* workaround for a bug in older eggdrops */
  1356. }
  1357. static void save_kick(globstats *gs, char *kick)
  1358. {
  1359. struct stats_kick *k, *nk;
  1360. quotestr *log, *l, *nl;
  1361. Context;
  1362. if (!gs)
  1363. return;
  1364. for (k = gs->kicks; k && k->next; k = k->next);
  1365. nk = nmalloc(sizeof(struct stats_kick));
  1366. nk->next = NULL;
  1367. nk->log = NULL;
  1368. if (!gs->log || (kick_context < 1)) {
  1369. nl = nmalloc(sizeof(quotestr));
  1370. nl->quote = nmalloc(strlen(kick) + 1);
  1371. strcpy(nl->quote, kick);
  1372. nl->next = NULL;
  1373. nk->log = nl;
  1374. } else {
  1375. for (log = gs->log; log; log = log->next) {
  1376. nl = nmalloc(sizeof(quotestr));
  1377. nl->quote = nmalloc(strlen(log->quote) + 1);
  1378. strcpy(nl->quote, log->quote);
  1379. nl->next = NULL;
  1380. for (l = nk->log; l && l->next; l = l->next);
  1381. if (l)
  1382. l->next = nl;
  1383. else
  1384. nk->log = nl;
  1385. }
  1386. }
  1387. if (k)
  1388. k->next = nk;
  1389. else
  1390. gs->kicks = nk;
  1391. debug1("Logged kick in %s.", gs->chan);
  1392. }