mail.c 25.5 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 * Copyright (C) 1997 Daniel Risacher 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
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
 *   GUMP - Gimp Useless Mail Plugin (or Gump Useless Mail Plugin if you prefer)
 *          version about .85 I would say... give or take a few decimal points
 *
 *
 *   by Adrian Likins <adrian@gimp.org>
 *      MIME encapsulation by Reagan Blundell <reagan@emails.net>
 *
 *
 *
 *   Based heavily on gz.c by Daniel Risacher
 *
 *     Lets you choose to send a image to the mail from the file save as dialog.
 *      images are piped to uuencode and then to mail...
 *
 *
 *   This works fine for .99.10. I havent actually tried it in combination with
 *   the gz plugin, but it works with all other file types. I will eventually get
 *   around to making sure it works with gz.
 *
 *  To use: 1) image->File->mail image
 *          2) when the mail dialog popups up, fill it out. Only to: and filename are required
 *             note: the filename needs to a type that the image can be saved as. otherwise
 *                   you will just send an empty message.
 *          3) click ok and it should be on its way
 *
 *
 * NOTE: You probabaly need sendmail installed. If your sendmail is in an odd spot
 *       you can change the #define below. If you use qmail or other MTA's, and this
 *       works after changing the MAILER, let me know how well or what changes were
 *       needed.
 *
 * NOTE: Uuencode is needed. If it is in the path, it should work fine as is. Other-
 *       wise just change the UUENCODE.
 *
 *
 * TODO: 1) the aforementioned abilty to specify the 
 *           uuencode filename                         *done*
 *       2) someway to do this without tmp files
 *              * wont happen anytime soon*
 *       3) MIME? *done*
 *       4) a pointlessly snazzier dialog
 *       5) make sure it works with gz     
 *               * works for .xcfgz but not .xcf.gz *
 *       6) add an option to choose if mail get 
 *          uuencode or not (or MIME'ed for that matter)
 *       7) realtime preview
 *       8) better entry for comments    *done*
 *       9) list of frequently used addreses     
 *      10) openGL compliance
 *      11) better handling of filesave errors
 *     
 *
 *  Version history
 *       .5  - 6/30/97 - inital relese
 *       .51 - 7/3/97  - fixed a few spelling errors and the like
 *       .65 - 7/4/97  - a fairly significant revision. changed it from a file
 *                       plugin to an image plugin.
 *                     - Changed some strcats into strcpy to be a bit more robust.
 *                     - added the abilty to specify the filename you want it sent as
 *                     - no more annoying hassles with the file saves as dialog
 *                     - plugin now registers itself as <image>/File/Mail image
 *       .7  - 9/12/97 - (RB) added support for MIME encapsulation
 *       .71 - 9/17/97 - (RB) included Base64 encoding functions from mpack
 *                       instead of using external program.
 *                     - General cleanup of the MIME handling code.
 *       .80 - 6/23/98 - Added a text box so you can compose real messages.
 *       .85 - 3/19/99 - Added a "From:" field. Made it check gimprc for a
 *                       "gump-from" token and use it. Also made "run with last 
 *                        values" work.
 * As always: The utility of this plugin is left as an exercise for the reader
 *
Elliot Lee's avatar
Elliot Lee committed
92 93 94 95 96
 */

#define ENCAPSULATION_UUENCODE 0
#define ENCAPSULATION_MIME     1

97 98
#define BUFFER_SIZE            256

99 100
#include "config.h"

101 102 103 104 105 106 107 108
#ifndef SENDMAIL
#define SENDMAIL "/usr/lib/sendmail"
#endif

#ifndef UUENCODE
#define UUENCODE "uuencode"
#endif

Elliot Lee's avatar
Elliot Lee committed
109 110
#include <stdlib.h>
#include <stdio.h>
111
#include <sys/types.h>
Elliot Lee's avatar
Elliot Lee committed
112 113 114 115 116 117
#include <sys/param.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
118 119 120 121
#ifdef __EMX__
#include <fcntl.h>
#include <process.h>
#endif
122 123 124

#include <gtk/gtk.h>

125 126
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
Elliot Lee's avatar
Elliot Lee committed
127

128
#include "libgimp/stdplugins-intl.h"
Elliot Lee's avatar
Elliot Lee committed
129

130

131
static void   query (void);
132 133
static void   run   (gchar      *name,
		     gint        nparams,
Sven Neumann's avatar
Sven Neumann committed
134
		     GimpParam  *param,
135
		     gint       *nreturn_vals,
Sven Neumann's avatar
Sven Neumann committed
136
		     GimpParam **return_vals);
137

Sven Neumann's avatar
Sven Neumann committed
138
static GimpPDBStatusType save_image (gchar  *filename,
139 140 141
			             gint32  image_ID,
			             gint32  drawable_ID,
				     gint32  run_mode);
142 143

static gint   save_dialog          (void);
144 145 146 147 148 149
static void   ok_callback          (GtkWidget     *widget,
				    gpointer       data);
static void   mail_entry_callback  (GtkWidget     *widget,
				    gpointer       data);
static void   mesg_body_callback   (GtkTextBuffer *buffer,
				    gpointer       data);
150 151

static gint   valid_file     (gchar *filename);
152
static void   create_headers (FILE  *mailpipe);
153
static char * find_extension (gchar *filename);
154 155 156 157 158 159 160
static gint   to64           (FILE  *infile,
			      FILE  *outfile);
static void   output64chunk  (gint   c1,
			      gint   c2,
			      gint   c3,
			      gint   pads,
			      FILE  *outfile);
Elliot Lee's avatar
Elliot Lee committed
161

Sven Neumann's avatar
Sven Neumann committed
162
GimpPlugInInfo PLUG_IN_INFO =
Elliot Lee's avatar
Elliot Lee committed
163
{
164 165 166 167
  NULL,  /* init_proc  */
  NULL,  /* quit_proc  */
  query, /* query_proc */
  run,   /* run_proc   */
Elliot Lee's avatar
Elliot Lee committed
168 169 170
};

typedef struct
171
{
172 173 174 175 176
  gchar receipt[BUFFER_SIZE];
  gchar subject[BUFFER_SIZE];
  gchar comment[BUFFER_SIZE];
  gchar from[BUFFER_SIZE];
  gchar filename[BUFFER_SIZE];
177 178
  gint  encapsulation;
}
Elliot Lee's avatar
Elliot Lee committed
179 180
m_info;

181 182
static m_info mail_info =
{
Elliot Lee's avatar
Elliot Lee committed
183 184 185 186 187
  /* I would a assume there is a better way to do this, but this works for now */
  "\0",
  "\0",
  "\0",
  "\0",
188
  "\0",
Elliot Lee's avatar
Elliot Lee committed
189 190 191 192
  ENCAPSULATION_MIME,  /* Change this to ENCAPSULATION_UUENCODE
			  if you prefer that as the default */
};

193
static gchar * mesg_body = NULL;
194
static gint    run_flag  = 0;
Elliot Lee's avatar
Elliot Lee committed
195 196 197 198

MAIN ()

