aboutsummaryrefslogtreecommitdiffstats
path: root/utils/create_nicksfile.pl
blob: abd6d3d2fd8b2236c962cf4d343d0fd04b30d80f (plain)
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
#!/usr/bin/perl
use strict;
use Getopt::Long;


my $conn                = undef;
my %readline_funcs      = ( 'licq' => \&import_readline_licq );
my %open_funcs          = ( 'licq' => \&import_open_licq );
my %close_funcs         = ( 'licq' => \&import_close_licq );
my $funcname            = undef;
my $dirname             = undef;
my $filename            = undef;
my $imported            = 0;
my $not_imported        = 0;
my $debug               = 0;

main();
exit(0);


sub main {
        my ($server,$port,$nick,$pass,$func,$dir,$file,$outfile);
        my $dirfile;
        if (($dirfile=pop @ARGV) =~ /^\-/ || !$dirfile) {
                tell_usage();
                exit(0);
        }
        if ($dirfile =~ m|/|) {
                $dirfile =~ m|^(.*)(/.+)$|;
                $dir=$1;
                $file=$2;
        } else {
                $dir=undef;
                $file = $dirfile;
        }
        GetOptions(
                'from=s',  => \$func,
                'of=s',  => \$outfile,
                'debug',  => \$debug
        );
        if (!import_start($func,$dir,$file,$outfile,$debug)) {
                tell_usage();
        }
}

sub tell_usage {
        print "Usage: create_nicksfile.pl [--from=FROM] [--of=OUTPUTFILE] [--debug] FILENAME\n";
        print "       FROM defines which application we import from.\n",
        print "       Note that currently the only valid value for FROM is licq.\n";
        print "       For further information, you might want to do perldoc create_nicksfile.pl\n";
}

sub import_start {
        $funcname          = (shift) || 'licq';
        $dirname        = shift;
        $filename       = shift;
        my $outfile        = shift || 'bitlbee.nicks';
        $debug          = shift;
        my ($alias,$protocol,$name,$found);
        open(OUT,'>'.$outfile) || die "unable to open $outfile";
        if (defined $open_funcs{$funcname}) {
                if (&{$open_funcs{$funcname}}($dirname,$filename)) {
                        do {
                                ($alias,$protocol,$name,$found)=&{$readline_funcs{$funcname}}();
                                print OUT "$alias $protocol $name\n" if $found;
                        } while ($found);
                } else {
                        import_err('Unable to open '.$filename);
                        return 0;
                }
        } else {
                import_err($funcname.' is no defined import function.');
                return 0;
        }
        close OUT;
        &{$close_funcs{$funcname}}();
        return 1;
}

sub import_err {
        my $msg=shift;
        print "\nError: $msg\n";
}

sub import_open_licq {
        my ($dir,$name)=@_;
        return open(IN,'<'.$dir.'/users.conf');
}
sub import_close_licq {
        close IN;
}
sub import_readline_licq {
        my ($uin,$alias);
        my $line;
GETLINE:
        $line=<IN>;
        if ($line) {
                while ($line && $line !~ /^User\d+/) {
                        $line=<IN>;
                }
                if ($line) {
                        if ($line =~ /^User\d+\s*=\s*(\d+)(\.Licq)?$/) { # getting UIN
                                $uin=$1;
                                open(ALIAS,'<'.$dirname.'/users/'.$uin.'.Licq') ||
                                open(ALIAS,'<'.$dirname.'/users/'.$uin.'.uin') || do {
                                        warn "unable to open userfile for $uin";
                                        return (undef,undef,0);
                                };
                                while (<ALIAS>) {
                                        if (/^Alias\s*=\s*(.*)$/) {
                                                $alias=$1;
                                                $alias =~ s/\s+/_/g;
                                                last;
                                        }
                                }
                                close ALIAS;
                                $imported++;
                                return ($uin,3,$alias,1);
                        } else {
                                warn('Unknown line format: '.$line);
                                $not_imported++;
                                goto GETLINE; #### grrrr, sometimes there are negative uins in licq files...
                        }
                } else {
                        return (undef,undef,0);
                }
        } else {
                return undef;
        }
}

__END__

=head1 NAME

create_nicksfile.pl - Create a valid bitlbee .nicks file

=head1 SYNOPSIS

create_nicksfile.pl [--from=FROM] [--of=OUTPUTFILE] [--debug] FILENAME

        FROM defines which application we import from.
        Note that currently the only valid value for FROM 
        is licq.

        If of is missing, we write to bitlbee.nicks.

=head1 DESCRIPTION

We run thru the
files where the contacts reside and create
a bitlbee .nicks-file from them.

=head1 DEPENDENCIES

On the perlside, we need Getopt::Long.

=head1 CAVEATS

=head1 TODO

&import_readline_... should take a filehandle as argument.

Add more import functions. If you are interested,
to do so, you need to write the following functions:

=over

=item *

import_open_<WHATEVER>(DIR,FILENAME)

=item *

import_close_<WHATEVER>()

=item *

import_readline_<WHATEVER>()

=back

and add them to the hashes

=over

=item *

%readline_funcs

=item *

%open_funcs

=item *

%close_funcs

=back

at the top of this script.


=head1 AUTHORS

Christian Friedl <vijeno@chello.at>

Updated for the new Licq list firmat by Hugo Buddelmeijer <kmail@hugo.doemaarwat.nl>

=cut