/*
** j###t ########## #### ####
** j###t ########## #### ####
** j###T "###L J###"
** ######P' ########## #########
** ######k, ########## T######T
** ####~###L ####
** #### q###L ########## .#####
** #### \###L ########## #####"
**
** $Id: Post.java,v 1.7 1997/07/28 15:33:22 subtle Exp subtle $
**
** Class History
**
** Date Name Description
** ---------|------------|-----------------------------------------------
** 21Jul97 subtle uses shortcuts now
**
*/
package key.commands;
import key.*;
import key.primitive.DateTime;
import key.util.Trie;
import java.util.StringTokenizer;
import java.io.*;
public class Post extends Command
{
public static final AtomicElement[] ELEMENTS =
{
AtomicElement.construct( Post.class, String.class, "spoonClause",
AtomicElement.PUBLIC_FIELD,
"the message sent when messages are sent to yourself" )
};
public static final AtomicStructure STRUCTURE = new AtomicStructure( Command.STRUCTURE, ELEMENTS );
public static final int MAX_LINES = 100;
public static final int MAX_BYTES = 50 * MAX_LINES;
public String spoonClause = "Try a post-it note. They work better";
public Post()
{
setKey( "post" );
// text for command
usage = "<name> <subject>";
}
public AtomicStructure getDeclaredStructure()
{
return( STRUCTURE );
}
public void run( Player p, StringTokenizer args, String fullLine, CategoryCommand caller, InteractiveConnection ic, Flags flags ) throws IOException
{
if( args.hasMoreTokens() )
{
String name = args.nextToken();
if( args.hasMoreTokens() )
{
Object t;
if( name.equalsIgnoreCase( "news" ) )
t = p.getRealm().news;
else
t = p.getLocation().getExactElement( name );
if( t != null )
{
if( t instanceof Container )
{
String subject = args.nextToken( "" );
if( subject.length() > Letter.MAX_SUBJECT )
{
ic.sendError( "Please use a subject of less than " + Letter.MAX_SUBJECT + " characters." );
return;
}
Letter l = createLetter( p, ic, subject );
if( l == null )
{
ic.sendFeedback( "Post aborted. Message NOT posted." );
return;
}
try
{
((Container)t).add( l );
}
catch( NonUniqueKeyException e )
{
throw new UnexpectedResult( "while sending mail: " + e.toString() );
}
catch( BadKeyException e )
{
throw new UnexpectedResult( "while sending mail: " + e.toString() );
}
return;
}
}
t = new Search( name, Key.shortcuts() ).result;
if( t == null )
{
ic.sendError( "Cannot find object or player '" + name + "'" );
return;
}
else if( t instanceof Trie )
{
ic.sendError( "Multiple matches: " + ((Trie)t).contents() );
return;
}
else if( t == p )
{
ic.sendFeedback( spoonClause );
return;
}
else if( t instanceof Player )
{
Letter l = createLetter( p, ic, args.nextToken( "" ) );
try
{
((Player)t).addMail( l );
}
catch( NonUniqueKeyException e )
{
throw new UnexpectedResult( "while sending mail: " + e.toString() );
}
catch( BadKeyException e )
{
throw new UnexpectedResult( "while sending mail: " + e.toString() );
}
return;
}
else
{
ic.sendError( "'" + name + "' is not something you can send mail to." );
return;
}
}
}
usage( ic );
}
private Letter createLetter( Player p, InteractiveConnection ic, String subject )
{
Paragraph main = Editor.edit( p, new TextParagraph(), ic, MAX_LINES, MAX_BYTES );
if( ((TextParagraph)main).getText().length() == 0 )
return( null );
Letter l = (Letter) Factory.makeAtom( Letter.class );
l.setProperty( "description", subject );
l.setProperty( "contents", main );
l.setProperty( "from", p.getName() );
l.setProperty( "when", new DateTime() );
return( l );
}
}