Advertisement
Azure

timeban.rb - 2011-07-08 00:08:30

Jul 7th, 2011
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.43 KB | None | 0 0
  1. require 'cinch'
  2. require 'time'
  3. require 'active_support/core_ext'
  4. require_relative '../../modules/stringhelpers'
  5. require_relative '../../modules/timeban'
  6.  
  7. class Timeban
  8.     include Cinch::Plugin
  9.     include StringHelpers
  10.     include Timebans
  11.    
  12.     BANMASK = "*!*@%h"
  13.    
  14.     react_on :channel
  15.    
  16.   def check_user(users, user)
  17.     user.refresh # be sure to refresh the data, or someone could steal the nick
  18.     ["h", "o", "a", "q"].any? {|mode| users[user].include?(mode) } ## ←
  19.   end
  20.    
  21.   timer 5, method: :timed
  22.   def timed
  23.         hostnames = Timebans::unbanned?
  24.         hostnames.each_pair {|c, m|
  25.             Channel(c).unban(m)
  26.             Timebans::remove_ban! c, m
  27.         }
  28.   end
  29.    
  30.     match /timeban ((\d+)(s|m|h|d))?\s?([\w\/\[\]\|]+)\s?(.+)?/, method: :set_timeban
  31.     def set_timeban(m, full_length, length, length_unit, nick, reason)
  32.         return unless check_user(m.channel.users, m.user)
  33.         return unless check_user(m.channel.users, User(@bot.nick))
  34.         outlaw = User(nick)
  35.         return m.reply("That's not a user...") if outlaw.unknown?
  36.         hostname = outlaw.mask BANMASK
  37.         date_banned = Time.now
  38.         date_unbanned = case length_unit
  39.             when "s"
  40.                 Time.now + length.to_i.second
  41.             when "m"
  42.                 Time.now + length.to_i.minute
  43.             when "h"
  44.                 Time.now + length.to_i.hour
  45.             when "d"
  46.                 Time.now + length.to_i.day
  47.             else
  48.                 nil
  49.         end
  50.        
  51.         kickreason = []
  52.         kickreason << "banned"
  53.         kickreason << (!full_length.blank? ? "for #{time_diff_in_natural_language(date_banned, date_unbanned)}" : "indefinitely") << "by #{m.user.nick}"
  54.         kickreason << (!reason.blank? ? "- #{reason}" : "")
  55.         kickreason = kickreason.reject(&:blank?).join(" ")
  56.        
  57.         m.channel.ban(hostname)
  58.         m.channel.kick(outlaw, kickreason)
  59.        
  60.         a_ban = Ban.new(
  61.             :channel => m.channel.name,
  62.             :hostname => hostname,
  63.             :datebanned => date_banned,
  64.             :dateunbanned => date_unbanned,
  65.             :reason => reason,
  66.             :bannedby => m.user.mask
  67.         )
  68.         a_ban.save
  69.     end
  70.  
  71.     match /unban (.+)/, method: :unban
  72.     def unban m, h
  73.         m.channel.unban h
  74.         Timebans::remove_ban m.channel.name, h
  75.         m.reply "*pouts*"
  76.     end
  77.    
  78.     match /banlist/, method: :listbans
  79.     def listbans m
  80.         banlist = Ban.all(:channel => m.channel.name)
  81.         if !banlist.blank?
  82.             m.reply "Banlist: hostname, time banned, time unbanned, reason, banned by"
  83.             banlist.each {|e| m.reply "#{e.hostname}, #{e.datebanned.to_s}, #{e.dateunbanned}, #{e.reason}, #{e.bannedby}" }
  84.         else
  85.             m.reply "There are no timed bans for #{m.channel.name}."
  86.         end
  87.     end
  88.  
  89. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement