/usr/share/perl5/Padre/Sync.pm is in padre 1.00+dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | package Padre::Sync;
=pod
=head1 NAME
Padre::Sync - Utility functions for handling remote Configuration Syncing
=head1 DESCRIPTION
The C<Padre::Sync> class contains logic for communicating with a remote
L<Madre::Sync> server. This class interacts with the
L<Padre::Wx::Dialog::Sync> class for user interface display.
=head1 METHODS
=cut
use 5.008;
use strict;
use warnings;
use Carp ();
use File::Spec ();
use Scalar::Util ();
use Params::Util ();
use JSON::XS ();
use LWP::UserAgent ();
use HTTP::Cookies ();
use HTTP::Request::Common ();
use Padre::Current ();
use Padre::Constant ();
our $VERSION = '1.00';
our $COMPATIBLE = '0.95';
#####################################################################
# Constructor and Accessors
=pod
=head2 C<new>
The constructor returns a new C<Padre::Sync> object, but
you should normally access it via the main Padre object:
my $manager = Padre->ide->config_sync;
First argument should be a Padre object.
=cut
sub new {
my $class = shift;
my $ide = Params::Util::_INSTANCE( shift, 'Padre' );
Carp::croak("Failed to create Padre::Sync") unless $ide;
# Create the useragent.
# We need this to handle login actions.
# Save cookies for state management from Padre session to session
# NOTE: Is this even wanted? Remove at padre close?
my $ua = LWP::UserAgent->new(
timeout => 10,
cookie_jar => HTTP::Cookies->new(
file => File::Spec->catfile(
Padre::Constant::CONFIG_DIR,
'lwp_cookies.dat',
),
autosave => 1,
)
);
my $self = bless {
ide => $ide,
state => 'not_logged_in',
ua => $ua,
@_,
}, $class;
return $self;
}
=pod
=head2 C<main>
A convenience method to get to the main window.
=cut
sub main {
$_[0]->{ide}->wx->main;
}
=pod
=head2 C<config>
A convenience method to get to the config object
=cut
sub config {
$_[0]->{ide}->config;
}
=pod
=head2 C<ua>
A convenience method to get to the useragent object
=cut
sub ua {
$_[0]->{ua};
}
=pod
=head2 C<register>
Attempts to register a user account with the information provided on the
Sync server.
Parameters: a list of key value pairs to be interpreted as POST parameters
Returns error string if user state is already logged in or serverside error
occurs.
=cut
sub register {
my $self = shift;
my %params = @_;
if ( $self->{state} ne 'not_logged_in' ) {
return 'Failure: cannot register account, user already logged in.';
}
# BUG: This crashes if server is unavailable.
my $server = $self->server or return 'Failure: no server found.';
my $response = $self->POST(
"$server/register",
'Content-Type' => 'application/json',
'Content' => $self->encode( \%params ),
);
if ( $response->code == 201 ) {
return 'Account registered successfully. Please log in.';
}
local $@;
my $h = eval { $self->decode( $response->content ); };
return "Registration failure(Server): $h->{error}" if $h->{error};
return "Registration failure(Padre): $@" if $@;
return "Registration failure(unknown)";
}
=pod
=head2 C<login>
Will log in to remote Sync server using given credentials. State will
be updated if login successful.
=cut
sub login {
my $self = shift;
if ( $self->{state} ne 'not_logged_in' ) {
return 'Failure: cannot log in, user already logged in.';
}
my $server = $self->server or return 'Failure: no server found.';
my $response = $self->POST( "$server/login", [ {@_} ] );
if ( $response->content !~ /Wrong username or password/i
and ( $response->code == 200 or $response->code == 302 ) )
{
$self->{state} = 'logged_in';
return 'Logged in successfully.';
}
return 'Login Failure.';
}
=pod
=head2 C<logout>
If currently logged in, will log the Sync session out from the server.
State will be updated.
=cut
sub logout {
my $self = shift;
if ( $self->{state} ne 'logged_in' ) {
return 'Failure: cannot logout, user not logged in.';
}
my $server = $self->server or return 'Failure: no server found.';
my $response = $self->GET("$server/logout");
if ( $response->code == 200 ) {
$self->{state} = 'not_logged_in';
return 'Logged out successfully.';
}
return 'Failed to log out.';
}
=pod
=head2 C<server_delete>
Given a logged in session, will attempt to delete the config currently
stored on the Sync server (if one currently exists).
Will fail if not logged in.
=cut
sub server_delete {
my $self = shift;
if ( $self->{state} ne 'logged_in' ) {
return 'Failure: user not logged in.';
}
my $server = $self->server or return 'Failure: no server found.';
my $response = $self->DELETE("$server/config");
if ( $response->code == 204 ) {
return 'Configuration deleted successfully.';
}
return 'Failed to delete serverside configuration file.';
}
=pod
=head2 C<local_to_server>
Given a logged in session, will attempt to place the current local config to
the Sync server.
=cut
sub local_to_server {
my $self = shift;
if ( $self->{state} ne 'logged_in' ) {
return 'Failure: user not logged in.';
}
# NOTE: There has be a better way to do this
my $conf = $self->config->human;
my %copy = %$conf;
my $server = $self->server or return 'Failure: no server found.';
my $response = $self->PUT(
"$server/config",
'Content-Type' => 'application/json',
'Content' => $self->encode( \%copy ),
);
if ( $response->code == 204 ) {
return 'Configuration uploaded successfully.';
}
return 'Failed to upload configuration file to server.';
}
=pod
=head2 C<server_to_local>
Given a logged in session, will replace the local config with what is stored
on the server.
TODO: is validation of config before replacement required?
=cut
sub server_to_local {
my $self = shift;
if ( $self->{state} ne 'logged_in' ) {
return 'Failure: user not logged in.';
}
my $server = $self->server or return 'Failure: no server found.';
my $response = $self->GET(
"$server/config",
'Accept' => 'application/json',
);
local $@;
my $json;
eval { $json = $self->decode( $response->content ); };
return 'Failed to deserialize serverside configuration.' if $@;
# Apply each setting to the global config. should only be HUMAN
# settings.
delete $json->{Version};
delete $json->{version};
my @errors;
my $config = $self->config;
for my $key ( keys %$json ) {
my $meta = eval { $config->meta($key); };
unless ( $meta and $meta->store == Padre::Constant::HUMAN ) {
# Skip unknown or non-HUMAN settings
next;
}
eval { $config->apply( $key, $json->{$key} ); };
push @errors, $@ if $@;
}
$config->write;
if ( $response->code == 200 && @errors == 0 ) {
return 'Configuration downloaded and applied successfully.';
} elsif ( $response->code == 200 && @errors ) {
warn @errors;
return 'Configuration downloaded successfully, some errors encountered applying to your current configuration.';
}
return 'Failed to download serverside configuration file to local Padre instance.';
}
=pod
=head2 C<english_status>
Will return a string explaining current state of Sync
dependent on $self->{state}
=cut
sub english_status {
my $self = shift;
return 'User is not currently logged into the system.' if $self->{state} eq 'not_logged_in';
return 'User is currently logged into the system.' if $self->{state} eq 'logged_in';
return "State unknown: $self->{state}";
}
######################################################################
# Support Methods
sub encode {
JSON::XS->new->encode( $_[1] );
}
sub decode {
JSON::XS->new->decode( $_[1] );
}
sub server {
my $self = shift;
my $server = $self->config->config_sync_server;
$server =~ s/\/$// if $server;
return $server;
}
sub GET {
shift->ua->request( HTTP::Request::Common::GET(@_) );
}
sub POST {
shift->ua->request( HTTP::Request::Common::POST(@_) );
}
sub PUT {
shift->ua->request( HTTP::Request::Common::PUT(@_) );
}
sub DELETE {
shift->ua->request( HTTP::Request::Common::DELETE(@_) );
}
1;
=pod
=head1 SEE ALSO
L<Padre>, L<Padre::Config>
=head1 COPYRIGHT & LICENSE
Copyright 2008-2013 The Padre development team as listed in Padre.pm.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl 5 itself.
=cut
# Copyright 2008-2013 The Padre development team as listed in Padre.pm.
# LICENSE
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl 5 itself.
|