Exemple #1
0
/**
 * set up the appropriate MIME and Base64 headers for 
 * the attachment of file specified in Mopts.attach
**/
static int
attachFiles(const char *boundary, dstrbuf *out)
{
	dstrbuf *file_name = NULL;
	dstrbuf *file_type = NULL;
	char *next_file = NULL;

	/*
	* What we will do here is parse Mopts.attach for comma delimited file
	* names.  If there was only one file specified with no comma, then strtok()
	* will just return that file and the next call to strtok() will be NULL
	* which will allow use to break out of our loop of attaching base64 stuff.
	*/
	while ((next_file = (char *)dlGetNext(Mopts.attach)) != NULL) {
		FILE *current = fopen(next_file, "r");
		if (!current) {
			#if 1 //Ren: to skip nonexistent file
			fprintf(stderr, "email: skip %s\n", next_file);
			continue;
			#else
			fatal("Could not open attachment: %s", next_file);
			return (ERROR);
			#endif
		}

		/* If the user specified an absolute path, just get the file name */
		file_type = mimeFiletype(next_file);
		file_name = mimeFilename(next_file);

		/* Set our MIME headers */
		dsbPrintf(out, "\r\n--%s\r\n", boundary);
		dsbPrintf(out, "Content-Transfer-Encoding: base64\r\n");
		dsbPrintf(out, "Content-Type: %s; name=\"%s\"\r\n", 
			file_type->str, file_name->str);
		dsbPrintf(out, "Content-Disposition: attachment; filename=\"%s\"\r\n", 
			file_name->str);
		dsbPrintf(out, "\r\n");

		/* Encode to 'out' */
		mimeB64EncodeFile(current, out);
		dsbDestroy(file_type);
		dsbDestroy(file_name);
	}
	return SUCCESS;
}
Exemple #2
0
dstrbuf *
mimeFiletype(const char *filename)
{
	bool found=false;
	int i=0, veclen=0;
	dstrbuf *type=NULL;
	dstrbuf *buf=DSB_NEW;
	dvector vec=NULL;
	const char *ext=NULL;
	dstrbuf *filen=NULL;
	FILE *file = fopen(MAGIC_FILE, "r");

	if (!file) {
		goto exit;
	}
	filen = mimeFilename(filename);
	ext = strrchr(filen->str, '.');

	/* If we don't know  the extension, we don't know what type
	 * of file it's going to be. Therefore, skip all of this.  */
	if (!ext) {
		goto exit;
	}
	/* Get past . in extension name. */
	ext++;

	while (!feof(file)) {
		dsbReadline(buf, file);
		if (buf->str[0] == '#' || buf->str[0] == '\n') {
			continue;
		}
		chomp(buf->str);

		/* If we still have an allocated type, free it */
		if (type) {
			dsbDestroy(type);
		}
		type = getMimeType(buf->str);
		if (type->len == 0) {
			continue;
		}
		vec = explode(buf->str, " \t");
		veclen = dvLength(vec);
		/* Start i at 1 since the first element in the
		 * vector is the mime type. The exts are after that. */
		for (i=1; i < veclen; i++) {
			if (strcmp((char *)vec[i], ext) == 0) {
				found = true;
				break;
			}
		}
		dvDestroy(vec);
		if (found) {
			/* Found it! */
			break;
		}
	}

exit:
	dsbDestroy(filen);
	dsbDestroy(buf);
	if (file) {
		fclose(file);
	}
	if (!type || type->len == 0) {
		type = DSB_NEW;
		dsbCopy(type, "application/unknown");
	}
	return type;
}