addalias.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 $txtignore = "Ignore me";
  43. my $btnsubmit = "Submit";
  44. my $btnupdate = "Update";
  45. my $txtfoot1 = "To update your settings, just enter your nickname and click Submit to retrieve your current settings.";
  46. my $txthelp = "For help on this form click <a href=\"$url/addalias.pl/help\" target=\"_blank\">here</a>.";
  47. my $txtlist = "For a complete list of known nicks please click <a href=\"$url/addalias.pl/list\">here</a>.";
  48. my $txtupdate = "These are your current settings. Edit them where needed and click Update to update your info.";
  49. my $txtaddok = "Your nickname was successfully added.:";
  50. my $txtignoreon = "You activated ignore and will not appear in the stats.";
  51. my $txtupdateok = "Your info was successfully updated.:";
  52. # Helptext:
  53. my $nickhelp = "Enter the name you want to use in the stats here.";
  54. 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*'";
  55. my $urlhelp = "You can enter a webpage or e-mail adress here to be linked to your nick in the stats.";
  56. my $pichelp = "If you enter a link to a picture here it will be added to your stats on the page.";
  57. my $sexhelp = "This setting is used to determine if lines in the stats should read 'his' or 'her' when referring to you.";
  58. my $ignorehelp = "If you don't want to be included in the stats, select this option.";
  59. ###################################
  60. ### End config section ###
  61. ### do not edit below this line ###
  62. ###################################
  63. # Main program
  64. my $path = path_info();
  65. $path =~ s!^/!!;
  66. my (%oldnicks, @users, @nick);
  67. my ($frm_nick, $frm_alias, $frm_link, $frm_pic, $frm_sex, $frm_ignore);
  68. my ($old_nick, $old_alias, $old_link, $old_pic, $old_sex, $old_ignore);
  69. my ($old_sexm, $old_sexf, $old_ignr);
  70. my ($cfg, $fnd);
  71. my ($submitbtn, $frmaction);
  72. htmlheader();
  73. if (!$path) {
  74. $submitbtn = $btnsubmit;
  75. $frmaction="\"$url/addalias.pl/input\"";
  76. $txtupdate = "";
  77. mainpage();
  78. } elsif ($path eq 'help') {
  79. helppage();
  80. } elsif ($path eq 'list') {
  81. $submitbtn = $btnsubmit;
  82. $frmaction="\"$url/addalias.pl/input\"";
  83. $txtupdate = "";
  84. mainpage();
  85. list();
  86. } elsif ($path eq 'input') {
  87. readparams();
  88. if ($frm_nick eq "") {
  89. no_nick();
  90. $submitbtn = $btnsubmit;
  91. $frmaction="\"$url/addalias.pl/input\"";
  92. $txtupdate = "";
  93. mainpage();
  94. } else {
  95. $cfg = read_config();
  96. if ($cfg ne "1") {
  97. $fnd = check_if_found();
  98. if ($fnd eq "1") {
  99. $submitbtn = $btnupdate;
  100. $frmaction="\"update\"";
  101. if ($old_sex eq "m" or $old_sex eq "M"){
  102. $old_sexm = "checked";
  103. }
  104. elsif ($old_sex eq "f" or $old_sex eq "F"){
  105. $old_sexf = "checked";
  106. }
  107. if ($old_ignore eq "1"){
  108. $old_ignr = "checked";
  109. }
  110. $txtfoot1="";
  111. mainpage();
  112. }
  113. else {
  114. addinfo();
  115. }
  116. }
  117. else {
  118. addinfo();
  119. }
  120. }
  121. } elsif ($path eq 'update') {
  122. readparams();
  123. if ($frm_nick eq "") {
  124. no_nick();
  125. } else {
  126. $cfg = read_config();
  127. updateinfo();
  128. }
  129. } else {
  130. print "Illegal calling of script<br>\n";
  131. }
  132. htmlfooter();
  133. # Subs
  134. sub htmlheader
  135. {
  136. print <<HTML
  137. Content-Type: text/html
  138. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  139. <html>
  140. <head>
  141. <title>$title</title>
  142. </head>
  143. <body bgcolor="$c_bgcolor" text="$c_text" link="$c_link" vlink="$c_vlink" alink="$c_alink">
  144. HTML
  145. }
  146. sub htmlfooter
  147. {
  148. print <<HTML
  149. </body>
  150. </html>
  151. HTML
  152. }
  153. sub mainpage
  154. {
  155. print <<HTML
  156. <p>$txthead1<br> $txthead2</p>
  157. $txtupdate<br>
  158. <form action=$frmaction method="POST">
  159. <table width="400" cellpadding="2" cellpadding="2" border="0" style="border: 1px
  160. ridge $c_border">
  161. <tr>
  162. <td><b>$txtnick</b></td>
  163. <td><input name="nick" type="text" width="30" value="$old_nick"></td>
  164. </tr>
  165. <tr>
  166. <td><b>$txtalias</b></td>
  167. <td><input name="alias" type="text" width="30" value="$old_alias"></td>
  168. </tr>
  169. <tr>
  170. <td><b>$txturl</b></td>
  171. <td><input name="link" type="text" width="30" value="$old_link"></td>
  172. </tr>
  173. <tr>
  174. <td><b>$txtpic</b></td>
  175. <td><input name="pic" type="text" width="30" value="$old_pic"></td>
  176. </tr>
  177. <tr>
  178. <td><b>$txtsex</b></td>
  179. <td>$txtmale<input type="radio" name="sex" value="m" $old_sexm>$txtfemale<input type="radio" name="sex" value="f" $old_sexf></td>
  180. </tr>
  181. <tr>
  182. <td><b>$txtignore</B></td>
  183. <td><input type="checkbox" name="ignore" $old_ignr></td>
  184. </tr>
  185. <tr>
  186. <td colspan="2" align="center">
  187. <input type="submit" value="$submitbtn">
  188. </td>
  189. </tr>
  190. </table>
  191. </form>
  192. $txtfoot1<br>
  193. $txthelp<br><br>
  194. $txtlist<br><br>
  195. HTML
  196. }
  197. sub helppage
  198. {
  199. print <<HTML
  200. <b>$txtnick:</b><br>
  201. $nickhelp<br>
  202. <b>$txtalias:</b><br>
  203. $aliashelp<br>
  204. <b>$txturl:</b><br>
  205. $urlhelp<br>
  206. <b>$txtpic:</b><br>
  207. $pichelp<br>
  208. <b>$txtsex:</b><br>
  209. $sexhelp<br>
  210. <b>$txtignore:</b><br>
  211. $ignorehelp<br>
  212. HTML
  213. }
  214. sub readparams
  215. {
  216. $frm_nick = param('nick');
  217. $frm_alias = param('alias');
  218. $frm_link = param('link');
  219. $frm_pic = param('pic');
  220. $frm_sex = param('sex');
  221. $frm_ignore = param('ignore');
  222. }
  223. sub list
  224. {
  225. open(FILE, "<$pisg_config") or die("Error opening pisg config file: $!");
  226. my $i = 0;
  227. my $nick;
  228. my $alias;
  229. while(<FILE>) {
  230. if($_ =~ /^<user/) {
  231. $users[$i] = $_;
  232. chomp $users[$i];
  233. $i++;
  234. }
  235. }
  236. close(FILE);
  237. $i = 0;
  238. 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";
  239. foreach (@users) {
  240. if ($users[$i] =~ /nick=.*/) {
  241. if ($users[$i] =~ /nick="(\S+)"(.*)/) {
  242. $nick = lc($1);
  243. }
  244. if ($users[$i] =~ /alias="(\S+)".*/ or $users[$i] =~ /alias="(.*)"\s.*/ or $users[$i] =~ /alias="(.*)">/ ) {
  245. $alias = $1;
  246. }
  247. print " <tr>\n <td>$nick</td><td>$alias</td>\n <tr>\n";
  248. }
  249. $i++
  250. }
  251. }
  252. sub read_config
  253. {
  254. open(FILE, "<$pisg_config") or die("Error opening pisg config file: $!");
  255. my $i = 0;
  256. while(<FILE>) {
  257. if($_ =~ /^<user/) {
  258. $users[$i] = $_;
  259. chomp $users[$i];
  260. $i++;
  261. }
  262. }
  263. close(FILE);
  264. my $search = 0;
  265. $i = 0;
  266. foreach (@users) {
  267. if ($users[$i] =~ /nick=/) {
  268. if ($users[$i] =~ /nick="(\S+)"(.*)/) {
  269. $nick[$i] = lc($1);
  270. $oldnicks{$nick[$i]}{'nick'} = $nick[$i];
  271. }
  272. if ($users[$i] =~ /alias="(\S+)".*/ or $users[$i] =~ /alias="(.*)"\s.* / or $users[$i] =~ /alias="(.*)">/ ) {
  273. my $alias = $1;
  274. $oldnicks{$nick[$i]}{'alias'} = $alias;
  275. }
  276. if ($users[$i] =~ /link="(\S+)"(.*)/) {
  277. my $link = $1;
  278. $oldnicks{$nick[$i]}{'link'} = $link;
  279. }
  280. if ($users[$i] =~ /pic="(\S+)"(.*)/) {
  281. my $pic = $1;
  282. $oldnicks{$nick[$i]}{'pic'} = $pic;
  283. }
  284. if ($users[$i] =~ /sex="(\S+)"(.*)/) {
  285. my $sex = $1;
  286. $oldnicks{$nick[$i]}{'sex'} = $sex;
  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>Warning:</b> Nick needs to be declared !</font><br>
  308. HTML
  309. ;
  310. }
  311. sub check_if_found
  312. {
  313. my $found = 0;
  314. foreach (@nick) {
  315. if ($oldnicks{$_}{'nick'} eq $frm_nick) {
  316. $found = 1;
  317. }
  318. }
  319. if ($found eq "1") {
  320. $old_nick = $oldnicks{$frm_nick}{'nick'};
  321. $old_alias = $oldnicks{$frm_nick}{'alias'};
  322. $old_link = $oldnicks{$frm_nick}{'link'};
  323. $old_pic = $oldnicks{$frm_nick}{'pic'};
  324. $old_sex = $oldnicks{$frm_nick}{'sex'};
  325. $old_ignore = $oldnicks{$frm_nick}{'ignore'};
  326. }
  327. return $found;
  328. }
  329. sub addinfo
  330. {
  331. my $line_to_add = "<user";
  332. if($frm_nick) {
  333. $line_to_add .= " nick=\"$frm_nick\"";
  334. }
  335. if($frm_alias) {
  336. $line_to_add .= " alias=\"$frm_alias\"";
  337. }
  338. if($frm_link) {
  339. $line_to_add .= " link=\"$frm_link\"";
  340. }
  341. if($frm_pic) {
  342. $line_to_add .= " pic=\"$frm_pic\"";
  343. }
  344. if($frm_sex) {
  345. $line_to_add .= " sex=\"$frm_sex\"";
  346. }
  347. if($frm_ignore eq "on") {
  348. $line_to_add .= " ignore=\"y\"";
  349. }
  350. $line_to_add .= ">";
  351. open(FILE, ">>$pisg_config") or die("Error writing to configfile: $!");
  352. print FILE "$line_to_add\n";
  353. close(FILE);
  354. print <<HTML
  355. $txtaddok<br><br>\n
  356. <table>
  357. <tr>
  358. <td>$txtnick:</td><td>$frm_nick</td>
  359. </tr>
  360. HTML
  361. ;
  362. if ($frm_alias) {
  363. print " <tr>\n";
  364. print " <td>$txtalias:</td><td>$frm_alias</td>\n";
  365. print " </tr>\n";
  366. }
  367. if ($frm_link) {
  368. print " <tr>\n";
  369. if($frm_link =~ /^http:|ftp:/) {
  370. print " <td>$txturl:</td><td><a href=\"$frm_link\">$frm_link</a></td>\n";
  371. }
  372. elsif ($frm_link =~ /(.*)@(.*).(.*)/) {
  373. print " <td>$txturl:</td><td><a href=\"mailto:$frm_link\">$frm_link</a></td>\n";
  374. }
  375. else {
  376. print " <td>$txturl:</td><td>$frm_link</td>\n";
  377. }
  378. print " </tr>\n";
  379. }
  380. if ($frm_pic) {
  381. print " <tr>\n";
  382. print " <td>$txtpic:</td><td><img src=\"$frm_pic\"></td>\n";
  383. print " </tr>\n";
  384. }
  385. if ($frm_sex eq "m") {
  386. print " <tr>\n";
  387. print " <td>$txtsex:</td><td>$txtmale</td>\n";
  388. print " </tr>\n";
  389. }
  390. if ($frm_sex eq "f") {
  391. print " <tr>\n";
  392. print " <td>$txtsex:</td><td>$txtfemale</td>\n";
  393. print " </tr>\n";
  394. }
  395. if ($frm_ignore eq "on") {
  396. print " <tr>\n";
  397. print " <td>$txtignore:</td><td>$txtignoreon</td>\n";
  398. print " </tr>\n";
  399. }
  400. }
  401. sub updateinfo
  402. {
  403. my $line;
  404. my $line_to_add = "<user";
  405. if ($frm_nick) {
  406. $line_to_add .= " nick=\"$frm_nick\"";
  407. }
  408. if ($frm_alias) {
  409. $line_to_add .= " alias=\"$frm_alias\"";
  410. }
  411. if ($frm_link) {
  412. $line_to_add .= " link=\"$frm_link\"";
  413. }
  414. if ($frm_pic) {
  415. $line_to_add .= " pic=\"$frm_pic\"";
  416. }
  417. if ($frm_sex) {
  418. $line_to_add .= " sex=\"$frm_sex\"";
  419. }
  420. if ($frm_ignore eq "on") {
  421. $line_to_add .= " ignore=\"y\"";
  422. }
  423. $line_to_add .= ">";
  424. open(OLDFILE, "$pisg_config") or die("Error reading configfile: $!");
  425. &lock_file(*OLDFILE);
  426. my @lines = <OLDFILE>;
  427. close(OLDFILE);
  428. open(NEWFILE, ">$pisg_config") or die("Error updating configfile: $!");
  429. &lock_file(*NEWFILE);
  430. foreach $line (@lines) {
  431. if ($line =~ /^<user.*nick=\"$frm_nick\"/i) {
  432. print NEWFILE "$line_to_add\n"
  433. } else {
  434. print NEWFILE $line;
  435. }
  436. }
  437. close (NEWFILE);
  438. print <<HTML
  439. $txtupdateok<br><br>\n
  440. <table>
  441. <tr>
  442. <td>$txtnick:</td><td>$frm_nick</td>
  443. </tr>
  444. HTML
  445. ;
  446. if ($frm_alias) {
  447. print " <tr>\n";
  448. print " <td>$txtalias:</td><td>$frm_alias</td>\n";
  449. print " </tr>\n";
  450. }
  451. if ($frm_link) {
  452. print " <tr>\n";
  453. if ($frm_link =~ /^http:|ftp:/) {
  454. print " <td>$txturl:</td><td><a href=\"$frm_link\">$frm_link</a></td>\n";
  455. } elsif ($frm_link =~ /(.*)@(.*).(.*)/) {
  456. print " <td>$txturl:</td><td><a href=\"mailto:$frm_link\">$frm_link</a></td>\n";
  457. } else {
  458. print " <td>$txturl:</td><td>$frm_link</td>\n";
  459. }
  460. print " </tr>\n";
  461. }
  462. if ($frm_pic) {
  463. print " <tr>\n";
  464. print " <td>$txtpic:</td><td><img src=\"$frm_pic\"></td>\n";
  465. print " </tr>\n";
  466. }
  467. if ($frm_sex eq "m") {
  468. print " <tr>\n";
  469. print " <td>$txtsex:</td><td>$txtmale</td>\n";
  470. print " </tr>\n";
  471. }
  472. if ($frm_sex eq "f") {
  473. print " <tr>\n";
  474. print " <td>$txtsex:</td><td>$txtfemale</td>\n";
  475. print " </tr>\n";
  476. }
  477. if ($frm_ignore eq "on") {
  478. print " <tr>\n";
  479. print " <td>$txtignore:</td><td>$txtignoreon</td>\n";
  480. print " </tr>\n";
  481. }
  482. }
  483. sub lock_file {
  484. my $lock = 2;
  485. flock($_[0], $lock);
  486. }