znmud-0.0.1/benchmark/
znmud-0.0.1/cmd/
znmud-0.0.1/cmd/emotes/
znmud-0.0.1/cmd/objects/
znmud-0.0.1/cmd/tiny/
znmud-0.0.1/doc/
znmud-0.0.1/farts/
znmud-0.0.1/lib/
znmud-0.0.1/lib/combat/
znmud-0.0.1/lib/core/bodytypes/
znmud-0.0.1/lib/engine/
znmud-0.0.1/lib/farts/
znmud-0.0.1/logs/
# File: maileditor.rb
# Author: Craig Smith
# This source code copyright (C) 2009 Craig Smith
# All rights reserved.
#
# Released under the terms of the GNU Public License
# See COPYING file for additional information.
#


require 'utility/log'
require 'olc/editor'
require 'core/world'

# Mail Editor Wrapper for olc/editor
class MailEditor < Editor
  logger 'DEBUG'

  attr_accessor :ch, :classname, :method, :editstr, :title, :menu, :menu_stack

  # Initializes an OLC Object
  def initialize(id, classname, method)
	self.title = "Mail Editor" if not title
	self.ch = get_object(id)
	self.classname = classname
	self.method = method
	if not classname.respond_to?(method) or not classname.respond_to?("#{method}=") 
		ch.sendto("ERROR: Invalid method (#{method}) for class #{classname}")
		exit_olc(true)
	end
	self.editstr = classname.send(method)
	if not editstr
		self.editstr = String.new
	elsif not editstr.is_a? String
		ch.sendto("Method #{method} is not a String.")
		exit_olc(true)
	end
	self.menu = olc_basemenu(title,self,nil)
	self.menu_stack = []
	ch.mode = :olc
	ch.sendroom("#{ch.name} starts composing a mail message...")
	show_menu
  end

  # Creates a basic menu with just a Quit button
  # [+title+] title of the menu
  # [+calling+] Calling object (one who controls this menu)
  # [+init+] Init function that sets up this menu
  # [+reset+] Optional reset
  # [+return+] basic menu object
  def olc_basemenu(title,calling,init,reset=nil)
	if reset == "reset"
		m = menu
	else
		m = OlcMenu.new(title,calling,init)
	end
	m.add_std_option(nil, "Subject", classname.subject, self, :set_subj)
	m.add_editor_option("E","Edit", editstr, self, :edit_str)
	m.add_text_option("X","Abort", self, :abort_menu)
	m.add_text_option("Q","Quit", self, :quit_menu)
	m
  end

  # Exits the OLC session and sends the mail if not aborted
  def exit_olc(cancel=false)
	ch.mode = :playing
	ch.olc = nil
	self.menu = nil
	if not cancel and editstr.size > 0
		# Strip off ALL \r\n at the end of sentences
		self.editstr.gsub!(/([\r\n]+)$/,"")
		classname.send("#{method}=", editstr)
		recipient = get_object(classname.to)
		recipient.mailbox << classname.id
		if get_object(0).connected_characters.include? classname.to
			get_object(classname.to).sendto("You have new mail.")
		end
		ch.sendto("Message Sent.")
	else
		classname.reset
		classname.unused = true
	end
	ch.sendroom("#{ch.name} has finished writing.")
  end

  def set_subj(str)
	classname.subject = str if str.size > 0
	show_menu
	classname.subject
  end

end