1 # A Perl module for communicating with the MO Submit Server
2 # (c) 2007 Martin Mares <mj@ucw.cz>
10 use IO::Socket::SSL; # qw(debug3);
14 my $user = $ENV{"USER"} or die "Environment variable USER not set\n";
15 my $home = $ENV{"HOME"} or die "Environment variable HOME not set\n";
17 my $root = $ENV{"MO_ROOT"} or die "Environment variable MO_ROOT not set\n";
19 "Contest" => "CPSPC 2007",
20 "Server" => "localhost:8888",
21 "Key" => "$mo/key.pem",
22 "Cert" => "$mo/cert.pem",
23 "CACert" => "$mo/ca-cert.pem",
24 "Trace" => defined $ENV{"MO_SUBMIT_TRACE"},
38 my ($self, $msg) = @_;
39 print STDERR "SUBMIT: $msg\n" if $self->{"Trace"};
43 my ($self, $msg) = @_;
44 print STDERR "ERROR: $msg\n" if $self->{"Trace"};
45 $self->{"error"} = $msg;
51 return defined $self->{"sk"};
56 if ($self->is_connected) {
58 $self->{"sk"} = undef;
59 $self->log("Disconnected");
66 $self->log("Connecting to submit server");
67 my $sk = new IO::Socket::INET(
68 PeerAddr => $self->{"Server"},
72 $self->err("Cannot connect to server: $!");
77 $self->err("Server failed to send a welcome message");
83 $self->err("Server rejected the connection: $z");
88 $self->log("Starting TLS");
89 $sk = IO::Socket::SSL->start_SSL(
91 SSL_version => 'TLSv1',
93 SSL_key_file => $self->{"Key"},
94 SSL_cert_file => $self->{"Cert"},
95 SSL_ca_file => $self->{"CACert"},
99 $self->err("Cannot establish TLS connection: " . IO::Socket::SSL::errstr());
105 $self->log("Logging in");
106 my $req = new Sherlock::Object("U" => $self->{"user"});
107 my $reply = $self->request($req);
108 my $err = $reply->get("-");
110 $self->err("Cannot log in: $err");
114 $self->log("Connected");
119 my ($self, $obj) = @_;
120 my $sk = $self->{"sk"};
121 $obj->write($sk); ### FIXME: Flushing
123 $self->err("Connection broken");
131 my ($self, $obj) = @_;
132 my $sk = $self->{"sk"};
133 my $reply = new Sherlock::Object;
134 if ($reply->read($sk)) {
137 $self->err("Connection broken");
143 my ($self, $fh, $size) = @_;
144 my $sk = $self->{"sk"};
146 my $l = ($size < 4096 ? $size : 4096);
148 if ($fh->read($buf, $l) != $l) {
149 $self->err("File shrunk during upload");
152 $sk->write($buf, $l);
154 $self->err("Connection broken");