static void
199
query (void)
Elliot Lee's avatar
Elliot Lee committed
200
{
Sven Neumann's avatar
Sven Neumann committed
201
  static GimpParamDef args[] =
Elliot Lee's avatar
Elliot Lee committed
202
  {
Sven Neumann's avatar
Sven Neumann committed
203 204 205 206 207 208 209 210 211
    { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
    { GIMP_PDB_IMAGE, "image", "Input image" },
    { GIMP_PDB_DRAWABLE, "drawable", "Drawable to save" },
    { GIMP_PDB_STRING, "filename", "The name of the file to save the image in" },
    { GIMP_PDB_STRING, "receipt", "The email address to send to" },
    { GIMP_PDB_STRING, "from", "The email address for the From: field" },
    { GIMP_PDB_STRING, "subject", "The subject" },
    { GIMP_PDB_STRING, "comment", "The Comment" },
    { GIMP_PDB_INT32,  "encapsulation", "Uuencode, MIME" }
Elliot Lee's avatar
Elliot Lee committed
212
  };
213

Elliot Lee's avatar
Elliot Lee committed
214
  gimp_install_procedure ("plug_in_mail_image",
Marc Lehmann's avatar
Marc Lehmann committed
215 216
			  "pipe files to uuencode then mail them",
			  "You need to have uuencode and mail installed",
Elliot Lee's avatar
Elliot Lee committed
217 218 219
			  "Adrian Likins, Reagan Blundell",
			  "Adrian Likins, Reagan Blundell, Daniel Risacher, Spencer Kimball and Peter Mattis",
			  "1995-1997",
220
			  N_("<Image>/File/Mail Image..."),
Elliot Lee's avatar
Elliot Lee committed
221
			  "RGB*, GRAY*, INDEXED*",
Sven Neumann's avatar
Sven Neumann committed
222
			  GIMP_PLUGIN,
223
			  G_N_ELEMENTS (args), 0,
224
			  args, NULL);
Elliot Lee's avatar
Elliot Lee committed
225 226 227
}

static void
228 229
run (gchar      *name,
     gint        nparams,
Sven Neumann's avatar
Sven Neumann committed
230
     GimpParam  *param,
231
     gint       *nreturn_vals,
Sven Neumann's avatar
Sven Neumann committed
232
     GimpParam **return_vals)
Elliot Lee's avatar
Elliot Lee committed
233
{
234
  static GimpParam   values[2];
235
  GimpRunMode        run_mode;
236
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
237 238
  gint32             image_ID;
  gint32             drawable_ID;
Elliot Lee's avatar
Elliot Lee committed
239

Manish Singh's avatar
Manish Singh committed
240
  INIT_I18N_UI();
Elliot Lee's avatar
Elliot Lee committed
241

242 243
  run_mode    = param[0].data.d_int32;
  image_ID    = param[1].data.d_image;
Elliot Lee's avatar
Elliot Lee committed
244 245
  drawable_ID = param[2].data.d_drawable;

246 247
  *nreturn_vals = 1;
  *return_vals  = values;
248

Sven Neumann's avatar
Sven Neumann committed
249 250
  values[0].type          = GIMP_PDB_STATUS;
  values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
Elliot Lee's avatar
Elliot Lee committed
251 252 253 254 255

  if (strcmp (name, "plug_in_mail_image") == 0)
    {
      switch (run_mode)
	{
Sven Neumann's avatar
Sven Neumann committed
256
	case GIMP_RUN_INTERACTIVE:
Elliot Lee's avatar
Elliot Lee committed
257 258
	  gimp_get_data ("plug_in_mail_image", &mail_info);
	  if (!save_dialog ())
Sven Neumann's avatar
Sven Neumann committed
259
	    status = GIMP_PDB_CANCEL;
Elliot Lee's avatar
Elliot Lee committed
260
	  break;
261

Sven Neumann's avatar
Sven Neumann committed
262
	case GIMP_RUN_NONINTERACTIVE:
Elliot Lee's avatar
Elliot Lee committed
263
	  /*  Make sure all the arguments are there!  */
264
	  if (nparams != 9)
265
	    {
Sven Neumann's avatar
Sven Neumann committed
266
	      status = GIMP_PDB_CALLING_ERROR;
267 268
	    }
	  else
Elliot Lee's avatar
Elliot Lee committed
269 270
	    {
	      /* this hasnt been tested yet */
271 272 273 274 275
	      strncpy (mail_info.filename, param[3].data.d_string, BUFFER_SIZE);
	      strncpy (mail_info.receipt, param[4].data.d_string, BUFFER_SIZE);
	      strncpy (mail_info.receipt, param[5].data.d_string, BUFFER_SIZE);
	      strncpy (mail_info.subject, param[6].data.d_string, BUFFER_SIZE);
	      strncpy (mail_info.comment, param[7].data.d_string, BUFFER_SIZE);
276
	      mail_info.encapsulation = param[8].data.d_int32;
Elliot Lee's avatar
Elliot Lee committed
277 278
	    }
	  break;
279

Sven Neumann's avatar
Sven Neumann committed
280
	case GIMP_RUN_WITH_LAST_VALS:
Elliot Lee's avatar
Elliot Lee committed
281 282 283 284 285 286 287
	  gimp_get_data ("plug_in_mail_image", &mail_info);
	  break;
	  
	default:
	  break;
	}

Sven Neumann's avatar
Sven Neumann committed
288
      if (status == GIMP_PDB_SUCCESS)
Elliot Lee's avatar
Elliot Lee committed
289
	{
290 291 292 293 294
	  status = save_image (mail_info.filename,
			       image_ID,
			       drawable_ID,
			       run_mode);

Sven Neumann's avatar
Sven Neumann committed
295
	  if (status == GIMP_PDB_SUCCESS)
296
	    gimp_set_data ("plug_in_mail_image", &mail_info, sizeof(m_info));
Elliot Lee's avatar
Elliot Lee committed
297 298 299
	}
    }
  else
300
    {
Sven Neumann's avatar
Sven Neumann committed
301
      status = GIMP_PDB_CALLING_ERROR;
302 303 304
    }

  values[0].data.d_status = status;
Elliot Lee's avatar
Elliot Lee committed
305 306
}

Sven Neumann's avatar
Sven Neumann committed
307
static GimpPDBStatusType
308 309 310 311
save_image (gchar  *filename,
	    gint32  image_ID,
	    gint32  drawable_ID,
	    gint32  run_mode)
Elliot Lee's avatar
Elliot Lee committed
312
{
313 314 315 316
  gchar  *ext;
  gchar  *tmpname;
  gchar   mailcmdline[512];
  gint    pid;
317
  gint    wpid;
318 319 320
  gint    process_status;
  FILE   *mailpipe;
  FILE   *infile;
Elliot Lee's avatar
Elliot Lee committed
321 322

  if (NULL == (ext = find_extension (filename)))
Sven Neumann's avatar
Sven Neumann committed
323
    return GIMP_PDB_CALLING_ERROR;
Elliot Lee's avatar
Elliot Lee committed
324 325

  /* get a temp name with the right extension and save into it. */
326
  tmpname = gimp_temp_name (ext + 1);
Elliot Lee's avatar
Elliot Lee committed
327 328

  /* construct the "sendmail user@location" line */
329
  strcpy (mailcmdline, SENDMAIL);
Elliot Lee's avatar
Elliot Lee committed
330 331 332 333
  strcat (mailcmdline, " ");
  strcat (mailcmdline, mail_info.receipt);

  /* create a pipe to sendmail */
334
#ifndef __EMX__
Elliot Lee's avatar
Elliot Lee committed
335
  mailpipe = popen (mailcmdline, "w");
336 337 338
#else
  mailpipe = popen (mailcmdline, "wb");
#endif
Elliot Lee's avatar
Elliot Lee committed
339 340 341 342 343
  create_headers (mailpipe);
  
  /* This is necessary to make the comments and headers work correctly. Not real sure why */
  fflush (mailpipe);      

344 345 346 347 348
  if (! (gimp_file_save (run_mode,
			 image_ID,
			 drawable_ID,
			 tmpname, 
			 tmpname) && valid_file (tmpname)) )
Elliot Lee's avatar
Elliot Lee committed
349 350
    {
      unlink (tmpname);
351
      g_free (tmpname);
352
      return GIMP_PDB_EXECUTION_ERROR;
Elliot Lee's avatar
Elliot Lee committed
353 354
    }

355 356
  if (mail_info.encapsulation == ENCAPSULATION_UUENCODE)
    {
357
#ifndef __EMX__
Elliot Lee's avatar
Elliot Lee committed
358 359
      /* fork off a uuencode process */
      if ((pid = fork ()) < 0)
360 361 362
	{
	  g_message ("mail: fork failed: %s\n", g_strerror (errno));
	  g_free (tmpname);
Sven Neumann's avatar
Sven Neumann committed
363
	  return GIMP_PDB_EXECUTION_ERROR;
364
	}
Elliot Lee's avatar
Elliot Lee committed
365
      else if (pid == 0)
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
	{
	  if (-1 == dup2 (fileno (mailpipe), fileno (stdout)))
	    {
	      g_message ("mail: dup2 failed: %s\n", g_strerror (errno));
	    }

	  execlp (UUENCODE, UUENCODE, tmpname, filename, NULL);
	  /* What are we doing here? exec must have failed */
	  g_message ("mail: exec failed: uuencode: %s\n", g_strerror (errno));

	  /* close the pipe now */
	  pclose (mailpipe);
	  g_free (tmpname);
	  _exit (127);
	}
Elliot Lee's avatar
Elliot Lee committed
381
      else
382 383 384 385 386 387 388
#else /* __EMX__ */
      int tfd;
      /* save fileno(stdout) */
      tfd = dup (fileno (stdout));
      if (dup2 (fileno (mailpipe), fileno (stdout)) == -1)
	{
	  g_message ("mail: dup2 failed: %s\n", g_strerror (errno));
389
	  close (tfd);
390
	  g_free (tmpname);
Sven Neumann's avatar
Sven Neumann committed
391
	  return GIMP_PDB_EXECUTION_ERROR;
392
	}
393
      fcntl (tfd, F_SETFD, FD_CLOEXEC);
394 395 396
      pid = spawnlp (P_NOWAIT, UUENCODE, UUENCODE, tmpname, filename, NULL);
      /* restore fileno(stdout) */
      dup2 (tfd, fileno (stdout));
397
      close (tfd);
398 399 400
      if (pid == -1)
	{
	  g_message ("mail: spawn failed: %s\n", g_strerror (errno));
401
	  g_free (tmpname);
Sven Neumann's avatar
Sven Neumann committed
402
	  return GIMP_PDB_EXECUTION_ERROR;
403 404
	}
#endif
405
        {
406
	  wpid = waitpid (pid, &process_status, 0);
407

408 409 410
	  if ((wpid < 0)
	      || !WIFEXITED (process_status)
	      || (WEXITSTATUS (process_status) != 0))
411 412 413
	    {
	      g_message ("mail: mail didnt work or something on file %s\n", tmpname);
	      g_free (tmpname);
Sven Neumann's avatar
Sven Neumann committed
414
	      return GIMP_PDB_EXECUTION_ERROR;
415 416 417 418 419
	    }
	}
    }
  else
    {  /* This must be MIME stuff. Base64 away... */
Elliot Lee's avatar
Elliot Lee committed
420 421 422
      infile = fopen(tmpname,"r");
      to64(infile,mailpipe);
      /* close off mime */
423 424 425 426 427 428
      if( mail_info.encapsulation == ENCAPSULATION_MIME )
	{
	  fprintf (mailpipe, "\n--GUMP-MIME-boundary--\n");
	}
    }

Elliot Lee's avatar
Elliot Lee committed
429 430
  /* delete the tmpfile that was generated */
  unlink (tmpname);
431 432
  g_free (tmpname);

Sven Neumann's avatar
Sven Neumann committed
433
  return GIMP_PDB_SUCCESS;
Elliot Lee's avatar
Elliot Lee committed
434 435 436 437
}


static gint
438
save_dialog (void)
Elliot Lee's avatar
Elliot Lee committed
439
{
440 441 442 443 444 445 446 447
  GtkWidget     *dlg;
  GtkWidget     *entry;
  GtkWidget     *table;
  GtkWidget     *label;
  GtkWidget     *vbox;
  GtkWidget     *scrolled_window;
  GtkWidget     *text_view;
  GtkTextBuffer *text_buffer;
Elliot Lee's avatar
Elliot Lee committed
448

449 450
  gchar      buffer[BUFFER_SIZE];
  gchar     *gump_from;
Elliot Lee's avatar
Elliot Lee committed
451

452
  gimp_ui_init ("mail", FALSE);
Elliot Lee's avatar
Elliot Lee committed
453

454
  /* check gimprc for a preffered "From:" address */
455
  gump_from = gimp_gimprc_query ("gump-from");         
456

457 458 459 460 461
  if (gump_from)
    {
      strncpy (mail_info.from, gump_from, BUFFER_SIZE);
      g_free (gump_from);
    }
462 463

  dlg = gimp_dialog_new (_("Send to Mail"), "mail",
464
			 gimp_standard_help_func, "filters/mail.html",
465 466 467
			 GTK_WIN_POS_MOUSE,
			 FALSE, TRUE, FALSE,

468
			 GTK_STOCK_CANCEL, gtk_widget_destroy,
469
			 NULL, 1, NULL, FALSE, TRUE,
Sven Neumann's avatar
Sven Neumann committed
470 471
			 GTK_STOCK_OK, ok_callback,
			 NULL, NULL, NULL, TRUE, FALSE,
472 473 474

			 NULL);

475 476 477
  g_signal_connect (G_OBJECT (dlg), "destroy",
                    G_CALLBACK (gtk_main_quit),
                    NULL);
Elliot Lee's avatar
Elliot Lee committed
478 479

  /* table */
Sven Neumann's avatar
Sven Neumann committed
480
  table = gtk_table_new (7, 2, FALSE);
481
  gtk_container_set_border_width (GTK_CONTAINER (table), 6);
Elliot Lee's avatar
Elliot Lee committed
482 483 484
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), table, TRUE, TRUE, 0);
  gtk_widget_show (table);

Sven Neumann's avatar
Sven Neumann committed
485 486
  gtk_table_set_row_spacings (GTK_TABLE (table), 4);
  gtk_table_set_col_spacings (GTK_TABLE (table), 4);
Elliot Lee's avatar
Elliot Lee committed
487 488 489

  /* to: dialog */
  entry = gtk_entry_new ();
490
  gtk_widget_set_size_request (entry, 200, -1);
491
  g_snprintf (buffer, sizeof (buffer), "%s", mail_info.receipt);
Elliot Lee's avatar
Elliot Lee committed
492
  gtk_entry_set_text (GTK_ENTRY (entry), buffer);
493
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
Sven Neumann's avatar
Sven Neumann committed
494
			     _("Recipient:"), 1.0, 0.5,
495
			     entry, 1, FALSE);
496 497 498
  g_signal_connect (G_OBJECT (entry), "changed",
                    G_CALLBACK (mail_entry_callback),
                    &mail_info.receipt);
499

Sven Neumann's avatar
Sven Neumann committed
500
  /* From entry */
501
  entry = gtk_entry_new ();
502
  gtk_widget_set_size_request (entry, 200, -1);
503
  g_snprintf (buffer, sizeof (buffer), "%s", mail_info.from);
504
  gtk_entry_set_text (GTK_ENTRY (entry), buffer);
505
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
Sven Neumann's avatar
Sven Neumann committed
506
			     _("Sender:"), 1.0, 0.5,
507
			     entry, 1, FALSE);
508 509 510
  g_signal_connect (G_OBJECT (entry), "changed",
                    G_CALLBACK (mail_entry_callback),
                    &mail_info.from);
Elliot Lee's avatar
Elliot Lee committed
511 512 513

  /* Subject entry */
  entry = gtk_entry_new ();
514
  gtk_widget_set_size_request (entry, 200, -1);
515
  g_snprintf (buffer, sizeof (buffer), "%s", mail_info.subject);
Elliot Lee's avatar
Elliot Lee committed
516
  gtk_entry_set_text (GTK_ENTRY (entry), buffer);
517
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 2,
518
			     _("Subject:"), 1.0, 0.5,
519
			     entry, 1, FALSE);
520 521 522
  g_signal_connect (G_OBJECT (entry), "changed",
                    G_CALLBACK (mail_entry_callback),
                    &mail_info.subject);
Elliot Lee's avatar
Elliot Lee committed
523

Sven Neumann's avatar
Sven Neumann committed
524
  /* Comment entry */
Elliot Lee's avatar
Elliot Lee committed
525
  entry = gtk_entry_new ();
526
  gtk_widget_set_size_request (entry, 200, -1);
