Created
November 17, 2022 18:59
-
-
Save Dan-Q/bfdd8e2b4cf6b6ff8dbe7233fc9e0ab4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# This program gets executed (by /etc/efingerd/luser) when somebody runs finger something@danq.me | |
# The output is returned directly to them | |
# For more information, see https://danq.me/wp-finger | |
require 'mysql2' | |
require 'word_wrap' | |
require 'word_wrap/core_ext' | |
db = Mysql2::Client.new(username: 'WORDPRESS_DB_USERNAME', password: 'WORDPRESS_DB_PASSWORD', database: 'WORDPRESS_DB_NAME') | |
req = ARGV[0].strip.gsub(/[^a-z0-9\-_]/i, '') # remove unsafe chars | |
# banner | |
puts '=' * 79 | |
puts " __ __ ___ \n | \\ /\\ |\\ | / \\ |\\/| |__ \n |__/ /~~\\ | \\| \\__X . | | |___ " | |
puts '=' * 79 | |
puts '' | |
# Handle requests for dan/blog@danq.me | |
if(req == 'dan' || req == 'blog') | |
# intro | |
puts "A selection of recent blog posts from https://danq.me/\nUse your finger command to read them:" | |
posts = db.query <<-END_OF_SQL | |
SELECT | |
ID i, | |
DATE(post_date_gmt) d, | |
post_name s, | |
post_title t, | |
post_excerpt e | |
FROM wp_posts | |
WHERE post_type = 'post' AND post_status = 'publish' | |
AND post_title IS NOT NULL AND post_title <> '' | |
AND post_excerpt IS NOT NULL AND post_excerpt <> '' | |
ORDER BY post_date_gmt DESC | |
LIMIT 4 | |
END_OF_SQL | |
posts.each do |post| | |
identifier = post['s'].length > 53 ? post['i'] : post['s'] # use ID, not slug, for long slugs | |
puts "\n#{'-'*79}\n\n" | |
puts "#{post['d']} #{post['t']}\n\n" | |
puts post['e'].wrap 79 | |
puts "\nRead with: finger #{identifier}@danq.me" | |
end | |
exit | |
end | |
# Handle requests for a specific post | |
posts = db.query <<-END_OF_SQL | |
SELECT | |
ID i, | |
DATE(post_date_gmt) d, | |
post_name s, | |
post_title t, | |
post_content c | |
FROM wp_posts | |
WHERE post_type = 'post' AND post_status = 'publish' | |
AND post_name = '#{req}' OR ID = '#{req}' | |
LIMIT 1 | |
END_OF_SQL | |
post = posts.first | |
if post | |
puts "#{post['d']} #{post['t']}\n" | |
puts '' | |
puts post['c'].gsub(/<\/?[^>]*>/, '').gsub(/[\r\n]{3,}/, "\n\n").wrap 79 | |
exit | |
end | |
puts "\nNothing found.\nTry: finger blog@danq.me" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment