################################################################################ # Mageia Online functions # # # # Copyright (C) 2004-2005 Mandrakesoft # # 2005-2010 Mandriva # # 2010-2017 Mageia # # # # Daouda Lo # # Thierry Vignaud # # # # 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, see . # ################################################################################ package mgaonline; use strict; use lib qw(/usr/lib/libDrakX); use common; use ugtk3; use LWP::UserAgent; use URI::Escape; use XML::Simple; use HTTP::Request::Common; use HTTP::Request; our @ISA = qw(Exporter); our @EXPORT = qw(find_current_distro fork_exec get_banner get_distro_list get_from get_product_id get_release get_stale_upgrade_filename get_urpmi_options read_sys_config xml2perl %config $config_file $product_id $root); our @EXPORT_OK = qw( get_my_mdv_profile ); our (%config, $product_id, $root); our $version = 1; use log; our $config_file = '/etc/sysconfig/mgaapplet'; my $release_file = find { -f $_ } map { "/etc/${_}release" } '', qw(mageia- mandriva- mandrakelinux- mandrake- redhat-); sub read_sys_config() { %config = getVarsFromSh($config_file); } sub get_stale_upgrade_filename() { '/var/lib/urpmi/stale_upgrade_in_progress'; } sub get_product_id() { $product_id = common::parse_LDAP_namespace_structure(cat_("$root/etc/product.id")); } sub get_release() { my ($r) = cat_($release_file) =~ /release\s+(\S+)/; ($r); } sub find_current_distro { find { $_->{version} eq $product_id->{version} } @_; } sub get_distro_list_() { #- contact the following URL to retrieve the list of released distributions. my $type = lc($product_id->{type}); $type =~ s/\s//g; my $extra_path = $::testing || uc($config{TEST_DISTRO_UPGRADE}) eq 'YES' ? 'testing-' : ''; my $list = join('&', "https://releases.mageia.org/api/b/$extra_path$product_id->{arch}?product=$product_id->{product}", "version=$product_id->{version}", "mgaonline_version=$mgaonline::version", ); log::explanations("trying distributions list from $list"); eval { my $urpm = Rpmdrake::open_db::fast_open_urpmi_db(); # prevent SIGCHILD handler's waitpid to interfere with urpmi waiting # for curl exit code, which broke downloads: local $SIG{CHLD} = 'DEFAULT'; urpm::ensure_valid_cachedir($urpm); urpm::download::get_content($urpm, $list); }; } sub get_distro_list() { return if $product_id->{product} =~ /Flash/; my @lines = get_distro_list_(); if (my $err = $@) { log::explanations("failed to download distribution list:\n$err"); return; # not a fatal error } if (!@lines) { log::explanations("empty distribution list"); return; } map { common::parse_LDAP_namespace_structure(chomp_($_)) } grep { /^[^#]/ } @lines; } sub clean_confdir() { my $confdir = '/root/.MdkOnline'; system "/bin/rm", "-f", "$confdir/*log.bz2", "$confdir/*log.bz2.uue", "$confdir/*.dif $confdir/rpm_qa_installed_before", "$confdir/rpm_qa_installed_after"; } sub fork_exec { run_program::raw({ detach => 1 }, @_); } sub get_banner_icon() { find { -e $_ } qw(/usr/share/mcc/themes/default/rpmdrake-mdk.png /usr/share/icons/hicolor/48x48/apps/mgaonline.png); } sub get_banner { my ($o_title) = @_; Gtk3::Banner->new(get_banner_icon(), $o_title || N("Distribution Upgrade")); } sub get_urpmi_options() { ({ sensitive_arguments => 1 }, 'urpmi.addmedia', '--xml-info', 'always'); } sub is_running { my ($name) = @_; my $found; foreach (`ps -o '%P %p %c' -u $ENV{USER}`) { my ($ppid, $pid, $n) = /^\s*(\d+)\s+(\d+)\s+(.*)/; if ($ppid != 1 && $pid != $$ && $n eq $name) { $found = $pid; last; } } $found; } sub get_from { my ($link, $header) = @_; my $ua = LWP::UserAgent->new; $ua->agent(sprintf('mgaapplet (mgaonline-%s; distribution: %s)', $mgaonline::version, $version)); $ua->env_proxy; my $response = $ua->post($link, $header); $response; } sub get_my_mdv_profile { my ($email, $password) = @_; xml2perl(get_from('https://my.FIXME.com/rest/authenticate', [ 'username', $email, 'password', $password, 'return', 'userdata' ])); } # callers need to require XML::Simple sub xml2perl { my ($res) = @_; my $ref = eval { XML::Simple->new->XMLin($res->{_content}) }; if (my $err = $@) { warn ">> XML error: $err\n"; $ref = { code => 1, message => $err, }; } $ref; } 1;