527
  g_snprintf (buffer, sizeof (buffer), "%s", mail_info.comment);
Elliot Lee's avatar
Elliot Lee committed
528
  gtk_entry_set_text (GTK_ENTRY (entry), buffer);
529
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 3,
530
			     _("Comment:"), 1.0, 0.5,
531
			     entry, 1, FALSE);
532 533 534
  g_signal_connect (G_OBJECT (entry), "changed",
                    G_CALLBACK (mail_entry_callback),
                    &mail_info.comment);
Elliot Lee's avatar
Elliot Lee committed
535

Sven Neumann's avatar
Sven Neumann committed
536
  /* Filename entry */
Elliot Lee's avatar
Elliot Lee committed
537
  entry = gtk_entry_new ();
538
  gtk_widget_set_size_request (entry, 200, -1);
539
  g_snprintf (buffer, sizeof (buffer), "%s", mail_info.filename);
Elliot Lee's avatar
Elliot Lee committed
540
  gtk_entry_set_text (GTK_ENTRY (entry), buffer);
541
  gimp_table_attach_aligned (GTK_TABLE (table), 0, 4,
542
			     _("Filename:"), 1.0, 0.5,
543
			     entry, 1, FALSE);
544 545 546
  g_signal_connect (G_OBJECT (entry), "changed",
                    G_CALLBACK (mail_entry_callback),
                    &mail_info.filename);
Elliot Lee's avatar
Elliot Lee committed
547

548
  /* comment  */
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_AUTOMATIC);
  gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), 4);
  gtk_table_attach (GTK_TABLE (table), scrolled_window,
                    0, 2, 5, 6,
                    GTK_EXPAND | GTK_FILL,
                    GTK_EXPAND | GTK_FILL,
                    0, 0);
  gtk_widget_show (scrolled_window);

  text_buffer = gtk_text_buffer_new (NULL);

  text_view = gtk_text_view_new_with_buffer (text_buffer);
  gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (text_view), GTK_WRAP_WORD);
  gtk_container_add (GTK_CONTAINER (scrolled_window), text_view);
  gtk_widget_show (text_view);

  g_object_unref (G_OBJECT (text_buffer));

  g_signal_connect (G_OBJECT (text_buffer), "changed",
                    G_CALLBACK (mesg_body_callback),
		    NULL);
573

Elliot Lee's avatar
Elliot Lee committed
574
  /* Encapsulation label */
575
  label = gtk_label_new (_("Encapsulation:"));
576
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
577 578
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 6, 7,
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
Sven Neumann's avatar
Sven Neumann committed
579
  gtk_widget_show (label);
Elliot Lee's avatar
Elliot Lee committed
580

581 582
  /* Encapsulation radiobuttons */
  vbox = gimp_radio_group_new2 (FALSE, NULL,
583
				G_CALLBACK (gimp_radio_button_update),
584 585
				&mail_info.encapsulation,
				(gpointer) mail_info.encapsulation,
Elliot Lee's avatar
Elliot Lee committed
586

587 588 589 590 591 592 593
				_("Uuencode"),
				(gpointer) ENCAPSULATION_UUENCODE, NULL,
				_("MIME"),
				(gpointer) ENCAPSULATION_MIME, NULL,

				NULL);
  gtk_table_attach (GTK_TABLE (table), vbox, 1, 2, 6, 8,
594
		    GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
595
  gtk_widget_show (vbox);
Elliot Lee's avatar
Elliot Lee committed
596 597

  gtk_widget_show (dlg);
598

Elliot Lee's avatar
Elliot Lee committed
599 600 601
  gtk_main ();
  gdk_flush ();

602
  return run_flag;
Elliot Lee's avatar
Elliot Lee committed
603 604
}

605 606
static gint 
valid_file (gchar *filename)
Elliot Lee's avatar
Elliot Lee committed
607 608 609 610 611 612 613 614 615 616 617 618
{
  int stat_res;
  struct stat buf;

  stat_res = stat (filename, &buf);

  if ((0 == stat_res) && (buf.st_size > 0))
    return 1;
  else
    return 0;
}

