Debian Codegraph @

scripts/storage/plain2sql.pl

raw
1 #!/usr/bin/perl
2
3
4 if (scalar(@ARGV) != 1) {
5 &usage;
6 exit;
7 }
8
9 &header;
10
11 # remove a trailing /
12 $dirpath = $ARGV[0];
13 $dirpath =~ s!(.*)/$!$1!;
14
15 opendir FILEDIR, $dirpath or die "Error opening filedir!\n";
16
17 while ($filename = readdir FILEDIR) {
18 if ($filename =~ m/^\./) { next; } #ignore . and ..
19
20
21 if ( ! (($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
22 $atime,$mtime,$ctime,$blksize,$blocks) = stat "$dirpath/$filename" )) {
23 print STDERR "Error stat-ing the file $pathdir/$filename!\n" ; next; }
24
25 $type = ($mode & 070000) >> 12;
26
27 if ($type != 0) {
28 print STDERR "File $dirpath/$filename its not regular\n";
29 next;
30 }
31 convertplain2db("$dirpath/$filename");
32 }
33
34 closedir FILEDIR;
35
36 sub convertplain2db {
37 my $filen = shift;
38 my ($userid, $count, $alist);
39
40 open FILE, $filen or die "Error opening file $filen\n";
41 print STDERR "Converting file $filen ... ";
42
43 $count = 0;
44 $userid = ""; undef @alist;
45 while($line = <FILE>) {
46 if ($line =~ m/".*"=".*"/) {
47
48 $line =~ m/^"([A-Za-z0-9]+)\\\\(.*)"="(.*)"/;
49
50 $tab = $1;
51 # skip Team as we now have another team structure
52 if ($tab =~ m/^team$/i) {
53 print STDERR "WARNING: skipping Team information!\n";
54 next;
55 } elsif ($tab =~ m/^\s*$/) {
56 print STDERR "WARNING: skipping empty tab!\n";
57 next;
58 }
59
60 $alist[$count]{tab} = $tab;
61 $alist[$count]{col} = &escape_key($2);
62 $alist[$count]{val} = $3;
63
64 $alist[$count]{col} =~ s!\\\\!_!g;
65
66 if ($alist[$count]{col} =~ m!userid$!) {
67 $userid = $alist[$count]{val};
68 }
69 $count++;
70 }
71 }
72 if ($userid ne "") {
73 &db_set($dbh, $userid, $alist);
74 }
75 close FILE;
76 print STDERR "done\n";
77 }
78
79 sub header {
80 print STDERR "Account files to db accounts converting tool.\n";
81 print STDERR " Copyright (C) 2002,2005 Dizzy (dizzy\@roedu.net)\n";
82 print STDERR " Player Versus Player Gaming Network (www.pvpgn.org)\n\n";
83 }
84
85 sub usage {
86 &header;
87 print STDERR "Usage:\n\n\tfiles2db.pl <filedir>\n\n";
88 print STDERR "\t <filedir>\t: directory with the account files\n";
89 }
90
91 sub db_set {
92 my $dbh = shift;
93 my $userid = shift;
94 my $alist = shift;
95 my ($i);
96
97 print("INSERT INTO BNET (uid) VALUES ($userid);\n");
98 print("INSERT INTO profile (uid) VALUES ($userid);\n");
99 print("INSERT INTO Record (uid) VALUES ($userid);\n");
100 print("INSERT INTO friend (uid) VALUES ($userid);\n");
101 for($i=0; $i<scalar(@alist);$i++) {
102 my $tab = $alist[$i]{tab};
103 my $col = $alist[$i]{col};
104 my $val = $alist[$i]{val};
105
106 $nval = &add_slashes($val);
107 print("ALTER TABLE $tab ADD COLUMN '$col' VARCHAR(128);\n");
108 print("UPDATE $tab SET '$col' = '$nval' WHERE uid = $userid;\n");
109 }
110 }
111
112 sub add_slashes {
113 my $str = shift;
114
115 $str =~ s/\\/\\\\/g;
116 $str =~ s/\'/\\\'/g;
117
118 return $str;
119 }
120
121 sub escape_key {
122 my $str = shift;
123
124 $str =~ tr/A-Z/a-z/;
125 $str =~ s/[^0-9a-zA-Z]/_/g;
126 return $str;
127 }