From 93aebe7498b3f3bf5079e4f218072b0ec5ad9b6c Mon Sep 17 00:00:00 2001 From: Joe Rayhawk Date: Sun, 27 Oct 2019 23:31:51 -0700 Subject: Minor and incomplete changes to cope with autolog path changes and kraken v3 shutdown --- bash/logtail | 10 ++++++++++ crystal/name-command.cr | 5 +++-- ircobsbridge.rb | 28 +++++++++++++++++++++++++--- twitchpubsub.rb | 12 +++++++----- 4 files changed, 45 insertions(+), 10 deletions(-) create mode 100755 bash/logtail diff --git a/bash/logtail b/bash/logtail new file mode 100755 index 0000000..361d121 --- /dev/null +++ b/bash/logtail @@ -0,0 +1,10 @@ +#!/bin/bash +( + echo -n "$1"/ + ls -tc "$1" | head -n 1 + inotifywait -q -m --exclude=xz -e CREATE --format %f "$1" +) | while read line; do + trap "jobs -rp | xargs --no-run-if-empty kill" EXIT; + jobs -rp | xargs --no-run-if-empty kill; + tail -F "$line" & +done diff --git a/crystal/name-command.cr b/crystal/name-command.cr index 3d0878f..fa1a0f9 100644 --- a/crystal/name-command.cr +++ b/crystal/name-command.cr @@ -3,6 +3,7 @@ require "json" require "pretty_print" require "xml" require "inotify" +require "time" home = Path["~/"].expand.to_s @@ -71,9 +72,9 @@ def commanderroot_changelog( uid ) end -log = File.open(home + "irclogs/twitch/#" + channel + ".log".to_s) +log = File.open(home + "irclogs/twitch/#" + channel + "/" + Time.local.to_s("%Y-%m-%d") + ".log".to_s) log.seek(log.size) -watcher = Inotify.watch(home + "irclogs/twitch/#" + channel + ".log".to_s) do | event | +watcher = Inotify.watch(home + "irclogs/twitch/#" + channel + "/" + Time.local.to_s("%Y-%m-%d") + ".log".to_s) do | event | if event.type.to_s == "MODIFY" log.read_string( log.size - log.pos ).each_line do | string | # FIXME: support old names? might get confusing with e.g. reverse5612 diff --git a/ircobsbridge.rb b/ircobsbridge.rb index 326b8d4..3b7bf74 100644 --- a/ircobsbridge.rb +++ b/ircobsbridge.rb @@ -216,6 +216,11 @@ $globallastused = 0 def command_dispatch( mode, user, command, arg1 ) # arg1 may be empty String safe = /^[-0-9a-zA-Z_]+$/ + # '@'-style user mode reporting is super flaky on the Twitch IRC gateway. + # FIXME: Should probably dynamically poll and cache .mods/.vips output. + if user == $CHANNEL + mode = '@' + end if mode == '@' && command == 'scene' if arg1 =~ safe SetCurrentScene( arg1 ) @@ -257,6 +262,9 @@ def command_dispatch( mode, user, command, arg1 ) elsif mode == '@' && ( command == 'nextsong' || command == 'next' ) ircsocketwrite( "| " + $m.next["file"] + "\n" ) return + elsif ( command == 'uptime' ) + ircsocketwrite( "| " + $status["stream-timecode"] + "\n" ) + return elsif ( command == 'currentsong' ) || ( command == 'current' ) ircsocketwrite( "| " + $m.currentsong["file"] + "\n" ) return @@ -282,7 +290,7 @@ def command_dispatch( mode, user, command, arg1 ) end # popen to get output someday? elsif mode == '@' && command == 'commands' - ircsocketwrite( "| !songrequest !currentsong !next !volume !delsong !source !scene !pubg !metaminute !nopgl1 !nopgl2\n" ) + ircsocketwrite( "| !uptime !songrequest !currentsong !next !volume !delsong !source !scene !pubg !metaminute !nopgl1 !nopgl2 !design\n" ) return end # ratelimited commands after +o commands @@ -292,6 +300,20 @@ def command_dispatch( mode, user, command, arg1 ) ircsocketwrite( "| You're doing great work, Dexem!\n" ) return end + end + if command =~ /^(design)$/ + if ( ! $lastused[ command ] || $lastused[ command ] <= ( Time.now.to_i - 5 ) ) # || mode == '@' + $lastused[ command ] = Time.now.to_i + ircsocketwrite( "| http://i.imgur.com/s29rvaw.jpg http://i.imgur.com/OjMkp6I.jpg\n" ) + return + end + end + if command =~ /^(mattscott)$/ + if ( ! $lastused[ command ] || $lastused[ command ] <= ( Time.now.to_i - 5 ) ) # || mode == '@' + $lastused[ command ] = Time.now.to_i + ircsocketwrite( "\"We're not looking to [...] change the monetization.\" https://forums-old.gamersfirst.com/topic/408317-/page-5#entry4522545\n" ) + return + end end if command =~ /^(pubg)$/ if ( ! $lastused[ command ] || $lastused[ command ] <= ( Time.now.to_i - 5 ) ) # || mode == '@' @@ -307,7 +329,7 @@ def command_dispatch( mode, user, command, arg1 ) transientsource( command ) return elsif command == 'commands' - ircsocketwrite( "| !songrequest !currentsong !pubg !metaminute !nopgl1 !nopgl2\n" ) + ircsocketwrite( "| !design !uptime !songrequest !currentsong !pubg !metaminute !nopgl1 !nopgl2\n" ) return end end @@ -353,7 +375,7 @@ end # irclog read loop event thread ircreader = Thread.new { begin - irclog = IO.popen('/usr/bin/tail -n0 -F ' + ENV['HOME'] + '/irclogs/twitch/#' + $CHANNEL + '.log' ) + irclog = IO.popen('logtail ' + ENV['HOME'] + '/irclogs/twitch/#' + $CHANNEL + '/' ) irclog.each_line do |line| if ( match = line.match(/^\d\d:\d\d <([@ +])(.+)?> *!([-0-9a-zA-Z]+) *(.*)/) ) command_dispatch( *match.to_a[1..4] ) diff --git a/twitchpubsub.rb b/twitchpubsub.rb index c3f3994..cd02c14 100755 --- a/twitchpubsub.rb +++ b/twitchpubsub.rb @@ -15,6 +15,7 @@ ENV['XDG_CONFIG_HOME'] && confdir = ENV['XDG_CONFIG_HOME'] + '/twitch/' access_token = IO.read( confdir + 'access_token' ).chomp || true channel = IO.read( confdir + 'channel' ).chomp +channel_id = IO.read( confdir + 'channel_id' ).chomp client_id = IO.read( confdir + 'client_id' ).chomp unix_socket_path = confdir + 'chat_socket' @@ -22,7 +23,8 @@ http = Net::HTTP.new('api.twitch.tv', 443) http.use_ssl = true http.read_timeout = 10 -channel_id = JSON.parse(http.request(Net::HTTP::Get.new("/kraken/channels/#{channel}?client_id=#{client_id}")).body)['_id'].to_s +# kraken broke +#channel_id = JSON.parse(http.request(Net::HTTP::Get.new("/kraken/channels/#{channel}?client_id=#{client_id}")).body)['_id'].to_s def normalize_twitch_pubsub_bullshit_data_because_seriously_fuck_twitch( parsed ) # Twitch may or may not have JSON-encoded this JSON because Twitch. @@ -46,9 +48,9 @@ end def dispatch_message( message, unix_socket_path ) unix_socket = UNIXSocket.new( unix_socket_path ) if message['context'] == 'subgift' then # Gifted subscription - unix_socket.print( message['recipient_user_name'] + ' has subscribed for ' + (message['months'].to_i+1).to_s + ' months at sub level #' + message['sub_plan'] + 'thanks to a gift from ' + message['user_name'] + ': "' + message['sub_message']['message'] + "\"\n" ) + unix_socket.print( message['recipient_user_name'] + ' has subscribed for ' + (message['cumulative_months'].to_i+1).to_s + ' months at sub level #' + message['sub_plan'] + 'thanks to a gift from ' + message['user_name'] + ': "' + message['sub_message']['message'] + "\"\n" ) elsif message['sub_plan'] then # Subscription - unix_socket.print( message['user_name'] + ' has ' + message['context'] + 'scribed for ' + (message['months'].to_i+1).to_s + ' months at sub level #' + message['sub_plan'] + ': "' + message['sub_message']['message'] + "\"\n" ) + unix_socket.print( message['user_name'] + ' has ' + message['context'] + 'scribed for ' + (message['cumulative_months'].to_i+1).to_s + ' months at sub level #' + message['sub_plan'] + ': "' + message['sub_message']['message'] + "\"\n" ) elsif message['item_description'] then # commerce unix_socket.print( message['user_name'] + ' has purchased ' + message['item_description'] + ': "' + message['purchase_message']['message'] + "\"\n" ) elsif message['bits_used'] then # bits @@ -102,14 +104,14 @@ t3 = Thread.new { dispatch_message( message, unix_socket_path ) end end - rescue IO::WaitReadable, OpenSSL::SSL::SSLErrorWaitReadable, JSON::ParserError + rescue IO::WaitReadable, OpenSSL::SSL::SSLErrorWaitReadable, JSON::ParserError, EOFError, OpenSSL::SSL::SSLError IO.select([tcp_socket]) retry end } # sub_frame = <<-'EOF' -# {"type":"MESSAGE","data":{"topic":"channel-subscribe-events-v1.59895482","message":"{\"user_name\":\"tobiidk\",\"display_name\":\"TobiiDK\",\"channel_name\":\"bungmonkey\",\"user_id\":\"22413536\",\"channel_id\":\"59895482\",\"time\":\"2017-07-12T11:21:07.118989503Z\",\"sub_message\":{\"message\":\"\",\"emotes\":null},\"sub_plan\":\"1000\",\"sub_plan_name\":\"Throw money at Bung\",\"months\":0,\"context\":\"sub\"}"}} +#{"type"=>"MESSAGE","data"=>{"topic"=>"channel-subscribe-events-v1.59895482","message"=>"{\"user_name\":\"totalsunny\",\"display_name\":\"TotalSunny\",\"channel_name\":\"bungmonkey\",\"user_id\":\"47871268\",\"channel_id\":\"59895482\",\"time\":\"2019-04-27T12:47:26.023643965Z\",\"sub_message\":{\"message\":\"Greetings\",\"emotes\":null},\"sub_plan\":\"1000\",\"sub_plan_name\":\"5\",\"months\":0,\"cumulative_months\":7,\"context\":\"resub\"}"}} # EOF # # sub_frame = <<-'EOF' -- cgit v1.2.3