#include "MimeSender.h"

/* since the mime type may not be text based, don't send the 
  normal headers or footers, instead make the Tagger
  explicitly send the content type and such

  Also, don't print text except in replace tags...that would
  mess up things like gifs  */

#ifdef USE_FCGI
#include "fcgi-cout-redirect.h"
extern fcgicout fcgi_cout;
#endif


Error MimeSender::send (void) {
  Error err;
  /* this is a possible memory leak...check it out... */
  SGMLTokenList & stl = parser->htmlTokens();

  // tell the userdata object who we are
  userdata->setExec (this);

  try {
    recurseOnTokens ( stl, err );
  }
  catch (ExecutorAbort ea){
    // Return gracefully
  }

  cout.flush();

  return (err);

}


void MimeSender::callFunction (SGMLToken * t, Error & err) {
  string command = t->command();
  char test[255];
  strcpy (test, command.c_str());

  if (command == "CONTENT") {
    processCONTENT ( (SGMLContainer *) t, err);
  } else {
    Sender::callFunction (t, err);
  }
}

/*
@nodoc
*/
void MimeSender::processGeneric (SGMLToken * t, Error & err) {
  // if we don't know what to do with the tag, do nothing! 

}

/*
@usehtml
@usage <CONTENT TYPE="string">
@IFLAG TYPE The content type of the file

@sections HTML (MIME)
@index Creates alternative MIME type files

Creates alternative MIME type files.  If a screen name ends with
".mime", a special MIME mode is used to allow the creation of text or
binary pages with a different content type.  The CONTENT tag is used
to define the content type of the resulting page.  The CONTENT tag
should be used only once, and should occur before any <a
href="functions.html#print">print()</a> or _REPLACE_ statements.

<P>

Please see the section <a href="mimetypes.html">Other MIME Types</a> for
more information.

@ex <!-- In a file named test.mime -->
@ex <HTML>
@ex  <CONTENT TYPE="text/plain">
@ex  <REPLACE VALUE="HI!\nThis is a plain test file">
@ex </HTML>

 */
void MimeSender::processCONTENT ( SGMLTag * t, Error & err) {
  AssocArray * flags = &(t->flags());

  cout << "Content-type: " << interpretString ((*flags)["TYPE"]) << "\n\n";
}