DATAHANDLING.~C 37 KB

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