attachment.c 10.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2009, 2010 Free
   Software Foundation, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
   Public License along with this library; if not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301 USA */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef HAVE_LIBGEN_H
# include <libgen.h>
#endif

#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif

#include <mailutils/cctype.h>
#include <mailutils/cstr.h>
#include <mailutils/body.h>
#include <mailutils/filter.h>
#include <mailutils/header.h>
#include <mailutils/message.h>
#include <mailutils/stream.h>
#include <mailutils/errno.h>
#include <mailutils/mutil.h>

#define BUF_SIZE	2048

struct _msg_info
{
  char *buf;
  size_t nbytes;
  char *header_buf;
  int header_len;
  int mu_header_size;
  mu_header_t hdr;
  mu_message_t msg;
  int ioffset;
  int ooffset;
  mu_stream_t stream;	/* output file/decoding stream for saving attachment */
  mu_stream_t fstream;	/* output file stream for saving attachment */
};

#define MSG_HDR "Content-Type: %s; name=%s\nContent-Transfer-Encoding: %s\nContent-Disposition: attachment; filename=%s\n\n"

int
mu_message_create_attachment (const char *content_type, const char *encoding,
			      const char *filename, mu_message_t *newmsg)
{
  mu_header_t hdr;
  mu_body_t body;
  mu_stream_t fstream = NULL, tstream = NULL;
  char *header = NULL, *name = NULL, *fname = NULL;
  int ret;

  if (newmsg == NULL)
    return MU_ERR_OUT_PTR_NULL;
  if (filename == NULL)
    return EINVAL;

  if ((ret = mu_message_create (newmsg, NULL)) == 0)
    {
      if (content_type == NULL)
	content_type = "text/plain";
      if (encoding == NULL)
	encoding = "7bit";
      if ((fname = strdup (filename)) != NULL)
	{
	  name = strrchr (fname, '/');
	  if (name)
	    name++;
	  else
	    name = fname;
	  if ((header =
	       malloc (strlen (MSG_HDR) + strlen (content_type) +
		       strlen (name) * 2 + strlen (encoding) + 1)) == NULL)
	    ret = ENOMEM;
	  else
	    {
	      sprintf (header, MSG_HDR, content_type, name, encoding, name);
	      if ((ret =
		   mu_header_create (&hdr, header, strlen (header),
				     *newmsg)) == 0)
		{
		  mu_message_get_body (*newmsg, &body);
		  if ((ret =
		       mu_file_stream_create (&fstream, filename,
					      MU_STREAM_READ)) == 0)
		    {
		      if ((ret = mu_stream_open (fstream)) == 0)
			{
			  if ((ret =
			       mu_filter_create (&tstream, fstream, encoding,
						 MU_FILTER_ENCODE,
						 MU_STREAM_READ)) == 0)
			    {
			      mu_body_set_stream (body, tstream, *newmsg);
			      mu_message_set_header (*newmsg, hdr, NULL);
			    }
			}
		    }
		}
	      free (header);
	    }
	}
    }
  if (ret)
    {
      if (*newmsg)
	mu_message_destroy (newmsg, NULL);
      if (hdr)
	mu_header_destroy (&hdr, NULL);
      if (fstream)
	mu_stream_destroy (&fstream, NULL);
      if (fname)
	free (fname);
    }
  return ret;
}


static int
_attachment_setup (struct _msg_info **info, mu_message_t msg,
		   mu_stream_t *stream, void **data)
{
  int sfl, ret;
  mu_body_t body;

  if ((ret = mu_message_get_body (msg, &body)) != 0 ||
      (ret = mu_body_get_stream (body, stream)) != 0)
    return ret;
  mu_stream_get_flags (*stream, &sfl);
  if (data == NULL && (sfl & MU_STREAM_NONBLOCK))
    return EINVAL;
  if (data)
    *info = *data;
  if (*info == NULL)
    {
      if ((*info = calloc (1, sizeof (struct _msg_info))) == NULL)
	return ENOMEM;
    }
  if (((*info)->buf = malloc (BUF_SIZE)) == NULL)
    {
      free (*info);
      return ENOMEM;
    }
  return 0;
}

