+ def probability?
+ return @bot.config['markov.probability']
+ end
+
+ def status(m,params)
+ if @bot.config['markov.enabled']
+ reply = _("markov is currently enabled, %{p}% chance of chipping in") % { :p => probability? }
+ l = @learning_queue.length
+ reply << (_(", %{l} messages in queue") % {:l => l}) if l > 0
+ l = @upgrade_queue.length
+ reply << (_(", %{l} chains to upgrade") % {:l => l}) if l > 0
+ else
+ reply = _("markov is currently disabled")
+ end
+ m.reply reply
+ end
+
+ def ignore?(m=nil)
+ return false unless m
+ return true if m.private?
+ return true if m.prefixed?
+ @bot.config['markov.ignore'].each do |mask|
+ return true if m.channel.downcase == mask.downcase
+ return true if m.source.matches?(mask)
+ end
+ return false
+ end
+
+ def readonly?(m=nil)
+ return false unless m
+ @bot.config['markov.readonly'].each do |mask|
+ return true if m.channel.downcase == mask.downcase
+ return true if m.source.matches?(mask)
+ end
+ return false
+ end
+
+ def ignore(m, params)
+ action = params[:action]
+ user = params[:option]
+ case action
+ when 'remove'
+ if @bot.config['markov.ignore'].include? user
+ s = @bot.config['markov.ignore']
+ s.delete user
+ @bot.config['ignore'] = s
+ m.reply _("%{u} removed") % { :u => user }
+ else
+ m.reply _("not found in list")
+ end
+ when 'add'
+ if user
+ if @bot.config['markov.ignore'].include?(user)
+ m.reply _("%{u} already in list") % { :u => user }
+ else
+ @bot.config['markov.ignore'] = @bot.config['markov.ignore'].push user
+ m.reply _("%{u} added to markov ignore list") % { :u => user }
+ end
+ else
+ m.reply _("give the name of a person or channel to ignore")
+ end
+ when 'list'
+ m.reply _("I'm ignoring %{ignored}") % { :ignored => @bot.config['markov.ignore'].join(", ") }
+ else
+ m.reply _("have markov ignore the input from a hostmask or a channel. usage: markov ignore add <mask or channel>; markov ignore remove <mask or channel>; markov ignore list")
+ end
+ end
+
+ def readonly(m, params)
+ action = params[:action]
+ user = params[:option]
+ case action
+ when 'remove'
+ if @bot.config['markov.readonly'].include? user
+ s = @bot.config['markov.readonly']
+ s.delete user
+ @bot.config['markov.readonly'] = s
+ m.reply _("%{u} removed") % { :u => user }
+ else
+ m.reply _("not found in list")
+ end
+ when 'add'
+ if user
+ if @bot.config['markov.readonly'].include?(user)
+ m.reply _("%{u} already in list") % { :u => user }
+ else
+ @bot.config['markov.readonly'] = @bot.config['markov.readonly'].push user
+ m.reply _("%{u} added to markov readonly list") % { :u => user }
+ end
+ else
+ m.reply _("give the name of a person or channel to read only")
+ end
+ when 'list'
+ m.reply _("I'm only reading %{readonly}") % { :readonly => @bot.config['markov.readonly'].join(", ") }
+ else
+ m.reply _("have markov not answer to input from a hostmask or a channel. usage: markov readonly add <mask or channel>; markov readonly remove <mask or channel>; markov readonly list")
+ end
+ end
+
+ def enable(m, params)
+ @bot.config['markov.enabled'] = true
+ m.okay
+ end
+
+ def probability(m, params)
+ if params[:probability]
+ @bot.config['markov.probability'] = params[:probability].to_i
+ m.okay
+ else
+ m.reply _("markov has a %{prob}% chance of chipping in") % { :prob => probability? }
+ end
+ end
+
+ def disable(m, params)
+ @bot.config['markov.enabled'] = false
+ m.okay
+ end
+
+ def should_talk(m)
+ return false unless @bot.config['markov.enabled']
+ prob = m.address? ? @bot.config['markov.answer_addressed'] : probability?
+ return true if prob > rand(100)
+ return false
+ end
+
+ # Generates all sequence pairs from array
+ # seq_pairs [1,2,3,4] == [ [1,2], [2,3], [3,4]]
+ def seq_pairs(arr)
+ res = []
+ 0.upto(arr.size-2) do |i|
+ res << [arr[i], arr[i+1]]
+ end
+ res
+ end
+
+ def set_delay(m, params)
+ if params[:delay] == "off"
+ @bot.config["markov.delay"] = 0
+ m.okay
+ elsif !params[:delay]
+ m.reply _("Message delay is %{delay}" % { :delay => @bot.config["markov.delay"]})
+ else
+ @bot.config["markov.delay"] = params[:delay].to_i
+ m.okay
+ end
+ end
+
+ def reply_delay(m, line)
+ m.replied = true
+ if @bot.config['markov.delay'] > 0
+ @bot.timer.add_once(1 + rand(@bot.config['markov.delay'])) {
+ m.reply line, :nick => false, :to => :public
+ }
+ else
+ m.reply line, :nick => false, :to => :public
+ end
+ end
+
+ def random_markov(m, message)
+ return unless should_talk(m)
+
+ words = clean_message(m).split(/\s+/)
+ if words.length < 2
+ line = generate_string words.first, nil
+
+ if line and message.index(line) != 0
+ reply_delay m, line
+ return
+ end
+ else
+ pairs = seq_pairs(words).sort_by { rand }
+ pairs.each do |word1, word2|
+ line = generate_string(word1, word2)
+ if line and message.index(line) != 0
+ reply_delay m, line
+ return
+ end
+ end
+ words.sort_by { rand }.each do |word|
+ line = generate_string word.first, nil
+ if line and message.index(line) != 0
+ reply_delay m, line
+ return
+ end
+ end
+ end
+ end
+
+ def chat(m, params)
+ line = generate_string(params[:seed1], params[:seed2])
+ if line and line != [params[:seed1], params[:seed2]].compact.join(" ")
+ m.reply line
+ else
+ m.reply _("I can't :(")
+ end
+ end
+
+ def rand_chat(m, params)
+ # pick a random pair from the db and go from there
+ word1, word2 = MARKER, MARKER
+ output = Array.new
+ @bot.config['markov.max_words'].times do
+ word3 = pick_word(word1, word2)
+ break if word3 == MARKER
+ output << word3
+ word1, word2 = word2, word3
+ end
+ if output.length > 1
+ m.reply output.join(" ")
+ else
+ m.reply _("I can't :(")
+ end
+ end
+
+ def learn(*lines)
+ lines.each { |l| @learning_queue.push l }
+ end
+
+ def unreplied(m)
+ return if ignore? m
+