619
static gchar *
620
find_content_type (gchar *filename)
Elliot Lee's avatar
Elliot Lee committed
621
{
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
  /* This function returns a MIME Content-type: value based on the
     filename it is given.  */
  gchar *type_mappings[20] =
  {
    "gif" , "image/gif",
    "jpg" , "image/jpeg",
    "jpeg", "image/jpeg",
    "tif" , "image/tiff",
    "tiff", "image/tiff",
    "png" , "image/png",
    "g3"  , "image/g3fax",
    "ps", "application/postscript",
    "eps", "application/postscript",
    NULL, NULL
  };

  gchar *ext;
  gchar *mimetype = malloc(100);
  gint i=0;

  ext = find_extension (filename);
  if (!ext)
    {
      strcpy (mimetype, "application/octet-stream");
      return mimetype;
Elliot Lee's avatar
Elliot Lee committed
647
    }
648 649 650 651 652 653 654

  while (type_mappings[i])
    {
      if (strcmp (ext+1, type_mappings[i]) == 0)
	{
	  strcpy (mimetype, type_mappings[i + 1]);
	  return mimetype;
Elliot Lee's avatar
Elliot Lee committed
655
	}
656
      i += 2;
Elliot Lee's avatar
Elliot Lee committed
657
    }
658 659 660
  strcpy (mimetype, "image/x-");
  strncat (mimetype, ext + 1, 91);
  mimetype[99] = '\0';
Elliot Lee's avatar
Elliot Lee committed
661

662
  return mimetype;
Elliot Lee's avatar
Elliot Lee committed
663 664
}

665 666
static gchar *
find_extension (gchar *filename)
Elliot Lee's avatar
Elliot Lee committed
667
{
668 669 670 671 672 673 674 675
  gchar *filename_copy;
  gchar *ext;

  /* this whole routine needs to be redone so it works for xccfgz and gz files*/
  /* not real sure where to start......                                       */
  /* right now saving for .xcfgz works but not .xcf.gz                        */
  /* this is all pretty close to straight from gz. It needs to be changed to  */
  /* work better for this plugin                                              */
Elliot Lee's avatar
Elliot Lee committed
676 677 678 679 680 681 682 683 684 685 686 687 688
  /* ie, FIXME */

  /* we never free this copy - aren't we evil! */
  filename_copy = malloc (strlen (filename) + 1);
  strcpy (filename_copy, filename);

  /* find the extension, boy! */
  ext = strrchr (filename_copy, '.');

  while (1)
    {
      if (!ext || ext[1] == 0 || strchr (ext, '/'))
	{
689
	  g_message (_("mail: some sort of error with the file extension or lack thereof \n"));
Elliot Lee's avatar
Elliot Lee committed
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
	  
	  return NULL;
	}
      if (0 != strcmp(ext,".gz"))
	{ 
	  return ext;
	}
      else
	{
	  /* we found somehting, loop back, and look again */
	  *ext = 0;
	  ext = strrchr (filename_copy, '.');
	}
    }
  return ext;
}

static void
708 709
ok_callback (GtkWidget *widget,
	     gpointer   data)
Elliot Lee's avatar
Elliot Lee committed
710
{
711
  run_flag = TRUE;
712
  
Elliot Lee's avatar
Elliot Lee committed
713 714 715 716
  gtk_widget_destroy (GTK_WIDGET (data));
}

static void
717 718
mail_entry_callback (GtkWidget *widget,
		     gpointer   data)
Elliot Lee's avatar
Elliot Lee committed
719
{
720
  strncpy ((gchar *) data, gtk_entry_get_text (GTK_ENTRY (widget)), BUFFER_SIZE);
Elliot Lee's avatar
Elliot Lee committed
721 722
}

723
static void 
724 725
mesg_body_callback (GtkTextBuffer *buffer,
		    gpointer       data)
726
{
727 728
  GtkTextIter start_iter;
  GtkTextIter end_iter;
729 730 731 732


  if (mesg_body)
    g_free (mesg_body);
733 734

  gtk_text_buffer_get_bounds (buffer, &start_iter, &end_iter);
735
  mesg_body = gtk_text_buffer_get_text (buffer, &start_iter, &end_iter, FALSE);
736 737
} 

