1 | #! /bin/env perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use Sophie::Client; |
---|
6 | use Getopt::Long; |
---|
7 | use Pod::Usage; |
---|
8 | use Config::General; |
---|
9 | use File::Temp; |
---|
10 | |
---|
11 | GetOptions( |
---|
12 | 'c=s' => \my $configfile, |
---|
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, |
---|
18 | 'proxy' => \my $proxy, |
---|
19 | 'daemon' => \my $daemon, |
---|
20 | |
---|
21 | # Command option: |
---|
22 | 'set-password=s' => \my $password, |
---|
23 | 'edit-var=s' => \my $data, |
---|
24 | ) or pod2usage; |
---|
25 | |
---|
26 | my %options; |
---|
27 | foreach (@options) { |
---|
28 | m/([^=]+)\s*=\s*(.*)/ or next; |
---|
29 | $options{$1} = $2; |
---|
30 | } |
---|
31 | |
---|
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}; |
---|
40 | $proxy ||= $config{proxy}; |
---|
41 | } |
---|
42 | |
---|
43 | my $sc = Sophie::Client->new( |
---|
44 | server => $url, |
---|
45 | login => $login, |
---|
46 | password => $pass, |
---|
47 | type => $type || 'Term', |
---|
48 | proxy => $proxy, |
---|
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 | |
---|
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 | |
---|
99 | $sc->run; |
---|