示例#1
0
static void
do_action (	int req,
		const char *opt)
{
	if (req == 1 || req == 2)
	{
		uint8_t *s = (uint8_t *)opt;
		size_t ilen = strlen ((const char *)s);
		if (req == 1)
		{
			char *data = base64_encode (s, ilen);
			if (data == NULL)
				exit (-1);
			else
			{
				printf ("%s\n", data);
				free (data);
			}
		}
		else if (req == 2)
		{
			char *data = base64_decode (s, ilen);
			if (data == NULL)
				return;
			else
			{
				printf ("%s\n", data);
				free (data);
			}
		}
	}
	
	if (req == 3 || req == 4)
	{
		struct stat file_stat;
		
		int input_file = open (optarg, O_RDONLY | O_NOFOLLOW);
		
		if (input_file == -1)
		{
			fprintf (stderr, "%s\n", strerror (errno));
			exit (-1);
		}
		
		fstat (input_file, &file_stat);
		off_t file_size = file_stat.st_size;
		
		if (req == 3)
			base64_encode_file (input_file, file_size);

		else if (req == 4)
			base64_decode_file (input_file, file_size);
				
		close (input_file);
	}
}
bool AmSmtpClient::send_data(const vector<string>& hdrs, const AmMail& mail)
{
  string part_delim = "----=_NextPart_" 
    + int2str(int(time(NULL))) 
    + "_" + int2str(int(getpid()));

  for( vector<string>::const_iterator hdr_it = hdrs.begin();
       hdr_it != hdrs.end(); ++hdr_it )
    SEND_LINE(*hdr_it);

  SEND_LINE("MIME-Version: 1.0");

  if(!mail.attachements.empty()){
    SEND_LINE("Content-Type: multipart/mixed; ");
    SEND_LINE("      boundary=\"" + part_delim + "\"");
    SEND_LINE(""); // EoH
    SEND_LINE("--" + part_delim);
  }

  if(mail.charset.empty()){
    SEND_LINE("Content-Type: text/plain");
  }
  else {
    SEND_LINE("Content-Type: text/plain; ");
    SEND_LINE("      charset=\"" + mail.charset + "\"");
  }
  SEND_LINE(""); //EoH
  SEND_LINE(mail.body);


  for( Attachements::const_iterator att_it = mail.attachements.begin();
       att_it != mail.attachements.end(); ++att_it	) {
	
    SEND_LINE("--" + part_delim );
    if(!att_it->content_type.empty()){
      SEND_LINE("Content-Type: " + att_it->content_type);
    }
    else {
      SEND_LINE("Content-Type: application/octet-stream");
    }
    SEND_LINE("Content-Transfer-Encoding: base64");
	
    if(att_it->filename.empty()) {
      SEND_LINE("Content-Disposition: inline"); // | "attachement"
    }
    else {
      SEND_LINE("Content-Disposition: inline; "); // | "attachement"
      SEND_LINE("      filename=\"" + att_it->filename + "\"");
    }
    SEND_LINE(""); // EoH

    base64_encode_file(att_it->fp,sd);
    SEND_LINE(""); // base64_encode_file() doesn't generate any EoL
  }

  if(!mail.attachements.empty()){
    SEND_LINE("--" + part_delim + "--");
  }

  return false;
}