static void
_attachment_free (struct _msg_info *info, int free_message)
{
  if (info->buf)
    free (info->buf);
  if (info->header_buf)
    free (info->header_buf);
  if (free_message)
    {
      if (info->msg)
	mu_message_destroy (&(info->msg), NULL);
      else if (info->hdr)
	mu_header_destroy (&(info->hdr), NULL);
    }
  free (info);
}

int
mu_message_save_attachment (mu_message_t msg, const char *filename,
			    void **data)
{
  mu_stream_t istream;
  struct _msg_info *info = NULL;
  int ret;
  size_t size;
  size_t nbytes;
  mu_header_t hdr;
  const char *fname = NULL;
  char *partname = NULL;

  if (msg == NULL)
    return EINVAL;

  if ((ret = _attachment_setup (&info, msg, &istream, data)) != 0)
    return ret;

  if (ret == 0 && (ret = mu_message_get_header (msg, &hdr)) == 0)
    {
      if (filename == NULL)
	{
	  /* FIXME: Charset info is ignored */
	  ret = mu_message_aget_attachment_name (msg, &partname, NULL);
	  if (partname)
	    fname = partname;
	}
      else
	fname = filename;
      if (fname
	  && (ret =
	      mu_file_stream_create (&info->fstream, fname,
				     MU_STREAM_WRITE | MU_STREAM_CREAT)) == 0)
	{
	  if ((ret = mu_stream_open (info->fstream)) == 0)
	    {
	      char *content_encoding;
	      char *content_encoding_mem = NULL;

	      mu_header_get_value (hdr, "Content-Transfer-Encoding", NULL, 0,
				   &size);
	      if (size)
		{
		  content_encoding_mem = malloc (size + 1);
		  if (content_encoding_mem == NULL)
		    ret = ENOMEM;
		  content_encoding = content_encoding_mem;
		  mu_header_get_value (hdr, "Content-Transfer-Encoding",
				       content_encoding, size + 1, 0);
		}
	      else
		content_encoding = "7bit";
	      ret =
		mu_filter_create (&info->stream, istream, content_encoding,
				  MU_FILTER_DECODE, MU_STREAM_READ);
	      free (content_encoding_mem);
	    }
	}
    }
  if (info->stream && istream)
    {
      if (info->nbytes)
	memmove (info->buf, info->buf + (BUF_SIZE - info->nbytes),
		 info->nbytes);
      while ((ret == 0 && info->nbytes)
	     ||
	     ((ret =
	       mu_stream_read (info->stream, info->buf, BUF_SIZE,
			       info->ioffset, &info->nbytes)) == 0
	      && info->nbytes))
	{
	  info->ioffset += info->nbytes;
	  while (info->nbytes)
	    {
	      if ((ret =
		   mu_stream_write (info->fstream, info->buf, info->nbytes,
				    info->ooffset, &nbytes)) != 0)
		break;
	      info->nbytes -= nbytes;
	      info->ooffset += nbytes;
	    }
	}
    }
  if (ret != EAGAIN && info)
    {
      mu_stream_close (info->fstream);
      mu_stream_destroy (&info->stream, NULL);
      mu_stream_destroy (&info->fstream, NULL);
      _attachment_free (info, ret);
    }

  /* Free fname if we allocated it. */
  if (partname)
    free (partname);

  return ret;
}

