From c67ec9f37c46f42297a6844806debe439be290cd Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Sun, 18 Sep 2022 21:12:26 +0100 Subject: tidy up files --- calendar/dayplan.pl | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++ calendar/modevent.pl | 76 ++++++++++++++++++++++++++++++++ 2 files changed, 198 insertions(+) create mode 100755 calendar/dayplan.pl create mode 100644 calendar/modevent.pl (limited to 'calendar') diff --git a/calendar/dayplan.pl b/calendar/dayplan.pl new file mode 100755 index 0000000..3074c2d --- /dev/null +++ b/calendar/dayplan.pl @@ -0,0 +1,122 @@ +#!/usr/bin/perl +# Porting dayplan.ksh to Perl + +use strict; +use warnings; +use DateTime; + + +my @quicknotes; +my @qfiles; +my ($dt, $d, $y, $m, $weekday); + +my @weekdays = qw(Monday Tuesday Wednesday Thursday Friday Saturday Sunday); +my $dayplans = '/home/lemon/Notes/journal/day_plans'; +#my $dayplans = '/tmp'; +my $numargs = $#ARGV + 1; + + +# Go back and get short notes from past files +foreach my $f (glob("$dayplans/*.txt")) { + open my $fh, "<", $f or die "Cannot open that file"; + while (<$fh>) { + if ($_ =~ /^(- \w.*)$/) { + push @quicknotes => "$1\n"; + push @qfiles => "$f\n"; + }; + + } +} +# deduplicate stuff +my %riddups = map { $_, "" } @quicknotes; +@quicknotes = keys %riddups; +my %riddfiles = map { $_, "" } @qfiles; +@qfiles = keys %riddfiles; + +if ($numargs == 1) { + ($y, $m, $d) = $ARGV[0] =~ /(\d\d\d\d)-(\d\d)-(\d\d)/; + $dt = DateTime->new( + year => $y, + month => $m, + day => $d + ); + $weekday = $weekdays[$dt->day_of_week - 1]; +} +else { + $dt = DateTime->today; + $d = $dt->day; + $m = $dt->month; + $y = $dt->year; + $weekday = $weekdays[$dt->day_of_week - 1]; +} + +sub schoollines { + my $day = shift; + if ($day =~ /Saturday|Sunday/) { + return ""; + } else + { + return " +08:15 - 08:20 - Harvey to school +08:45 - 09:00 - Sophie to school +09:15 - 09:30 - Email"; + } +} + +my $reminders = qx(ssh bobbins remind ~/.reminders $y-$m-$d); +$reminders =~ s/\s{2,}/\n/gs; +$reminders =~ s/^Reminders.+\:\n//; + +my $s = schoollines($weekday); + +$" = ""; + +my $qnote_block; +if (scalar @quicknotes == 0) { + $qnote_block = "No quicknotes today.\n"; +} else +{ + $qnote_block = "@quicknotes"."from:"."\n"."@qfiles"; +} + +my $mname = $dt->month_name; +my $template = "Goal for $weekday $d $mname $y: [replace this with your goal] +--- + +$qnote_block +Reminders: +--------- +$reminders +$s +09:30 - 10:00 - +10:00 - 11:00 - +11:00 - 12:00 - +12:15 - 13:00 - Lunch +13:00 - 14:00 - +14:00 - 15:00 - +15:00 - 16:00 - +16:00 - 17:00 - +"; + +sub write_file { + my $f = shift; + + open( FH, ">$f"); + print FH $template; + my $today = DateTime->today; + if ($today != $dt) { + printf (FH "\nWARNING: This dayplan was generated in advance on %d-%02d-%d. Reminders and quicknotes may not be up to date.", $today->year, $today->month, $today->day); + } + + close FH; + exec("vim", "$f"); +} + +my $today_planner = sprintf("%s/%d-%02d-%02d.txt", $dayplans,$y,$m,$d); + +if (-e $today_planner) { + exec("vim", "$today_planner"); +} else +{ + write_file($today_planner) +} diff --git a/calendar/modevent.pl b/calendar/modevent.pl new file mode 100644 index 0000000..674e97c --- /dev/null +++ b/calendar/modevent.pl @@ -0,0 +1,76 @@ +#!/usr/bin/perl + +use warnings; +use strict; +use feature qw(say); +use JSON; +use DateTime; +use DateTime::Format::ISO8601; +use Net::OpenSSH; + +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; + + # 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 $remfile = "~/.reminders/work.rem"; +my ($host, $user) = check_env(); +my $ssh = get_connection($host, 2222, $user); +check_remind_file_exists($ssh, $host, $remfile ); +append_to_remfile($ssh, $host, $remfile, 'REM 16 Sept 2022 AT 11:11 MSG TEST2 %1'); +exit; + + + + -- cgit v1.2.3