note.c

./mudd/src/note.c · 17.6 KB

From the archive emlen.tar.gz

Download the archive   « Back to the archive contents

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "mud.h"
bool is_note_to  (CHAR_DATA * ch, NOTE_DATA * pnote);
void note_attach (CHAR_DATA * ch);
void note_remove (CHAR_DATA * ch, NOTE_DATA * pnote);
void note_delete (NOTE_DATA * pnote);

#define MAX_BOARDS 6
char *boards[MAX_BOARDS] = {
"General Discussion  ", /* 0 */
"Flames/Pkill        ", /* 1 */
"Announcements       ", /* 2 */
"Ideas/Complaints    ", /* 3 */
"Alignment Board     ", /* 4 */
"Clan Board          ", /* 5 */

};

bool 
is_note_to (CHAR_DATA * ch, NOTE_DATA * pnote)
{
  /* X-align notes don't work if you are below admin level */

  if (pnote->board_num == 4 && LEVEL(ch) < MAX_LEVEL && !alliance[ALIGN(ch)][pnote->note_type])
    return FALSE;

  /* Clan notes are for own clan only. */

  if (pnote->board_num == 5 && LEVEL(ch) < MAX_LEVEL && pnote->note_type != clan_number(ch))
    return FALSE;
  if (!str_cmp (NAME (ch), pnote->sender))
    return TRUE;
  if (is_name ("all", pnote->to_list))
    return TRUE;
  if (IS_IMMORTAL (ch) && (is_name ("immortal", pnote->to_list) ||is_name ("imm", pnote->to_list) ||is_name ("imms", pnote->to_list) ||is_name ("admin", pnote->to_list) ||is_name ("admins", pnote->to_list)))
    return TRUE;
  if (is_name (NAME (ch), pnote->to_list))
    return TRUE;
  if (LEVEL (ch) >= MAX_LEVEL)
    return TRUE;
  return FALSE;
}

void 
note_attach (CHAR_DATA * ch)
{
  NOTE_DATA *pnote;
  
  if (ch->pcdata->pnote != NULL)
    return;
  if (note_free == NULL)
    {
      pnote = mem_alloc (sizeof (*ch->pcdata->pnote));
    }
  else
    {
      pnote = note_free;
      note_free = note_free->next;
    }
  pnote->next = NULL;
  pnote->sender = str_dup (NAME (ch));
  pnote->date = str_dup("");
  pnote->to_list = str_dup ("");
  pnote->subject = str_dup ("");
  pnote->text = str_dup ("");
  pnote->lines = 0;
  pnote->board_num = 0;
  pnote->note_type = 0;
  ch->pcdata->pnote = pnote;
  return;
}

