Skip to content

Commit 8853685

Browse files
committed
Merge pull request NARKOZ#52 from robaganrab/perl-implementation
Add Perl Implementation
2 parents ac6edc5 + 3c4205c commit 8853685

File tree

4 files changed

+282
-0
lines changed

4 files changed

+282
-0
lines changed

perl/fucking-coffee.pl

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use DateTime;
7+
use YAML;
8+
use Net::Telnet;
9+
10+
# Config
11+
my $conf = Load( <<'...' );
12+
---
13+
coffee_machine_ip: 10.10.42.42
14+
password: 1234
15+
password_prompt: Password:
16+
delay_before_brew: 17
17+
delay: 24
18+
...
19+
20+
# Skip on weekends
21+
my $date = DateTime->now;
22+
if ( $date->day_of_week >= 6 ) {
23+
exit;
24+
}
25+
26+
# Exit early if no sessions with my username are found
27+
open( my $cmd_who, '-|', 'who' ) || die "Cannot pipe who command ". $!;
28+
29+
my @sessions = grep {
30+
m/$ENV{'USER'}/
31+
} <$cmd_who>;
32+
33+
close $cmd_who;
34+
35+
exit if ( scalar( @sessions ) == 0 );
36+
37+
sleep $conf->{'delay_before_brew'};
38+
39+
my $con = Net::Telnet->new(
40+
'Host' => $conf->{'coffee_machine_ip'},
41+
);
42+
43+
$con->watifor( $conf->{'password_prompt'} );
44+
$con->cmd( $conf->{'password'} );
45+
$con->cmd( 'sys brew' );
46+
sleep $conf->{'delay'};
47+
$con->cmd( 'sys pour' );
48+
$con->close;
49+

perl/hangover.pl

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use DateTime;
7+
use SMS::Send;
8+
use YAML;
9+
10+
# Config
11+
my $conf = Load( <<'...' );
12+
---
13+
phone_numbers:
14+
my_number: +15005550006
15+
boss_number: +xxx
16+
reasons:
17+
- Locked out
18+
- Pipes broke
19+
- Food poisoning
20+
- Not feeling well
21+
...
22+
23+
my $date = DateTime->now;
24+
25+
# Skip on weekends
26+
if ( $date->day_of_week >= 6 ) {
27+
exit;
28+
}
29+
30+
# Exit early if no sessions with my username are found
31+
open( my $cmd_who, '-|', 'who' ) || die "Cannot pipe who command ". $!;
32+
33+
my @sessions = grep {
34+
m/$ENV{'USER'}/
35+
} <$cmd_who>;
36+
37+
close $cmd_who;
38+
39+
exit if ( scalar( @sessions ) == 0 );
40+
41+
# Load Twilio API config
42+
open( my $env, '<', '../.env' ) || die "Cannot find .env file in project root.";
43+
LINE: while ( my $line = <$env> ) {
44+
next LINE unless ( $line =~ m/^(TWILIO[^=]+)=(.*)(?:[\n\r]*)/ );
45+
$conf->{'env'}->{ $1 } = $2;
46+
}
47+
48+
close $env;
49+
50+
# Randomize excuse
51+
my $reason_number = int( rand( scalar( @{ $conf->{'reasons'} } ) ) );
52+
my $sms_text = "Gonna work from home. ". $conf->{'reasons'}[ $reason_number ];
53+
54+
# Create an object. There are three required values:
55+
my $sender = SMS::Send->new('Twilio',
56+
_accountsid => $conf->{'env'}->{'TWILIO_ACCOUNT_SID'},
57+
_authtoken => $conf->{'env'}->{'TWILIO_AUTH_TOKEN'},
58+
_from => $conf->{'phone_numbers'}->{'my_number'},
59+
);
60+
61+
# Send a message to me
62+
my $sent = $sender->send_sms(
63+
text => $sms_text,
64+
to => $conf->{'phone_numbers'}->{'boss_number'},
65+
);
66+
67+
# Did it send?
68+
if ( $sent ) {
69+
print "Sent message.\n";
70+
} else {
71+
print "Message failed.\n";
72+
}
73+

perl/kumar-asshole.pl

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use YAML;
7+
use DateTime;
8+
use Mail::Webmail::Gmail;
9+
10+
# Config
11+
my $conf = Load( <<'...' );
12+
---
13+
kumar_mail: [email protected]
14+
database_regex: \S+_staging
15+
keywords_regex: sorry|help|wrong
16+
backup_path: /home/backups/databases/
17+
...
18+
$conf->{'database_regex'} = qr/ ( $conf->{'database_regex'} ) /x;
19+
$conf->{'keywords_regex'} = qr/ ( $conf->{'keywords_regex'} ) /x;
20+
21+
my $date = DateTime->now->subtract(
22+
'days' => 1
23+
);
24+
25+
# Load GMail API config
26+
open( my $env, '<', '../.env' ) || die "Cannot find .env file in project root.";
27+
LINE: while ( my $line = <$env> ) {
28+
next LINE unless ( $line =~ m/^(GMAIL[^=]+)=(.*)(?:[\n\r]*)/ );
29+
$conf->{'env'}->{ $1 } = $2;
30+
}
31+
32+
close $env;
33+
34+
my $gmail = Mail::Webmail::Gmail->new(
35+
username => $conf->{'env'}->{'GMAIL_USERNAME'},
36+
password => $conf->{'env'}->{'GMAIL_PASSWORD'},
37+
encrypt_session => 1,
38+
);
39+
40+
my $messages = $gmail->get_messages( label => $Mail::Webmail::Gmail::FOLDERS{ 'INBOX' } );
41+
die "Cannot fetch emails: ". $gmail->error_msg();
42+
43+
MESSAGE: foreach my $message ( @{ $messages } ) {
44+
unless (
45+
( $message->{ 'new' } )
46+
&& ( $message->{'sender_email'} eq $conf->{'kumars_email'} )
47+
&& ( $message->{'body'} =~ m/$conf->{'keywords_regex'}/ )
48+
&& ( $message->{'body'} =~ m/$conf->{'database_regex'}/ )
49+
) {
50+
print "Skipping mail from=[". $message->{'sender_email'}."] subject=[". $message->{'subject'} ."]\n";
51+
next MESSAGE;
52+
}
53+
exit 1;
54+
55+
my $database = $1;
56+
my $backup_file = $conf->{'backup_path'} . $database .'-'. $date->ymd() .'.gz';
57+
58+
unless ( -f $backup_file ) {
59+
die 'Cannot find backup file=['. $backup_file ."]\n";
60+
}
61+
62+
print 'Restoring database=['. $database .'] from day=['. $date->ymd() .'] from file=['. $backup_file ."]\n";
63+
64+
# Restore DB
65+
system( 'gunzip -c '. $backup_file .' | psql '. $database );
66+
die "Error while restoring the database=[". $database ."] from file=[". $backup_file ."]" if ( $? >> 8 );
67+
68+
# Mark as read, add label, reply
69+
$gmail->edit_labels(
70+
'label' => 'Database fixes',
71+
'action' => 'add',
72+
'msgid' => $message->{'id'}
73+
);
74+
75+
$gmail->send_message(
76+
'to' => $conf->{'kumars_email'},
77+
'subject' => 'RE: '. $message->{'subject'},
78+
'msgbody' => "No problem. I've fixed it. \n\n Please be careful next time.",
79+
);
80+
81+
$gmail->edit_labels(
82+
'label' => 'unread',
83+
'action' => 'remove',
84+
'msgid' => $message->{'id'}
85+
);
86+
87+
}
88+

perl/smack-my-bitch-up.pl

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
use DateTime;
7+
use SMS::Send;
8+
use YAML;
9+
10+
# Config
11+
my $conf = Load( <<'...' );
12+
---
13+
phone_numbers:
14+
my_number: +15005550006
15+
her_number: +xxx
16+
reasons:
17+
- Working hard
18+
- Gotta ship this feature
19+
- Someone fucked the system again
20+
...
21+
22+
my $date = DateTime->now;
23+
24+
# Skip on weekends
25+
if ( $date->day_of_week >= 6 ) {
26+
exit;
27+
}
28+
29+
# Exit early if no sessions with my username are found
30+
open( my $cmd_who, '-|', 'who' ) || die "Cannot pipe who command ". $!;
31+
32+
my @sessions = grep {
33+
m/$ENV{'USER'}/
34+
} <$cmd_who>;
35+
36+
close $cmd_who;
37+
38+
exit if ( scalar( @sessions ) == 0 );
39+
40+
# Load Twilio API config
41+
open( my $env, '<', '../.env' ) || die "Cannot find .env file in project root.";
42+
LINE: while ( my $line = <$env> ) {
43+
next LINE unless ( $line =~ m/^(TWILIO[^=]+)=(.*)(?:[\n\r]*)/ );
44+
$conf->{'env'}->{ $1 } = $2;
45+
}
46+
47+
close $env;
48+
49+
# Randomize excuse
50+
my $reason_number = int( rand( scalar( @{ $conf->{'reasons'} } ) ) );
51+
my $sms_text = "Late at work. ". $conf->{'reasons'}[ $reason_number ];
52+
53+
# Create an object. There are three required values:
54+
my $sender = SMS::Send->new('Twilio',
55+
_accountsid => $conf->{'env'}->{'TWILIO_ACCOUNT_SID'},
56+
_authtoken => $conf->{'env'}->{'TWILIO_AUTH_TOKEN'},
57+
_from => $conf->{'phone_numbers'}->{'my_number'},
58+
);
59+
60+
# Send a message to me
61+
my $sent = $sender->send_sms(
62+
text => $sms_text,
63+
to => $conf->{'phone_numbers'}->{'her_number'},
64+
);
65+
66+
# Did it send?
67+
if ( $sent ) {
68+
print "Sent message.\n";
69+
} else {
70+
print "Message failed.\n";
71+
}
72+

0 commit comments

Comments
 (0)