Elliot Lee's avatar
Elliot Lee committed
738
static void
739
create_headers (FILE *mailpipe)
Elliot Lee's avatar
Elliot Lee committed
740 741 742 743 744 745 746 747
{
  /* create all the mail header stuff. Feel free to add your own */
  /* It is advisable to leave the X-Mailer header though, as     */
  /* there is a possibilty of a Gimp mail scanner/reader in the  */
  /* future. It will probabaly need that header.                 */

  fprintf (mailpipe, "To: %s \n", mail_info.receipt);
  fprintf (mailpipe, "Subject: %s \n", mail_info.subject);
748 749 750
  fprintf (mailpipe, "From: %s \n", mail_info.from);
  fprintf (mailpipe, "X-Mailer: GIMP Useless Mail Program v.85\n");

751 752
  if (mail_info.encapsulation == ENCAPSULATION_MIME )
    {
Elliot Lee's avatar
Elliot Lee committed
753 754
      fprintf (mailpipe, "MIME-Version: 1.0\n");
      fprintf (mailpipe, "Content-type: multipart/mixed; boundary=GUMP-MIME-boundary\n");
755
    }
Elliot Lee's avatar
Elliot Lee committed
756
  fprintf (mailpipe, "\n\n");
757 758
  if (mail_info.encapsulation == ENCAPSULATION_MIME )
    {
Elliot Lee's avatar
Elliot Lee committed
759 760
      fprintf (mailpipe, "--GUMP-MIME-boundary\n");
      fprintf (mailpipe, "Content-type: text/plain; charset=US-ASCII\n\n");
761
    }
Elliot Lee's avatar
Elliot Lee committed
762 763
  fprintf (mailpipe, mail_info.comment);
  fprintf (mailpipe, "\n\n");
764 765
  if (mesg_body)
    fprintf (mailpipe, mesg_body); 
766
  fprintf (mailpipe, "\n\n");
767 768
  if (mail_info.encapsulation == ENCAPSULATION_MIME )
    {
Elliot Lee's avatar
Elliot Lee committed
769
      char *content;
770
      content = find_content_type (mail_info.filename);
Elliot Lee's avatar
Elliot Lee committed
771 772 773 774 775 776
      fprintf (mailpipe, "--GUMP-MIME-boundary\n");
      fprintf (mailpipe, "Content-type: %s\n",content);
      fprintf (mailpipe, "Content-transfer-encoding: base64\n");
      fprintf (mailpipe, "Content-disposition: attachment; filename=\"%s\"\n",mail_info.filename);
      fprintf (mailpipe, "Content-description: %s\n\n",mail_info.filename);
      free(content);
777
    }
Elliot Lee's avatar
Elliot Lee committed
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
}

/*
 * The following code taken from codes.c in the mpack-1.5 distribution
 * by Carnegie Mellon University. 
 * 
 *
 * (C) Copyright 1993,1994 by Carnegie Mellon University
 * All Rights Reserved.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of Carnegie
 * Mellon University not be used in advertising or publicity
 * pertaining to distribution of the software without specific,
 * written prior permission.  Carnegie Mellon University makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */
/*
Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)

Permission to use, copy, modify, and distribute this material 
for any purpose and without fee is hereby granted, provided 
that the above copyright notice and this permission notice 
appear in all copies, and that the name of Bellcore not be 
used in advertising or publicity pertaining to this 
material without the specific, prior written permission 
of an authorized representative of Bellcore.  BELLCORE 
MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY 
OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS", 
WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.  */


824 825
static gchar basis_64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
Elliot Lee's avatar
Elliot Lee committed
826

827 828 829
static gint
to64 (FILE *infile,
      FILE *outfile) 
Elliot Lee's avatar
Elliot Lee committed
830
{
831
  gint c1, c2, c3, ct = 0, written = 0;
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849

  while ((c1 = getc (infile)) != EOF)
    {
      c2 = getc (infile);
      if (c2 == EOF)
	{
	  output64chunk (c1, 0, 0, 2, outfile);
        }
      else
	{
	  c3 = getc (infile);
	  if (c3 == EOF)
	    {
	      output64chunk (c1, c2, 0, 1, outfile);
            }
	  else
	    {
	      output64chunk (c1, c2, c3, 0, outfile);
Elliot Lee's avatar
Elliot Lee committed
850 851
            }
        }
852 853 854 855 856 857
      ct += 4;
      if (ct > 71)
	{
	  putc ('\n', outfile);
	  written += 73;
	  ct = 0;
Elliot Lee's avatar
Elliot Lee committed
858 859
        }
    }
860 861 862 863
  if (ct)
    {
      putc ('\n', outfile);
      ct++;
Elliot Lee's avatar
Elliot Lee committed
864
    }
865 866

  return written + ct;
Elliot Lee's avatar
Elliot Lee committed
867 868 869
}

static void
870 871 872 873 874
output64chunk (gint  c1,
	       gint  c2,
	       gint  c3,
	       gint  pads,
	       FILE *outfile)
Elliot Lee's avatar
Elliot Lee committed
875
{
876 877
  putc (basis_64[c1>>2], outfile);
  putc (basis_64[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)], outfile);
878

879 880 881 882 883 884 885 886 887 888 889 890 891 892
  if (pads == 2)
    {
      putc ('=', outfile);
      putc ('=', outfile);
    }
  else if (pads)
    {
      putc (basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);
      putc ('=', outfile);
    }
  else
    {
      putc (basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);
      putc (basis_64[c3 & 0x3F], outfile);
Elliot Lee's avatar
Elliot Lee committed
893 894
    }
}