File: addalias.pl

package info (click to toggle)
pisg 0.71-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,228 kB
  • ctags: 357
  • sloc: perl: 7,472; xml: 3,144; sed: 169; makefile: 48; tcl: 23; awk: 15
file content (501 lines) | stat: -rw-r--r-- 13,634 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#!/usr/bin/perl

use strict;
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser carpout);

###########################################################################
#                                                                         #
#  addalias version 2.2 by deadlock (deadlock@cheeseheadz.net)            #
#                                                                         #
#  This script can be used on a webpage for users to enter and edit their #
#  own info for the pisg ircstats program by mbrix.                       #
#                                                                         #
#  addalias v2+ is based on the original addalias program by Doomshammer  #
#                                                                         #
###########################################################################

#############################
### Configuration section ###
#############################

# File locations:

my $pisg_config = "/path/to/pisg.cfg";

# Server URL
# If your script resists in http://myserver/cgi-bin/addalias.pl you should
# set it to: "/cgi-bin"

my $url         = "/cgi-bin";

# Page layout:

my $c_bgcolor   = "#FFFFFF";
my $c_text      = "#000000";
my $c_link      = "blue";
my $c_vlink     = "#C0C0C0";
my $c_alink     = "#C0FFC0";
my $c_border    = "#FFFFFF";

my $title       = "IRC statistics - user addition page";

# Text on the main page

my $txthead1    = "In this form you can enter the settings (aliases, link and user picture) for your nickname in the IRC-stats.";
my $txthead2    = "Nicknames are allowed only once.";
my $txtnick     = "Nickname";
my $txtalias    = "Alias(es)";
my $txturl      = "URL/E-Mail";
my $txtpic      = "Userpic";
my $txtsex      = "Sex";
my $txtmale     = "M";
my $txtfemale   = "F";
my $txtbot      = "B";
my $txtignore   = "Ignore me";
my $btnsubmit   = "Submit";
my $btnupdate   = "Update";
my $txtfoot1    = "To update your settings, just enter your nickname and click Submit to retrieve your current settings.";
my $txthelp     = "For help on this form click <a href=\"$url/addalias.pl/help\" target=\"_blank\">here</a>.";
my $txtlist     = "For a complete list of known nicks please click <a href=\"$url/addalias.pl/list\">here</a>.";
my $txtupdate   = "These are your current settings. Edit them where needed and click Update to update your info.";
my $txtaddok    = "Your nickname was successfully added.";
my $txtignoreon = "You activated ignore and will not appear in the stats.";
my $txtupdateok = "Your info was successfully updated.";

# Helptext:

my $nickhelp    = "Enter the name you want to use in the stats here.";
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*'";
my $urlhelp     = "You can enter a webpage or e-mail adress here to be linked to your nick in the stats.";
my $pichelp     = "If you enter a link to a picture here it will be added to your stats on the page.";
my $sexhelp     = "This setting is used to determine if lines in the stats should read 'his' or 'her' or 'bot' when referring to you.";
my $ignorehelp  = "If you don't want to be included in the stats, select this option.";


###################################
### End config section          ###
### do not edit below this line ###
###################################

# Main program

my $path = path_info();
$path =~ s!^/!!;
my (%oldnicks, @users, @nick);
my ($frm_nick, $frm_alias, $frm_link, $frm_pic, $frm_sex, $frm_ignore);
my ($old_nick, $old_alias, $old_link, $old_pic, $old_sex, $old_ignore);
my ($old_sexm, $old_sexf, $old_sexb, $old_ignr);
my ($cfg, $fnd);
my ($submitbtn, $frmaction);

htmlheader();
if (!$path) {
    $submitbtn = $btnsubmit;
    $frmaction="\"$url/addalias.pl/input\"";
    $txtupdate = "";
    mainpage();
} elsif ($path eq 'help') {
    helppage();
} elsif ($path eq 'list') {
    $submitbtn = $btnsubmit;
    $frmaction="\"$url/addalias.pl/input\"";
    $txtupdate = "";
    mainpage();
    list();

} elsif ($path eq 'input') {
    readparams();
    if ($frm_nick eq "") {
       no_nick();
       $submitbtn = $btnsubmit;
       $frmaction="\"$url/addalias.pl/input\"";
       $txtupdate = "";
       mainpage();
    } else {
       $cfg = read_config();
       if ($cfg ne "1") {
           $fnd = check_if_found();
           if ($fnd eq "1") {
               $submitbtn = $btnupdate;
               $frmaction="\"update\"";
               $txtfoot1="";
               mainpage();
           }
           else {
               $submitbtn = $btnupdate;
               $frmaction="\"update\"";
               addinfo();
               mainpage();
           }
       }
       else {
           $submitbtn = $btnupdate;
           $frmaction="\"update\"";
           addinfo();
           mainpage();
       }
    }
} elsif ($path eq 'update') {
    readparams();
    if ($frm_nick eq "") {
       no_nick();
       mainpage();
    } else {
       $cfg = read_config();
       $submitbtn = $btnupdate;
       $frmaction="\"update\"";
       updateinfo();
       mainpage();
    }
} else {
    print "Illegal calling of script<br>\n";
}

