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::    cmd_mail.rb

require 'olc/maileditor'

# This source code copyright (C) 2009 Craig Smith
# All rights reserved.
#
# Released under the terms of the TeensyMUD Public License
# See LICENSE file for additional information.
#

module Cmd

  bindtextdomain("cmd")

  # Mail management command
  def cmd_mail(args)
	case args
	when "", nil
		if mailbox.size > 0
			msg = ""
			mailbox.each do |mailid|
				mail = get_object(mailid)
				read = ""
				read = "*" if not mail.read
				msg << "#{mail.id}) #{read}#{mail.subject}\n"
			end
			add_event(id, id, :show, msg)
		else
			sendto _("You have no messages.")
		end
	when /^read (\d+)/
		msgid = $1.to_i
		if mailbox.include? msgid
			mail = get_object(msgid)
			mail.read = true
			msg = _("From: %{name}\n" % {:name => get_object(mail.from).name })
			msg << _("Date: %{date}\n" % {:date => mail.date })
			msg << _("Subject: %{subject}\n" % {:subject => mail.subject })
			msg << mail.body
			add_event(id, id, :show, msg)
		else
			sendto _("Unknown message id.")
		end
	when /^del (\d+)/
		msgid = $1.to_i
		if mailbox.include? msgid
			mail = get_object(msgid)
			mail.reset
			mail.unused = true
			mailbox.delete(msgid)
			sendto _("Message deleted.")
		else
			sendto _("Unknown message id.")
		end
	when /^del all/
		mailbox.each do |msgid|
			mail = get_object(msgid)
			mail.reset
			mail.unused = true
		end
		mailbox.clear
		sendto _("Mailbox cleared.")
	when /(\S+)\s*(.*)/
		touser = $1
		msgsubj = $2
		toperson = nil
		world.all_characters.each do |oid|
			p = get_object(oid)
			toperson = p if p.name=~/^#{touser}$/i
		end
		if toperson
			if msgsubj.size > 0
				subj = msgsubj
			else
				subj = _("No Subject")
			end
			emptyletter = nil
			world.find_objects("mail").each do |m|
				if m.is_a? MailMsg
					emptyletter = m if m.unused
				end
			end
			if emptyletter
				mail = emptyletter
				mail.reset
			else
				mail = MailMsg.new("mail from #{name}", id)
			end
			mail.subject = subj
			mail.from = id
			mail.to = toperson.id
			mail.date = Time.now
			put_object(mail)
			@mode = :olc
			@olc = MailEditor.new(id, mail, "body")
		else
			sendto _("Unknown user.")
		end
	end
  end

end