Skip to content

Instantly share code, notes, and snippets.

@petertseng
Created May 30, 2015 05:27
Show Gist options
  • Save petertseng/8c859323d60b747cc8eb to your computer and use it in GitHub Desktop.
Save petertseng/8c859323d60b747cc8eb to your computer and use it in GitHub Desktop.
Crappy cinch auth
require 'cinch'
require 'set'
require 'yaml'
# I'm not using this, but we can think about it.
module Cinch; class Authorization
class Group
attr_reader :authnames
def initialize
# authname => User
@autnames = {}
end
end
# More fields for user?
User = Struct.new(:level)
def self.load_file(filename)
return nil unless File.exist?(filename)
YAML.load_file(filename)
end
def self.save_file(hash, filename)
File.open(filename, 'w') { |f| f.puts(YAML.dump(hash)) }
end
def initialize(
channels_groups:,
filename:,
master_nicks:,
master_authnames:,
default_levels: nil
)
@channels_groups = channels_groups
@filename = filename
# group_id => Group
@groups = self.class.load_file(@filename) || {}
@groups.default_proc = proc { |h, k| h[k] = Group.new }
@master_nicks = master_nicks
@master_authnames = master_authnames
@pending_registrations = {}
@default_levels = default_levels
end
def has_permission?(m, permission)
group_id = @channels_groups[m.channel.name.downcase]
return false unless group_id && @groups.has_key?(group_id)
unless m.user.authed?
# if they aren't authed, refresh them to see if they've recently authed.
m.user.refresh
# still not authed
return false unless m.user.authed?
end
group = @groups[group_id]
authname = m.user.authname.downcase
if group.authnames.has_key?(authname)
level = group.authnames[authname].level
elsif (default_level = @default_levels[m.channel.name_downcase])
level = default_level
else
return false
end
return level >= permission
end
def master?(m)
return false unless @master_nicks.include?(m.user.nick)
m.user.refresh
return @master_authnames.include?(m.user.authname)
end
def ask_to_register(m)
# Different implementations might want different things.
# Some out of band verification.
end
def confirm_register(_)
# Make sure the confirmation is legitimate
# Call add_user
end
def add_user(_)
# TODO
authname = nil
level = nil
group_id = nil
@pending_registrations.delete(authname)
@groups[group_id].authnames[authname] = User.new(level)
save
end
def reload
@groups.replace(self.class.load_file(@filename))
end
def save
self.class.save_file(@groups, @filename)
end
end; end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment