summaryrefslogtreecommitdiffstats
path: root/mdkapplet-restricted-helper
blob: ee605915a71b8e36aff7c92b9aac1cd269fc3bc4 (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
#!/usr/bin/perl
################################################################################
# Mandriva Online                                                              # 
#                                                                              #
# Copyright (C) 2008 Mandriva                                             #
#                                                                              #
# Thierry Vignaud <tvignaud at mandriva dot com>                               #
#                                                                              #
# This program is free software; you can redistribute it and/or modify         #
# it under the terms of the GNU General Public License Version 2 as            #
# published by the Free Software Foundation.                                   #
#                                                                              #
# This program is distributed in the hope that it will be useful,              #
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
# GNU General Public License for more details.                                 #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with this program; if not, write to the Free Software                  #
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.   #
################################################################################

use strict;
use lib qw(/usr/lib/libDrakX /usr/lib/libDrakX/drakfirsttime);
use standalone; # for explanations
use common;
use run_program;
use feature 'state';

BEGIN { unshift @::textdomains, 'mdkonline' }

use mygtk2 qw(gtknew); #- do not import gtkadd which conflicts with ugtk2 version
use ugtk2 qw(:all);
use urpm::cfg;
use mdkonline;
use mdkapplet_gui;
use interactive;

use XML::Simple;
use URI::Escape;
use LWP::UserAgent;         
use HTTP::Request::Common;
use HTTP::Request;

foreach my $opt (@ARGV) {
    if ($opt =~ /--(rpm-root|urpmi-root)=(.*)/) {
        $::rpmdrake_options{$1}[0] = $2;
    }
}

get_restricted_authentication();

ugtk2::exit(0);


sub get_restricted_authentication() {
    my $w = ugtk2->new(N("Adding an additional package medium"), width => $width + 20);
    my ($password_w, $email_w, $password);

    $password_w = gtknew('Entry');
    $password_w->set_visibility(0);
    $w->{real_window}->signal_connect(destroy => sub { $password = $password_w->get_text });
    require interactive::gtk;

    my $res =
      fill_n_run_portable_dialog(
          $w,
          [
              get_banner(N("Adding an additional package medium")),
              gtknew('Label_Left', text => N("Please fill in your account ID to add an additional package medium"),
                     @common),
              gtknew('HButtonBox', layout => 'start', children_tight => [
                  interactive::gtk::add_padding(
                      new_link_button('https://my.mandriva.com/info', N("More information on your user account")))
              ]),
              gtknew('Table', col_spacings => 5, row_spacings => 5, children => [
                  [ N("Your email"), $email_w = gtknew('Entry') ],
                  [ N("Your password"), $password_w ],
              ]),
              gtknew('HButtonBox', layout => 'start', children_tight => [
                  interactive::gtk::add_padding(
                      new_link_button('https://my.mandriva.com/reset/password/', N("Forgotten password")))
              ]),
              create_okcancel($w, N("Next"), N("Cancel")),
          ]);

    my $email = $email_w->get_text;
    if ($res) {
        if ($email && $password) {
            add_restricted_medium($email, $password);
        } else {
            interactive->vnew->ask_warn(N("Error"), N("Password and email cannot be empty."));
            goto &get_restricted_authentication;
        }
    } else {
        0;
    }
}

sub get_from {
    my ($link, $header) = @_;
    
    my $ua = LWP::UserAgent->new;
    $ua->agent("mdkapplet (mdkonline-$mdkonlin::version)");
    $ua->env_proxy;

    my $response = $ua->request(POST $link, $header);
    $response;
}

my $error;
sub add_restricted_medium {
    my ($email, $password) = @_;

    my $res =  get_from("https://my.mandriva.com/rest/authenticate", [ 'username', $email, 'password', $password,
                                                                       '22return', 'userdata' ]);
    my $ref = XML::Simple->new->XMLin($res->{_content});

    if ($ref->{code} != 0) {
        my $in = interactive->vnew;
        $in->ask_warn(N("Error"), N("An error occurred") . "\n" . $ref->{message});
        goto &get_restricted_authentication;
    } else {
        $error = 0;
        my $arch = urpm::cfg::get_arch();
        actually_add_restricted_medium($ref, $password, $arch)
          or adding_media_failed();
        # FIXME: is not enough if we ever support sparc64, ppc64 and the like:
        if ($arch eq 'x86_64') {
            actually_add_restricted_medium($ref, $password, 'i586')
              or adding_media_failed();
        }
        if (!$error) {
            #interactive->vnew->ask_okcancel(N("Error"), N("An error occurred while adding medium"));
            my $w = ugtk2->new(N("Successfully added media %s.", 'Restricted'), grab => 1);
            $w->_ask_okcancel(N("Successfully added media %s.", 'Restricted'), N("Ok"), undef);
            ugtk2::main($w);
        }
    }
}

sub adding_media_failed {
    $error = 1;
    interactive->vnew->ask_warn(N("Error"), N("An error occurred while adding medium"));
    goto &get_restricted_authentication;
}

sub actually_add_restricted_medium {
    my ($ref, $password, $arch) = @_;
    my @options = ({ sensitive_arguments => 1 }, 'urpmi.addmedia');
    my $uri = "https://" . uri_escape($ref->{data}{email}) . ":$password\@dl.mandriva.com/rpm/comm/2009.0/";
    run_program::raw(@options, 'Restricted ' . int(rand(100000)), "$uri$arch") or return 0;
    run_program::raw(@options, '--update', 'Restricted Updates ' . int(rand(100000)), "${uri}updates/$arch");
}