void 
note_remove (CHAR_DATA * ch, NOTE_DATA * pnote)
{
  char to_new[SML_LENGTH];
  char to_one[SML_LENGTH];
  FILE *fp;
  NOTE_DATA *prev;
  char *to_list;
/*
   * Build a new to_list.
   * Strip out this recipient.
 */
  to_new[0] = '\0';
  to_list = pnote->to_list;
  while (*to_list != '\0')
    {
      to_list = one_argy (to_list, to_one);
      if (to_one[0] != '\0' && str_cmp (NAME (ch), to_one))
	{
	  strcat (to_new, " ");
	  strcat (to_new, to_one);
	}
    }
/*
   * Just a simple recipient removal?
 */
  if (LEVEL (ch) < MAX_LEVEL && !is_note_to (ch, pnote))
    {
      if (str_cmp (NAME (ch), pnote->sender) && to_new[0] != '\0')
	{
	  free_string (pnote->to_list);
	  pnote->to_list = str_dup (to_new + 1);
	  return;
	}
    }
/*
   * Remove note from linked list.
 */
  if (pnote == note_list)
    {
      note_list = pnote->next;
    }
  else
    {
      for (prev = note_list; prev != NULL; prev = prev->next)
	{
	  if (prev->next == pnote)
	    break;
	}
      if (prev == NULL)
	{
	  bug ("Note_remove: pnote not found.", 0);
	  return;
	}
      prev->next = pnote->next;
    }
  if (pnote->text) 
  free_string (pnote->text);
  if (pnote->subject)
  free_string (pnote->subject);
  if (pnote->to_list)
  free_string (pnote->to_list);
  if(pnote->date)
  free_string (pnote->date);
  if (pnote->sender)
  free_string (pnote->sender);
  pnote->next = note_free;
  note_free = pnote;
/*
   * Rewrite entire list.
 */
#ifndef WINDOWS
//  fclose (fpReserve);
#endif
  if ((fp = fopen (NOTE_FILE, "w")) == NULL)
    {
      perror (NOTE_FILE);
    }
  else
    {
      for (pnote = note_list; pnote != NULL; pnote = pnote->next)
	{
	  fprintf (fp, "Sender %s~\n", fix_string (pnote->sender));
	  fprintf (fp, "TBoard %d\n", pnote->board_num);
	  fprintf (fp, "Date %s~\n", pnote->date);
	  fprintf (fp, "Stamp %ld\n", pnote->date_stamp);
	  fprintf (fp, "To %s~\n", fix_string (pnote->to_list));
	  fprintf (fp, "Subject %s~\n", fix_string (pnote->subject));
	  fprintf (fp, "Text\n%s~\n", fix_string (pnote->text));
	  fprintf (fp, "End\n");
	}
      fclose (fp);
    }
#ifndef WINDOWS
//  fpReserve = fopen (NULL_FILE, "r");
#endif
  return;
}

