Skip to content

Instantly share code, notes, and snippets.

@mkroman
Created December 17, 2015 14:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkroman/a98951c45d05c6a3f559 to your computer and use it in GitHub Desktop.
Save mkroman/a98951c45d05c6a3f559 to your computer and use it in GitHub Desktop.
A bunch of scripts I once made for weechat
# the oh-so-famous fish encryption for weechat.
# Copyright (C) 2010, Mikkel Kroman <mk@maero.dk>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Requires weechat-ruby (http://github.com/dominikh/weechat-ruby).
#
# A special thanks goes to anonymous (whoever you are), the person who
# ported FiSH to ruby, all I did was implement it in weechat.
#
# 2010-07-25, mk <mk@maero.dk>
# version 1.0: initial release
require 'crypt/blowfish'
require 'weechat'
include Weechat
@script = {
name: "fish",
author: "Mikkel Kroman <mk@maero.dk>",
license: "GPL3",
version: "1.0.0",
description: "The oh-so-famous FiSH encryption."
}
@config = Script::Config.new({
"keys" => [Hash, {}]
})
module IRC
class BadInputError < StandardError; end
class MBase64
B64 = './0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
def self.decode(encoded)
if not encoded.length % 12 == 0
raise IRC::BadInputError,
"can only decode strings which are a multiple of 12 characters."
end
decoded = String.new
k = -1
while (k < encoded.length - 1) do
right = 0
left = 0
(0..5).each do |i|
k = k + 1
right |= B64.index(encoded[k]) << (i * 6)
end
(0..5).each do |i|
k = k + 1
left |= B64.index(encoded[k]) << (i * 6)
end
(0..3).each do |i|
decoded += ((left & (0xFF << ((3 - i) * 8))) >> ((3 - i) * 8)).chr
end
(0..3).each do |i|
decoded += ((right & (0xFF << ((3 - i) * 8))) >> ((3 - i) * 8)).chr
end
end
return decoded
end
def self.encode(decoded)
if not decoded.length % 8 == 0
raise IRC::BadInputError,
"can only encode strings which are a multiple of 8 characters."
end
encoded = String.new
k = -1
while (k < decoded.length - 1) do
k = k.next
left = (decoded[k].ord << 24)
k = k.next
left += (decoded[k].ord << 16)
k = k.next
left += (decoded[k].ord << 8)
k = k.next
left += decoded[k].ord
k = k.next
right = (decoded[k].ord << 24)
k = k.next
right += (decoded[k].ord << 16)
k = k.next
right += (decoded[k].ord << 8)
k = k.next
right += decoded[k].ord
(0..5).each do
encoded += B64[right & 0x3F].chr
right = right >> 6
end
(0..5).each do
encoded += B64[left & 0x3F].chr
left = left >> 6
end
end
return encoded
end
end
class FiSH
def initialize(key)
@blowfish = Crypt::Blowfish.new(key)
end
def encrypt(text)
text = pad(text, 8)
result = ""
num_block = text.length / 8
num_block.times do |n|
block = text[n*8..(n+1)*8-1]
enc = @blowfish.encrypt_block(block)
result += MBase64.encode(enc)
end
return result
end
def decrypt(text)
if not text.length % 12 == 0
raise IRC::BadInputError,
"can only decrypt strings which are a multiple of 12 characters."
end
result = ""
num_block = (text.length / 12).to_i
num_block.times do |n|
block = MBase64.decode( text[n*12..(n+1)*12-1] )
result += @blowfish.decrypt_block(block)
end
return result.gsub(/\0*$/, '')
end
private
def pad(text, n=8)
pad_num = n - (text.length % n)
if pad_num > 0 and pad_num != n
pad_num.times { text += 0.chr }
end
return text
end
end
end
def setup
Modifiers::Print.new do |plugin, buffer, tags, line|
if buffer.channel? and tags.include? "irc_privmsg"
if line.message =~ /^\+OK (.*)$/
if @config.keys.key? buffer.name
fish = IRC::FiSH.new @config.keys[buffer.name]
line.message = "[+] #{fish.decrypt($1)}"
else
line.message = "[-] #{line.message}"
end
end
end
line
end
Command.new command: "fish", args: "[enable|disable] [key]", description: "Toggle FiSH" do |buffer, args|
args = args.split
case args.first
when "enable"
@config.keys[buffer.name] = args[1]
when "disable"
@config.keys.delete buffer.name
end
end
Modifier.new "irc_out_PRIVMSG" do |server, line|
if line.message =~ /^PRIVMSG (.*?) :(.*)/
if @config.keys[server.name + ".#{$1}"]
fish = IRC::FiSH.new @config.keys[server.name + ".#{$1}"]
line.message = "PRIVMSG #{$1} :+OK #{fish.encrypt($2)}"
end
end
line
end
end
# mplextended (the equivalent of screen_away.pl for irssi) for weechat.
# Copyright (C) 2010, Mikkel Kroman <mk@maero.dk>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Requires weechat-ruby (http://github.com/dominikh/weechat-ruby).
#
# 2010-09-03, mk <mk@maero.dk>
# version 1.0.3: minor changes, OCD programming is more of a curse than
# a benefit.
# 2010-07-27, mk <mk@maero.dk>
# version 1.0.2: changed global variables to instance variables and added
# buffer clearing as soon as it has been printed
# 2010-07-23, mk <mk@maero.dk>
# version 1.0.1: refactored a tiny bit of the code
# 2010-07-22, mk <mk@maero.dk>
# version 1.0: screen and tmux (untested) support
require 'weechat'
include Weechat
@script = {
name: "mplext",
author: "Mikkel Kroman <mk@maero.dk>",
license: "GPL3",
version: "1.0.3",
description: "Set away status and log messages when detaching or reattaching from a terminal multiplexer."
}
@config = Script::Config.new({
"color" => [Color, "lightgreen"],
"format" => [String, "%H:%M:%S"],
"message" => [String, "not here.."],
"interval" => [Integer, 5]
})
@sock, @away, @buffer = nil, false, []
def setup
if ENV.include? 'STY'
if %x{env LC_ALL=C screen -ls} =~ %r{Sockets? in (/.+)\.}
@socket = File.join $1, ENV['STY']
end
elsif ENV.include? 'TMUX'
@socket = ENV['TMUX'].split(?,).first
end
Weechat::Hooks::Print.new &method(:check_highlight)
Weechat::Timer.new @config.interval.to_i * 1000, &method(:check_presence)
end
def check_presence remaining
attached = File.executable? @socket
if attached and @away
Weechat.puts "Terminal multiplexer attached. Clearing away status."
Weechat.exec "/away -all"
print_buffer
@away = false
elsif not attached and not @away
Weechat.puts "Terminal multiplexer detached. Setting away status."
Weechat.exec "/away -all #{@config.message}"
@away = true
end
end
def check_highlight buffer, date, tags, display, highlighted, prefix, message
if @away and highlighted
time, channel, nick = Time.now.to_i, buffer.short_name, prefix.chop
@buffer << [time, channel, nick, message]
end
end
def print_buffer
@buffer.each do |time, channel, nickname, message|
timestamp = Time.new(time).strftime @config.format
format = [@config.color, timestamp, channel, nick, message]
Weechat.puts "%s %s [%s] %s: %s" % format
end.clear
end
# rmpd for weechat - prints the currently playing song specifications.
# Copyright (C) 2010, Mikkel Kroman <mk@maero.dk>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Requires weechat-ruby (http://github.com/dominikh/weechat-ruby).
#
# 2010-06-23, mk <mk@maero.dk>
# version 1.1: complete rewrite
# 2010-06-20, mk <mk@maero.dk>
# version 1.0: initial release
require 'weechat'
require 'socket'
include Weechat
@script = {
name: "rmpd",
author: "Mikkel Kroman <mk@maero.dk>",
license: "GPL3",
version: "1.1.0",
description: "prints the currently playing mpd song specifiations on the local or a remote machine."
}
@config = Script::Config.new({
"port" => [Integer, 6600],
"command" => [String, "/say np: %title - %artist (%bitratekbps)"],
"hostname" => [String, "localhost"],
"password" => [String, ""]
})
class MPD
attr_reader :version
def initialize hostname, port, password = nil, &block
@port = port
@hostname = hostname
@password = password unless password.empty?
@connected = false
instance_eval &block if block_given?
end
def connect
disconnect if connected?
@socket = TCPSocket.new @hostname.to_s, @port.to_i
receive
if @password
transmit :password, @password
if receive.include? "incorrect password"
raise "incorrect password"
end
end
self
end
def status
transmit :status
read_values
end
def current_song
transmit :currentsong
read_values
end
def disconnect
@socket.close
@socket = nil
@connected = false
end
def connected?
@connected
end
private
def transmit command, *args
@socket.puts "#{command} #{args.join ' '}"
end
def read_values
result = {}
until (data = receive) =~ /^(OK$|ACK)/
key, value = *data.split(': ', 2)
result[key.downcase.to_sym] = value.strip
end
result
end
def receive
@socket.gets
end
end
def setup
Command.new "np", description: @script[:description] do |buffer, args|
host, port, pass = @config.hostname, @config.port, @config.password
mpd = nil
begin
mpd = MPD.new(host, port, pass).connect
song = mpd.current_song
status = mpd.status
mpd.disconnect
format = @config.command.dup
formatting = {
volume: {
value: status[:volume],
default: 0
},
repeat: {
value: status[:repeat] == "1" ? "on" : "off",
default: "off"
},
random: {
value: status[:random],
default: "off"
},
state: {
value: status[:state],
default: "stopped"
},
bitrate: {
value: status[:bitrate],
default: 0
},
time: {
value: status[:time].split(':').first.to_i,
default: 0
},
title: {
value: song[:title],
default: File.basename(song[:file],"mp3")
},
artist: {
value: song[:artist],
default: "unknown"
},
album: {
value: song[:album],
default: "unknown"
},
duration: {
value: song[:duration],
default: 0
}
}
formatting.each do |k,v|
format.gsub! "%#{k}", v[:value].to_s || v[:default].to_s
end
Weechat.exec format
rescue Exception => exception
Weechat::puts "Could not connect to MPD: #{exception.message}"
end
end
end
# sysinfo for weechat (a port of sysinfo.pl for irssi).
# Copyright (C) 2010, Mikkel Kroman <mk@maero.dk>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Requires weechat-ruby (http://github.com/dominikh/weechat-ruby).
#
# 2010-06-20, mk <mk@maero.dk>
# version 1.0: initial release
require 'rbconfig'
require 'weechat'
include Weechat
@script = {
name: "sysinfo",
author: "Mikkel Kroman <mk@maero.dk>",
license: "GPL3",
version: "1.0.0",
description: "Linux system information script."
}
@os = {
arch: %x{uname -m}.chop,
name: %x{uname -s}.chop,
version: %x{uname -r}.chop,
hostname: %x{uname -n}.chop
}
def setup
Command.new "sysinfo", description: "Print system information" do |buffer, args|
buffer.say "Hostname: #{@os[:hostname]} OS: #{@os[:name]} #{@os[:version]}/#{@os[:arch]} CPU: #{cpu} Uptime: #{uptime} "
end
end
def cpu
cpuinfo = File.read "/proc/cpuinfo"
cpulist = cpuinfo.scan(/model name\s+: (.*)/).flatten
"#{cpulist.first.split.join ' '} x #{cpulist.length}"
end
def uptime
uptime = File.read "/proc/uptime"
uptime = uptime.split.first.to_i
seconds_to_s uptime
end
def seconds_to_s seconds
buffer = ""
weeks = seconds / 604800
seconds %= 86400
days = seconds / 86400
seconds %= 86400
hours = seconds / 3600
seconds %= 3600
mins = seconds / 60
seconds %= 60
buffer << "#{weeks}w " if weeks >= 1
buffer << "#{days}d " if days >= 1
buffer << "#{hours}h " if hours >= 1
buffer << "#{mins}m " if mins >= 1
buffer << "#{seconds}s"
buffer
end
# updike (the equivalent of updike.pl for irssi) for weechat.
# Copyright (C) 2010, Mikkel Kroman <mk@maero.dk>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Requires weechat-ruby (http://github.com/dominikh/weechat-ruby).
#
# 2010-07-23, mk <mk@maero.dk>
# version 1.0: initial release
require 'weechat'
include Weechat
@script = {
name: "updike",
author: "Mikkel Kroman <mk@maero.dk>",
license: "GPL3",
version: "1.0.0",
gem_version: "0.0.6",
description: "Export uptime to IRC using common uptime interchange format"
}
def setup
IRC::CTCP.new "UPDIKE" do |server, ctcp|
uptime = File.read "/proc/uptime"
days = uptime.split.first.to_i / 86400
server.command "QUOTE NOTICE %s :\001%s\001" % [ctcp.prefix.nick, "UPDIKE 8#{"=" * days}D"]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment