-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.pl
executable file
·110 lines (96 loc) · 2.13 KB
/
convert.pl
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
#!/usr/bin/perl
use strict;
use IO::File;
use Data::Dumper;
## FORMAT
## Field 0 - unused?
## Field 1 - name
## Field 2 - location - eg. "forum", <website url>
## Field 3 - username
## Field 4 - password
## Field 5 - comment - may contain ',', may contain nuts, may be multiline, must NOT begin with #^,#
my $input_file = 'yaps.txt';
my $output_file = 'yaps.xml';
my $dest_header = <<XML;
<!DOCTYPE KEEPASSX_DATABASE>
<database>
XML
my $dest_footer = <<XML;
</database>
XML
my $source_hash = {};
my $count = 0;
my $DEBUG = 0;
my $name = 'none';
my $start_record = '#,';
## get them all into a hash, then sort by name
my $source = IO::File->new;
$source->open($input_file);
my $dest = IO::File->new;
$dest->open(">$output_file");
## XXX multiline comments field is not handled properly
while(<$source>)
{
chomp;
if(/^\#\,/)
{
## Start of new record
my @line = split(',', $_, 6);
if(exists $source_hash->{$line[1]})
{
$name = $line[1] . "-$count";
}
else
{
$name = $line[1];
}
$source_hash->{$name} = {
name => $line[1],
location => $line[2],
username => $line[3],
password => $line[4],
comment => "$line[5]\n"
};
}
else
{
## Continuation of previous record comments
$source_hash->{$name}->{'comment'} .= "$_\n";
}
}
if($DEBUG)
{
foreach my $name (sort keys %$source_hash)
{
## remove the trailing newline from the comment
my $comment = $source_hash->{$name}->{'comment'};
chomp($comment);
print STDERR Dumper($name, $comment);
}
}
## Convert to KeePassX XML
$dest->print($dest_header);
foreach my $name (sort keys %$source_hash)
{
## remove the trailing newline from the comment
my $comment = $source_hash->{$name}->{'comment'};
chomp($comment);
my $title = $source_hash->{$name}->{'location'};
if($title =~ /^\s*$/)
{
$title = $source_hash->{$name}->{'name'};
}
my $group = <<XML;
<group>
<title>$name</title>
<entry>
<title>$title</title>
<username>@{[ $source_hash->{$name}->{'username'} ]}</username>
<password>@{[ $source_hash->{$name}->{'password'} ]}</password>
<comment>$comment</comment>
</entry>
</group>
XML
$dest->print($group);
}
$dest->print($dest_footer);