Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # encoding=utf-8
  3.  
  4. require 'yaml'
  5.  
  6. # These are all very annoying.
  7. # rubocop:disable Metrics/LineLength, Metrics/ClassLength, Metrics/CyclomaticComplexity
  8. # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize
  9.  
  10. module Cinch
  11. module Plugins
  12. # Autovoice/halfop/op people
  13. # .automode on to turn on, .automode off to turn off
  14. # .add {op,voice,halfop} nick!user@hostmask to add
  15. # someone to the autowhatever list
  16. # .del {op,voice,halfop} nick!user@hostmask to delete
  17. # someone from the autowhatever list
  18. # This code probably sucks but whatever
  19. class Automode
  20. include Cinch::Plugin
  21. set :prefix, /^\./
  22. listen_to :join
  23.  
  24. private
  25.  
  26. def read_users(type, channel, server)
  27. filename = "#{server}#{channel}#{type}.yml"
  28. if File.file?(filename)
  29. f = File.open(filename, 'r')
  30. users = YAML.load(f)
  31. else
  32. users = []
  33. end
  34. users
  35. end
  36.  
  37. def write_users(type, hostmask, channel, server, aord)
  38. filename = "#{server}#{channel}#{type}.yml"
  39. if File.file?(filename)
  40. f = File.open(filename, 'r')
  41. users = YAML.load(f)
  42. # catch empty or improperly formatted file
  43. users = [] unless users
  44. f.close
  45. else
  46. users = []
  47. end
  48.  
  49. return "User #{hostmask} already in #{type} file!" if users.include?(hostmask) && aord == 'a'
  50. return "User #{hostmask} is not in #{type} file!" if !users.include?(hostmask) && aord == 'd'
  51.  
  52. unless hostmask == 'all' || hostmask == 'none'
  53. if aord == 'a'
  54. users.delete('all')
  55. users.delete('none')
  56. end
  57. end
  58.  
  59. f = File.open(filename, 'w+')
  60. users << hostmask if aord == 'a'
  61. users.delete(hostmask) if aord == 'd'
  62. users.delete('all') if hostmask == 'none'
  63. print "USERS\n#{users}"
  64. f.write(users.to_yaml)
  65. f.close
  66.  
  67. meth = aord == 'a' ? 'Now' : 'No longer'
  68. if hostmask == 'all'
  69. return "#{meth} auto-#{type}'ing everyone!"
  70. elsif hostmask == 'none'
  71. return "#{meth} auto-#{type}'ing no one!"
  72. end
  73.  
  74. meth = aord == 'a' ? 'now stored in' : 'deleted from'
  75. "#{hostmask} #{meth} #{channel} #{type} file."
  76. end
  77.  
  78. def check_user(raw, type, channel, server)
  79. hostmask = raw.split(' ')[0].delete(':')
  80. users = read_users(type, channel, server)
  81. puts "USERS:#{type}\n#{users}\nENDUSERS"
  82. return true if users.include?('all')
  83. return false if users.include?('none')
  84. users.include?(hostmask)
  85. end
  86.  
  87. public
  88.  
  89. def listen(m)
  90. # making shorter lines
  91. c = m.channel
  92. srv = @bot.config[:server]
  93. hostmask = m.raw.split(' ')[0].delete(':')
  94.  
  95. return unless @automode
  96. return if m.user.nick == bot.nick
  97.  
  98. puts "------- #{hostmask} Trying op?"
  99. if check_user(m.raw, 'op', c, srv)
  100. c.op(m.user)
  101. return
  102. end
  103. puts "------- #{hostmask} Trying halfop?"
  104. if check_user(m.raw, 'halfop', c, srv)
  105. c.mode("+h #{m.user}")
  106. return
  107. end
  108.  
  109. puts "------- #{hostmask} Trying voice?"
  110. if check_user(m.raw, 'voice', c, srv)
  111. c.voice(m.user)
  112. return
  113. end
  114.  
  115. puts '------- Nothing!'
  116. end
  117.  
  118. match(/automode (on|off)$/, method: :endisable)
  119. def endisable(m, option)
  120. return unless check_user(m.raw, 'admins', '', '')
  121. @automode = option == 'on'
  122.  
  123. m.reply "Automode is now #{@automode ? 'enabled' : 'disabled'}"
  124. end
  125.  
  126. match(/add(?: (.+))?/, method: :add_user)
  127. def add_user(m, query)
  128. manipulate_user(m, query, 'a')
  129. end
  130.  
  131. match(/del(?: (.+))?/, method: :del_user)
  132. def del_user(m, query)
  133. manipulate_user(m, query, 'd')
  134. end
  135.  
  136. def manipulate_user(m, query, aord)
  137. return unless check_user(m.raw, 'admins', '', '')
  138. type = query.split(' ')[0]
  139. goodtypes = %w(op halfop voice)
  140. hostmask = query.split(' ')[1]
  141. goodmask = /(.*)\!(.*)@(.*)/ =~ hostmask
  142. goodmask = true if hostmask == 'all' || hostmask == 'none'
  143. m.reply('Bad hostmask!') && return unless goodmask
  144. m.reply('Bad mode type!') && return unless goodtypes.include?(type)
  145.  
  146. m.reply write_users(type, hostmask, m.channel, @bot.config[:server], aord)
  147. end
  148. end
  149. end
  150. end
  151.  
  152. # vim:tabstop=2 softtabstop=2 expandtab shiftwidth=2 smarttab foldmethod=syntax:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement