Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use three-arg open() syntax #13

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 22 additions & 31 deletions twirssi.pl
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,9 @@ sub cmd_login {
};

if ($twit) {
if ( open( OAUTH, Irssi::settings_get_str("twirssi_oauth_store") ) )
if ( open( my $oauth_fh, '<', Irssi::settings_get_str("twirssi_oauth_store") ) )
{
while (<OAUTH>) {
while (<$oauth_fh>) {
chomp;
next unless m/$user\@$service (\S+) (\S+)/i;
print "Trying cached oauth creds for $user\@$service"
Expand All @@ -580,7 +580,6 @@ sub cmd_login {
$twit->access_token_secret($2);
last;
}
close OAUTH;
}

unless ( $twit->authorized ) {
Expand Down Expand Up @@ -654,21 +653,19 @@ sub cmd_oauth {
my $store_file = Irssi::settings_get_str("twirssi_oauth_store");
if ($store_file) {
my @store;
if ( open( OAUTH, $store_file ) ) {
while (<OAUTH>) {
if ( open( my $oauth_fh, '<', $store_file ) ) {
while (<$oauth_fh>) {
chomp;
next if /$key/i;
push @store, $_;
}
close OAUTH;

}

push @store, "$key $access_token $access_token_secret";

if ( open( OAUTH, ">$store_file.new" ) ) {
print OAUTH "$_\n" foreach @store;
close OAUTH;
if ( open( my $oauth_fh, '>', "$store_file.new" ) ) {
print $oauth_fh "$_\n" foreach @store;
rename "$store_file.new", $store_file
or &notice("Failed to rename $store_file.new: $!");
} else {
Expand Down Expand Up @@ -894,15 +891,15 @@ sub cmd_upgrade {
return;
}

unless ( open( CUR, $loc ) ) {
my $cur_fh;
unless ( open( $cur_fh, '<', $loc ) ) {
&notice("Failed to read $loc."
. " Check that /set twirssi_location is set to the correct location."
);
return;
}

my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
close CUR;
my $cur_md5 = Digest::MD5::md5_hex(<$cur_fh>);

if ( $cur_md5 eq $md5 ) {
&notice("Current twirssi seems to be up to date.");
Expand All @@ -925,15 +922,15 @@ sub cmd_upgrade {
}

unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
unless ( open( NEW, "$loc.upgrade" ) ) {
my $new_fh;
unless ( open( $new_fh, '<', "$loc.upgrade" ) ) {
&notice("Failed to read $loc.upgrade."
. " Check that /set twirssi_location is set to the correct location."
);
return;
}

my $new_md5 = Digest::MD5::md5_hex(<NEW>);
close NEW;
my $new_md5 = Digest::MD5::md5_hex(<$new_fh>);

if ( $new_md5 ne $md5 ) {
&notice("MD5 verification failed. expected $md5, got $new_md5");
Expand Down Expand Up @@ -1085,7 +1082,7 @@ sub get_updates {
} else {
print $fh "-- $new_poll";
}
close $fh;

rename $filename, "$filename.done";
exit;
} else {
Expand Down Expand Up @@ -1395,11 +1392,11 @@ sub monitor_child {
# first time we run we don't want to print out *everything*, so we just
# pretend

if ( open FILE, $filename ) {
if ( open my $fh, '<', $filename ) {
binmode FILE, ":" . &get_charset;
my @lines;
my %new_cache;
while (<FILE>) {
while (<$fh>) {
last if /^__friends__/;
unless (/\n$/) { # skip partial lines
# print "Skipping partial line: $_" if &debug;
Expand Down Expand Up @@ -1567,7 +1564,6 @@ sub monitor_child {
}
}

close FILE;
unlink $filename
or warn "Failed to remove $filename: $!"
unless &debug;
Expand All @@ -1594,9 +1590,8 @@ sub monitor_child {
and my $file =
Irssi::settings_get_str("twirssi_replies_store") )
{
if ( open JSON, ">$file" ) {
print JSON JSON::Any->objToJson( \%id_map );
close JSON;
if ( open my $json_fh, '>', "$file" ) {
print $json_fh JSON::Any->objToJson( \%id_map );
} else {
&ccrap("Failed to write replies to $file: $!");
}
Expand All @@ -1607,8 +1602,6 @@ sub monitor_child {
}
}

close FILE;

if ( $attempt < 24 ) {
Irssi::timeout_add_once( 5000, 'monitor_child',
[ $filename, $attempt + 1 ] );
Expand Down Expand Up @@ -2000,9 +1993,8 @@ sub get_text {
print "nicks: ", join ", ", sort keys %nicks;
print "searches: ", Dumper \%{ $id_map{__searches} };
print "last poll: $last_poll";
if ( open DUMP, ">/tmp/twirssi.cache.txt" ) {
print DUMP Dumper \%tweet_cache;
close DUMP;
if ( open my $dump_fh, '>', '/tmp/twirssi.cache.txt' ) {
print $dump_fh Dumper \%tweet_cache;
print "cache written out to /tmp/twirssi.cache.txt";
}
}
Expand Down Expand Up @@ -2098,10 +2090,9 @@ sub get_text {

my $file = Irssi::settings_get_str("twirssi_replies_store");
if ( $file and -r $file ) {
if ( open( JSON, $file ) ) {
local $/;
my $json = <JSON>;
close JSON;
if ( open( my $json_fh, '<', $file ) ) {
my $json;
do { local $/; $json = <$json_fh>; };
eval {
my $ref = JSON::Any->jsonToObj($json);
%id_map = %$ref;
Expand Down