~ubuntu-branches/debian/experimental/lftp/experimental

« back to all changes in this revision

Viewing changes to src/convert-mozilla-cookies

  • Committer: Package Import Robot
  • Author(s): Noël Köthe
  • Date: 2015-08-21 16:06:22 UTC
  • mfrom: (1.1.20) (24.1.38 sid)
  • Revision ID: package-import@ubuntu.com-20150821160622-lckdmbiqx16wefgy
Tags: 4.6.4-1
new upstream release 2015-08-21

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
# Copyright (c) 2001,2005,2007,2010 Alexander V. Lukyanov <lav@yars.free.net>
 
3
# See COPYING file (GNU GPL) for complete license.
 
4
 
 
5
# This script converts mozilla-style cookies to lftp set commands.
 
6
 
 
7
use strict;
 
8
use DBI;
 
9
 
 
10
my $file=$ARGV[0] if defined $ARGV[0];
 
11
$file=qx{
 
12
        ls -t \$HOME/.netscape/cookies \\
 
13
              `find \$HOME/.mozilla -name cookies.txt -or -name cookies.sqlite` | head -1
 
14
},chomp $file if !defined $file;
 
15
 
 
16
print "# converted from $file\n";
 
17
my %cookie;
 
18
sub add_cookie($$$$$) {
 
19
   my ($domain,$path,$secure_bool,$name,$value)=@_;
 
20
   for($domain,$path,$name,$value) { s/"/\\"/g; s/'/\\'/g; s/ /%20/g; }
 
21
   my $secure='';
 
22
   $secure=';secure' if $secure_bool eq 'TRUE' || $secure_bool eq '1';
 
23
   $domain="*$domain" if $domain =~ /^\./;
 
24
   $path='' if $path eq '/';
 
25
   $path=";path=$path" if $path ne '';
 
26
   $value="=$value" if $name ne '';
 
27
   $cookie{"$domain$path$secure"}.=" $name$value";
 
28
}
 
29
if($file=~/\.sqlite$/) {
 
30
   my $file1="$file.$$.tmp";
 
31
   $SIG{__DIE__}=sub { unlink $file1 };
 
32
   system('cp',$file,$file1);
 
33
   my $dbh=DBI->connect("dbi:SQLite:dbname=$file1",'','');
 
34
   unlink $file1;
 
35
   my $q=$dbh->prepare("select host,path,isSecure,name,value from moz_cookies");
 
36
   $q->execute;
 
37
   $q->bind_columns(\my($domain,$path,$secure_bool,$name,$value));
 
38
   while($q->fetch) {
 
39
      add_cookie($domain,$path,$secure_bool,$name,$value);
 
40
   }
 
41
} else {
 
42
   open COOKIES,'<',$file or die "open($file): $!";
 
43
   while(<COOKIES>)
 
44
   {
 
45
      chomp;
 
46
      next if /^#/ or /^$/;
 
47
      my ($domain,undef,$path,$secure_bool,$expires,$name,$value)=split /\t/;
 
48
      add_cookie($domain,$path,$secure_bool,$name,$value);
 
49
   }
 
50
}
 
51
foreach(sort keys %cookie)
 
52
{
 
53
   $cookie{$_}=~s/^ //;
 
54
   my $c=($_=~/;/?qq{"$_"}:$_);
 
55
   print qq{set http:cookie/$c "$cookie{$_}"\n};
 
56
}