#-- vim:sw=2:et #++ # # :title: Welcome Message for rbot # # Author:: Renan "ShadowBelmolve" Fernandes # # Copyright:: (C) 2009 Renan Fernandes # License:: GPL v2 # # Automatic do a welcome message when user enter in channel # can ignore certain users in certain channels # require 'yaml' class WelcomePlugin < Plugin def initialize super @dir = datafile @file_name = "messages.yaml" @file = File.join(@dir, @file_name) @messages = {} end def help(m, topic="") case topic when "add" "welcome add at , give to users when it join in " when "change" "welcome change at , change of " when "remove" "welcome remove , remove message and ignored users from " when "ignore" "welcome ignore at , not give message to when it join in , e.g. welcome ignore foo bar at #any" when "not ignore" "welcome not ignore at , not ignore users(see ignore), welcome not ignore foo bar at #any" when "list" "welcome list [:channel], list channels with data or see message and ignored users of " when "about" "Welcome Message v0.1 plugin write by Renan Fernandes http://renanfernandes.com.br" else "actions: add, change, remove, ignore, not ignore, about" end end def join(m, params = {}) message(m, m.channel.to_s, m.source) end def message(m, channel, user) load_messages() if @messages[channel] and !@messages[channel][:ignored_users].include?(user.nick) @bot.say(channel, "#{user.nick}, #{@messages[channel][:msg]}") end end def load_messages(m = "") Dir.mkdir(@dir) unless FileTest.directory? @dir @messages = {} and return if not File.exists?(@file) @messages = YAML::load( File.read(@file) ) return end def save_messages(m = "") Dir.mkdir(@dir) unless FileTest.directory? @dir File.open(@file, "w") { |f| f.write( YAML::dump(@messages) ) } return true end def add_message(m, params) load_messages() channel = params[:channel] if @messages[channel] m.reply "Channel aleadly created, use change instead." return false end @messages[channel] = { :msg => "", :ignored_users => [] } @messages[channel][:msg] = params[:message].to_s save_messages() m.okay end def change_message(m, params) load_messages() channel = params[:channel] msg = params[:message].to_s unless @messages[channel] m.reply "Channel not configured, creating..." add_message(m, params) else @messages[channel][:msg] = msg save_messages m.okay end end def remove_channel(m, params) load_messages() channel = params[:channel] m.reply "Channel not created" and return unless @messages[channel] @messages.delete channel save_messages() m.okay end def ignore_user(m, params) load_messages() channel = params[:channel] users = params[:users] m.reply "No configuration set to this channel" and return unless @messages[channel] if user.size == 1 and @messages[channel][:ignored_users].include?(user) m.reply "User aleadly registered." else @messages[channel][:ignored_users] -= users @messages[channel][:ignored_users] += users save_messages m.okay end end def remove_ignored_user(m, params) load_messages() channel = params[:channel] users = params[:users] m.reply "No configuration set to this channel" unless @messages[channel] if users.size == 1 and not @messages[channel][:ignored_users].include?(user) m.reply "User is not inclued in 'ignored users' of this channel" else @messages[channel][:ignored_users] -= users @messages[channel][:ignored_users] += users save_messages m.okay end end def list(m, params) load_messages() channel = params[:channel] if channel if @messages[channel] m.reply "Message: #{@messages[channel][:msg]}" m.reply "Ignored users: #{@messages[channel][:ignored_users].join(', ')}" else m.reply "No data for this channel" end else m.reply @messages.map { |x| x[0] }.join(', ') end end def report_error(m, e) puts "ERROR: #{e.message}" e.backtrace.each { |x| puts x } m.reply "ERROR: #{e.message}" if defined?(m.reply) end def eval_cmd(m, params) m.reply eval(params[:cmd].to_s) end end plugin = WelcomePlugin.new plugin.map "welcome add *message at :channel", :action => 'add_message' plugin.map 'welcome change *message at :channel', :action => 'change_message' plugin.map 'welcome remove :channel', :action => 'remove_channel' plugin.map 'welcome ignore *users at :channel', :action => 'ignore_user' plugin.map 'welcome not ignore *users at :channel', :action => 'remove_ignored_user' plugin.map 'welcome list [:channel]', :action => 'list' plugin.map 'welcome eval *cmd', :action => 'eval_cmd' plugin.default_auth("*",false)