[69] | 1 | #! /bin/env perl |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use Sophie::Client; |
---|
[74] | 6 | use Getopt::Long; |
---|
| 7 | use Pod::Usage; |
---|
[187] | 8 | use Config::General; |
---|
[210] | 9 | use File::Temp; |
---|
[69] | 10 | |
---|
[74] | 11 | GetOptions( |
---|
[187] | 12 | 'c=s' => \my $configfile, |
---|
[74] | 13 | 'u=s' => \my $url, |
---|
| 14 | 'l=s' => \my $login, |
---|
| 15 | 'p=s' => \my $pass, |
---|
| 16 | 't=s' => \my $type, |
---|
| 17 | 'd=s' => \my @options, |
---|
[217] | 18 | 'proxy' => \my $proxy, |
---|
[171] | 19 | 'daemon' => \my $daemon, |
---|
[210] | 20 | |
---|
| 21 | # Command option: |
---|
| 22 | 'set-password=s' => \my $password, |
---|
| 23 | 'edit-var=s' => \my $data, |
---|
[74] | 24 | ) or pod2usage; |
---|
[69] | 25 | |
---|
[74] | 26 | my %options; |
---|
| 27 | foreach (@options) { |
---|
| 28 | m/([^=]+)\s*=\s*(.*)/ or next; |
---|
| 29 | $options{$1} = $2; |
---|
| 30 | } |
---|
| 31 | |
---|
[187] | 32 | $configfile ||= "$ENV{HOME}/.sophie.conf"; |
---|
| 33 | |
---|
| 34 | if (my $conf = Config::General->new($configfile)) { |
---|
| 35 | my %config = $conf->getall; |
---|
| 36 | $url ||= $config{url}; |
---|
| 37 | $login ||= $config{login}; |
---|
| 38 | $pass ||= $config{pass}; |
---|
| 39 | $daemon ||= $config{daemon}; |
---|
[217] | 40 | $proxy ||= $config{proxy}; |
---|
[187] | 41 | } |
---|
| 42 | |
---|
[210] | 43 | my $sc = Sophie::Client->new( |
---|
| 44 | server => $url, |
---|
| 45 | login => $login, |
---|
| 46 | password => $pass, |
---|
[288] | 47 | type => $type || 'Term', |
---|
[217] | 48 | proxy => $proxy, |
---|
[210] | 49 | ); |
---|
| 50 | |
---|
| 51 | if ($password) { |
---|
| 52 | my $res = $sc->send_request('user.set_password', $password); |
---|
| 53 | if (ref $res && !$res->is_fault) { |
---|
| 54 | print $res->value . "\n"; |
---|
| 55 | exit(0); |
---|
| 56 | } else { |
---|
| 57 | print( (ref $res ? $res->string : $res) . "\n"); |
---|
| 58 | exit(1); |
---|
| 59 | } |
---|
| 60 | } elsif ($data) { |
---|
| 61 | my $res = $sc->send_request('user.dumpdata', $data); |
---|
| 62 | if (ref $res && !$res->is_fault) { |
---|
| 63 | my $tmp = File::Temp->new(); |
---|
| 64 | print $tmp $res->value . "\n"; |
---|
| 65 | close($tmp); |
---|
| 66 | system($ENV{EDITOR} || 'vi', $tmp->filename); |
---|
| 67 | if (open(my $fh, '<', $tmp->filename)) { |
---|
| 68 | my $string = join('', <$fh>); |
---|
| 69 | close($fh); |
---|
| 70 | unlink($tmp->filename); |
---|
| 71 | my $res2 = $sc->send_request('user.loaddata', $data, $string); |
---|
| 72 | if (ref $res2 && !$res2->is_fault) { |
---|
| 73 | print $res2->value . "\n"; |
---|
| 74 | exit(0); |
---|
| 75 | } else { |
---|
| 76 | print( (ref $res2 ? $res2->string : $res2) . "\n"); |
---|
| 77 | exit(1); |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | } else { |
---|
| 81 | print( (ref $res ? $res->string : $res) . "\n"); |
---|
| 82 | exit(1); |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
[171] | 86 | if ($daemon) { |
---|
| 87 | my $pid = fork(); |
---|
| 88 | |
---|
| 89 | if (defined($pid)) { |
---|
| 90 | if ($pid) { |
---|
| 91 | # father, exiting; |
---|
| 92 | exit(0); |
---|
| 93 | } else { |
---|
| 94 | # child |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
[69] | 99 | $sc->run; |
---|