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
|
#!/usr/bin/env perl
use warnings;
use strict;
use feature qw(say);
use JSON;
use DateTime;
use DateTime::Format::ISO8601;
use Net::OpenSSH;
my $rfile;
unless ($ARGV[0] =~ /^-h/ || $ARGV[0] =~ /^-w/) {
say q(You must indicate either -h for home.rem or -w for work.rem.); exit;
}
unless ($ARGV[1] =~ /^REM/) {
say q(Your message must start with REM. Format is REM DATE AT TIME MSG Message - any traditional remind format.); exit;
}
if ($ARGV[0] eq "-h") {
$rfile = "~/.reminders/home.rem";
say "Using home.rem."
} elsif ($ARGV[0] eq "-w") {
$rfile = "~/.reminders/work.rem";
say "Using work.rem."
}
sub check_env {
# Log into remote server
my $host = $ENV{"TW_HOOK_REMIND_REMOTE_HOST"}
or die "Cannot get TW_HOOK_REMIND_REMOTE_HOST environment variable";
my $user = $ENV{"TW_HOOK_REMIND_REMOTE_USER"}
or die "Cannot get TW_HOOK_REMIND_REMOTE_USER environment variable";
return ($host, $user);
}
sub get_connection {
my $host = shift;
my $port = shift;
my $user = shift;
# use correct port
if ( $host =~ m/.*\.xyz$/ ) { $port = 2222 }
say "Trying to establish connection at $host:$port ...";
my $ssh = Net::OpenSSH->new( $host, user => $user, port => $port );
$ssh->error and die "Couldn't establish SSH connection: " . $ssh->error;
return $ssh;
}
sub check_remind_file_exists {
my $ssh = shift;
my $host = shift;
my $remfile = shift;
#
# Check for presence or remind file
if ( $ssh->test("ls $remfile") != 1 ) {
die "Cannot find $remfile on $host.";
}
# If it is there, back it up
$ssh->system("cp $remfile $remfile.bak")
or die "Cannot create a back-up of remind file.";
}
sub append_to_remfile {
my $ssh = shift;
my $host = shift;
my $remfile = shift;
my $remline = shift;
if ($remline =~ /\sAT\s/) {
chomp $remline;
$remline = $remline . " %3\n";
} else
{
chomp $remline;
$remline = $remline . " %b\n";
}
# Append the Remind formatted line to the original remind file
$ssh->system( { stdin_data => $remline }, "cat >> $remfile" )
or die "Cannot append text: " . $ssh->error;
# $ssh->system("echo >> $remline $remfile")
# or die "Cannot append text: " . $ssh->error;
# Get content of remind file
my @out_file = $ssh->capture("cat $remfile");
print qq/
Contents of $remfile on $host is now:\n/, @out_file;
}
my ($host, $user) = check_env();
my $ssh = get_connection($host, 2222, $user);
check_remind_file_exists($ssh, $host, $rfile);
append_to_remfile($ssh, $host, $rfile, "$ARGV[1]\n");
exit;
|