htmlfooter();


# Subs

sub htmlheader
{
print <<HTML
Content-Type: text/html


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title>$title</title>
 </head>
 <body bgcolor="$c_bgcolor" text="$c_text" link="$c_link" vlink="$c_vlink" alink="$c_alink">
HTML

}


sub htmlfooter
{
print <<HTML
 </body>
</html>
HTML
}


sub mainpage
{
print <<HTML
  <p>$txthead1<br> $txthead2</p>
  $txtupdate<br>
  <form action=$frmaction method="POST">
   <table width="400" cellpadding="2" cellpadding="2" border="0" style="border: 1px
   ridge $c_border">
    <tr>
     <td><b>$txtnick</b></td>
     <td><input name="nick" type="text" width="30" value="$old_nick"></td>
    </tr>
    <tr>
     <td><b>$txtalias</b></td>
     <td><input name="alias" type="text" width="30" value="$old_alias"></td>
    </tr>
    <tr>
     <td><b>$txturl</b></td>
     <td><input name="link" type="text" width="30" value="$old_link"></td>
    </tr>
    <tr>
     <td><b>$txtpic</b></td>
     <td><input name="pic" type="text" width="30" value="$old_pic"></td>
    </tr>
    <tr>
     <td><b>$txtsex</b></td>
     <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>
    </tr>
    <tr>
     <td><b>$txtignore</B></td>
     <td><input type="checkbox" name="ignore" $old_ignr></td>
    </tr>
    <tr>
     <td></td>
     <td>
      <input type="submit" value="$submitbtn">
      <input type="reset" value="Reset form">
     </td>
    </tr>
   </table>
  </form>
  $txtfoot1<br>
  $txthelp<br><br>
  $txtlist<br><br>
HTML
}


sub helppage
{
print <<HTML
  <b>$txtnick:</b><br>
  $nickhelp<br>
  <b>$txtalias:</b><br>
  $aliashelp<br>
  <b>$txturl:</b><br>
  $urlhelp<br>
  <b>$txtpic:</b><br>
  $pichelp<br>
  <b>$txtsex:</b><br>
  $sexhelp<br>
  <b>$txtignore:</b><br>
  $ignorehelp<br>
HTML
}

sub readparams
{
   $frm_nick = param('nick');
   $frm_alias = param('alias');
   $frm_link = param('link');
   $frm_pic = param('pic');
   $frm_sex = param('sex');
   $frm_ignore = param('ignore');
}

sub list
{
    open(FILE, "<$pisg_config") or die("Error opening pisg config file: $!");
    my $i = 0;
    my $nick;
    my $alias;
    while(<FILE>) {
        if($_ =~ /^<user/) {
            $users[$i] = $_;
            chomp $users[$i];
            $i++;
        }
    }
    close(FILE);
    $i = 0;

    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";

    foreach (@users) {
        if ($users[$i] =~ /nick=.*/) {
            if ($users[$i] =~ /nick="(\S+)"(.*)/) {
                $nick = $1;
            }
            if ($users[$i] =~ /alias="(\S+)".*/ or $users[$i] =~ /alias="(.*)"\s.*/ or $users[$i] =~ /alias="(.*)">/ ) {
                $alias = $1;
            }
            print " <tr>\n  <td>$nick</td><td>$alias</td>\n <tr>\n";
        }
    $i++
    }
}

sub read_config
{
    open(FILE, "<$pisg_config") or die("Error opening pisg config file: $!");
    my $i = 0;
    while(<FILE>) {
        if($_ =~ /^<user/) {
            $users[$i] = $_;
            chomp $users[$i];
            $i++;
        }
    }
    close(FILE);

    my $search = 0;
    $i = 0;
    foreach (@users) {
        if ($users[$i] =~ /nick=/) {
            if ($users[$i] =~ /nick="(\S+)"(.*)/) {
                $nick[$i] = lc($1);
                $oldnicks{$nick[$i]}{'nick'} = $1;
            }
            if ($users[$i] =~ /alias="(\S+)".*/ or $users[$i] =~ /alias="(.*)"\s.* / or $users[$i] =~ /alias="(.*)">/ ) {
                $oldnicks{$nick[$i]}{'alias'} = $1;
            }
            if ($users[$i] =~ /link="(\S+)"(.*)/) {
                $oldnicks{$nick[$i]}{'link'} = $1;
            }
            if ($users[$i] =~ /pic="(\S+)"(.*)/) {
                $oldnicks{$nick[$i]}{'pic'} = $1;
            }
            if ($users[$i] =~ /sex="(\S+)"(.*)/) {
                $oldnicks{$nick[$i]}{'sex'} = $1;
            }
            if ($users[$i] =~ /ignore="(\S+)"(.*)/) {
                my $ignore = $1;
                if($ignore eq "y" or $ignore eq "Y") {
                    $ignore = 1;
                } else {
                    $ignore = 0;
                }
                $oldnicks{$nick[$i]}{'ignore'} = $ignore;
            }
        } else {
            $search = 1;
        }
        $i++;
    }
    return $search;
}


