#include <stdio.h>
#include <time.h>
#include <string.h>


#include "global.h"
#include "custom.h"		/* for date FORMAT strings */


ATTACHMENT_NODE *attachment_head = 0;	/* linked list of file info */


#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_news_util.h"
#include "manhat-lib/shared_msg_line.h"
#include "manhat-lib/shared_translate_url.h"


static void
read_parameters (char *course, char *key, int *del_link)
{
  char tmp[10];


  cs_get_required_parameter ("crs", course, MAX_FILENAME);	/* cgihtml library */
  cs_get_required_parameter ("id", key, MAX_KEY);

  /* just check if the "del_link" parameter is there */
  cs_get_optional_parameter ("del_link", tmp, 5);
  *del_link = strlen(tmp);

}



static void
set_back_url (const char *course, const char *key, SESSION *user)
{
  char url[MAX_PATH+1];

  snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s","admin_main_menu", course, key );
  cs_set_value("back_url", url);
}




static void
process_clipboard_info (CLIPBOARD_INFO * clipboard_info, char *infopath,
                        SESSION *user)
{
  FILE *fp;
  int i;
  ATTACHMENT_NODE *aptr = 0;
  char full_path[MAX_PATH + 1];


  snprintf (infopath, MAX_PATH + 1, "%s%s/", CLIPBOARD_PATH, user->id);

  snprintf (full_path, MAX_PATH + 1, "%s%s", infopath, CLIPBOARD_INF_FNAME);

  fp = fopen (full_path, "r");
  if (!fp)
    cs_critical_error (ERR_CLIPBOARD_IS_EMPTY, "");
  if (fread (clipboard_info, sizeof (CLIPBOARD_INFO), 1, fp) != 1)
    cs_critical_error (ERR_FREAD_FAILED, "");

  /* process attachments, build linked list */

  for (i = 1; i <= clipboard_info->attachments; i++)
    {
      if (!attachment_head)
	{
	  attachment_head =
	    (ATTACHMENT_NODE *) malloc (sizeof (ATTACHMENT_NODE));
	  if (!attachment_head)
	    cs_critical_error (ERR_MALLOC_FAILED, "");
	  aptr = attachment_head;
	}
      else
	{
	  aptr->next = (ATTACHMENT_NODE *) malloc (sizeof (ATTACHMENT_NODE));
	  if (!aptr->next)
	    cs_critical_error (ERR_MALLOC_FAILED, "");
	  aptr = aptr->next;
	}

      if (fread (&aptr->data, sizeof (ATTACHMENT), 1, fp) != 1)
	cs_critical_error (ERR_FREAD_FAILED, "");
      aptr->next = 0;
    }				/* for .. */

  fclose (fp);
}





static void
set_message_header (CLIPBOARD_INFO * clipboard_info,
		       char *course,  char *key, SESSION *user)
{
  ATTACHMENT_NODE *aptr;
  char timestring[MAX_TIMESTRING + 1];
  char temp[MAX_PATH + 1];
  char name[MAX_PATH];
  int i =0;
  char *escaped_subject = 0;

 /***** From: *******/
  snprintf (temp, MAX_PATH +1, "%s", 
	  clipboard_info->group == ANONYMOUS_DISCUSSION ? "Anonymous" :
	  clipboard_info->sender_realname);
  cs_set_value("msg.author_realname", temp);

 /***** Date: **********/
  strftime (timestring, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT,
	    localtime (&(clipboard_info->time_sent)));
  cs_set_value("msg.sent_time", timestring);


  /********* Subject *********/

  cs_html_escape_string(clipboard_info->subject, &escaped_subject);   /* shared_cs_util.c */
  if(escaped_subject)
   {
    cs_set_value("msg.subject", escaped_subject);
    free(escaped_subject);
   } 

/*** Attachments ****/

  if (clipboard_info->website_attached)
    {   /* website attached */
     snprintf(temp, MAX_PATH + 1, "%s?crs=%s&amp;id=%s&amp;sender=%s&amp;inf=%s&amp;grp=%d&amp;read=1&amp;loc=0&amp;from=%s",
        "web_page_start",
        course,
        key,
        user->username,
        CLIPBOARD_INF_FNAME,
        CLIPBOARD,
        "clipboard_view");
        
      cs_set_value("view_website_url", temp);

        
    }
  else
    {	/* file attachments */
      if (attachment_head)
	{
	  cs_set_int_value("msg.has_attachments", 1);
	  for (aptr = attachment_head, i = 0; aptr; aptr = aptr->next, i++)
	  {
	   snprintf(name, MAX_PATH, "attachment.%d.filename", i);
	   cs_set_value(name, aptr->data.orig_fname);

	    snprintf(name, MAX_PATH, "attachment.%d.url", i);
	    snprintf (temp, MAX_PATH +1, 
		"%s?crs=%s&amp;id=%s&amp;fname=%s&amp;grp=%d&amp;user=%s&amp;attach=%d&amp;info=dummy",
	                                  "send_file", course, key, aptr->data.unique_fname, CLIPBOARD,
	                                      user->username, clipboard_info->attachments);
	   cs_set_value(name, temp);
	   
	  }
	}
    }
}



static void
set_message_data (const char *msg_fname, const char *msg_path)
{
  FILE *fp;
  char fullpath[MAX_PATH + 1];

  char buffer[300];
  char *ptr;
  int line;
  char *formatted_line;
  char hdf_content_name[50];
  char hdf_reply_level_name[50];
  int reply_level;

  snprintf (fullpath, MAX_PATH + 1, "%s%s", msg_path, msg_fname);
  fp = fopen (fullpath, "r");
  if (!fp)
    cs_critical_error (ERR_FOPEN_READ_FAILED, fullpath);


   line = 0;
   while (fgets (buffer, 300, fp))
   {
      remove_smart_quotes(buffer);  /* shared_msg_line.c */
      snprintf(hdf_content_name, 50, "line.%d.content", line);
      snprintf(hdf_reply_level_name, 50, "line.%d.reply_level", line);
      line++;

      for(reply_level = 0, ptr=buffer; *ptr && (*ptr == '>'); ptr++, reply_level++);

      alloc_formatted_line(buffer, 1, 1, &formatted_line);   /* shared_msg_line.c */
      if(formatted_line)
       {
         cs_set_value(hdf_content_name,formatted_line);
         cs_set_int_value(hdf_reply_level_name, reply_level);
         free(formatted_line);
         formatted_line = 0;
       }
    }
  fclose (fp);

}




static void
set_clipboard_data (char *course, char *key, SESSION *user, CONFIG_STRUCT *conf)
{
  CLIPBOARD_INFO clipboard_info;
  char clipboard_path[MAX_PATH + 1];
  
  process_clipboard_info (&clipboard_info, clipboard_path, user);

  cs_set_course_info(conf);
  cs_set_current_time();


  set_back_url (course, key, user);

  set_message_header (&clipboard_info, course, key, user);
  set_message_data (clipboard_info.msg_fname, clipboard_path);

  
}


static void
free_attachment_list ()
{
  ATTACHMENT_NODE *aptr;

  while (attachment_head)
    {
      aptr = attachment_head;
      attachment_head = attachment_head->next;
      free (aptr);
    }
}



int
main (void)
{
  char course[MAX_PATH + 1];	/* from crs=? command line */
  char key[MAX_KEY + 1];	/* from id=? command line */

  int del_link;			/* from del_link=?command line  do we need to rm a symbolic link
				 ** to an attached web page?
				 */
  SESSION user;
  CONFIG_STRUCT conf;

  cs_cgi_init();
  read_parameters (course, key, &del_link);
  read_configuration_file (course, &conf);	/* shared_util.c */
  validate_key (key, &user, &conf);	/* shared_util.c */


  if (user.group != FACULTY && user.group !=ADMIN)
    cs_critical_error (ERR_REQUEST_DENIED, "");

  if(user.group == ADMIN)
      cs_critical_error(ERR_ADMINS_CANT_VIEW_CLIPBOARD, "");

      
  set_clipboard_data (course, key, &user, &conf);

  if (del_link)
    remove_stale_symlinks (user.username, course, conf.access);  /* shared_news_util.c */

  free_attachment_list ();

  cs_cgi_display("clipboard_view", 1);
  cs_cgi_destroy();

  return 0;
}