int
mu_message_encapsulate (mu_message_t msg, mu_message_t *newmsg, void **data)
{
  mu_stream_t istream, ostream;
  const char *header;
  struct _msg_info *info = NULL;
  int ret = 0;
  size_t nbytes;
  mu_body_t body;

  if (msg == NULL)
    return EINVAL;
  if (newmsg == NULL)
    return MU_ERR_OUT_PTR_NULL;

  if ((ret = _attachment_setup (&info, msg, &ostream, data)) != 0)
    return ret;

  if (info->msg == NULL
      && (ret = mu_message_create (&(info->msg), NULL)) == 0)
    {
      header =
	"Content-Type: message/rfc822\nContent-Transfer-Encoding: 7bit\n\n";
      if ((ret =
	   mu_header_create (&(info->hdr), header, strlen (header),
			     msg)) == 0)
	ret = mu_message_set_header (info->msg, info->hdr, NULL);
    }
  if (ret == 0 && (ret = mu_message_get_stream (msg, &istream)) == 0)
    {
      if ((ret = mu_message_get_body (info->msg, &body)) == 0 &&
	  (ret = mu_body_get_stream (body, &ostream)) == 0)
	{
	  if (info->nbytes)
	    memmove (info->buf, info->buf + (BUF_SIZE - info->nbytes),
		     info->nbytes);
	  while ((ret == 0 && info->nbytes)
		 ||
		 ((ret =
		   mu_stream_read (istream, info->buf, BUF_SIZE,
				   info->ioffset, &info->nbytes)) == 0
		  && info->nbytes))
	    {
	      info->ioffset += info->nbytes;
	      while (info->nbytes)
		{
		  if ((ret =
		       mu_stream_write (ostream, info->buf, info->nbytes,
					info->ooffset, &nbytes)) != 0)
		    break;
		  info->nbytes -= nbytes;
		  info->ooffset += nbytes;
		}
	    }
	}
    }
  if (ret == 0)
    *newmsg = info->msg;
  if (ret != EAGAIN && info)
    _attachment_free (info, ret);
  return ret;
}

int
mu_message_unencapsulate (mu_message_t msg, mu_message_t *newmsg,
			  void **data)
{
  size_t size, nbytes;
  int ret = 0;
  mu_header_t hdr;
  mu_stream_t istream, ostream;
  struct _msg_info *info = NULL;

  if (msg == NULL)
    return EINVAL;
  if (newmsg == NULL)
    return MU_ERR_OUT_PTR_NULL;

  if ((data == NULL || *data == NULL)
      && (ret = mu_message_get_header (msg, &hdr)) == 0)
    {
      mu_header_get_value (hdr, "Content-Type", NULL, 0, &size);
      if (size)
	{
	  char *content_type;
	  if ((content_type = malloc (size + 1)) == NULL)
	    return ENOMEM;
	  mu_header_get_value (hdr, "Content-Type", content_type, size + 1,
			       0);
	  ret =
	    mu_c_strncasecmp (content_type, "message/rfc822",
			      strlen ("message/rfc822"));
	  free (content_type);
	  if (ret != 0)
	    return EINVAL;
	}
      else
	return EINVAL;
    }
  if ((ret = _attachment_setup (&info, msg, &istream, data)) != 0)
    return ret;
  if (info->msg == NULL)
    ret = mu_message_create (&(info->msg), NULL);
  if (ret == 0)
    {
      mu_message_get_stream (info->msg, &ostream);
      if (info->nbytes)
	memmove (info->buf, info->buf + (BUF_SIZE - info->nbytes),
		 info->nbytes);
      while ((ret == 0 && info->nbytes)
	     ||
	     ((ret =
	       mu_stream_read (istream, info->buf, BUF_SIZE, info->ioffset,
			       &info->nbytes)) == 0 && info->nbytes))
	{
	  info->ioffset += info->nbytes;
	  while (info->nbytes)
	    {
	      if ((ret =
		   mu_stream_write (ostream, info->buf, info->nbytes,
				    info->ooffset, &nbytes)) != 0)
		break;
	      info->nbytes -= nbytes;
	      info->ooffset += nbytes;
	    }
	}
    }
  if (ret == 0)
    *newmsg = info->msg;
  if (ret != EAGAIN && info)
    _attachment_free (info, ret);
  return ret;
}