void 
do_note (CHAR_DATA * ch, char *argy)
{
  char buf[STD_LENGTH * 6];
  char buf1[STD_LENGTH * 6];
  char buf2[20];
  char buf3[50];
  char arg[SML_LENGTH * 3];
  char str[STD_LENGTH * 3];
  char border[SML_LENGTH];
  NOTE_DATA *pnote;
  int i;
  int vnum;
  int lines;
  int board_here;
  SINGLE_OBJECT *obj;
  int anum;
  char *t;
  DEFINE_COMMAND ("note", do_note, POSITION_DEAD, 0, LOG_NORMAL, "This command is used to post/read/remove notes.")


    if (IS_MOB (ch))
    return;
  board_here = 0;
  obj = get_obj_here (ch, "board", SEARCH_ROOM_FIRST);
  if (obj == NULL)
    {
      board_here = 1;
    }
  else
    board_here = obj->pIndexData->value[9];
  buf1[0] = '\0';
  argy = one_argy (argy, arg);
  smash_tilde (argy);
  if (IS_SET(ch->pcdata->act2, PLR_ASCII))
    {
      sprintf(border, "\x1B[0;32mÄÄÄÄÄÄ\x1B[1;32mÄÄÄÄÄÄÄÄÄÄÄÄÄ\x1B[1;37mÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\x1B[1;32mÄÄÄÄÄÄÄÄÄÄÄÄÄ\x1B[0;32mÄÄÄÄÄÄ\x1B[0m\n\r");
    }
  else
    {
      sprintf(border, "\x1B[0;32m+-----\x1B[1;32m-------------\x1B[1;37m-----------------------------------------\x1B[1;32m-------------\x1B[0;32m-----+\x1B[0m\n\r"); 
    }
  if (arg[0] == '\0')
    {
      do_note (ch, "read");
      return;
    }
  if (ch->pcdata->pnote && ch->pcdata->pnote->text)
    {
      lines = 0;
      for (t = ch->pcdata->pnote->text; *t; t++)
	{
	  if (*t == '\n' && *(t + 1) == '\r')
	    lines++;
	}
      ch->pcdata->pnote->lines = lines;
    }
  

  if (!str_cmp (arg, "list"))
    {
      vnum = 0;
      sprintf (buf1, "\n\r%s", border);
      
        strcat(buf1, "\x1B[1;32m Number  Sender\x1B[0;32m/\x1B[1;32mFrom         Subject\x1B[0m\n\r");

      strcat (buf1, border);
      buf[0] = '\0';
      buf2[0] = '\0';
      buf3[0] = '\0';
      for (pnote = note_list; pnote != NULL; pnote = pnote->next)
	{
	  if (is_note_to (ch, pnote) &&
	      (board_here == pnote->board_num || IS_IMMORTAL (ch)))
	    {
              sprintf(buf, "%d", vnum);
              for (i = strlen(buf) - 5;i < 0;i++) strcat(buf2, " ");
              for (i = strlen(pnote->subject) - 39;i < 0;i++) strcat(buf3, " ");

              sprintf (buf, "   \x1B[0;32m(\x1B[1;32m%d\x1B[0;32m)%s\x1B[0m%-20s\x1B[37;0m'\x1B[1;37m%s\x1B[37;0m'%s%s\n\r",
                       vnum, buf2, pnote->sender, pnote->subject, buf3,
                       (pnote->date_stamp > ch->pcdata->last_note && str_cmp (pnote->sender, NAME (ch))) ? "\x1B[0;31m(\x1B[1;31mNew Note\x1B[0;31m)\x1B[0m" : "          ");
	      strcat (buf1, buf);
              vnum++;buf2[0] = '\0';buf3[0] = '\0';
	    }
	  else
	    vnum++;
	  if (strlen (buf1) > (STD_LENGTH * 16))
	    {
              strcat (buf1, "\n\rThere are too many notes on the board to be able to get a complete list.\n\r");
	      break;
	    }
	}
      page_to_char (buf1, ch); 
      send_to_char ("\x1b[0;37m", ch);
      return;
    }
  

  if (!str_cmp (arg, "read"))
    {
      bool fAll;
      buf[0] = '\0';
      buf2[0] = '\0';
      buf3[0] = '\0';
      if (!str_cmp (argy, "all"))
	{
	  fAll = TRUE;
	  anum = 0;
	}
      else if (argy[0] == '\0' || !str_prefix (argy, "next"))
	{
	  vnum = 0;
	  for (pnote = note_list; pnote != NULL; pnote = pnote->next)
	    {
	      if (is_note_to (ch, pnote) &&
		  (board_here == pnote->board_num || IS_IMMORTAL (ch))
		  && str_cmp (NAME (ch), pnote->sender)
		  && ch->pcdata->last_note < pnote->date_stamp)
		{
                  sprintf (buf1, "\n\r%s", border);
      
		  sprintf(buf, "%d", vnum - 1);
		  for (i = strlen(buf) - 5;i < 0;i++) strcat(buf2, " ");

		  sprintf(buf,"\x1B[0;32m[\x1B[1mNote Number %d\x1B[0;32m] \x1B[0mFrom\x1B[1;30m:\x1B[1;37m %-12s%s\x1B[0mTo\x1B[1;30m:\x1B[1;37m %s\n\r\x1B[0m", vnum - 1, pnote->sender, buf2, capitalize(pnote->to_list));
		  strcat (buf1, buf);
		  sprintf(buf,"\x1B[0;32m[\x1B[1mPosted %s\x1B[0;32m]\x1B[0m     Subject\x1B[1;30m:\x1B[0m '\x1B[1m%s\x1B[0m'\n\r", pnote->date, pnote->subject);
		  strcat (buf1, buf);
		  buf2[0] = '\0';
		  strcat (buf1, border);
                  buf2[0] = '\0';
		  strcat (buf1, pnote->text);
		  ch->pcdata->last_note = UMAX (ch->pcdata->last_note, pnote->date_stamp);
		  
                  page_to_char (buf1, ch); 
		  return;
		}
	      else
		vnum++;
	    }
	  send_to_char ("You have no unread notes.\n\r", ch);
	  return;
	}
      else if (is_number (argy))
	{
	  fAll = FALSE;
	  anum = atoi (argy);
	}
      else
	{
	  send_to_char ("Note read which number?\n\r", ch);
	  return;
	}
      vnum = 0;
      for (pnote = note_list; pnote != NULL; pnote = pnote->next)
	{
	  if ((vnum++ == anum || fAll) && is_note_to (ch, pnote) &&
	      (board_here == pnote->board_num || IS_IMMORTAL (ch)))
	    {
              sprintf (buf,"\n\r%s", border); 
              strcat (buf1, buf);

	      sprintf(buf, "%d", vnum - 1);
	      for (i = strlen(buf) - 5;i < 0;i++) strcat(buf2, " ");
	      
	      sprintf(buf,"\x1B[0;32m[\x1B[1mNote Number %d\x1B[0;32m] \x1B[0mFrom\x1B[1;30m:\x1B[1;37m %-12s%s\x1B[0mTo\x1B[1;30m:\x1B[1;37m %s\n\r\x1B[0m", vnum - 1, pnote->sender, buf2, capitalize(pnote->to_list));
	      strcat (buf1, buf);
	      sprintf(buf,"\x1B[0;32m[\x1B[1mPosted %s\x1B[0;32m]\x1B[0m     Subject\x1B[1;30m:\x1B[0m '\x1B[1m%s\x1B[0m'\n\r", pnote->date, pnote->subject);
	      strcat (buf1, buf);
	      buf2[0] = '\0';
	      strcat (buf1, border);
	      buf2[0] = '\0';
	      strcat (buf1, pnote->text);
	      
              page_to_char (buf1, ch); 
	      return;
	    }
	}
      send_to_char ("No such note.\n\r", ch);
      return;
    }
  if (!str_cmp (arg, "write"))
    {
      if (LEVEL (ch) < IMM_LEVEL && board_here == 1)
	{
	  send_to_char ("You can't write on the immortal notice board.\n\r", ch);
	  return;
	}
      if (IS_SET(ch->act, PLR_LAMER))
	{
	  send_to_char("You have lost your board writing privileges.\n\r", ch);
	  return;
	}
      
      note_attach (ch);
      string_append (ch, &ch->pcdata->pnote->text);
      return;
    }
  if (!str_cmp (arg, "+"))
    {
      if (argy[0] == '\0' || argy == "")
	{
          send_to_char ("Syntax: note + <text>.\n\r", ch);
	  return;
	}
      note_attach (ch);
      strcpy (buf, ch->pcdata->pnote->text);
      if (strlen (buf) + strlen (argy) >= STD_LENGTH * 10)
	{
	  send_to_char ("Note too long.\n\r", ch);
	  return;
	}
      strcat (buf, argy);
      strcat (buf, "\n\r");
      free_string (ch->pcdata->pnote->text);
      ch->pcdata->pnote->text = str_dup (buf);
      ch->pcdata->pnote->lines += 1;
      send_to_char ("Ok.\n\r", ch);
      return;
    }  
  if (!str_cmp (arg, "-"))
    {
      int count, lines;
      char *t;
      if (ch->pcdata->pnote == NULL)
	{
	  send_to_char ("You have to start a note first.\n\r", ch);
	  return;
	}
      if (ch->pcdata->pnote->lines == 0 || ch->pcdata->pnote->text == NULL)
	{
	  send_to_char ("Nothing to delete.\n\r", ch);
	  return;
	}
      lines = ch->pcdata->pnote->lines;
      t = ch->pcdata->pnote->text;
      count = 1;
      if (lines == 1)
	{
	  free_string (ch->pcdata->pnote->text);
	  ch->pcdata->pnote->text = str_dup ("");
	  ch->pcdata->pnote->lines--;
	  send_to_char ("Ok.\n\r", ch);
	  return;
	}
      strcpy (buf, ch->pcdata->pnote->text);
      free_string (ch->pcdata->pnote->text);
      for (t = buf; *t; t++)
	{ 
	  if (*t == '\n' && *(t + 1) == '\r')
	    {
	      if (++count == lines)
		{
		  t++;
		  t++;
		  *t = '\0';
		}
	    }
	}
      ch->pcdata->pnote->text = str_dup (buf);
      ch->pcdata->pnote->lines -= 1;
      send_to_char ("Ok.\n\r", ch);
      return;
    }
  if (!str_cmp (arg, "subject"))
    {
      if (IS_SET(ch->act, PLR_LAMER))
	{
	  send_to_char("You have lost your board writing privileges.\n\r", ch);
	  return;
	}
      if (argy[0] == '\0' || argy == "")
	{
          send_to_char ("Syntax: note subject <subject>.\n\r", ch);
	  return;
	}
      if (strlen(argy) > 50)
	{
          send_to_char ("The character length of the suject of your note is too long.\n\r", ch);
	  return;
	}
      note_attach (ch);
      free_string (ch->pcdata->pnote->subject);
      sprintf (str, "%s\x1b[0;37m", anseval (str_dup (argy), ch));
      ch->pcdata->pnote->subject = str_dup (str);
      send_to_char ("Ok.\n\r", ch);
      return;
    }
  if (!str_cmp (arg, "to"))
    {
      if (IS_SET(ch->act, PLR_LAMER))
	{
	  send_to_char("You have lost your board writing privileges.\n\r", ch);
	  return;
	}
      if (argy[0] == '\0' || argy == "")
	{
          send_to_char ("Syntax: note to <recipient>.\n\r", ch);
	  return;
	}
      if (strlen(argy) > 28)
	{
          send_to_char ("The character length of the recipient of your note is too long.\n\r", ch);
	  return;
	}
      note_attach (ch);
      free_string (ch->pcdata->pnote->to_list);
      ch->pcdata->pnote->to_list = str_dup (argy);
      send_to_char ("Ok.\n\r", ch);
      return;
    }
  if (!str_cmp (arg, "clear"))
    {
      if (IS_SET(ch->act, PLR_LAMER))
	{
	  send_to_char("You have lost your board writing privileges.\n\r", ch);
	  return;
	}
      
      if (ch->pcdata->pnote != NULL)
	{
	  free_string (ch->pcdata->pnote->text);
	  free_string (ch->pcdata->pnote->subject);
	  free_string (ch->pcdata->pnote->to_list);
	  free_string (ch->pcdata->pnote->date);
	  free_string (ch->pcdata->pnote->sender);
	  ch->pcdata->pnote->next = note_free;
	  note_free = ch->pcdata->pnote;
	  ch->pcdata->pnote = NULL;
	}
      send_to_char ("Ok.\n\r", ch);
      return;
    }
  if (!str_cmp (arg, "show"))
    {
      if (IS_SET(ch->act, PLR_LAMER))
	{
	  send_to_char("You have lost your board writing privileges.\n\r", ch);
	  return;
	}
      
      if (ch->pcdata->pnote == NULL)
	{
	  send_to_char ("You have no note in progress.\n\r", ch);
	  return;
	}
      sprintf (buf,"\n\r%s", border); 
      strcat (buf1, buf);

      sprintf (buf, "\x1B[1;32mNote Subject\x1B[0;32m:\x1B[0m %-28s \x1B[1;32mLines\x1B[0;32m:\x1B[0m %d\n\r", ch->pcdata->pnote->subject , ch->pcdata->pnote->lines);
      strcat (buf1, buf);
      sprintf (buf, "\x1B[1;32mFrom\x1B[0;32m:\x1B[0m %-36s \x1B[1;32mTo\x1B[0;32m:\x1B[0m %s\n\r", ch->pcdata->pnote->sender, ch->pcdata->pnote->to_list);
      strcat (buf1, buf);
      strcat (buf1, border);
      strcat (buf1, ch->pcdata->pnote->text);

      page_to_char (buf1, ch); 
      return;
    }
  if (!str_cmp (arg, "post") || !str_cmp (arg, "send"))
    {
      FILE *fp;
      char *strtime;
      
      if (ch->pcdata->pnote == NULL)
	{
	  send_to_char ("You have no note in progress.\n\r", ch);
	  return;
	}
      else if ((ch->pcdata->pnote->to_list[0] == '\0' || ch->pcdata->pnote->to_list == "") && (ch->pcdata->pnote->subject[0] == '\0' || ch->pcdata->pnote->subject == ""))
	{
          send_to_char ("You need to provide a recipient and subject to your note before posting it.\n\r", ch);
	  return;
	}
      else if (ch->pcdata->pnote->to_list[0] == '\0' || ch->pcdata->pnote->to_list == "")
	{
          send_to_char ("You need to provide a recipient to your note before posting it.\n\r", ch);
	  return;
	}
      else if (ch->pcdata->pnote->subject[0] == '\0' || ch->pcdata->pnote->subject == "")
	{
          send_to_char ("You need to provide a subject to your note before posting it.\n\r", ch);
	  return;
	}
      else if (ch->pcdata->pnote->text[0] == '\0' || ch->pcdata->pnote->text == "")
        {
          send_to_char ("You need to write something in your note before posting it.\n\r", ch);
	  return;
        }
      else if (board_here == 1 && LEVEL (ch) < IMM_LEVEL)
	{
	  send_to_char ("This board is reserved for immortals posting messages to mortals.\n\r", ch);
	  send_to_char ("Please post on a public board if you wish to write a note.\n\r", ch);
	  return;
	}
      else if (IS_SET(ch->act, PLR_LAMER))
	{
	  send_to_char("You have lost your board writing privileges.\n\r", ch);
	  return;
	}
      ch->pcdata->online_spot->notes_this_reboot++;
      if(LEVEL(ch) <IMM_LEVEL && ch->pcdata->online_spot->notes_this_reboot > 12)
	{
	  send_to_char("You have already written too many notes this reboot.\n\r", ch);
	  return;
	}

      ch->pcdata->pnote->next = NULL;
      strtime = ctime (&current_time);
      strtime[strlen (strtime) - 1] = '\0';
      ch->pcdata->pnote->board_num = board_here;
      ch->pcdata->pnote->date = str_dup (strtime);
      ch->pcdata->pnote->date_stamp = current_time;
      if (note_list == NULL)
	{
	  note_list = ch->pcdata->pnote;
	}
      else
	{
	  for (pnote = note_list; pnote->next != NULL; pnote = pnote->next)
             ;
                   pnote->next = ch->pcdata->pnote;
	}
      pnote = ch->pcdata->pnote;
      ch->pcdata->pnote = NULL;
#ifndef WINDOWS
//      fclose (fpReserve);
#endif
      if ((fp = fopen (NOTE_FILE, "a")) == NULL)
	{
	  perror (NOTE_FILE);
	}
      else
	{
	  fprintf (fp, "Sender %s~\n", fix_string (pnote->sender));
	  fprintf (fp, "TBoard %d\n", pnote->board_num);
	  fprintf (fp, "Date %s~\n", pnote->date);
	  fprintf (fp, "Stamp %ld\n", pnote->date_stamp);
	  fprintf (fp, "To %s~\n", fix_string (pnote->to_list));
	  fprintf (fp, "Subject %s~\n", fix_string (pnote->subject));
	  fprintf (fp, "Text\n%s~\n", pnote->text);
	  fprintf (fp, "End\n\n");
	  fclose (fp);
	}
#ifndef WINDOWS
//      fpReserve = fopen (NULL_FILE, "r");
#endif
      send_to_char ("Ok.\n\r", ch);
      return;
    }
  if (!str_cmp (arg, "remove"))
    {
      if (!is_number (argy))
	{
	  send_to_char ("Note remove which number?\n\r", ch);
	  return;
	}
      anum = atoi (argy);
      vnum = 0;
      for (pnote = note_list; pnote != NULL; pnote = pnote->next)
	{
          if (vnum++ == anum && ((is_note_to (ch, pnote) && !is_name ("all", pnote->to_list)) || LEVEL (ch) > 109))
	    {
	      note_remove (ch, pnote);
	      send_to_char ("Ok.\n\r", ch);
	      return;
	    }
	}
      send_to_char ("No such note.\n\r", ch);
      return;
    }
  send_to_char ("Huh? Type 'help note' for usage.\n\r", ch);
  return;
}

Site Stats

Snippets: 1791
Codebases: 571