Skip to content
This repository
tree: 8e747b8c5c
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 383 lines (310 sloc) 10.317 kb
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
#!/usr/bin/perl
########################################################################
# Copyright (c) 2012 Andrew Fresh <andrew@afresh1.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
########################################################################
use strict;
use warnings;
use 5.010;

use DB_File;
use File::Basename;
use File::ChangeNotify;
use File::Find;
use Net::Twitter;

my $seen_file = $ENV{HOME} . '/.tweeted_changes';
my $auth_file = $ENV{HOME} . '/.auth_tokens';

my %accounts = (
    cvs => 'openbsd_cvs',
    src => 'openbsd_src',
    ports => 'openbsd_ports',
    xenocara => 'openbsd_xenocar',
    www => 'openbsd_www',
);

# Login to twitter
foreach my $key ( sort keys %accounts ) {
    my $account = $accounts{$key};
    get_twitter_account($account);
}

my @dirs = (
    'Maildir/.lists.openbsd.source-changes/',
    'Maildir/.lists.openbsd.ports-changes/',
);

find( sub { check_message($_) }, @dirs );

my $watcher
    = File::ChangeNotify->instantiate_watcher( directories => \@dirs, );
while ( my @events = $watcher->wait_for_events() ) {
    foreach my $event (@events) {
        next unless $event->type eq 'create';
        check_message( $event->path );
    }
}

sub check_message {
    my ($file) = @_;
    state $seen = load_seen();

    my $commit = parse_commit($file);
    return unless $commit;
    return unless $commit->{id};

    return if $seen->{ $commit->{id} };

    my ( $message, $params ) = make_tweet($commit);
    tweet( $message, $params );

    if ( $params->{who} ne 'openbsd_cvs' ) {
        tweet( shorten( $commit->{'Module name'} . ': ' . $message ),
            { %{$params}, who => 'openbsd_cvs' } );
    }
    $seen->{ $commit->{id} } = time;
    sync_seen();
}

sub account_for {
    my ($module) = @_;
    return $accounts{$module} || 'openbsd_cvs';
}

sub change_for {
    my ($commit) = @_;
    my %changes;
    my @dirs;

    my $has_regress = 0;
    my $has_non_regress = 0;
    foreach my $key ( keys %{$commit} ) {
        if ( $key =~ /^(\w+)\s+files$/ ) {
            $changes{ lc $1 }++;
            foreach ( keys %{ $commit->{$key} } ) {
                my $dir = $_;
                my @files = @{ $commit->{$key}->{$dir} || [] };
                @files = '' unless @files;

                if ( $dir =~ s{^regress/}{} ) { $has_regress++ }
                else { $has_non_regress++ }

                push @dirs, map {"$dir/$_"} @files;
            }
        }
    }

    my @changes = keys %changes;
    my $changed = @changes == 1 ? $changes[0] : 'changed';

    unless (@dirs) {
        if (@changes) {
            return "$changed something";
        }
        return "did something the parser didn't understand";
    }

    # Put them shortest first
    @dirs = sort { length $a <=> length $b } @dirs;
    my $num_changed = @dirs;

    my $match = shift @dirs;
    $match //= '';

    my $last = '/';
    foreach my $dir (@dirs) {
        $last = chop $match while $dir !~ /^\Q$match/;
    }
    $match .= '*' unless not $match or $last eq '/' or $match =~ s{/$}{};

    $match =~ s{^[\.\/]+}{}; # No need for leading ./
    $match =~ s{/+$}{}; # one less char most likely

    my $message = $changed;
    if ( !$match ) {
        if ($has_non_regress) {
            if ( $num_changed > 5 ) { $message .= ' many things' }
            elsif ( $num_changed > 2 ) { $message .= ' a few things' }
            elsif ( $num_changed > 1 ) { $message .= ' a couple things' }
            else { $message .= ' something' }
        }
        $message .= ' including' if $has_regress and $has_non_regress;
        $message .= ' regression tests' if $has_regress;
    }
    elsif ($has_regress) {
        if ($has_non_regress) {
            $message .= " $match and regression tests";
        }
        else {
            $message .= " regress/$match";
        }
    }
    else {
        $message .= " $match";
    }

    return $message;
}

sub make_tweet {
    my ($commit) = @_;
    my %params = ( who => account_for( $commit->{'Module name'} ), );

    my $by = $commit->{'Changes by'};
    $by =~ s/\@.*$/\@/;

    my $change = change_for($commit);

    my $message = "$by $change: " . $commit->{'Log message'};
    $message =~ s/\s+/ /gms;

    return shorten($message), \%params;
}

sub shorten {
    my ($message) = @_;
    if ( length $message > 140 ) {
        $message =~ s/^(.{137}).*/$1/ms;
        $message =~ s/\s+$//ms;
        $message .= '...';
    }
    return $message;
}

