OSDN Git Service

add wp_imgswap2.py for new OSDN Magazine
[otptools/otptools.git] / otp_attach.pl
1 #!/usr/bin/perl
2 # SourceForge.JP Wiki manipulator
3 # by hylom
4 # This code is under GPL.
5 #
6
7 use warnings;
8 use strict;
9
10 use utf8;
11 use open IO => ':utf8';
12 use open ':std';
13
14 # 下記は環境に応じて適当に変更
15 # Otp.pm およびOtpディレクトリ(Otp/Attach.pm)があるディレクトリを指定する
16 # use lib '~/bin'
17 use lib 'C:\Users\hirom\bin\otptools';
18
19 use Otp;
20 use IO::File;
21 use Term::Prompt;
22 use URI::Escape;
23 use Encode;
24 use File::Temp;
25 use Getopt::Std;
26
27 my $usage = <<EOD;
28 otp_attach.pl file 
29
30 example:
31     otp_attach.pl login -u loginname -p password
32     otp_attach.pl logout
33     otp_attach.pl attach -i sid -f attachmentfile
34 EOD
35
36 my $command = shift @ARGV;
37 unless( $command ) {
38   print $usage;
39   exit;
40 }
41
42 if ( $command eq 'login' ) {    #login
43   my %opts;
44   getopt('up', \%opts);
45   my $login_name;
46   my $password;
47
48   if ( defined $opts{'u'} ) {
49         $login_name = $opts{'u'};
50   } else {
51         $login_name = prompt( 'x', 'loginname:', '', '' );
52   }
53
54   if ( defined $opts{'p'} ) {
55         $password = $opts{'p'};
56   } else {
57         $password = prompt( 'p', 'password:', '', '' );
58         print "\n";
59   }
60
61   print "do login...\n";
62   login( $login_name, $password);
63
64 } elsif ( $command eq 'attach' ) { #attach
65   my %opts;
66   getopt('if', \%opts);
67
68   my $sid = $opts{'i'};
69   my $file = $opts{'f'};
70   add_attachment( $sid, $file );
71
72 } elsif ( $command eq 'logout' ) { #logout
73   print "do logout...\n";
74   logout();
75
76 } else {                                                #other
77   print $usage;
78   exit;
79 }
80
81 sub login {
82   my $login_name = shift @_;
83   my $password = shift @_;
84
85   my $otp = Otp->new();
86   $otp->login( $login_name, $password );
87 }
88
89 sub logout {
90   my $otp = Otp->new();
91   $otp->logout();
92 }
93
94 sub add_attachment {
95   my $sid = shift @_;
96   my $file = shift @_;
97
98   if ( ! -e $file ) {
99         die "$file is not exist.\n";
100   }
101
102   my $otp = Otp->new();
103   my $ret = $otp->Attach->post_attachment( sid => $sid,
104                                                                                   file => $file );
105
106   if ( $ret ) {
107         print "$file: upload succeed.\n";
108   } else {
109         print "$file: upload failed.\n";
110   }
111 }
112