addalias.pl 13 KB

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