1
#!/usr/bin/perl
2
use utf8;
3
use strict;
4
use warnings;
5
use feature 'say';
6
use lib '/home/joenio/perl5/share/perl/5.10.0/';
7
use WWW::Mechanize::TWiki;
8
use HTML::Entities;
9
use File::Slurp;
10
use Term::ReadPassword;
11
use Config::IniFiles;
12
use Getopt::Long;
13
14
use constant SCRIPTURL => 'http://intranet.colivre.coop.br';
15
16
my $local = undef;
17
GetOptions(local => \$local);
18
19
my $mech = WWW::Mechanize::TWiki->new(agent => 'colivre-robot', autocheck => 1 ) or die $!;
20
$mech->cgibin(SCRIPTURL, { scriptSuffix => '' });
21
22
sub twiki_logon {
23
   my ($username, $password) = @_;
24
   $mech->get('https://www.colivre.coop.br/bin/login/');
25
   $mech->form_name('loginform');
26
   $mech->field('username', $username);
27
   $mech->field('password', $password);
28
   $mech->submit();
29
}
30
sub apache_logon {
31
   my ($username, $password) = @_;
32
   $mech->get("https://$username:$password\@intranet.colivre.coop.br");
33
}
34
35
sub logon {
36
   my ($username, $password) = @_;
37
   print STDERR "logon... ";
38
   apache_logon($username, $password);
39
   say STDERR "ok";
40
}
41
42
sub get_remote_tasks {
43
   my $topic = 'Intranet.MinhasAtividades?template=view.text';
44
   print STDERR "acessando $topic... ";
45
   $mech->get("/$topic");
46
   say STDERR "ok";
47
   return $mech->response->decoded_content;
48
}
49
50
sub get_local_tasks {
51
   open LOCAL, '<:utf8', "$ENV{HOME}/doc/todo";
52
   local $/ = undef;
53
   my $local_tasks = <LOCAL>;
54
}
55
56
sub concat_tasks  {
57
   my ($remote_tasks, $local_tasks) = @_;
58
   open TASKS, '>:utf8', "$ENV{HOME}/.gtimelog/remote-tasks.txt";
59
   say TASKS $remote_tasks, "\n", $local_tasks;
60
   close TASKS;
61
}
62
63
my $conf = Config::IniFiles->new(-file => "$ENV{HOME}/.gtimelog/gtimelogrc");
64
my $remote_user = $conf->val('gtimelog', 'remote-user') || 'YourWikiName';
65
66
my $remote_tasks = '';
67
unless ($local) {
68
   print "wikiname [$remote_user]: ";
69
   chomp(my $username = <STDIN>);
70
   if ($username) {
71
      $conf->setval('gtimelog', 'remote-user', $username);
72
   }
73
   else {
74
      $username = $remote_user;
75
   }
76
   my $password = read_password('senha: ');
77
   logon($username, $password);
78
   $remote_tasks = get_remote_tasks;
79
}
80
81
concat_tasks($remote_tasks, get_local_tasks);
82
83
$conf->RewriteConfig;