pisg.pl 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use Getopt::Long;
  4. # pisg - Perl IRC Statistics Generator
  5. #
  6. # Copyright (C) 2001 <Morten 'LostStar' Brix Pedersen> - morten@wtf.dk
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. my $config;
  22. my $words;
  23. # Values that _MUST_ be set below (unless you pass them on commandline)
  24. $config->{channel} = "#channel"; # The name of your channel.
  25. $config->{logfile} = "channel.log"; # The exact filename of the logfile
  26. $config->{format} = "mIRC"; # logfile format. see FORMATS file
  27. $config->{network} = "SomeIRCNet"; # Network the channels is using.
  28. $config->{outputfile} = "index.html"; # The name of the html file to be generated
  29. $config->{maintainer} = "MAINTAINER"; # The maintainer or bot which makes the logfile
  30. $config->{pagehead} = "none"; # Some 'page header' file which you want to
  31. # include in top of the stats
  32. $config->{configfile} = "pisg.cfg"; # Path to config file (aliases, ignores,
  33. # pics and more, see pisg.cfg for examples)
  34. $config->{imagepath} = ""; # If your user pictures is located
  35. # some special directory, set the path here.
  36. $config->{logdir} = ""; # If you specify a path to a dir here, then
  37. # pisg will take that dir, and parse ALL
  38. # logfiles in it, and create 1 HTML file
  39. # from it
  40. $config->{lang} = 'EN'; # Language to use:
  41. # EN | DE | DK | FR | ES | PL
  42. $config->{langfile} = 'lang.txt'; # Name of language file
  43. $config->{prefix} = ""; # If you specify a string here and have $logdir
  44. # set, then only those files starting with
  45. # $prefix in $logdir will be read.
  46. # Here you can set the colors for your stats page..
  47. $config->{bgcolor} = "#dedeee"; # Background color of the page
  48. $config->{text} = "black"; # Normal text color
  49. $config->{hbgcolor} = "#666699"; # Background color in headlines
  50. $config->{hcolor} = "white"; # Text color in headline
  51. $config->{hicell} = "#BABADD"; # Background color in highlighted cells
  52. $config->{hicell2} = "#CCCCCC"; # Background color in highlighted cells
  53. $config->{tdcolor} = "black"; # Color of text in tables
  54. $config->{tdtop} = "#C8C8DD"; # Top color in some tables.
  55. $config->{link} = "#0b407a"; # Color of links
  56. $config->{vlink} = "#0b407a"; # Color of visited links
  57. $config->{hlink} = "#0b407a"; # Color of hovered links
  58. $config->{headline} = "#000000"; # Border color of headlines
  59. $config->{rankc} = "#CCCCCC"; # Colors of 'ranks' (1,2,3,4)
  60. $config->{pic1} = "pipe-blue.png"; # Bar-graphic-file for normal times
  61. $config->{pic2} = "pipe-purple.png"; # Bar-graphic-file for top-times
  62. # Other things that you might set, but not everyone cares about them
  63. $config->{minquote} = "25"; # Minimal value of letters for a random quote
  64. $config->{maxquote} = "65"; # Maximum value of letters for a random quote
  65. $config->{wordlength} = "5"; # The minimum number of chars an interesting
  66. # word may be (in 'most referenced words')
  67. $config->{activenicks} = "25"; # Number of nicks to show in the 'top 25'
  68. $config->{activenicks2} = "30"; # Nicks to show in 'these didnt make it...'
  69. $config->{topichistory} = "3"; # How many topics to show in 'latest topics'
  70. $config->{nicktracking} = 0; # Track nickchanges and create aliases (can
  71. # be slow, so it's disabled by default)
  72. $config->{timeoffset} = "+0"; # A time offset on the stats page - if your
  73. # country has a different timezone than the
  74. # machine where the stats are being
  75. # generated, then for example do +1
  76. # to add 1 hour to the time
  77. $words->{foul} = "ass fuck bitch shit scheisse scheiße kacke arsch ficker ficken schlampe"; # If not set in pisg.cfg set your Foulwords here.
  78. # You shouldn't care about anything below this point
  79. $config->{debug} = 0; # 0 = Debugging off, 1 = Debugging on
  80. $config->{debugfile} = "debug.log"; # Path to debug file(must be set if $debug == 1)
  81. $config->{version} = "v0.18-cvs";
  82. my ($lines, $kicked, $gotkicked, $smile, $longlines, $time, $timestamp, %alias,
  83. $normalline, $actionline, $thirdline, @ignore, $line, $processtime, @topics,
  84. %monologue, %kicked, %gotkick, %line, %length, %qpercent, %lpercent, %sadface,
  85. %smile, $nicks, %longlines, %mono, %times, %question, %loud, $totallength,
  86. %gaveop, %tookop, %joins, %actions, %sayings, %wordcount, %lastused, %gotban,
  87. %setban, %foul, $days, $oldtime, $lastline, $actions, $normals, %userpics,
  88. %userlinks, %T, $repeated, $lastnormal, $foulwords);
  89. sub main
  90. {
  91. init_config(); # Init config. (Aliases, ignores, other options etc.)
  92. init_words(); # Init words. (Foulwords etc)
  93. init_pisg(); # Init commandline arguments and other things
  94. init_lineformats(); # Attempt to set line formats in compliance with user specification (--format)
  95. init_debug(); # Init the debugging file
  96. if ($config->{logdir}) {
  97. parse_dir(); # Run through all logfiles in dir
  98. } else {
  99. parse_file($config->{logfile}); # Run through the whole logfile
  100. }
  101. create_html(); # Create the HTML
  102. # (look here if you want to remove some of the
  103. # stats which you don't care about)
  104. close_debug(); # Close the debugging file
  105. print "\nFile was parsed succesfully in $processtime on $time.\n";
  106. }
  107. sub init_pisg
  108. {
  109. print "pisg $config->{version} - Perl IRC Statistics Generator\n\n";
  110. get_cmdlineoptions();
  111. get_language_templates();
  112. $timestamp = time;
  113. if ($config->{timeoffset} =~ /\+(\d+)/) {
  114. # We must plus some hours to the time
  115. $timestamp += 3600 * $1; # 3600 seconds per hour
  116. } elsif ($config->{timeoffset} =~ /-(\d+)/) {
  117. # We must remove some hours from the time
  118. $timestamp -= 3600 * $1; # 3600 seconds per hour
  119. }
  120. # Set useful values.
  121. $days = 1;
  122. $oldtime = "00";
  123. $lastline = "";
  124. $actions = "0";
  125. $normals = "0";
  126. $time = localtime($timestamp);
  127. $repeated = 0;
  128. $lastnormal = "";
  129. # Add trailing slash when it's not there..
  130. if (substr($config->{imagepath}, -1) ne '/') {
  131. $config->{imagepath} =~ s/(.*)/$1\//;
  132. }
  133. print "Statistics for channel $config->{channel} \@ $config->{network} by $config->{maintainer}\n\n";
  134. }
  135. sub init_lineformats {
  136. # These are the regular expressions which matches the lines in the logfile,
  137. # and looks different if it's xchat, mIRC or whatever.
  138. # If you want to add support for a new format - you first have to add the
  139. # regex here, and then you also have to modify the parse subroutines called
  140. # 'parse_normalline()', 'parse_actionline()' and 'parse_thirdline()'
  141. if ($config->{format} eq 'xchat') {
  142. $normalline = '^(\d+):\d+:\d+ <([^>]+)>\s+(.*)';
  143. $actionline = '^(\d+):\d+:\d+ \*\s+(\S+) (.*)';
  144. $thirdline = '^(\d+):(\d+):\d+ .--\s+(\S+) (\S+) (\S+) (\S+) (\S+) (\S+) (.*)';
  145. } elsif ($config->{format} eq 'mIRC') {
  146. $normalline = '^\[(\d+):\d+\] <([^>]+)> (.*)';
  147. $actionline = '^\[(\d+):\d+\] \* (\S+) (.*)';
  148. $thirdline = '^\[(\d+):(\d+)\] \*\*\* (\S+) (\S+) (\S+) (\S+) (\S+)(.*)';
  149. } elsif ($config->{format} eq 'eggdrop') {
  150. $normalline = '^\[(\d+):\d+\] <([^>]+)> (.*)';
  151. $actionline = '^\[(\d+):\d+\] Action: (\S+) (.*)';
  152. $thirdline = '^\[(\d+):(\d+)\] (\S+) (\S+) (\S+) (\S+)(.*)';
  153. } elsif ($config->{format} eq 'bxlog') {
  154. $normalline = '^\[\d+ \S+\/(\d+):\d+\] <([^>]+)> (.*)';
  155. $actionline = '^\[\d+ \S+\/(\d+):\d+\] \* (\S+) (.*)';
  156. $thirdline = '^\[\d+ \S+\/(\d+):(\d+)\] ([<>@!]) (.*)';
  157. } elsif ($config->{format} eq 'grufti') {
  158. $normalline = '^\[(\d+):\d+\] <([^>]+)> (.*)';
  159. $actionline = '^\[(\d+):\d+\] \* (\S+) (.*)';
  160. $thirdline = '^\[(\d+):(\d+)\] (\S+) (\S+) (\S+) (\S+) (\S+) (\S+)(.*)';
  161. } else {
  162. die("Logfile format not supported, check \$config->{format} setting.\n");
  163. }
  164. }
  165. sub init_config
  166. {
  167. if (open(CONFIG, $config->{configfile})) {
  168. my $lineno = 0;
  169. while (<CONFIG>)
  170. {
  171. $lineno++;
  172. my $line = $_;
  173. next if /^#/;
  174. if ($line =~ /<user.*>/) {
  175. my $nick;
  176. if ($line =~ /nick="([^"]+)"/) {
  177. $nick = $1;
  178. } else {
  179. print STDERR "Warning: no nick specified in $config->{configfile} on line $lineno\n";
  180. next;
  181. }
  182. if ($line =~ /alias="([^"]+)"/) {
  183. my @thisalias = split(/\s+/, $1);
  184. push(@{ $alias{$nick} }, @thisalias);
  185. }
  186. if ($line =~ /pic="([^"]+)"/) {
  187. $userpics{$nick} = $1;
  188. }
  189. if ($line =~ /link="([^"]+)"/) {
  190. $userlinks{$nick} = $1;
  191. }
  192. if ($line =~ /ignore="Y"/i) {
  193. push(@ignore, $nick);
  194. }
  195. } elsif ($line =~ /<settings(.*)>/) {
  196. my $settings = $1;
  197. while ($settings =~ s/[ \t]([^=]+)=["']([^"']*)["']//) {
  198. my $vars = $1;
  199. my $keys = $2;
  200. while ($vars =~ s/([A-Z])/\l$1/g) {
  201. $config->{$vars} = $keys;
  202. }
  203. $config->{$1} = $2;
  204. debug("Conf: $1 = $2");
  205. }
  206. } elsif ($line =~ /<words(.*)>/) {
  207. my $settings = $1;
  208. while ($settings =~ s/[ \t]([^=]+)=["']([^"']*)["']//) {
  209. $words->{$1} = $2;
  210. debug("Words: $1 = $2");
  211. }
  212. }
  213. }
  214. close(CONFIG);
  215. }
  216. }
  217. sub init_words {
  218. my (@words, $i);
  219. if($words->{foul}) {
  220. @words = split(/\ /, $words->{foul});
  221. $i = 0;
  222. foreach (@words) {
  223. if($foulwords) {
  224. $foulwords = $foulwords . "\|" . $words[$i];
  225. } else {
  226. $foulwords = $foulwords . $words[$i];
  227. }
  228. $i++;
  229. }
  230. }
  231. }
  232. sub init_debug
  233. {
  234. if ($config->{debug}) {
  235. print "[ Debugging => $config->{debugfile} ]\n";
  236. open(DEBUG,"> $config->{debugfile}") or print STDERR "$0: Unable to open debug
  237. file($config->{debugfile}): $!\n";
  238. debug("*** pisg debug file for $config->{logfile}\n");
  239. }
  240. }
  241. sub parse_dir
  242. {
  243. print "Going into $config->{logdir} and parsing all files there...\n\n";
  244. my $files = `ls $config->{logdir}`;
  245. my @filesarray = split(/\n/, $files);
  246. # Add trailing slash when it's not there..
  247. if (substr($config->{logdir}, -1) ne '/') {
  248. $config->{logdir} =~ s/(.*)/$1\//;
  249. }
  250. foreach my $file (@filesarray) {
  251. if ($config->{prefix} eq "" || $file =~ /^$config->{prefix}/) {
  252. $file = $config->{prefix} . $file;
  253. parse_file($file);
  254. }
  255. }
  256. }
  257. sub parse_file
  258. {
  259. my $file = shift;
  260. # This parses the file..
  261. print "Analyzing log($file) in '$config->{format}' format...\n";
  262. if ($file =~ /.bz$/ || $file =~ /.bz2$/) {
  263. open (LOGFILE, "bunzip2 -c $file |") or die("$0: Unable to open logfile($file): $!\n");
  264. } elsif ($file =~ /.gz$/) {
  265. open (LOGFILE, "gunzip -c $file |") or die("$0: Unable to open logfile($file): $!\n");
  266. } else {
  267. open (LOGFILE, $file) or die("$0: Unable to open logfile($file): $!\n");
  268. }
  269. while($line = <LOGFILE>) {
  270. $lines++; # Increment number of lines.
  271. $line = strip_mirccodes($line);
  272. my $hashref;
  273. # Match normal lines.
  274. if ($hashref = parse_normalline($line)) {
  275. my ($hour, $nick, $saying, $i);
  276. for ($i = 0; $i <= $repeated; $i++) {
  277. if ($i > 0) {
  278. $hashref = parse_normalline($lastnormal);
  279. $lines++; #Increment number of lines for repeated lines
  280. }
  281. $hour = $hashref->{hour};
  282. $nick = find_alias($hashref->{nick});
  283. $saying = $hashref->{saying};
  284. # Timestamp collecting
  285. $times{$hour}++;
  286. unless (grep /^\Q$nick\E$/i, @ignore) {
  287. $normals++;
  288. $line{$nick}++;
  289. # Count up monologues
  290. if ($lastline eq $nick) {
  291. $mono{$nick}++;
  292. if ($mono{$nick} == "5") {
  293. $monologue{$nick}++;
  294. $mono{$nick} = 0;
  295. }
  296. } else {
  297. $mono{$nick} = 0;
  298. }
  299. $lastline = $nick;
  300. my $l = length($saying);
  301. if ($l > $config->{minquote} && $l < $config->{maxquote}) {
  302. # Creates $hash{nick}[n] - a hash of an array.
  303. push (@{ $sayings{$nick} }, htmlentities($saying));
  304. $longlines{$nick}++;
  305. }
  306. $question{$nick}++
  307. if ($saying =~ /\?/);
  308. $loud{$nick}++
  309. if ($saying =~ /!/);
  310. $foul{$nick}++
  311. if ($saying =~ /$foulwords/);
  312. # Who smiles the most?
  313. # A regex matching al lot of smilies
  314. $smile{$nick}++
  315. if ($saying =~ /[8;:=][ ^-o]?[)pPD}\]>]/);
  316. $sadface{$nick}++
  317. if ($saying =~ /[8;:=][ ^-]?[\(\[\\\/{]/);
  318. # Don't count http:// as a :/ face
  319. $sadface{$nick}--
  320. if ($saying =~ /\w+:\/\//);
  321. foreach my $word (split(/[\s,!?.:;)(]+/, $saying)) {
  322. # remove uninteresting words
  323. next unless (length($word) > $config->{wordlength});
  324. # ignore contractions
  325. next if ($word =~ m/'..?$/);
  326. $wordcount{htmlentities($word)}++ unless (grep /^\Q$word\E$/i, @ignore);
  327. $lastused{htmlentities($word)} = $nick;
  328. }
  329. $length{$nick} += $l;
  330. $totallength += $l;
  331. }
  332. }
  333. $lastnormal = $line;
  334. $repeated = 0;
  335. }
  336. # Match action lines.
  337. elsif ($hashref = parse_actionline($line)) {
  338. my ($hour, $nick, $saying);
  339. $hour = $hashref->{hour};
  340. $nick = find_alias($hashref->{nick});
  341. $saying = $hashref->{saying};
  342. # Timestamp collecting
  343. $times{$hour}++;
  344. unless (grep /^\Q$nick\E$/i, @ignore) {
  345. $actions++;
  346. $line{$nick}++;
  347. my $len = length($saying);
  348. $length{$nick} += $len;
  349. $totallength += $len;
  350. }
  351. }
  352. # Match *** lines.
  353. elsif (($hashref = parse_thirdline($line)) && $hashref->{nick}) {
  354. my ($hour, $min, $nick, $kicker, $newtopic, $newmode, $newjoin, $newnick);
  355. $hour = $hashref->{hour};
  356. $min = $hashref->{min};
  357. $nick = find_alias($hashref->{nick});
  358. $kicker = $hashref->{kicker};
  359. $newtopic = $hashref->{newtopic};
  360. $newmode = $hashref->{newmode};
  361. $newjoin = $hashref->{newjoin};
  362. $newnick = $hashref->{newnick};
  363. # Timestamp collecting
  364. $times{$hour}++;
  365. unless (grep /^\Q$nick\E$/i, @ignore) {
  366. if (defined($kicker)) {
  367. unless (grep /^\Q$kicker\E$/i, @ignore) {
  368. $gotkick{$nick}++;
  369. $kicked{$kicker}++;
  370. }
  371. } elsif (defined($newtopic)) {
  372. my $tcount = @topics;
  373. $topics[$tcount]{topic} = htmlentities($newtopic);
  374. $topics[$tcount]{nick} = $nick;
  375. $topics[$tcount]{hour} = $hour;
  376. $topics[$tcount]{min} = $min;
  377. # Strip off the quotes (')
  378. $topics[$tcount]{topic} =~ s/^\'(.*)\'$/$1/;
  379. } elsif (defined($newmode)) {
  380. my @opchange = opchanges($newmode);
  381. unless (exists $gaveop{$nick}) { $gaveop{$nick} = 0 }
  382. $gaveop{$nick} += $opchange[0] if $opchange[0];
  383. $tookop{$nick} += $opchange[1] if $opchange[1];
  384. } elsif (defined($newjoin)) {
  385. $joins{$nick}++;
  386. } elsif (defined($newnick) && ($config->{nicktracking} == 1)) {
  387. if (find_alias($newnick) eq $newnick) {
  388. if (defined($alias{$nick}) && !defined($alias{$newnick})) {
  389. push (@{$alias{$nick}}, $newnick);
  390. } elsif (defined($alias{$newnick}) && !defined($alias{$nick})) {
  391. push (@{$alias{$newnick}}, $nick);
  392. } elsif ($nick =~ /Guest/) {
  393. push (@{$alias{$newnick}}, ($newnick, $nick));
  394. } else {
  395. push (@{$alias{$nick}}, ($nick, $newnick));
  396. }
  397. }
  398. }
  399. }
  400. if ($hour < $oldtime) { $days++ }
  401. $oldtime = $hour;
  402. }
  403. }
  404. close(LOGFILE);
  405. my ($sec,$min,$hour) = gmtime(time - $^T);
  406. $processtime = sprintf("%02d hours, %02d minutes and %02d seconds", $hour, $min, $sec);
  407. $nicks = scalar keys %line;
  408. print "Finished analyzing log, $days days total.\n";
  409. }
  410. # Here is the 3 parse sub routines, their function is to return a hash with
  411. # the elements in the line. This is where we add the format dependent stuff.
  412. sub parse_normalline
  413. {
  414. # Parse a normal line - returns a hash with 'hour', 'nick' and 'saying'
  415. my $line = shift;
  416. my %hash;
  417. if ($line =~ /$normalline/) {
  418. debug("[$lines] Normal: $1 $2 $3");
  419. if (($config->{format} eq 'mIRC') || ($config->{format} eq 'xchat') || ($config->{format} eq
  420. 'eggdrop') || ($config->{format} eq 'bxlog') || ($config->{format} eq 'grufti')) {
  421. $hash{hour} = $1;
  422. $hash{nick} = $2;
  423. $hash{saying} = $3;
  424. } else {
  425. die("Format not supported?!\n");
  426. }
  427. return \%hash;
  428. } else {
  429. return;
  430. }
  431. }
  432. sub parse_actionline
  433. {
  434. # Parse an action line - returns a hash with 'hour', 'nick' and 'saying'
  435. my $line = shift;
  436. my %hash;
  437. if ($line =~ /$actionline/) {
  438. debug("[$lines] Action: $1 $2 $3");
  439. if (($config->{format} eq 'mIRC') || ($config->{format} eq 'xchat') || ($config->{format} eq
  440. 'eggdrop') || $config->{format} eq 'bxlog' || ($config->{format} eq 'grufti')) {
  441. $hash{hour} = $1;
  442. $hash{nick} = $2;
  443. $hash{saying} = $3;
  444. } else {
  445. die("Format not supported?!\n");
  446. }
  447. return \%hash;
  448. } else {
  449. return;
  450. }
  451. }
  452. sub parse_thirdline
  453. {
  454. # Parses the 'third' line - (the third line is everything else, like
  455. # topic changes, mode changes, kicks, etc.)
  456. # parse_thirdline() have to return a hash with the following keys, for
  457. # every format:
  458. # hour - the hour we're in (for timestamp loggin)
  459. # min - the minute we're in (for timestamp loggin)
  460. # nick - the nick
  461. # kicker - the nick which were kicked (if any)
  462. # newtopic - the new topic (if any)
  463. # newmode - a deop or an op, must be '+o' or '-o'
  464. # newjoin - a new nick which has joined the channel
  465. # newnick - a person has changed nick and this is the new nick
  466. my $line = shift;
  467. my %hash;
  468. if ($line =~ /$thirdline/) {
  469. if ($7) {
  470. debug("[$lines] ***: $1 $2 $3 $4 $5 $6 $7");
  471. } elsif ($5) {
  472. debug("[$lines] ***: $1 $2 $3 $4 $5 $6");
  473. } else {
  474. debug("[$lines] ***: $1 $2 $3 $4");
  475. }
  476. if ($config->{format} eq 'mIRC') {
  477. $hash{hour} = $1;
  478. $hash{min} = $2;
  479. $hash{nick} = $3;
  480. if (($4.$5) eq 'waskicked') {
  481. $hash{kicker} = $7;
  482. } elsif (($4.$5) eq 'changestopic') {
  483. $hash{newtopic} = "$7 $8";
  484. } elsif (($4.$5) eq 'setsmode:') {
  485. $hash{newmode} = $6;
  486. } elsif (($4.$5) eq 'hasjoined') {
  487. $hash{newjoin} = $3;
  488. } elsif (($4.$5) eq 'nowknown') {
  489. $hash{newnick} = $8;
  490. }
  491. } elsif ($config->{format} eq 'xchat') {
  492. $hash{hour} = $1;
  493. $hash{min} = $2;
  494. $hash{nick} = $3;
  495. if (($4.$5) eq 'haskicked') {
  496. $hash{kicker} = $3;
  497. $hash{nick} = $6;
  498. } elsif (($4.$5) eq 'haschanged') {
  499. $hash{newtopic} = $9;
  500. } elsif (($4.$5) eq 'giveschannel') {
  501. $hash{newmode} = '+o';
  502. } elsif (($4.$5) eq 'removeschannel') {
  503. $hash{newmode} = '-o';
  504. } elsif (($5.$6) eq 'hasjoined') {
  505. $hash{newjoin} = $1;
  506. } elsif (($5.$6) eq 'nowknown') {
  507. $hash{newnick} = $8;
  508. }
  509. } elsif ($config->{format} eq 'eggdrop') {
  510. $hash{hour} = $1;
  511. $hash{min} = $2;
  512. $hash{nick} = $3;
  513. if (($4.$5) eq 'kickedfrom') {
  514. $7 =~ /^ by ([\S]+):.*/;
  515. $hash{kicker} = $1;
  516. } elsif ($3 eq 'Topic') {
  517. $7 =~ /^ by ([\S]+)![\S]+: (.*)/;
  518. $hash{nick} = $1;
  519. $hash{newtopic} = $2;
  520. } elsif (($4.$5) eq 'modechange') {
  521. $hash{newmode} = $6;
  522. $7 =~ /^ .+ by ([\S]+)!.*/;
  523. $hash{nick} = $1;
  524. $hash{newmode} =~ s/^\'//;
  525. } elsif ($5 eq 'joined') {
  526. $hash{newjoin} = $3;
  527. } elsif (($3.$4) eq 'Nickchange:') {
  528. $hash{nick} = $5;
  529. $7 =~ /([\S]+)/;
  530. $hash{newnick} = $1;
  531. } elsif (($3.$4.$5) eq 'Lastmessagerepeated') {
  532. $repeated = $6;
  533. }
  534. } elsif ($config->{format} eq 'bxlog') {
  535. $hash{hour} = $1;
  536. $hash{min} = $2;
  537. if ($3 eq '<') {
  538. if ($4 =~ /^([^!]+)!([\S+]) was kicked off (\S+) by ([^!]+)!(\S+) \(([^)]+)\)$/) {
  539. $hash{kicker} = $4;
  540. $hash{nick} = $1;
  541. }
  542. } elsif ($3 eq '>') {
  543. if ($4 =~ /^([^!])+!(\S+) has joined (\S+)$/) {
  544. $hash{nick} = $1;
  545. $hash{newjoin} = $1;
  546. }
  547. } elsif ($3 eq '@') {
  548. if ($4 =~ /^Topic by ([^!:])[!:]*: (.*)$/) {
  549. $hash{nick} = $1;
  550. $hash{newtopic} = $2;
  551. } elsif ($4 =~ /^mode (\S+) \[([+-]o)\S* (\S+)[^\]]*\] by ([^!]+)!(\S+)$/) {
  552. $hash{newmode} = $2;
  553. $hash{nick} = $4;
  554. }
  555. } elsif ($3 eq '!') {
  556. if ($4 =~ /^(\S+) is known as (\S+)$/) {
  557. $hash{nick} = $1;
  558. $hash{newnick} = $2;
  559. }
  560. }
  561. } elsif ($config->{format} eq 'grufti') {
  562. $hash{hour} = $1;
  563. $hash{min} = $2;
  564. $hash{nick} = $3;
  565. if ($5 eq 'kicked') {
  566. $hash{kicker} = $3;
  567. $hash{nick} = $6;
  568. } elsif (($4.$5) eq 'haschanged') {
  569. $hash{newtopic} = $9;
  570. } elsif (($4.$5) eq 'modechange') {
  571. $hash{newmode} = substr($6, 1);
  572. $hash{nick} = substr($9, 1);
  573. } elsif ($5 eq 'joined') {
  574. $hash{newjoin} = $1;
  575. } elsif (($3.$4) eq 'Nickchange') {
  576. $hash{nick} = $7;
  577. $hash{newnick} = $9;
  578. }
  579. } else {
  580. die("Format not supported?!\n");
  581. }
  582. return \%hash;
  583. } else {
  584. return;
  585. }
  586. }
  587. sub opchanges
  588. {
  589. my @modes = split(//, $_[0]);
  590. my ($mode,$plus) = ("",0);
  591. my ($gaveop,$tookop) = (0,0);
  592. foreach (@modes) {
  593. if (/^\+$/) { $plus = 1; next; }
  594. elsif (/^-$/) { $plus = 0; next; }
  595. else { next unless /^o$/ }
  596. if ($plus) { $gaveop++ } else { $tookop++ }
  597. }
  598. my @out = ($gaveop,$tookop);
  599. return @out;
  600. }
  601. sub strip_mirccodes
  602. {
  603. my $line = shift;
  604. my $boldcode = chr(2);
  605. my $colorcode = chr(3);
  606. my $plaincode = chr(15);
  607. my $reversecode = chr(22);
  608. my $underlinecode = chr(31);
  609. # Strip mIRC color codes
  610. $line =~ s/$colorcode\d{1,2},\d{1,2}//g;
  611. $line =~ s/$colorcode\d{0,2}//g;
  612. # Strip mIRC bold, plain, reverse and underline codes
  613. $line =~ s/[$boldcode$underlinecode$reversecode$plaincode]//g;
  614. return $line;
  615. }
  616. sub htmlentities
  617. {
  618. my $str = shift;
  619. $str =~ s/\&/\&amp;/g;
  620. $str =~ s/\</\&lt;/g;
  621. $str =~ s/\>/\&gt;/g;
  622. return $str;
  623. }
  624. sub html
  625. {
  626. my $html = shift;
  627. print OUTPUT $html . "\n";
  628. }
  629. sub template_text
  630. {
  631. # This function is for the homemade template system. It receives a name
  632. # of a template and a hash with the fields in the template to update to
  633. # its corresponding value
  634. my $template = shift;
  635. my %hash = @_;
  636. my $text;
  637. if (!$T{$config->{lang}}{$template}) {
  638. # Fall back to English if the language template doesn't exist
  639. $text = $T{EN}{$template};
  640. }
  641. $text = $T{$config->{lang}}{$template};
  642. if (!$text) {
  643. die("No such template $template\n");
  644. }
  645. foreach my $key (sort keys %hash) {
  646. $text =~ s/\[:$key\]/$hash{$key}/;
  647. $text =~ s/ü/&uuml;/g;
  648. $text =~ s/ö/&ouml;/g;
  649. $text =~ s/ä/&auml;/g;
  650. $text =~ s/ß/&szlig;/g;
  651. $text =~ s/å/&aring;/g;
  652. $text =~ s/æ/&aelig;/g;
  653. $text =~ s/ø/&oslash;/g;
  654. }
  655. return $text;
  656. }
  657. sub replace_links
  658. {
  659. # Sub to replace urls and e-mail addys to links
  660. my $str = shift;
  661. my $nick = shift;
  662. if ($nick) {
  663. $str =~ s/(http|https|ftp|telnet|news)(:\/\/[-a-zA-Z0-9_]+.[-a-zA-Z0-9.,_~=:;&@%?#\/+]+)/<a href="$1$2" target="_blank" title="Open in new window: $1$2">$nick<\/a>/g;
  664. $str =~ s/([-a-zA-Z0-9._]+@[-a-zA-Z0-9_]+.[-a-zA-Z0-9._]+)/<a href="mailto:$1" title="Mail to $nick">$nick<\/a>/g;
  665. } else {
  666. $str =~ s/(http|https|ftp|telnet|news)(:\/\/[-a-zA-Z0-9_]+.[-a-zA-Z0-9.,_~=:;&@%?#\/+]+)/<a href="$1$2" target="_blank" title="Open in new window: $1$2">$1$2<\/a>/g;
  667. $str =~ s/([-a-zA-Z0-9._]+@[-a-zA-Z0-9_]+.[-a-zA-Z0-9._]+)/<a href="mailto:$1" title="Mail to $1">$1<\/a>/g;
  668. }
  669. return $str;
  670. }
  671. sub debug
  672. {
  673. if ($config->{debug}) {
  674. my $debugline = $_[0] . "\n";
  675. print DEBUG $debugline;
  676. }
  677. }
  678. sub find_alias
  679. {
  680. my $nick = shift;
  681. foreach (keys %alias) {
  682. return $_ if (grep /^\Q$nick\E$/i, @{$alias{$_}});
  683. }
  684. return $nick;
  685. }
  686. sub create_html
  687. {
  688. # This is where all subroutines get executed, you can actually design
  689. # your own layout here, the lines should be self-explainable
  690. print "Now generating HTML($config->{outputfile})...\n";
  691. open (OUTPUT, "> $config->{outputfile}") or die("$0: Unable to open outputfile($config->{outputfile}): $!\n");
  692. htmlheader();
  693. pageheader();
  694. activetimes();
  695. activenicks();
  696. headline(template_text('bignumtopic'));
  697. html("<table width=\"614\">\n"); # Needed for sections
  698. questions();
  699. loudpeople();
  700. mostsmiles();
  701. mostsad();
  702. longlines();
  703. shortlines();
  704. html("</table>"); # Needed for sections
  705. mostusedword();
  706. mostreferenced();
  707. headline(template_text('othernumtopic'));
  708. html("<table width=\"614\">\n"); # Needed for sections
  709. gotkicks();
  710. mostkicks();
  711. mostop();
  712. mostmonologues();
  713. mostjoins();
  714. mostfoul();
  715. html("</table>"); # Needed for sections
  716. headline(template_text('latesttopic'));
  717. html("<table width=\"614\">\n"); # Needed for sections
  718. lasttopics();
  719. html("</table>"); # Needed for sections
  720. my %hash = ( lines => $lines );
  721. html(template_text('totallines', %hash) . "<br><br>");
  722. htmlfooter();
  723. close(OUTPUT);
  724. }
  725. sub activetimes
  726. {
  727. # The most actives times on the channel
  728. my (%output, $tbgcolor);
  729. &headline(template_text('activetimestopic'));
  730. my @toptime = sort { $times{$b} <=> $times{$a} } keys %times;
  731. my $highest_value = $times{$toptime[0]};
  732. my @now = localtime($timestamp);
  733. my $image;
  734. for my $hour (sort keys %times) {
  735. debug("Time: $hour => ". $times{$hour});
  736. if ($toptime[0] == $hour) {
  737. $image = $config->{pic2};
  738. } else {
  739. $image = $config->{pic1};
  740. }
  741. my $size = ($times{$hour} / $highest_value) * 100;
  742. my $percent = ($times{$hour} / $lines) * 100;
  743. $percent =~ s/(\.\d)\d+/$1/;
  744. if ($config->{timeoffset} =~ /\+(\d+)/) {
  745. # We must plus some hours to the time
  746. $hour += $1;
  747. $hour = $hour % 24;
  748. if ($hour < 10) { $hour = "0" . $hour; }
  749. } elsif ($config->{timeoffset} =~ /-(\d+)/) {
  750. # We must remove some hours from the time
  751. $hour -= $1;
  752. $hour = $hour % 24;
  753. if ($hour < 10) { $hour = "0" . $hour; }
  754. }
  755. $output{$hour} = "<td align=\"center\" valign=\"bottom\" class=\"asmall\">$percent%<br><img src=\"$image\" width=\"15\" height=\"$size\" alt=\"$percent\"></td>\n";
  756. }
  757. html("<table border=\"0\" width=\"614\"><tr>\n");
  758. for ($b = 0; $b < 24; $b++) {
  759. if ($b < 10) { $a = "0" . $b; } else { $a = $b; }
  760. if (!defined($output{$a}) || $output{$a} eq "") {
  761. html("<td align=\"center\" valign=\"bottom\" class=\"asmall\">0%</td>");
  762. } else {
  763. html($output{$a});
  764. }
  765. }
  766. html("</tr><tr>");
  767. for ($b = 0; $b < 24; $b++) {
  768. if ($now[2] == $b) { $tbgcolor = "\#AAAAAA"; } else { $tbgcolor = "\#CCCCCC"; }
  769. html("<td bgcolor=\"$tbgcolor\" align=\"center\" class=\"small\">$b</td>");
  770. }
  771. html("</tr></table>");
  772. }
  773. sub activenicks
  774. {
  775. # The most active nicks (those who wrote most lines)
  776. headline(template_text('activenickstopic'));
  777. html("<table border=\"0\" width=\"614\"><tr>");
  778. html("<td>&nbsp;</td><td bgcolor=\"$config->{tdtop}\"><b>" . template_text('nick') . "</b></td><td bgcolor=\"$config->{tdtop}\"><b>" . template_text('numberlines') ."</b></td><td bgcolor=\"$config->{tdtop}\"><b>". template_text('randquote') ."</b></td>");
  779. if (%userpics) {
  780. html("<td bgcolor=\"$config->{tdtop}\"><b>" . template_text('userpic') ."</b></td>");
  781. }
  782. html("</tr>");
  783. my @active = sort { $line{$b} <=> $line{$a} } keys %line;
  784. if ($config->{activenicks} > $nicks) {
  785. $config->{activenicks} = $nicks;
  786. print "Note: There was less nicks in the logfile than your specificied there to be in most active nicks...\n";
  787. }
  788. my ($nick, $visiblenick, $randomline);
  789. my $i = 1;
  790. for (my $c = 0; $c < $config->{activenicks}; $c++) {
  791. $nick = $active[$c];
  792. $visiblenick = $active[$c];
  793. if (!$longlines{$nick}) {
  794. $randomline = "";
  795. } else {
  796. my $rand = rand($longlines{$nick});
  797. $randomline = $sayings{$nick}[$rand];
  798. }
  799. # Convert URLs and e-mail addys to links
  800. $randomline = replace_links($randomline);
  801. # Add a link to the nick if there is any
  802. if ($userlinks{$nick}) {
  803. $visiblenick = replace_links($userlinks{$nick}, $nick);
  804. }
  805. my $h = $config->{hicell};
  806. $h =~ s/^#//;
  807. $h = hex $h;
  808. my $h2 = $config->{hicell2};
  809. $h2 =~ s/^#//;
  810. $h2 = hex $h2;
  811. my $f_b = $h & 0xff;
  812. my $f_g = ($h & 0xff00) >> 8;
  813. my $f_r = ($h & 0xff0000) >> 16;
  814. my $t_b = $h2 & 0xff;
  815. my $t_g = ($h2 & 0xff00) >> 8;
  816. my $t_r = ($h2 & 0xff0000) >> 16;
  817. my $col_b = sprintf "%0.2x", abs int(((($t_b - $f_b) / $config->{activenicks}) * +$c) + $f_b);
  818. my $col_g = sprintf "%0.2x", abs int(((($t_g - $f_g) / $config->{activenicks}) * +$c) + $f_g);
  819. my $col_r = sprintf "%0.2x", abs int(((($t_r - $f_r) / $config->{activenicks}) * +$c) + $f_r);
  820. html("<tr><td bgcolor=\"$config->{rankc}\" align=\"left\">");
  821. my $line = $line{$nick};
  822. html("$i</td><td bgcolor=\"#$col_r$col_g$col_b\">$visiblenick</td><td bgcolor=\"#$col_r$col_g$col_b\">$line</td><td bgcolor=\"#$col_r$col_g$col_b\">");
  823. html("\"$randomline\"</td>");
  824. if ($userpics{$nick}) {
  825. html("<td bgcolor=\"#$col_r$col_g$col_b\" align=\"center\"><img valign=\"middle\" src=\"$config->{imagepath}$userpics{$nick}\"></td>");
  826. }
  827. html("</tr>");
  828. $i++;
  829. }
  830. html("</table><br>");
  831. # Almost as active nicks ('These didn't make it to the top..')
  832. my $nickstoshow = $config->{activenicks} + $config->{activenicks2};
  833. unless ($nickstoshow > $nicks) {
  834. html("<br><b><i>" . template_text('nottop') . "</i></b><table><tr>");
  835. for (my $c = $config->{activenicks}; $c < $nickstoshow; $c++) {
  836. unless ($c % 5) { unless ($c == $config->{activenicks}) { html("</tr><tr>"); } }
  837. html("<td bgcolor=\"$config->{rankc}\" class=\"small\">");
  838. my $nick = $active[$c];
  839. my $lines = $line{$nick};
  840. html("$nick ($lines)</td>");
  841. }
  842. html("</table>");
  843. }
  844. }
  845. sub mostusedword
  846. {
  847. # Lao the infamous word usage statistics
  848. my %usages;
  849. foreach my $word (sort keys %wordcount) {
  850. next if exists $line{$word};
  851. $usages{$word} = $wordcount{$word};
  852. }
  853. my @popular = sort { $usages{$b} <=> $usages{$a} } keys %usages;
  854. if (@popular) {
  855. &headline(template_text('mostwordstopic'));
  856. html("<table border=\"0\" width=\"614\"><tr>");
  857. html("<td>&nbsp;</td><td bgcolor=\"$config->{tdtop}\"><b>" . template_text('word') . "</b></td>");
  858. html("<td bgcolor=\"$config->{tdtop}\"><b>" . template_text('numberuses') . "</b></td>");
  859. html("<td bgcolor=\"$config->{tdtop}\"><b>" . template_text('lastused') . "</b></td>");
  860. for(my $i = 0; $i < 10; $i++) {
  861. last unless $i < $#popular;
  862. my $a = $i + 1;
  863. my $popular = $popular[$i];
  864. my $wordcount = $wordcount{$popular[$i]};
  865. my $lastused = $lastused{$popular[$i]};
  866. html("<tr><td bgcolor=\"$config->{rankc}\"><b>$a</b>");
  867. html("<td bgcolor=\"$config->{hicell}\">$popular</td>");
  868. html("<td bgcolor=\"$config->{hicell}\">$wordcount</td>");
  869. html("<td bgcolor=\"$config->{hicell}\">$lastused</td>");
  870. html("</tr>");
  871. }
  872. html("</table>");
  873. }
  874. }
  875. sub mostreferenced
  876. {
  877. my %usages;
  878. foreach my $word (sort keys %wordcount) {
  879. next unless exists $line{$word};
  880. $usages{$word} = $wordcount{$word};
  881. }
  882. my @popular = sort { $usages{$b} <=> $usages{$a} } keys %usages;
  883. if (@popular) {
  884. &headline(template_text('referencetopic'));
  885. html("<table border=\"0\" width=\"614\"><tr>");
  886. html("<td>&nbsp;</td><td bgcolor=\"$config->{tdtop}\"><b>" . template_text('nick') . "</b></td>");
  887. html("<td bgcolor=\"$config->{tdtop}\"><b>" . template_text('numberuses') . "</b></td>");
  888. html("<td bgcolor=\"$config->{tdtop}\"><b>" . template_text('lastused') . "</b></td>");
  889. for(my $i = 0; $i < 5; $i++) {
  890. last unless $i < $#popular;
  891. my $a = $i + 1;
  892. my $popular = $popular[$i];
  893. my $wordcount = $wordcount{$popular[$i]};
  894. my $lastused = $lastused{$popular[$i]};
  895. html("<tr><td bgcolor=\"$config->{rankc}\"><b>$a</b>");
  896. html("<td bgcolor=\"$config->{hicell}\">$popular</td>");
  897. html("<td bgcolor=\"$config->{hicell}\">$wordcount</td>");
  898. html("<td bgcolor=\"$config->{hicell}\">$lastused</td>");
  899. html("</tr>");
  900. }
  901. html("</table>");
  902. }
  903. }
  904. sub questions
  905. {
  906. # Persons who asked the most questions
  907. foreach my $nick (sort keys %question) {
  908. if ($line{$nick} > 100) {
  909. $qpercent{$nick} = ($question{$nick} / $line{$nick}) * 100;
  910. $qpercent{$nick} =~ s/(\.\d)\d+/$1/;
  911. }
  912. }
  913. my @question = sort { $qpercent{$b} <=> $qpercent{$a} } keys %qpercent;
  914. if (@question) {
  915. my %hash = (
  916. nick => $question[0],
  917. per => $qpercent{$question[0]}
  918. );
  919. my $text = template_text('question1', %hash);
  920. html("<tr><td bgcolor=\"$config->{hicell}\">$text");
  921. if (@question >= 2) {
  922. my %hash = (
  923. nick => $question[1],
  924. per => $qpercent{$question[1]}
  925. );
  926. my $text = template_text('question2', %hash);
  927. html("<br><span class=\"small\">$text</span>");
  928. }
  929. html("</td></tr>");
  930. } else {
  931. html("<tr><td bgcolor=\"$config->{hicell}\">" . template_text('question3') . "</td></tr>");
  932. }
  933. }
  934. sub loudpeople
  935. {
  936. # The ones who speak LOUDLY!
  937. foreach my $nick (sort keys %loud) {
  938. if ($line{$nick} > 100) {
  939. $lpercent{$nick} = ($loud{$nick} / $line{$nick}) * 100;
  940. $lpercent{$nick} =~ s/(\.\d)\d+/$1/;
  941. }
  942. }
  943. my @loud = sort { $lpercent{$b} <=> $lpercent{$a} } keys %lpercent;
  944. if (@loud) {
  945. my %hash = (
  946. nick => $loud[0],
  947. per => $lpercent{$loud[0]}
  948. );
  949. my $text = template_text('loud1', %hash);
  950. html("<tr><td bgcolor=\"$config->{hicell}\">$text");
  951. if (@loud >= 2) {
  952. my %hash = (
  953. nick => $loud[1],
  954. per => $lpercent{$loud[1]}
  955. );
  956. my $text = template_text('loud2', %hash);
  957. html("<br><span class=\"small\">$text</span>");
  958. }
  959. html("</td></tr>");
  960. } else {
  961. my $text = template_text('loud3');
  962. html("<tr><td bgcolor=\"$config->{hicell}\">$text</td></tr>");
  963. }
  964. }
  965. sub gotkicks
  966. {
  967. # The persons who got kicked the most
  968. my @gotkick = sort { $gotkick{$b} <=> $gotkick{$a} } keys %gotkick;
  969. if (@gotkick) {
  970. my %hash = (
  971. nick => $gotkick[0],
  972. kicks => $gotkick{$gotkick[0]}
  973. );
  974. my $text = template_text('gotkick1', %hash);
  975. html("<tr><td bgcolor=\"$config->{hicell}\">$text");
  976. if (@gotkick >= 2) {
  977. my %hash = (
  978. nick => $gotkick[1],
  979. kicks => $gotkick{$gotkick[1]}
  980. );
  981. my $text = template_text('gotkick2', %hash);
  982. html("<br><span class=\"small\">$text</span>");
  983. }
  984. html("</td></tr>");
  985. }
  986. }
  987. sub mostjoins
  988. {
  989. my @joins = sort { $joins{$b} <=> $joins{$a} } keys %joins;
  990. if (@joins) {
  991. my %hash = (
  992. nick => $joins[0],
  993. joins => $joins{$joins[0]}
  994. );
  995. my $text = template_text('joins', %hash);
  996. html("<tr><td bgcolor=\"$config->{hicell}\">$text</td></tr>");
  997. }
  998. }
  999. sub mostkicks
  1000. {
  1001. # The person who got kicked the most
  1002. my @kicked = sort { $kicked{$b} <=> $kicked{$a} } keys %kicked;
  1003. if (@kicked) {
  1004. my %hash = (
  1005. nick => $kicked[0],
  1006. kicked => $kicked{$kicked[0]}
  1007. );
  1008. my $text = template_text('kick1', %hash);
  1009. html("<tr><td bgcolor=\"$config->{hicell}\">$text");
  1010. if (@kicked >= 2) {
  1011. my %hash = (
  1012. oldnick => $kicked[0],
  1013. nick => $kicked[1],
  1014. kicked => $kicked{$kicked[1]}
  1015. );
  1016. my $text = template_text('kick2', %hash);
  1017. html("<br><span class=\"small\">$text</span>");
  1018. }
  1019. html("</td></tr>");
  1020. } else {
  1021. my $text = template_text('kick3');
  1022. html("<tr><td bgcolor=\"$config->{hicell}\">$text</td></tr>");
  1023. }
  1024. }
  1025. sub mostmonologues
  1026. {
  1027. # The person who had the most monologues (speaking to himself)
  1028. my @monologue = sort { $monologue{$b} <=> $monologue{$a} } keys %monologue;
  1029. if (@monologue) {
  1030. my %hash = (
  1031. nick => $monologue[0],
  1032. monos => $monologue{$monologue[0]}
  1033. );
  1034. my $text = template_text('mono1', %hash);
  1035. html("<tr><td bgcolor=\"$config->{hicell}\">$text");
  1036. if (@monologue >= 2) {
  1037. my %hash = (
  1038. nick => $monologue[1],
  1039. monos => $monologue{$monologue[1]}
  1040. );
  1041. my $text = template_text('mono2', %hash);
  1042. if ($monologue{$monologue[1]} == 1 && $config->{lang} eq 'EN') {
  1043. $text = substr $text, 0, -1;
  1044. }
  1045. html("<br><span class=\"small\">$text</span>");
  1046. }
  1047. html("</td></tr>");
  1048. }
  1049. }
  1050. sub longlines
  1051. {
  1052. my %len;
  1053. # The person(s) who wrote the longest lines
  1054. foreach my $nick (sort keys %length) {
  1055. if ($line{$nick} > 100) {
  1056. $len{$nick} = $length{$nick} / $line{$nick};
  1057. $len{$nick} =~ s/(\.\d)\d+/$1/;
  1058. }
  1059. }
  1060. my @len = sort { $len{$b} <=> $len{$a} } keys %len;
  1061. my $all_lines = $normals + $actions;
  1062. my $totalaverage;
  1063. if ($all_lines > 0) {
  1064. $totalaverage = $totallength / $all_lines;
  1065. $totalaverage =~ s/(\.\d)\d+/$1/;
  1066. }
  1067. if (@len) {
  1068. my %hash = (
  1069. nick => $len[0],
  1070. letters => $len{$len[0]}
  1071. );
  1072. my $text = template_text('long1', %hash);
  1073. html("<tr><td bgcolor=\"$config->{hicell}\">$text<br>");
  1074. if (@len >= 2) {
  1075. %hash = (
  1076. channel => $config->{channel},
  1077. avg => $totalaverage
  1078. );
  1079. $text = template_text('long2', %hash);
  1080. html("<span class=\"small\">$text</span></td></tr>");
  1081. }
  1082. }
  1083. }
  1084. sub shortlines
  1085. {
  1086. # This sub should be combined with the longlines sub at some point.. it
  1087. # does basically the same thing.
  1088. my %len;
  1089. # The person(s) who wrote the shortest lines
  1090. foreach my $nick (sort keys %length) {
  1091. if ($line{$nick} > 5) {
  1092. $len{$nick} = $length{$nick} / $line{$nick};
  1093. $len{$nick} =~ s/(\.\d)\d+/$1/;
  1094. }
  1095. }
  1096. my @len = sort { $len{$a} <=> $len{$b} } keys %len;
  1097. if (@len) {
  1098. my %hash = (
  1099. nick => $len[0],
  1100. letters => $len{$len[0]}
  1101. );
  1102. my $text = template_text('short1', %hash);
  1103. html("<tr><td bgcolor=\"$config->{hicell}\">$text<br>");
  1104. if (@len >= 2) {
  1105. %hash = (
  1106. nick => $len[1],
  1107. letters => $len{$len[1]}
  1108. );
  1109. $text = template_text('short2', %hash);
  1110. html("<span class=\"small\">$text</span></td></tr>");
  1111. }
  1112. }
  1113. }
  1114. sub mostfoul
  1115. {
  1116. my %spercent;
  1117. foreach my $nick (sort keys %foul) {
  1118. if ($line{$nick} > 15) {
  1119. $spercent{$nick} = ($foul{$nick} / $line{$nick}) * 100;
  1120. $spercent{$nick} =~ s/(\.\d)\d+/$1/;
  1121. }
  1122. }
  1123. my @foul = sort { $spercent{$b} <=> $spercent{$a} } keys %spercent;
  1124. if (@foul) {
  1125. my %hash = (
  1126. nick => $foul[0],
  1127. per => $spercent{$foul[0]}
  1128. );
  1129. my $text = template_text('foul1', %hash);
  1130. html("<tr><td bgcolor=\"$config->{hicell}\">$text");
  1131. if (@foul >= 2) {
  1132. my %hash = (
  1133. nick => $foul[1],
  1134. per => $spercent{$foul[1]}
  1135. );
  1136. my $text = template_text('foul2', %hash);
  1137. html("<br><span class=\"small\">$text</span>");
  1138. }
  1139. html("</td></tr>");
  1140. } else {
  1141. my %hash = (
  1142. channel => $config->{channel}
  1143. );
  1144. my $text = template_text('foul3', %hash);
  1145. html("<tr><td bgcolor=\"$config->{hicell}\">$text</td></tr>");
  1146. }
  1147. }
  1148. sub mostsad
  1149. {
  1150. my %spercent;
  1151. foreach my $nick (sort keys %sadface) {
  1152. if ($line{$nick} > 100) {
  1153. $spercent{$nick} = ($sadface{$nick} / $line{$nick}) * 100;
  1154. $spercent{$nick} =~ s/(\.\d)\d+/$1/;
  1155. }
  1156. }
  1157. my @sadface = sort { $spercent{$b} <=> $spercent{$a} } keys %spercent;
  1158. if (@sadface) {
  1159. my %hash = (
  1160. nick => $sadface[0],
  1161. per => $spercent{$sadface[0]}
  1162. );
  1163. my $text = template_text('sad1', %hash);
  1164. html("<tr><td bgcolor=\"$config->{hicell}\">$text");
  1165. if (@sadface >= 2) {
  1166. my %hash = (
  1167. nick => $sadface[1],
  1168. per => $spercent{$sadface[1]}
  1169. );
  1170. my $text = template_text('sad2', %hash);
  1171. html("<br><span class=\"small\">$text</span>");
  1172. }
  1173. html("</td></tr>");
  1174. } else {
  1175. my %hash = (
  1176. channel => $config->{channel}
  1177. );
  1178. my $text = template_text('sad3', %hash);
  1179. html("<tr><td bgcolor=\"$config->{hicell}\">$config->text</td></tr>");
  1180. }
  1181. }
  1182. sub mostop
  1183. {
  1184. my @ops = sort { $gaveop{$b} <=> $gaveop{$a} } keys %gaveop;
  1185. my @deops = sort { $tookop{$b} <=> $tookop{$a} } keys %tookop;
  1186. if (@ops) {
  1187. my %hash = (
  1188. nick => $ops[0],
  1189. ops => $gaveop{$ops[0]}
  1190. );
  1191. my $text = template_text('mostop1', %hash);
  1192. html("<tr><td bgcolor=\"$config->{hicell}\">$text");
  1193. if (@ops >= 2) {
  1194. my %hash = (
  1195. nick => $ops[1],
  1196. ops => $gaveop{$ops[1]}
  1197. );
  1198. my $text = template_text('mostop2', %hash);
  1199. html("<br><span class=\"small\">$text</span>");
  1200. }
  1201. html("</td></tr>");
  1202. } else {
  1203. my %hash = ( channel => $config->{channel} );
  1204. my $text = template_text('mostop3', %hash);
  1205. html("<tr><td bgcolor=\"$config->{hicell}\">$text</td></tr>");
  1206. }
  1207. if (@deops) {
  1208. my %hash = (
  1209. channel => $config->{channel},
  1210. nick => $deops[0],
  1211. deops => $tookop{$deops[0]}
  1212. );
  1213. my $text = template_text('mostdeop1', %hash);
  1214. html("<tr><td bgcolor=\"$config->{hicell}\">$text");
  1215. if (@deops >= 2) {
  1216. my %hash = (
  1217. nick => $deops[1],
  1218. deops => $tookop{$deops[1]}
  1219. );
  1220. my $text = template_text('mostdeop2', %hash);
  1221. html("<br><span class=\"small\">$text</span>");
  1222. }
  1223. html("</td></tr>");
  1224. } else {
  1225. my %hash = ( channel => $config->{channel} );
  1226. my $text = template_text('mostdeop3', %hash);
  1227. html("<tr><td bgcolor=\"$config->{hicell}\">$text");
  1228. }
  1229. }
  1230. sub mostsmiles
  1231. {
  1232. # The person(s) who smiled the most :-)
  1233. my %spercent;
  1234. foreach my $nick (sort keys %smile) {
  1235. if ($line{$nick} > 100) {
  1236. $spercent{$nick} = ($smile{$nick} / $line{$nick}) * 100;
  1237. $spercent{$nick} =~ s/(\.\d)\d+/$1/;
  1238. }
  1239. }
  1240. my @smiles = sort { $spercent{$b} <=> $spercent{$a} } keys %spercent;
  1241. if (@smiles) {
  1242. my %hash = (
  1243. nick => $smiles[0],
  1244. per => $spercent{$smiles[0]}
  1245. );
  1246. my $text = template_text('smiles1', %hash);
  1247. html("<tr><td bgcolor=\"$config->{hicell}\">$text");
  1248. if (@smiles >= 2) {
  1249. my %hash = (
  1250. nick => $smiles[1],
  1251. per => $spercent{$smiles[1]}
  1252. );
  1253. my $text = template_text('smiles2', %hash);
  1254. html("<br><span class=\"small\">$text</span>");
  1255. }
  1256. html("</td></tr>");
  1257. } else {
  1258. my %hash = (
  1259. channel => $config->{channel}
  1260. );
  1261. my $text = template_text('smiles3', %hash);
  1262. html("<tr><td bgcolor=\"$config->{hicell}\">$text</td></tr>");
  1263. }
  1264. }
  1265. sub lasttopics
  1266. {
  1267. debug("Total number of topics: ". scalar @topics);
  1268. if (@topics) {
  1269. my $ltopic = @topics - 1;
  1270. my $tlimit = 0;
  1271. $config->{topichistory} -= 1;
  1272. if ($ltopic > $config->{topichistory}) { $tlimit = $ltopic - $config->{topichistory}; }
  1273. for (my $i = $ltopic; $i >= $tlimit; $i--) {
  1274. $topics[$i]{"topic"} = replace_links($topics[$i]{"topic"});
  1275. my $topic = $topics[$i]{topic};
  1276. my $nick = $topics[$i]{nick};
  1277. my $hour = $topics[$i]{hour};
  1278. my $min = $topics[$i]{min};
  1279. html("<tr><td bgcolor=\"$config->{hicell}\"><i>$topic</i></td>");
  1280. html("<td bgcolor=\"$config->{hicell}\">By <b>$nick</b> at <b>$hour:$min</b></td></tr>");
  1281. }
  1282. } else {
  1283. html("<tr><td bgcolor=\"$config->{hicell}\">" . template_text('notopic') ."</td></tr>");
  1284. }
  1285. }
  1286. # Some HTML subs
  1287. sub htmlheader
  1288. {
  1289. print OUTPUT <<HTML;
  1290. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  1291. <html>
  1292. <head>
  1293. <title>$config->{channel} @ $config->{network} channel statistics</title>
  1294. <style type="text/css">
  1295. a { text-decoration: none }
  1296. a:link { color: $config->{link}; }
  1297. a:visited { color: $config->{vlink}; }
  1298. a:hover { text-decoration: underline; color: $config->{hlink} }
  1299. body {
  1300. background-color: $config->{bgcolor};
  1301. font-family: verdana, arial, sans-serif;
  1302. font-size: 13px;
  1303. color: $config->{text};
  1304. }
  1305. td {
  1306. font-family: verdana, arial, sans-serif;
  1307. font-size: 13px;
  1308. color: $config->{tdcolor};
  1309. }
  1310. .title {
  1311. font-family: tahoma, arial, sans-serif;
  1312. font-size: 16px;
  1313. font-weight: bold;
  1314. }
  1315. .headline { color: $config->{hcolor}; }
  1316. .small { font-family: verdana, arial, sans-serif; font-size: 10px; }
  1317. .asmall { font-family: arial narrow, sans-serif; font-size: 10px }
  1318. </style></head>
  1319. <body>
  1320. <div align="center">
  1321. HTML
  1322. my %hash = (
  1323. channel => $config->{channel},
  1324. network => $config->{network},
  1325. maintainer => $config->{maintainer},
  1326. time => $time,
  1327. days => $days,
  1328. nicks => $nicks,
  1329. channel => $config->{channel}
  1330. );
  1331. print OUTPUT "<span class=\"title\">" . template_text('pagetitle1', %hash) . "</span><br>";
  1332. print OUTPUT "<br>" . template_text('pagetitle2', %hash) . "<br>";
  1333. print OUTPUT template_text('pagetitle3', %hash) . "<br><br>";
  1334. }
  1335. sub htmlfooter
  1336. {
  1337. print OUTPUT <<HTML;
  1338. <span class="small">
  1339. Stats generated by <a href="http://www.wtf.dk/hp/index.php?page=pisg" title="Go to the pisg homepage">pisg</a> $config->{version}<br>
  1340. pisg by <a href="http://www.wtf.dk/hp/" title="Go to the authors homepage">Morten "LostStar" Brix Pedersen</a> and others<br>
  1341. Stats generated in $processtime
  1342. </span>
  1343. </div>
  1344. </body>
  1345. </html>
  1346. HTML
  1347. }
  1348. sub headline
  1349. {
  1350. my ($title) = @_;
  1351. print OUTPUT <<HTML;
  1352. <br>
  1353. <table width="610" cellpadding="1" cellspacing="0" border="0">
  1354. <tr>
  1355. <td bgcolor="$config->{headline}">
  1356. <table width="100%" cellpadding="2" cellspacing="0" border="0" align="center">
  1357. <tr>
  1358. <td bgcolor="$config->{hbgcolor}" class="text10">
  1359. <div align="center" class="headline"><b>$title</b></div>
  1360. </td>
  1361. </tr>
  1362. </table>
  1363. </td>
  1364. </tr>
  1365. </table>
  1366. HTML
  1367. }
  1368. sub pageheader
  1369. {
  1370. if ($config->{pagehead} ne 'none') {
  1371. open(PAGEHEAD, $config->{pagehead}) or die("$0: Unable to open $config->{pagehead} for reading: $!\n");
  1372. while (<PAGEHEAD>) {
  1373. html($_);
  1374. }
  1375. }
  1376. }
  1377. sub close_debug
  1378. {
  1379. if ($config->{debug}) {
  1380. close(DEBUG) or print STDERR "$0: Cannot close debugfile($config->{debugfile}): $!\n";
  1381. }
  1382. }
  1383. sub get_cmdlineoptions
  1384. {
  1385. my $tmp;
  1386. # Commandline options
  1387. my $help;
  1388. my $usage = <<END_USAGE;
  1389. Usage: pisg.pl [-c channel] [-l logfile] [-o outputfile] [-m
  1390. maintainer] [-f format] [-n network] [-d logdir] [-a aliasfile]
  1391. [-i ignorefile] [-h]
  1392. -c --channel=xxx : Set channel name
  1393. -l --logfile=xxx : Log file to parse
  1394. -o --outfile=xxx : Name of html file to create
  1395. -m --maintainer=xxx : Channel/statistics maintainer
  1396. -f --format=xxx : Logfile format [see FORMATS file]
  1397. -n --network=xxx : IRC Network this channel is on.
  1398. -d --dir=xxx : Analyze all files in this dir. Ignores logfile.
  1399. -p --prefix=xxx : Analyse only files starting with xxx in dir.
  1400. Only works with --dir
  1401. -u --configfile=xxx : Config file
  1402. -h --help : Output this message and exit (-? also works).
  1403. Example:
  1404. \$ pisg.pl -n IRCnet -f xchat -o suid.html -c \\#channel -l logfile.log
  1405. As always, all options may also be defined by editing the source and calling
  1406. pisg without arguments.
  1407. END_USAGE
  1408. if (GetOptions('channel=s' => \$config->{channel},
  1409. 'logfile=s' => \$config->{logfile},
  1410. 'format=s' => \$config->{format},
  1411. 'network=s' => \$config->{network},
  1412. 'maintainer=s' => \$config->{maintainer},
  1413. 'outfile=s' => \$config->{outputfile},
  1414. 'dir=s' => \$config->{logdir},
  1415. 'prefix=s' => \$config->{prefix},
  1416. 'ignorefile=s' => \$tmp,
  1417. 'aliasfile=s' => \$tmp,
  1418. 'configfile=s' => \$config->{configfile},
  1419. 'help|?' => \$help
  1420. ) == 0 or $help) {
  1421. die($usage);
  1422. }
  1423. if (@ARGV) {
  1424. if ($ARGV[0]) { $config->{channel} = $ARGV[0]; }
  1425. if ($ARGV[1]) { $config->{logfile} = $ARGV[1]; }
  1426. if ($ARGV[2]) { $config->{outputfile} = $ARGV[2]; }
  1427. if ($ARGV[3]) { $config->{maintainer} = $ARGV[3]; }
  1428. }
  1429. if ($tmp) {
  1430. die("The aliasfile and ignorefile has been obsoleted by the new
  1431. pisg.cfg, please use that instead [look in pisg.cfg]\n");
  1432. }
  1433. }
  1434. sub get_language_templates
  1435. {
  1436. use FindBin;
  1437. open(FILE, $config->{langfile}) or open (FILE, $FindBin::Bin . "/$config->{langfile}") or die("$0: Unable to open language file($config->{langfile}): $!\n");
  1438. my $current_lang;
  1439. while (<FILE>)
  1440. {
  1441. my $line = $_;
  1442. next if /^#/;
  1443. if ($line =~ /<lang name=\"([^"]+)\">/) {
  1444. # Found start tag, setting the current language
  1445. $current_lang = "$1";
  1446. }
  1447. elsif ($line =~ /<\/lang>/) {
  1448. # Found end tag, resetting the current language
  1449. $current_lang = '';
  1450. }
  1451. elsif ($line =~ /(\w+) = "(.*)"$/ && $current_lang ne '') {
  1452. $T{$current_lang}{$1} = $2;
  1453. }
  1454. }
  1455. close(FILE);
  1456. print "Using language template: $config->{lang}\n\n" if ($config->{lang} ne 'EN');
  1457. }
  1458. &main(); # Run the script