addalias.pl 12 KB

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