addalias.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. #!/usr/bin/perl
  2. use strict;
  3. use CGI qw/:standard/;
  4. use CGI::Carp qw(fatalsToBrowser carpout);
  5. ###########################################################################
  6. # #
  7. # addalias version 2.2 by deadlock (deadlock@cheeseheadz.net) #
  8. # #
  9. # This script can be used on a webpage for users to enter and edit their #
  10. # own info for the pisg ircstats program by mbrix. #
  11. # #
  12. # addalias v2+ is based on the original addalias program by Doomshammer #
  13. # #
  14. ###########################################################################
  15. #############################
  16. ### Configuration section ###
  17. #############################
  18. # File locations:
  19. my $pisg_config = "/path/to/pisg.cfg";
  20. # Server URL
  21. # If your script resists in http://myserver/cgi-bin/addalias.pl you should
  22. # set it to: "/cgi-bin"
  23. my $url = "/cgi-bin";
  24. # Page layout:
  25. my $c_bgcolor = "#FFFFFF";
  26. my $c_text = "#000000";
  27. my $c_link = "blue";
  28. my $c_vlink = "#C0C0C0";
  29. my $c_alink = "#C0FFC0";
  30. my $c_border = "#FFFFFF";
  31. my $title = "IRC statistics - user addition page";
  32. # Text on the main page
  33. my $txthead1 = "In this form you can enter the settings (aliases, link and user picture) for your nickname in the IRC-stats.";
  34. my $txthead2 = "Nicknames are allowed only once.";
  35. my $txtnick = "Nickname";
  36. my $txtalias = "Alias(es)";
  37. my $txturl = "URL/E-Mail";
  38. my $txtpic = "Userpic";
  39. my $txtsex = "Sex";
  40. my $txtmale = "M";
  41. my $txtfemale = "F";
  42. my $txtbot = "B";
  43. my $txtignore = "Ignore me";
  44. my $btnsubmit = "Submit";
  45. my $btnupdate = "Update";
  46. my $txtfoot1 = "To update your settings, just enter your nickname and click Submit to retrieve your current settings.";
  47. my $txthelp = "For help on this form click <a href=\"$url/addalias.pl/help\" target=\"_blank\">here</a>.";
  48. my $txtlist = "For a complete list of known nicks please click <a href=\"$url/addalias.pl/list\">here</a>.";
  49. my $txtupdate = "These are your current settings. Edit them where needed and click Update to update your info.";
  50. my $txtaddok = "Your nickname was successfully added.";
  51. my $txtignoreon = "You activated ignore and will not appear in the stats.";
  52. my $txtupdateok = "Your info was successfully updated.";
  53. # Helptext:
  54. my $nickhelp = "Enter the name you want to use in the stats here.";
  55. my $aliashelp = "Add all aliases you use here, seperated by spaces, so they will be joined in the stats. A * is allowed as a wildcard. For example: MyNick[Zzz], MyNick-afk and MyNick-work could be entered as 'MyNick[Zzz] MyNick-*' or just as 'MyNick*'";
  56. my $urlhelp = "You can enter a webpage or e-mail adress here to be linked to your nick in the stats.";
  57. my $pichelp = "If you enter a link to a picture here it will be added to your stats on the page.";
  58. my $sexhelp = "This setting is used to determine if lines in the stats should read 'his' or 'her' or 'bot' when referring to you.";
  59. my $ignorehelp = "If you don't want to be included in the stats, select this option.";
  60. ###################################
  61. ### End config section ###
  62. ### do not edit below this line ###
  63. ###################################
  64. # Main program
  65. my $path = path_info();
  66. $path =~ s!^/!!;
  67. my (%oldnicks, @users, @nick);
  68. my ($frm_nick, $frm_alias, $frm_link, $frm_pic, $frm_sex, $frm_ignore);
  69. my ($old_nick, $old_alias, $old_link, $old_pic, $old_sex, $old_ignore);
  70. my ($old_sexm, $old_sexf, $old_sexb, $old_ignr);
  71. my ($cfg, $fnd);
  72. my ($submitbtn, $frmaction);
  73. htmlheader();
  74. if (!$path) {
  75. $submitbtn = $btnsubmit;
  76. $frmaction="\"$url/addalias.pl/input\"";
  77. $txtupdate = "";
  78. mainpage();
  79. } elsif ($path eq 'help') {
  80. helppage();
  81. } elsif ($path eq 'list') {
  82. $submitbtn = $btnsubmit;
  83. $frmaction="\"$url/addalias.pl/input\"";
  84. $txtupdate = "";
  85. mainpage();
  86. list();
  87. } elsif ($path eq 'input') {
  88. readparams();
  89. if ($frm_nick eq "") {
  90. no_nick();
  91. $submitbtn = $btnsubmit;
  92. $frmaction="\"$url/addalias.pl/input\"";
  93. $txtupdate = "";
  94. mainpage();
  95. } else {
  96. $cfg = read_config();
  97. if ($cfg ne "1") {
  98. $fnd = check_if_found();
  99. if ($fnd eq "1") {
  100. $submitbtn = $btnupdate;
  101. $frmaction="\"update\"";
  102. $txtfoot1="";
  103. mainpage();
  104. }
  105. else {
  106. $submitbtn = $btnupdate;
  107. $frmaction="\"update\"";
  108. addinfo();
  109. mainpage();
  110. }
  111. }
  112. else {
  113. $submitbtn = $btnupdate;
  114. $frmaction="\"update\"";
  115. addinfo();
  116. mainpage();
  117. }
  118. }
  119. } elsif ($path eq 'update') {
  120. readparams();
  121. if ($frm_nick eq "") {
  122. no_nick();
  123. mainpage();
  124. } else {
  125. $cfg = read_config();
  126. $submitbtn = $btnupdate;
  127. $frmaction="\"update\"";
  128. updateinfo();
  129. mainpage();
  130. }
  131. } else {
  132. print "Illegal calling of script<br>\n";
  133. }
  134. htmlfooter();
  135. # Subs
  136. sub htmlheader
  137. {
  138. print <<HTML
  139. Content-Type: text/html
  140. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  141. <html>
  142. <head>
  143. <title>$title</title>
  144. </head>
  145. <body bgcolor="$c_bgcolor" text="$c_text" link="$c_link" vlink="$c_vlink" alink="$c_alink">
  146. HTML
  147. }
  148. sub htmlfooter
  149. {
  150. print <<HTML
  151. </body>
  152. </html>
  153. HTML
  154. }
  155. sub mainpage
  156. {
  157. print <<HTML
  158. <p>$txthead1<br> $txthead2</p>
  159. $txtupdate<br>
  160. <form action=$frmaction method="POST">
  161. <table width="400" cellpadding="2" cellpadding="2" border="0" style="border: 1px
  162. ridge $c_border">
  163. <tr>
  164. <td><b>$txtnick</b></td>
  165. <td><input name="nick" type="text" width="30" value="$old_nick"></td>
  166. </tr>
  167. <tr>
  168. <td><b>$txtalias</b></td>
  169. <td><input name="alias" type="text" width="30" value="$old_alias"></td>
  170. </tr>
  171. <tr>
  172. <td><b>$txturl</b></td>
  173. <td><input name="link" type="text" width="30" value="$old_link"></td>
  174. </tr>
  175. <tr>
  176. <td><b>$txtpic</b></td>
  177. <td><input name="pic" type="text" width="30" value="$old_pic"></td>
  178. </tr>
  179. <tr>
  180. <td><b>$txtsex</b></td>
  181. <td>$txtmale<input type="radio" name="sex" value="m" $old_sexm>$txtfemale<input type="radio" name="sex" value="f" $old_sexf>$txtbot<input type="radio" name="sex" value="b" $old_sexb></td>
  182. </tr>
  183. <tr>
  184. <td><b>$txtignore</B></td>
  185. <td><input type="checkbox" name="ignore" $old_ignr></td>
  186. </tr>
  187. <tr>
  188. <td></td>
  189. <td>
  190. <input type="submit" value="$submitbtn">
  191. <input type="reset" value="Reset form">
  192. </td>
  193. </tr>
  194. </table>
  195. </form>
  196. $txtfoot1<br>
  197. $txthelp<br><br>
  198. $txtlist<br><br>
  199. HTML
  200. }
  201. sub helppage
  202. {
  203. print <<HTML
  204. <b>$txtnick:</b><br>
  205. $nickhelp<br>
  206. <b>$txtalias:</b><br>
  207. $aliashelp<br>
  208. <b>$txturl:</b><br>
  209. $urlhelp<br>
  210. <b>$txtpic:</b><br>
  211. $pichelp<br>
  212. <b>$txtsex:</b><br>
  213. $sexhelp<br>
  214. <b>$txtignore:</b><br>
  215. $ignorehelp<br>
  216. HTML
  217. }
  218. sub readparams
  219. {
  220. $frm_nick = param('nick');
  221. $frm_alias = param('alias');
  222. $frm_link = param('link');
  223. $frm_pic = param('pic');
  224. $frm_sex = param('sex');
  225. $frm_ignore = param('ignore');
  226. }
  227. sub list
  228. {
  229. open(FILE, "<$pisg_config") or die("Error opening pisg config file: $!");
  230. my $i = 0;
  231. my $nick;
  232. my $alias;
  233. while(<FILE>) {
  234. if($_ =~ /^<user/) {
  235. $users[$i] = $_;
  236. chomp $users[$i];
  237. $i++;
  238. }
  239. }
  240. close(FILE);
  241. $i = 0;
  242. print "<table width=\"800\" cellpadding=\"2\" cellpadding=\"2\" border=\"0\" style=\"border: 1px ridge $c_border\">\n <tr>\n <td align=\"left\"><b>$txtnick</b></td><td align=\"left\"><b>$txtalias</b></td> </tr>\n";
  243. foreach (@users) {
  244. if ($users[$i] =~ /nick=.*/) {
  245. if ($users[$i] =~ /nick="(\S+)"(.*)/) {
  246. $nick = $1;
  247. }
  248. if ($users[$i] =~ /alias="(\S+)".*/ or $users[$i] =~ /alias="(.*)"\s.*/ or $users[$i] =~ /alias="(.*)">/ ) {
  249. $alias = $1;
  250. }
  251. print " <tr>\n <td>$nick</td><td>$alias</td>\n <tr>\n";
  252. }
  253. $i++
  254. }
  255. }
  256. sub read_config
  257. {
  258. open(FILE, "<$pisg_config") or die("Error opening pisg config file: $!");
  259. my $i = 0;
  260. while(<FILE>) {
  261. if($_ =~ /^<user/) {
  262. $users[$i] = $_;
  263. chomp $users[$i];
  264. $i++;
  265. }
  266. }
  267. close(FILE);
  268. my $search = 0;
  269. $i = 0;
  270. foreach (@users) {
  271. if ($users[$i] =~ /nick=/) {
  272. if ($users[$i] =~ /nick="(\S+)"(.*)/) {
  273. $nick[$i] = lc($1);
  274. $oldnicks{$nick[$i]}{'nick'} = $1;
  275. }
  276. if ($users[$i] =~ /alias="(\S+)".*/ or $users[$i] =~ /alias="(.*)"\s.* / or $users[$i] =~ /alias="(.*)">/ ) {
  277. $oldnicks{$nick[$i]}{'alias'} = $1;
  278. }
  279. if ($users[$i] =~ /link="(\S+)"(.*)/) {
  280. $oldnicks{$nick[$i]}{'link'} = $1;
  281. }
  282. if ($users[$i] =~ /pic="(\S+)"(.*)/) {
  283. $oldnicks{$nick[$i]}{'pic'} = $1;
  284. }
  285. if ($users[$i] =~ /sex="(\S+)"(.*)/) {
  286. $oldnicks{$nick[$i]}{'sex'} = $1;
  287. }
  288. if ($users[$i] =~ /ignore="(\S+)"(.*)/) {
  289. my $ignore = $1;
  290. if($ignore eq "y" or $ignore eq "Y") {
  291. $ignore = 1;
  292. } else {
  293. $ignore = 0;
  294. }
  295. $oldnicks{$nick[$i]}{'ignore'} = $ignore;
  296. }
  297. } else {
  298. $search = 1;
  299. }
  300. $i++;
  301. }
  302. return $search;
  303. }
  304. sub no_nick
  305. {
  306. print <<HTML
  307. <font color="red" size="+1"><b>Error:</b> No Nickname given!</font><br>
  308. HTML
  309. ;
  310. }
  311. sub check_if_found
  312. {
  313. my $found = 0;
  314. my $lcnick = lc($frm_nick);
  315. foreach (@nick) {
  316. if (lc($oldnicks{$_}{'nick'}) eq $lcnick) {
  317. $found = 1;
  318. last;
  319. }
  320. }
  321. if ($found eq "1") {
  322. $old_nick = $oldnicks{$lcnick}{'nick'};
  323. $old_alias = $oldnicks{$lcnick}{'alias'};
  324. $old_link = $oldnicks{$lcnick}{'link'};
  325. $old_pic = $oldnicks{$lcnick}{'pic'};
  326. $old_sex = $oldnicks{$lcnick}{'sex'};
  327. $old_ignore = $oldnicks{$lcnick}{'ignore'};
  328. if ($old_sex eq "m" or $old_sex eq "M"){
  329. $old_sexm = "checked";
  330. }
  331. elsif ($old_sex eq "f" or $old_sex eq "F"){
  332. $old_sexf = "checked";
  333. }
  334. elsif ($old_sex eq "b" or $old_sex eq "B"){
  335. $old_sexb = "checked";
  336. }
  337. if ($old_ignore eq "1"){
  338. $old_ignr = "checked";
  339. }
  340. }
  341. return $found;
  342. }
  343. sub addinfo
  344. {
  345. my $line_to_add = "<user";
  346. if($frm_nick) {
  347. $line_to_add .= " nick=\"$frm_nick\"";
  348. }
  349. if($frm_alias) {
  350. $line_to_add .= " alias=\"$frm_alias\"";
  351. }
  352. if($frm_link) {
  353. $line_to_add .= " link=\"$frm_link\"";
  354. }
  355. if($frm_pic) {
  356. $line_to_add .= " pic=\"$frm_pic\"";
  357. }
  358. if($frm_sex) {
  359. $line_to_add .= " sex=\"$frm_sex\"";
  360. }
  361. if($frm_ignore eq "on") {
  362. $line_to_add .= " ignore=\"y\"";
  363. }
  364. $line_to_add .= ">";
  365. open(FILE, ">>$pisg_config") or die("Error writing to configfile: $!");
  366. print FILE "$line_to_add\n";
  367. close(FILE);
  368. $old_nick = $frm_nick;
  369. $old_alias = $frm_alias;
  370. $old_link = $frm_link;
  371. $old_pic = $frm_pic;
  372. $old_sex = $frm_sex;
  373. $old_ignore = $frm_ignore;
  374. if ($old_sex eq "m" or $old_sex eq "M"){
  375. $old_sexm = "checked";
  376. }
  377. elsif ($old_sex eq "f" or $old_sex eq "F"){
  378. $old_sexf = "checked";
  379. }
  380. elsif ($old_sex eq "b" or $old_sex eq "B"){
  381. $old_sexb = "checked";
  382. }
  383. if ($old_ignore eq "1"){
  384. $old_ignr = "checked";
  385. }
  386. print "<font color=\"green\">$txtaddok</font><p>\n";
  387. }
  388. sub updateinfo
  389. {
  390. my $line;
  391. my $line_to_add = "<user";
  392. if ($frm_nick) {
  393. $line_to_add .= " nick=\"$frm_nick\"";
  394. }
  395. if ($frm_alias) {
  396. $line_to_add .= " alias=\"$frm_alias\"";
  397. }
  398. if ($frm_link) {
  399. $line_to_add .= " link=\"$frm_link\"";
  400. }
  401. if ($frm_pic) {
  402. $line_to_add .= " pic=\"$frm_pic\"";
  403. }
  404. if ($frm_sex) {
  405. $line_to_add .= " sex=\"$frm_sex\"";
  406. }
  407. if ($frm_ignore eq "on") {
  408. $line_to_add .= " ignore=\"y\"";
  409. }
  410. $line_to_add .= ">";
  411. open(OLDFILE, "$pisg_config") or die("Error reading configfile: $!");
  412. &lock_file(*OLDFILE);
  413. my @lines = <OLDFILE>;
  414. close(OLDFILE);
  415. open(NEWFILE, ">$pisg_config") or die("Error updating configfile: $!");
  416. &lock_file(*NEWFILE);
  417. foreach $line (@lines) {
  418. if ($line =~ /^<user.*nick=\"\Q$frm_nick\E\"/i) {
  419. print NEWFILE "$line_to_add\n"
  420. } else {
  421. print NEWFILE $line;
  422. }
  423. }
  424. close (NEWFILE);
  425. $old_nick = $frm_nick;
  426. $old_alias = $frm_alias;
  427. $old_link = $frm_link;
  428. $old_pic = $frm_pic;
  429. $old_sex = $frm_sex;
  430. $old_ignore = $frm_ignore;
  431. if ($old_sex eq "m" or $old_sex eq "M"){
  432. $old_sexm = "checked";
  433. }
  434. elsif ($old_sex eq "f" or $old_sex eq "F"){
  435. $old_sexf = "checked";
  436. }
  437. elsif ($old_sex eq "b" or $old_sex eq "B"){
  438. $old_sexb = "checked";
  439. }
  440. if ($old_ignore eq "1"){
  441. $old_ignr = "checked";
  442. }
  443. print "<font color=\"green\">$txtupdateok</font><p>\n";
  444. }
  445. sub lock_file {
  446. my $lock = 2;
  447. flock($_[0], $lock);
  448. }