Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # encoding=utf-8
- require 'yaml'
- # These are all very annoying.
- # rubocop:disable Metrics/LineLength, Metrics/ClassLength, Metrics/CyclomaticComplexity
- # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize
- module Cinch
- module Plugins
- # Autovoice/halfop/op people
- # .automode on to turn on, .automode off to turn off
- # .add {op,voice,halfop} nick!user@hostmask to add
- # someone to the autowhatever list
- # .del {op,voice,halfop} nick!user@hostmask to delete
- # someone from the autowhatever list
- # This code probably sucks but whatever
- class Automode
- include Cinch::Plugin
- set :prefix, /^\./
- listen_to :join
- private
- def read_users(type, channel, server)
- filename = "#{server}#{channel}#{type}.yml"
- if File.file?(filename)
- f = File.open(filename, 'r')
- users = YAML.load(f)
- else
- users = []
- end
- users
- end
- def write_users(type, hostmask, channel, server, aord)
- filename = "#{server}#{channel}#{type}.yml"
- if File.file?(filename)
- f = File.open(filename, 'r')
- users = YAML.load(f)
- # catch empty or improperly formatted file
- users = [] unless users
- f.close
- else
- users = []
- end
- return "User #{hostmask} already in #{type} file!" if users.include?(hostmask) && aord == 'a'
- return "User #{hostmask} is not in #{type} file!" if !users.include?(hostmask) && aord == 'd'
- unless hostmask == 'all' || hostmask == 'none'
- if aord == 'a'
- users.delete('all')
- users.delete('none')
- end
- end
- f = File.open(filename, 'w+')
- users << hostmask if aord == 'a'
- users.delete(hostmask) if aord == 'd'
- users.delete('all') if hostmask == 'none'
- print "USERS\n#{users}"
- f.write(users.to_yaml)
- f.close
- meth = aord == 'a' ? 'Now' : 'No longer'
- if hostmask == 'all'
- return "#{meth} auto-#{type}'ing everyone!"
- elsif hostmask == 'none'
- return "#{meth} auto-#{type}'ing no one!"
- end
- meth = aord == 'a' ? 'now stored in' : 'deleted from'
- "#{hostmask} #{meth} #{channel} #{type} file."
- end
- def check_user(raw, type, channel, server)
- hostmask = raw.split(' ')[0].delete(':')
- users = read_users(type, channel, server)
- puts "USERS:#{type}\n#{users}\nENDUSERS"
- return true if users.include?('all')
- return false if users.include?('none')
- users.include?(hostmask)
- end
- public
- def listen(m)
- # making shorter lines
- c = m.channel
- srv = @bot.config[:server]
- hostmask = m.raw.split(' ')[0].delete(':')
- return unless @automode
- return if m.user.nick == bot.nick
- puts "------- #{hostmask} Trying op?"
- if check_user(m.raw, 'op', c, srv)
- c.op(m.user)
- return
- end
- puts "------- #{hostmask} Trying halfop?"
- if check_user(m.raw, 'halfop', c, srv)
- c.mode("+h #{m.user}")
- return
- end
- puts "------- #{hostmask} Trying voice?"
- if check_user(m.raw, 'voice', c, srv)
- c.voice(m.user)
- return
- end
- puts '------- Nothing!'
- end
- match(/automode (on|off)$/, method: :endisable)
- def endisable(m, option)
- return unless check_user(m.raw, 'admins', '', '')
- @automode = option == 'on'
- m.reply "Automode is now #{@automode ? 'enabled' : 'disabled'}"
- end
- match(/add(?: (.+))?/, method: :add_user)
- def add_user(m, query)
- manipulate_user(m, query, 'a')
- end
- match(/del(?: (.+))?/, method: :del_user)
- def del_user(m, query)
- manipulate_user(m, query, 'd')
- end
- def manipulate_user(m, query, aord)
- return unless check_user(m.raw, 'admins', '', '')
- type = query.split(' ')[0]
- goodtypes = %w(op halfop voice)
- hostmask = query.split(' ')[1]
- goodmask = /(.*)\!(.*)@(.*)/ =~ hostmask
- goodmask = true if hostmask == 'all' || hostmask == 'none'
- m.reply('Bad hostmask!') && return unless goodmask
- m.reply('Bad mode type!') && return unless goodtypes.include?(type)
- m.reply write_users(type, hostmask, m.channel, @bot.config[:server], aord)
- end
- end
- end
- end
- # vim:tabstop=2 softtabstop=2 expandtab shiftwidth=2 smarttab foldmethod=syntax:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement