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

Last change on this file since 405 was 401, checked in by nanardon, 13 years ago
  • die if irc nick name is already in use
File size: 2.8 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) or pod2usage;
27
28my %options;
29foreach (@options) {
30    m/([^=]+)\s*=\s*(.*)/ or next;
31    $options{$1} = $2;
32}
33
34$configfile ||= "/etc/sophie/sophie-bot.conf";
35
36if (my $conf = Config::General->new($configfile)) {
37    my %config = $conf->getall;
38    $url         ||= $config{url};
39    $login       ||= $config{login};
40    $pass        ||= $config{pass};
41    $daemon      ||= $config{daemon};
42    $proxy       ||= $config{proxy};
43    $runas       ||= $config{runas};
44    $pidfile     ||= $config{pidfile};
45}
46
47my $sc = Sophie::Bot->new(
48    server => $url,
49    login => $login,
50    password => $pass,
51    type => $type || 'Term',
52    proxy => $proxy,
53);
54
55if ($password) {
56    my $res = $sc->send_request('user.set_password', $password);
57    if (ref $res && !$res->is_fault) {
58        print $res->value . "\n";
59        exit(0);
60    } else {
61        print( (ref $res ? $res->string : $res) . "\n");
62        exit(1);
63    }
64} elsif ($data) {
65    my $res = $sc->send_request('user.dumpdata', $data);
66    if (ref $res && !$res->is_fault) {
67        my $tmp = File::Temp->new();
68        print $tmp $res->value . "\n";
69        close($tmp);
70        system($ENV{EDITOR} || 'vi', $tmp->filename);
71        if (open(my $fh, '<', $tmp->filename)) {
72            my $string = join('', <$fh>);
73            close($fh);
74            unlink($tmp->filename);
75            my $res2 = $sc->send_request('user.loaddata', $data, $string);
76            if (ref $res2 && !$res2->is_fault) {
77                print $res2->value . "\n";
78                exit(0);
79            } else {
80                print( (ref $res2 ? $res2->string : $res2) . "\n");
81                exit(1);
82            }
83        }
84    } else {
85        print( (ref $res ? $res->string : $res) . "\n");
86        exit(1);
87    }
88}
89
90if ($runas) {
91    my ($login,$pass,$uid,$gid) = $runas =~ /^\d/
92    ? getpwuid($runas)
93    : getpwnam($runas);
94
95    $> = $uid; $) = $gid;
96    if ($> ne $uid) {
97        die "Cannot change to user $runas\n";
98    }
99}
100
101if ($daemon) {
102
103    my $pid = fork();
104
105    if (defined($pid)) {
106        if ($pid) {
107            # father, exiting;
108            exit(0);
109        } else {
110            # child
111        }
112    }
113}
114
115if ($pidfile) {
116    if (open(my $handle, '>', $pidfile)) {
117        print $handle $$ . "\n";
118        close($handle);
119    } else {
120    }
121}
122
123$sc->run;
124
Note: See TracBrowser for help on using the repository browser.