// ******************************************************************************** //
// **Creator:    Dazzle                Licence:      Diku Merc Rom Terms         ** //
// **Difficulty:    3                  Snippet:      Send Backup                 ** // 
// ******************************************************************************** //
// **                           Contact Information                              ** //
// **Yahoo:      ldevil.geo                 Msn:          ldevil@hotmail.com     ** //
// **Aim:        pocketweasle               Email:      sandstorm@arthmoor.com   ** //
// **Webpage:    http://sandstorm.arthmoor.com                                   ** //
// ******************************************************************************** //
// **Terms of Usage:                                                             ** //
// **Follow the Diku, Merc, Rom Licences, also, if you have a snippet helpfile   ** //
// **Put my name in there, if not, leave my name in the source.                  ** //
// **Also, this code is given AS-IS, straight from my mud, so there will be some ** //
// **Effort required to make this work in your mud.                              ** //
// **Install at your own risk, if you have any comments or questions on this     ** //
// **Visit the website mentioned above, and enter the forum, post and bugs or    ** //
// **suggestions there.                                                          ** //
// ******************************************************************************** //

//        This system requires that you have mutt installed on your server
                      Without Mutt, this won't do dick for you

	fgetc comes with popen.c, you will require it.

This system is one of my more favorite of my systems, this will backup your area's datafiles,
player-files, into a nice tarball, and email them to you, however you need to have mutt installed
on your system todo so, it is command initiated, so simply add do_backup to your interp.c and interp.h
And punch everything in, fill in the blacks in the send_backup function for your email address, and your
muds name and such.  Compile, fix any errors that may show up cuz my code is allot different then stock
so you will have to find matching functions to replace mine.

Then run simply when your in the mud, type backup confirm, and within afew minutes, you will recieve
a nice backup of your muds areafiles, data files, playerfiles, you can expand this to encompass everything
including the source-code, but i recommend keeping a backup of your sourcecode offline anyways.

My mud has been sucessfuly using this system for afew months now, and it has saved us allot of time
when it comes to dealing with bad situations, such as entire area-files get wiped out, it is good
to have a backup.

My system sends a backup once a month, and also on command, but i dont' want to release my automated
backup code just yet, still needs some fine-tuning to ensure it doesn't send a backup everytime the mud
reboots ;) little issue with it, sometimes it does it, sometime it doesn't, so when i have that fixed
i'll release that code to enhance this :)

void do_backup (CharData *ch, char *argument)
{
   if( !is_exact_name( argument, "confirm" ) )
   {
      ch->Send( "This command is CPU intensive, and requires confirmation.\n\r" );
      ch->Send( "Syntax:backup confirm\n\r" );
      return;
   }

   do_asave( NULL, "world" );
   send_backup(  );
}

/* NOT recommended to be used as a conventional command! */
void command_pipe( CharData * ch, char *argument )
{
   char buf[LBUF * 2];
   FILE *fp;

   fp = popen( argument, "r" );
   fgetf( buf, LBUF * 2, fp );

   if( ch )
   {
      if(!NullString(buf))
	      page_to_char( buf, ch );
   }

   pclose( fp );
   return;
}


// *Backup :)* //
void send_backup( void )
{
   static char sendstring[LBUF * 2];
   FILE *mfp;
   FileData *fp;
   char backup_data[LBUF];
   char backup_name[LBUF];
   char backup_file[LBUF];
   char subject[100];
   char email[100];
   char file[LBUF];
   BUFFER *output = new_buf(  );
#define MUTT_ROOT_DIR "mutt"

   mudstrlcpy( sendstring, "", LBUF * 2 );

   // *Remove the old crap* //
   command_pipe( NULL, "rm -rf ../area/core.*" );
   command_pipe( NULL, "rm -rf ../area/*~" );
   command_pipe( NULL, "rm -rf ../data/*~" );
   command_pipe( NULL, "rm -rf ../player/*~" );

   snprintf( backup_name, LBUF, "%s-backup.tgz", grab_date_log( mud_data.current_time ) );
   snprintf( subject, 100, "<your muds name here> Backup, %s", backup_name );

   snprintf( email, 100, "Your Email Address Here" );


   snprintf( backup_data, LBUF, "tar czfv %s ../area/* ../data/* ../player/* ../gods/*", backup_name );
   command_pipe( NULL, backup_data );

   snprintf( backup_file, LBUF, "../area/%s", backup_name );

   BufPrintf( output, "\"Dear %s, this is the available backup for <your muds name here> area's, players, data-files.\n\r", email );
   BufPrintf( output, "Please enjoy the backup\"\n\r\n\r" );
   BufPrintf( output, "                                       Truly Yours.... The Automated Backup\n\r" );

   snprintf( file, LBUF, "message.txt" );
   fp = FileOpen( file, "w" );
   fwritef( fp, "%s\n", buf_string( output ) );
   FileClose( fp );

   //      Mail Program    Subject  backup  addy,   message  //
   snprintf( sendstring, LBUF * 2, "%s -s \"%s\" -a \"%s\" %s", MUTT_ROOT_DIR, subject, backup_file, email );
   if( ( mfp = popen( sendstring, "w" ) ) == NULL )
   {
      bugf( "mutt:  Could not location mail." );
      return;
   }
   pclose( mfp );
   free_buf( output );
   unlink( file );

}

// *If you do not have this function, add it.* //

void BufPrintf( BUFFER * buffer, char *fmt, ... )
{
   char buf[LBUF];
   va_list args;

   buf[0] = '\0';

   va_start( args, fmt );
   vsnprintf( buf, LBUF, fmt, args );
   va_end( args );

   AddBuf( buffer, buf );
   return;
}