source: bot/trunk/bin/sophie-bot @ 455

Last change on this file since 455 was 455, checked in by nanardon, 12 years ago
  • add options to list stored variables
File size: 3.2 KB
Line 
1#! /bin/env perl
2
3use strict;
4use warnings;
5use Sophie::Bot;
6use Getopt::Long;
7use Pod::Usage;
8use Config::General;
9use File::Temp;
10
11GetOptions(
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    'runas=s'   => \my $runas,
21    'pidfile=s' => \my $pidfile,
22
23    # Command option:
24    'set-password=s' => \my $password,
25    'edit-var=s'     => \my $data,
26    'list-var'       => \my $listvar,
27) or pod2usage;
28
29my %options;
30foreach (@options) {
31    m/([^=]+)\s*=\s*(.*)/ or next;
32    $options{$1} = $2;
33}
34
35$configfile ||= "/etc/sophie/sophie-bot.conf";
36
37if (my $conf = Config::General->new($configfile)) {
38    my %config = $conf->getall;
39    $url         ||= $config{url};
40    $login       ||= $config{login};
41    $pass        ||= $config{pass};
42    $daemon      ||= $config{daemon};
43    $proxy       ||= $config{proxy};
44    $runas       ||= $config{runas};
45    $pidfile     ||= $config{pidfile};
46    $type        ||= $config{type};
47}
48
49my $sc = Sophie::Bot->new(
50    server => $url,
51    login => $login,
52    password => $pass,
53    type => $type || 'Term',
54    proxy => $proxy,
55) or die "Cannot get sophie object\n";
56
57if ($password) {
58    my $res = $sc->send_request('user.set_password', $password);
59    if (ref $res && !$res->is_fault) {
60        print $res->value . "\n";
61        exit(0);
62    } else {
63        print( (ref $res ? $res->string : $res) . "\n");
64        exit(1);
65    }
66} elsif ($data) {
67    my $res = $sc->send_request('user.dumpdata', $data);
68    if (ref $res && !$res->is_fault) {
69        my $tmp = File::Temp->new();
70        print $tmp $res->value . "\n";
71        close($tmp);
72        system($ENV{EDITOR} || 'vi', $tmp->filename);
73        if (open(my $fh, '<', $tmp->filename)) {
74            my $string = join('', <$fh>);
75            close($fh);
76            unlink($tmp->filename);
77            my $res2 = $sc->send_request('user.loaddata', $data, $string);
78            if (ref $res2 && !$res2->is_fault) {
79                print $res2->value . "\n";
80                exit(0);
81            } else {
82                print( (ref $res2 ? $res2->string : $res2) . "\n");
83                exit(1);
84            }
85        }
86    } else {
87        print( (ref $res ? $res->string : $res) . "\n");
88        exit(1);
89    }
90} elsif ($listvar) {
91    my $res = $sc->send_request('user.listdata');
92    if (ref $res && !$res->is_fault) {
93        foreach (@{ $res->value || []}) {
94            print $_ . "\n";
95        }
96        exit (0);
97    } else {
98        print( (ref $res ? $res->string : $res) . "\n");
99        exit(1);
100    }
101}
102
103if ($daemon) {
104    my $pid = fork();
105
106    if (defined($pid)) {
107        if ($pid) {
108            # father, exiting;
109            exit(0);
110        } else {
111            # child
112        }
113    }
114}
115
116if ($pidfile) {
117    if (open(my $handle, '>', $pidfile)) {
118        print $handle $$ . "\n";
119        close($handle);
120    } else {
121    }
122}
123
124if ($runas) {
125    my ($login,$pass,$uid,$gid) = $runas =~ /^\d/
126    ? getpwuid($runas)
127    : getpwnam($runas);
128
129    $> = $uid; $) = $gid;
130    if ($> ne $uid) {
131        die "Cannot change to user $runas\n";
132    }
133}
134
135$sc->run;
136
Note: See TracBrowser for help on using the repository browser.