sub no_nick
{
    print <<HTML
<font color="red" size="+1"><b>Error:</b> No Nickname given!</font><br>
HTML
;
}

sub check_if_found
{

    my $found = 0;
    my $lcnick = lc($frm_nick);
    foreach (@nick) {
        if (lc($oldnicks{$_}{'nick'}) eq $lcnick) {
            $found = 1;
            last;
        }
    }
    if ($found eq "1") {
        $old_nick = $oldnicks{$lcnick}{'nick'};
        $old_alias = $oldnicks{$lcnick}{'alias'};
        $old_link = $oldnicks{$lcnick}{'link'};
        $old_pic = $oldnicks{$lcnick}{'pic'};
        $old_sex = $oldnicks{$lcnick}{'sex'};
        $old_ignore = $oldnicks{$lcnick}{'ignore'};
        if ($old_sex eq "m" or $old_sex eq "M"){
            $old_sexm = "checked";
        }
        elsif ($old_sex eq "f" or $old_sex eq "F"){
            $old_sexf = "checked";
        }
        elsif ($old_sex eq "b" or $old_sex eq "B"){
            $old_sexb = "checked";
        }
        if ($old_ignore eq "1"){
            $old_ignr = "checked";
        }
    }
    return $found;
}

sub addinfo
{
    my $line_to_add = "<user";
    if($frm_nick) {
        $line_to_add .= " nick=\"$frm_nick\"";
    }
    if($frm_alias) {
        $line_to_add .= " alias=\"$frm_alias\"";
    }
    if($frm_link) {
        $line_to_add .= " link=\"$frm_link\"";
    }
    if($frm_pic) {
        $line_to_add .= " pic=\"$frm_pic\"";
    }
    if($frm_sex) {
        $line_to_add .= " sex=\"$frm_sex\"";
    }
    if($frm_ignore eq "on") {
        $line_to_add .= " ignore=\"y\"";
    }

    $line_to_add .= ">";

    open(FILE, ">>$pisg_config") or die("Error writing to configfile: $!");
    print FILE "$line_to_add\n";
    close(FILE);

    $old_nick = $frm_nick;
    $old_alias = $frm_alias;
    $old_link = $frm_link;
    $old_pic = $frm_pic;
    $old_sex = $frm_sex;
    $old_ignore = $frm_ignore;
    if ($old_sex eq "m" or $old_sex eq "M"){
        $old_sexm = "checked";
    }
    elsif ($old_sex eq "f" or $old_sex eq "F"){
        $old_sexf = "checked";
    }
    elsif ($old_sex eq "b" or $old_sex eq "B"){
        $old_sexb = "checked";
    }
    if ($old_ignore eq "1"){
        $old_ignr = "checked";
    }
    print "<font color=\"green\">$txtaddok</font><p>\n";
}

sub updateinfo
{
    my $line;
    my $line_to_add = "<user";
    if ($frm_nick) {
        $line_to_add .= " nick=\"$frm_nick\"";
    }
    if ($frm_alias) {
        $line_to_add .= " alias=\"$frm_alias\"";
    }
    if ($frm_link) {
        $line_to_add .= " link=\"$frm_link\"";
    }
    if ($frm_pic) {
        $line_to_add .= " pic=\"$frm_pic\"";
    }
    if ($frm_sex) {
        $line_to_add .= " sex=\"$frm_sex\"";
    }
    if ($frm_ignore eq "on") {
        $line_to_add .= " ignore=\"y\"";
    }

    $line_to_add .= ">";

    open(OLDFILE, "$pisg_config") or die("Error reading configfile: $!");
    &lock_file(*OLDFILE);
    my @lines = <OLDFILE>;
    close(OLDFILE);
    open(NEWFILE, ">$pisg_config") or die("Error updating configfile: $!");
    &lock_file(*NEWFILE);
    foreach $line (@lines) {
       if ($line =~ /^<user.*nick=\"\Q$frm_nick\E\"/i) {
         print NEWFILE "$line_to_add\n"
       } else {
         print NEWFILE $line;
       }
    }
    close (NEWFILE);

    $old_nick = $frm_nick;
    $old_alias = $frm_alias;
    $old_link = $frm_link;
    $old_pic = $frm_pic;
    $old_sex = $frm_sex;
    $old_ignore = $frm_ignore;
    if ($old_sex eq "m" or $old_sex eq "M"){
        $old_sexm = "checked";
    }
    elsif ($old_sex eq "f" or $old_sex eq "F"){
        $old_sexf = "checked";
    }
    elsif ($old_sex eq "b" or $old_sex eq "B"){
        $old_sexb = "checked";
    }
    if ($old_ignore eq "1"){
        $old_ignr = "checked";
    }

    print "<font color=\"green\">$txtupdateok</font><p>\n";
}

sub lock_file {
    my $lock = 2;
    flock($_[0], $lock);
}