addalias.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. $txtfoot1="";
  102. mainpage();
  103. }
  104. else {
  105. $submitbtn = $btnupdate;
  106. $frmaction="\"update\"";
  107. addinfo();
  108. mainpage();
  109. }
  110. }
  111. else {
  112. $submitbtn = $btnupdate;
  113. $frmaction="\"update\"";
  114. addinfo();
  115. mainpage();
  116. }
  117. }
  118. } elsif ($path eq 'update') {
  119. readparams();
  120. if ($frm_nick eq "") {
  121. no_nick();
  122. mainpage();
  123. } else {
  124. $cfg = read_config();
  125. $submitbtn = $btnupdate;
  126. $frmaction="\"update\"";
  127. updateinfo();
  128. mainpage();
  129. }
  130. } else {
  131. print "Illegal calling of script<br>\n";
  132. }
  133. htmlfooter();
  134. # Subs
  135. sub htmlheader
  136. {
  137. print <<HTML
  138. Content-Type: text/html
  139. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  140. <html>
  141. <head>
  142. <title>$title</title>
  143. </head>
  144. <body bgcolor="$c_bgcolor" text="$c_text" link="$c_link" vlink="$c_vlink" alink="$c_alink">
  145. HTML
  146. }
  147. sub htmlfooter
  148. {
  149. print <<HTML
  150. </body>
  151. </html>
  152. HTML
  153. }
  154. sub mainpage
  155. {
  156. print <<HTML
  157. <p>$txthead1<br> $txthead2</p>
  158. $txtupdate<br>
  159. <form action=$frmaction method="POST">
  160. <table width="400" cellpadding="2" cellpadding="2" border="0" style="border: 1px
  161. ridge $c_border">
  162. <tr>
  163. <td><b>$txtnick</b></td>
  164. <td><input name="nick" type="text" width="30" value="$old_nick"></td>
  165. </tr>
  166. <tr>
  167. <td><b>$txtalias</b></td>
  168. <td><input name="alias" type="text" width="30" value="$old_alias"></td>
  169. </tr>
  170. <tr>
  171. <td><b>$txturl</b></td>
  172. <td><input name="link" type="text" width="30" value="$old_link"></td>
  173. </tr>
  174. <tr>
  175. <td><b>$txtpic</b></td>
  176. <td><input name="pic" type="text" width="30" value="$old_pic"></td>
  177. </tr>
  178. <tr>
  179. <td><b>$txtsex</b></td>
  180. <td>$txtmale<input type="radio" name="sex" value="m" $old_sexm>$txtfemale<input type="radio" name="sex" value="f" $old_sexf></td>
  181. </tr>
  182. <tr>
  183. <td><b>$txtignore</B></td>
  184. <td><input type="checkbox" name="ignore" $old_ignr></td>
  185. </tr>
  186. <tr>
  187. <td></td>
  188. <td>
  189. <input type="submit" value="$submitbtn">
  190. <input type="reset" value="Reset form">
  191. </td>
  192. </tr>
  193. </table>
  194. </form>
  195. $txtfoot1<br>
  196. $txthelp<br><br>
  197. $txtlist<br><br>
  198. HTML
  199. }
  200. sub helppage
  201. {
  202. print <<HTML
  203. <b>$txtnick:</b><br>
  204. $nickhelp<br>
  205. <b>$txtalias:</b><br>
  206. $aliashelp<br>
  207. <b>$txturl:</b><br>
  208. $urlhelp<br>
  209. <b>$txtpic:</b><br>
  210. $pichelp<br>
  211. <b>$txtsex:</b><br>
  212. $sexhelp<br>
  213. <b>$txtignore:</b><br>
  214. $ignorehelp<br>
  215. HTML
  216. }
  217. sub readparams
  218. {
  219. $frm_nick = param('nick');
  220. $frm_alias = param('alias');
  221. $frm_link = param('link');
  222. $frm_pic = param('pic');
  223. $frm_sex = param('sex');
  224. $frm_ignore = param('ignore');
  225. }
  226. sub list
  227. {
  228. open(FILE, "<$pisg_config") or die("Error opening pisg config file: $!");
  229. my $i = 0;
  230. my $nick;
  231. my $alias;
  232. while(<FILE>) {
  233. if($_ =~ /^<user/) {
  234. $users[$i] = $_;
  235. chomp $users[$i];
  236. $i++;
  237. }
  238. }
  239. close(FILE);
  240. $i = 0;
  241. 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";
  242. foreach (@users) {
  243. if ($users[$i] =~ /nick=.*/) {
  244. if ($users[$i] =~ /nick="(\S+)"(.*)/) {
  245. $nick = $1;
  246. }
  247. if ($users[$i] =~ /alias="(\S+)".*/ or $users[$i] =~ /alias="(.*)"\s.*/ or $users[$i] =~ /alias="(.*)">/ ) {
  248. $alias = $1;
  249. }
  250. print " <tr>\n <td>$nick</td><td>$alias</td>\n <tr>\n";
  251. }
  252. $i++
  253. }
  254. }
  255. sub read_config
  256. {
  257. open(FILE, "<$pisg_config") or die("Error opening pisg config file: $!");
  258. my $i = 0;
  259. while(<FILE>) {
  260. if($_ =~ /^<user/) {
  261. $users[$i] = $_;
  262. chomp $users[$i];
  263. $i++;
  264. }
  265. }
  266. close(FILE);
  267. my $search = 0;
  268. $i = 0;
  269. foreach (@users) {
  270. if ($users[$i] =~ /nick=/) {
  271. if ($users[$i] =~ /nick="(\S+)"(.*)/) {
  272. $nick[$i] = lc($1);
  273. $oldnicks{$nick[$i]}{'nick'} = $1;
  274. }
  275. if ($users[$i] =~ /alias="(\S+)".*/ or $users[$i] =~ /alias="(.*)"\s.* / or $users[$i] =~ /alias="(.*)">/ ) {
  276. $oldnicks{$nick[$i]}{'alias'} = $1;
  277. }
  278. if ($users[$i] =~ /link="(\S+)"(.*)/) {
  279. $oldnicks{$nick[$i]}{'link'} = $1;
  280. }
  281. if ($users[$i] =~ /pic="(\S+)"(.*)/) {
  282. $oldnicks{$nick[$i]}{'pic'} = $1;
  283. }
  284. if ($users[$i] =~ /sex="(\S+)"(.*)/) {
  285. $oldnicks{$nick[$i]}{'sex'} = $1;
  286. }
  287. if ($users[$i] =~ /ignore="(\S+)"(.*)/) {
  288. my $ignore = $1;
  289. if($ignore eq "y" or $ignore eq "Y") {
  290. $ignore = 1;
  291. } else {
  292. $ignore = 0;
  293. }
  294. $oldnicks{$nick[$i]}{'ignore'} = $ignore;
  295. }
  296. } else {
  297. $search = 1;
  298. }
  299. $i++;
  300. }
  301. return $search;
  302. }
  303. sub no_nick
  304. {
  305. print <<HTML
  306. <font color="red" size="+1"><b>Error:</b> No Nickname given!</font><br>
  307. HTML
  308. ;
  309. }
  310. sub check_if_found
  311. {
  312. my $found = 0;
  313. my $lcnick = lc($frm_nick);
  314. foreach (@nick) {
  315. if (lc($oldnicks{$_}{'nick'}) eq $lcnick) {
  316. $found = 1;
  317. last;
  318. }
  319. }
  320. if ($found eq "1") {
  321. $old_nick = $oldnicks{$lcnick}{'nick'};
  322. $old_alias = $oldnicks{$lcnick}{'alias'};
  323. $old_link = $oldnicks{$lcnick}{'link'};
  324. $old_pic = $oldnicks{$lcnick}{'pic'};
  325. $old_sex = $oldnicks{$lcnick}{'sex'};
  326. $old_ignore = $oldnicks{$lcnick}{'ignore'};
  327. if ($old_sex eq "m" or $old_sex eq "M"){
  328. $old_sexm = "checked";
  329. }
  330. elsif ($old_sex eq "f" or $old_sex eq "F"){
  331. $old_sexf = "checked";
  332. }
  333. if ($old_ignore eq "1"){
  334. $old_ignr = "checked";
  335. }
  336. }
  337. return $found;
  338. }
  339. sub addinfo
  340. {
  341. my $line_to_add = "<user";
  342. if($frm_nick) {
  343. $line_to_add .= " nick=\"$frm_nick\"";
  344. }
  345. if($frm_alias) {
  346. $line_to_add .= " alias=\"$frm_alias\"";
  347. }
  348. if($frm_link) {
  349. $line_to_add .= " link=\"$frm_link\"";
  350. }
  351. if($frm_pic) {
  352. $line_to_add .= " pic=\"$frm_pic\"";
  353. }
  354. if($frm_sex) {
  355. $line_to_add .= " sex=\"$frm_sex\"";
  356. }
  357. if($frm_ignore eq "on") {
  358. $line_to_add .= " ignore=\"y\"";
  359. }
  360. $line_to_add .= ">";
  361. open(FILE, ">>$pisg_config") or die("Error writing to configfile: $!");
  362. print FILE "$line_to_add\n";
  363. close(FILE);
  364. $old_nick = $frm_nick;
  365. $old_alias = $frm_alias;
  366. $old_link = $frm_link;
  367. $old_pic = $frm_pic;
  368. $old_sex = $frm_sex;
  369. $old_ignore = $frm_ignore;
  370. if ($old_sex eq "m" or $old_sex eq "M"){
  371. $old_sexm = "checked";
  372. }
  373. elsif ($old_sex eq "f" or $old_sex eq "F"){
  374. $old_sexf = "checked";
  375. }
  376. if ($old_ignore eq "1"){
  377. $old_ignr = "checked";
  378. }
  379. print "<font color=\"green\">$txtaddok</font><p>\n";
  380. }
  381. sub updateinfo
  382. {
  383. my $line;
  384. my $line_to_add = "<user";
  385. if ($frm_nick) {
  386. $line_to_add .= " nick=\"$frm_nick\"";
  387. }
  388. if ($frm_alias) {
  389. $line_to_add .= " alias=\"$frm_alias\"";
  390. }
  391. if ($frm_link) {
  392. $line_to_add .= " link=\"$frm_link\"";
  393. }
  394. if ($frm_pic) {
  395. $line_to_add .= " pic=\"$frm_pic\"";
  396. }
  397. if ($frm_sex) {
  398. $line_to_add .= " sex=\"$frm_sex\"";
  399. }
  400. if ($frm_ignore eq "on") {
  401. $line_to_add .= " ignore=\"y\"";
  402. }
  403. $line_to_add .= ">";
  404. open(OLDFILE, "$pisg_config") or die("Error reading configfile: $!");
  405. &lock_file(*OLDFILE);
  406. my @lines = <OLDFILE>;
  407. close(OLDFILE);
  408. open(NEWFILE, ">$pisg_config") or die("Error updating configfile: $!");
  409. &lock_file(*NEWFILE);
  410. foreach $line (@lines) {
  411. if ($line =~ /^<user.*nick=\"\Q$frm_nick\E\"/i) {
  412. print NEWFILE "$line_to_add\n"
  413. } else {
  414. print NEWFILE $line;
  415. }
  416. }
  417. close (NEWFILE);
  418. $old_nick = $frm_nick;
  419. $old_alias = $frm_alias;
  420. $old_link = $frm_link;
  421. $old_pic = $frm_pic;
  422. $old_sex = $frm_sex;
  423. $old_ignore = $frm_ignore;
  424. if ($old_sex eq "m" or $old_sex eq "M"){
  425. $old_sexm = "checked";
  426. }
  427. elsif ($old_sex eq "f" or $old_sex eq "F"){
  428. $old_sexf = "checked";
  429. }
  430. if ($old_ignore eq "1"){
  431. $old_ignr = "checked";
  432. }
  433. print "<font color=\"green\">$txtupdateok</font><p>\n";
  434. }
  435. sub lock_file {
  436. my $lock = 2;
  437. flock($_[0], $lock);
  438. }