sub tweet {
    my ( $message, $params ) = @_;

    say "Tweeting $message";
    eval { get_twitter_account( $params->{who} )->update($message) };
    if ($@) {
        warn $@;
        return 0;
    }
    return 1;
}

sub parse_commit {
    my ($file) = @_;
    return {} unless -f $file;

    my %commit;

    my $in = 'HEADER';
    open my $fh, '<', $file or die $!;
    my $key = '';
    my $dir = '';
    while (<$fh>) {
        chomp;

        if ( $in eq 'HEADER' ) {
            if (/^Message-ID:\s+(.+?)\s*$/i) { $commit{id} = $1 }
            unless ($_) { $in = 'BODY' }
            next;
        }

        if (/^\s*(CVSROOT|Module name|Changes by|Release Tags):\s+(.*)$/) {
            $commit{$1} = $2;
            next;
        }
        return unless $commit{CVSROOT}; # first thing should be CVSROOT

        if (/^\s*N\s+(.*)\/([^\/]+)/) {
            push @{ $commit{'Imported files'}{$1} }, $2;
        }

        if (/^(Update of)\s+(.*)\/([^\/]+)$/) {
            $commit{'Updated files'}{$2} = [$3];
            next;
        }

        if (/^(\w+ files):/) {
            $key = $1;
            next;
        }

        if ($key) {
            chomp;
            s/^\s+//;
            unless ($_) { $key = ''; next; }

            my (@files) = split /\s*:\s+/;
            $dir = shift @files if @files > 1;
            @files = map {split} @files;
            next unless $dir;

            push @{ $commit{$key}{$dir} }, @files;
            next;
        }

        if (/^Log [Mm]essage:/) {
            while (<$fh>) {
                if (/^\s*Vendor Tag:\s+(.*)$/) {
                    $commit{'Vendor Tag'} = $1;
                    $commit{'Log message'} =~ s/\s*Status:\s*$//ms;
                    last;
                }
                $commit{'Log message'} .= $_;
            }
            next;
        }
    }
    close $fh;

    if ( my $changes = $commit{'Changes by'} ) {
        my ( $who, $when ) = split /\s+/, $changes, 2;
        $commit{'Changes by'} = $who;
        $commit{'Changes on'} = $when;
    }

    $commit{'Log message'} =~ s/\s+$//ms;

    return \%commit;
}

{
    my $X;

    sub load_seen {
        $X = tie my %seen, 'DB_File', $seen_file or die;
        return \%seen;
    }

    sub sync_seen {
        $X->sync;
    }

}

{
    my %tokens;

    sub get_access_tokens {
        my ( $account, $nt ) = @_;

        return $tokens{$account} if exists $tokens{$account};

        open my $fh, '<', $auth_file or die $!;
        while (<$fh>) {
            chomp;
            my ($account_from_file, $access_token, $access_token_secret,
                $user_id, $screen_name
            ) = split /\s+/;

            if ( $account_from_file eq 'consumer' ) {
                $tokens{$account_from_file} = {
                    consumer_key => $access_token,
                    consumer_secret => $access_token_secret,
                };
            }
            else {
                $tokens{$account_from_file} = {
                    access_token => $access_token,
                    access_token_secret => $access_token_secret,
                    user_id => $user_id,
                    screen_name => $screen_name,
                };
            }
        }
        close $fh;
        return $tokens{$account} if exists $tokens{$account};

        return unless $nt;

        my $auth_url = $nt->get_authorization_url;
        print
            " Authorize $account for this application at:\n $auth_url\nThen, enter the PIN# provided to continue ";

        my $pin = <STDIN>; # wait for input
        chomp $pin;

        # request_access_token stores the tokens in $nt AND returns them
        my ( $access_token, $access_token_secret, $user_id, $screen_name )
            = $nt->request_access_token( verifier => $pin );

        # save the access tokens
        $tokens{$account} = {
            access_token => $access_token,
            access_token_secret => $access_token_secret,
            user_id => $user_id,
            screen_name => $screen_name,
        };

        save_access_tokens();

        return $tokens{$account};
    }

    sub save_access_tokens {
        open my $fh;
        foreach my $key ( sort keys %tokens ) {
            my @keys
                = $key eq 'consumer'
                ? qw( consumer_key consumer_secret )
                : qw( access_token access_token_secret user_id screen_name );
            say join "\t", $key, @{ $tokens{$key} }{@keys};
        }
        close $fh;
    }
}

sub get_twitter_account {
    my ($account) = @_;

    my $consumer_tokens = get_access_tokens('consumer');

    my $nt = Net::Twitter->new(
        traits => [qw/API::REST OAuth/],
        %{$consumer_tokens}
    );

    my $tokens = get_access_tokens( $account, $nt );

    $nt->access_token( $tokens->{access_token} );
    $nt->access_token_secret( $tokens->{access_token_secret} );

    #my $status = $nt->user_timeline( { count => 1 } );
    #print Dumper $status;
    #print Dumper $nt;

    return $nt;
}
Something went wrong with that request. Please try again.