Skip to content

Instantly share code, notes, and snippets.

@rennex
rennex / url_twitter.rb
Created April 14, 2022 00:13
Twitter -> IRC
require "open-uri"
require "json"
require "htmlentities"
module URLTitle
class Twitter
class << self
def fetch_tweet(id, token)
@rennex
rennex / nanobouncer.rb
Last active March 13, 2022 22:26
Line-oriented bouncer between TLS server and Unix socket
require "socket"
require "openssl"
unless ARGV.size == 3
puts "Usage: #$0 <serverhost> <serverport> <listensocketpath>"
exit
end
class NanoBouncer
@rennex
rennex / tls-connect.rb
Created March 10, 2022 20:54
TLS connection in Ruby
require "socket"
require "openssl"
sock = TCPSocket.new(ARGV[0], ARGV[1].to_i)
ctx = OpenSSL::SSL::SSLContext.new
ctx.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER)
io = OpenSSL::SSL::SSLSocket.new(sock, ctx).tap do |socket|
socket.sync_close = true
@rennex
rennex / 40_iso
Created February 20, 2022 01:39
Grub2 entry to boot Ubuntu live ISO from 2nd GPT partition
#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries. Simply type the
# menu entries you want to add after this comment. Be careful not to change
# the 'exec tail' line above.
menuentry "Ubuntu 20.04 ISO" {
set root=(hd0,gpt2)
set isofile="/ubuntu-20.04.3-desktop-amd64.iso"
loopback loop $isofile
linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$isofile persistent persistent-path=/ub2004-1/ fsck.mode=skip quiet splash --
@rennex
rennex / threadweirdness.rb
Created March 15, 2020 10:48
Special variable $1 getting clobbered from another thread
class ProcCaller
def add(&block)
(@procs ||= []) << block
end
def callem!
@procs.map do |p|
Thread.new { p.call }
end.each &:join
@rennex
rennex / warningtest.rb
Created September 29, 2019 02:15
Can't ignore "ambiguous first argument" warning
#!/usr/bin/ruby -w
require "warning"
def foo(regexp)
end
Warning.ignore(:ambiguous_slash)
foo /why you do dis/
# still prints:
@rennex
rennex / luhn.rb
Created January 17, 2019 15:26
Luhn's algorithm (calculates the last digit of a credit card number)
def luhn_calc(num)
sum = 0
num.to_s.gsub(/\s/, "").each_char.to_a.reverse.each_with_index do |d,i|
multiplier = 2 - (i&1)
sum += (multiplier * d.to_i).divmod(10).reduce(:+)
end
sum * 9 % 10
end
@rennex
rennex / mpclog.rbw
Created December 2, 2018 02:05
Media Player Classic activity logger
# encoding: UTF-8
# ----- Settings: -----
# what machine's MPC is monitored, and on which port
MPC_HOST = "localhost:13579"
LOGFILE = "log.txt"
PRINTLOG = false # also print to console?
ENDTOLERANCE = 0.95 # seen 95% = seen all of it
INTERVAL = 5 # seconds to wait after each status update
OUTPUT_CHARSET = "CP850" # this is a fix for windows's cmd.exe
@rennex
rennex / jimms-4k-notifier.rb
Created September 4, 2018 05:00
This script checks for changes in the list of 4k G-sync monitors that Jimm's sells.
#!/usr/bin/env ruby
require "nokogiri"
require "open-uri"
filename = "4k_monitors.txt"
old_names = File.readlines(filename).map(&:strip) rescue []
page = Nokogiri::HTML(open("https://www.jimms.fi/fi/Product/List/000-1HJ/oheislaitteet--naytot--g-sync?ob=6&fq=4k"))
@rennex
rennex / rodaplugin_redirect_to_slash.rb
Created February 1, 2018 00:44
Roda plugin to easily support (and prefer) trailing slashes in certain paths.
# frozen-string-literal: true
class Roda
module RodaPlugins
# Treats any string matchers with a trailing slash ("/") almost like
# the slash wasn't there, but GET requests to that path without the
# slash are redirected to the path with a trailing slash.
#
# Example:
#