home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / common / mail.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-10  |  26.0 KB  |  890 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  * Copyright (C) 1997 Daniel Risacher 
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  */
  19.  
  20. /*
  21.  *   GUMP - Gimp Useless Mail Plugin (or Gump Useless Mail Plugin if you prefer)
  22.  *          version about .85 I would say... give or take a few decimal points
  23.  *
  24.  *
  25.  *   by Adrian Likins <adrian@gimp.org>
  26.  *      MIME encapsulation by Reagan Blundell <reagan@emails.net>
  27.  *
  28.  *
  29.  *
  30.  *   Based heavily on gz.c by Daniel Risacher
  31.  *
  32.  *     Lets you choose to send a image to the mail from the file save as dialog.
  33.  *      images are piped to uuencode and then to mail...
  34.  *
  35.  *
  36.  *   This works fine for .99.10. I havent actually tried it in combination with
  37.  *   the gz plugin, but it works with all other file types. I will eventually get
  38.  *   around to making sure it works with gz.
  39.  *
  40.  *  To use: 1) image->File->mail image
  41.  *          2) when the mail dialog popups up, fill it out. Only to: and filename are required
  42.  *             note: the filename needs to a type that the image can be saved as. otherwise
  43.  *                   you will just send an empty message.
  44.  *          3) click ok and it should be on its way
  45.  *
  46.  *
  47.  * NOTE: You probabaly need sendmail installed. If your sendmail is in an odd spot
  48.  *       you can change the #define below. If you use qmail or other MTA's, and this
  49.  *       works after changing the MAILER, let me know how well or what changes were
  50.  *       needed.
  51.  *
  52.  * NOTE: Uuencode is needed. If it is in the path, it should work fine as is. Other-
  53.  *       wise just change the UUENCODE.
  54.  *
  55.  *
  56.  * TODO: 1) the aforementioned abilty to specify the 
  57.  *           uuencode filename                         *done*
  58.  *       2) someway to do this without tmp files
  59.  *              * wont happen anytime soon*
  60.  *       3) MIME? *done*
  61.  *       4) a pointlessly snazzier dialog
  62.  *       5) make sure it works with gz     
  63.  *               * works for .xcfgz but not .xcf.gz *
  64.  *       6) add an option to choose if mail get 
  65.  *          uuencode or not (or MIME'ed for that matter)
  66.  *       7) realtime preview
  67.  *       8) better entry for comments    *done*
  68.  *       9) list of frequently used addreses     
  69.  *      10) openGL compliance
  70.  *      11) better handling of filesave errors
  71.  *     
  72.  *
  73.  *  Version history
  74.  *       .5  - 6/30/97 - inital relese
  75.  *       .51 - 7/3/97  - fixed a few spelling errors and the like
  76.  *       .65 - 7/4/97  - a fairly significant revision. changed it from a file
  77.  *                       plugin to an image plugin.
  78.  *                     - Changed some strcats into strcpy to be a bit more robust.
  79.  *                     - added the abilty to specify the filename you want it sent as
  80.  *                     - no more annoying hassles with the file saves as dialog
  81.  *                     - plugin now registers itself as <image>/File/Mail image
  82.  *       .7  - 9/12/97 - (RB) added support for MIME encapsulation
  83.  *       .71 - 9/17/97 - (RB) included Base64 encoding functions from mpack
  84.  *                       instead of using external program.
  85.  *                     - General cleanup of the MIME handling code.
  86.  *       .80 - 6/23/98 - Added a text box so you can compose real messages.
  87.  *       .85 - 3/19/99 - Added a "From:" field. Made it check gimprc for a
  88.  *                       "gump-from" token and use it. Also made "run with last 
  89.  *                        values" work.
  90.  * As always: The utility of this plugin is left as an exercise for the reader
  91.  *
  92.  */
  93. #ifndef MAILER
  94. #define MAILER "/usr/lib/sendmail"
  95. #endif
  96.  
  97. #ifndef UUENCODE
  98. #define UUENCODE "uuencode"
  99. #endif
  100.  
  101. #define ENCAPSULATION_UUENCODE 0
  102. #define ENCAPSULATION_MIME     1
  103.  
  104. #define BUFFER_SIZE            256
  105.  
  106. #include "config.h"
  107.  
  108. #include <stdlib.h>
  109. #include <stdio.h>
  110. #include <sys/types.h>
  111. #include <sys/param.h>
  112. #include <sys/wait.h>
  113. #include <sys/stat.h>
  114. #include <string.h>
  115. #include <unistd.h>
  116. #include <errno.h>
  117. #ifdef __EMX__
  118. #include <fcntl.h>
  119. #include <process.h>
  120. #endif
  121.  
  122. #include <gtk/gtk.h>
  123.  
  124. #include <libgimp/gimp.h>
  125. #include <libgimp/gimpui.h>
  126.  
  127. #include "libgimp/stdplugins-intl.h"
  128.  
  129.  
  130. static void   query (void);
  131. static void   run   (gchar      *name,
  132.              gint        nparams,
  133.              GimpParam  *param,
  134.              gint       *nreturn_vals,
  135.              GimpParam **return_vals);
  136.  
  137. static GimpPDBStatusType save_image (gchar  *filename,
  138.                          gint32  image_ID,
  139.                          gint32  drawable_ID,
  140.                      gint32  run_mode);
  141.  
  142. static gint   save_dialog          (void);
  143. static void   ok_callback          (GtkWidget *widget,
  144.                     gpointer   data);
  145. static void   mail_entry_callback  (GtkWidget *widget,
  146.                     gpointer   data);
  147. static void   mesg_body_callback   (GtkWidget *widget,
  148.                     gpointer   data);
  149.  
  150. static gint   valid_file     (gchar *filename);
  151. static void   create_headers (FILE  *mailpipe);
  152. static char * find_extension (gchar *filename);
  153. static gint   to64           (FILE  *infile,
  154.                   FILE  *outfile);
  155. static void   output64chunk  (gint   c1,
  156.                   gint   c2,
  157.                   gint   c3,
  158.                   gint   pads,
  159.                   FILE  *outfile);
  160.  
  161. GimpPlugInInfo PLUG_IN_INFO =
  162. {
  163.   NULL,  /* init_proc  */
  164.   NULL,  /* quit_proc  */
  165.   query, /* query_proc */
  166.   run,   /* run_proc   */
  167. };
  168.  
  169. typedef struct
  170. {
  171.   gchar receipt[BUFFER_SIZE];
  172.   gchar subject[BUFFER_SIZE];
  173.   gchar comment[BUFFER_SIZE];
  174.   gchar from[BUFFER_SIZE];
  175.   gchar filename[BUFFER_SIZE];
  176.   gint  encapsulation;
  177. }
  178. m_info;
  179.  
  180. static m_info mail_info =
  181. {
  182.   /* I would a assume there is a better way to do this, but this works for now */
  183.   "\0",
  184.   "\0",
  185.   "\0",
  186.   "\0",
  187.   "\0",
  188.   ENCAPSULATION_MIME,  /* Change this to ENCAPSULATION_UUENCODE
  189.               if you prefer that as the default */
  190. };
  191.  
  192. static gchar * mesg_body = "\0";
  193. static gint    run_flag  = 0;
  194.  
  195. MAIN ()
  196.  
  197. static void
  198. query (void)
  199. {
  200.   static GimpParamDef args[] =
  201.   {
  202.     { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
  203.     { GIMP_PDB_IMAGE, "image", "Input image" },
  204.     { GIMP_PDB_DRAWABLE, "drawable", "Drawable to save" },
  205.     { GIMP_PDB_STRING, "filename", "The name of the file to save the image in" },
  206.     { GIMP_PDB_STRING, "receipt", "The email address to send to" },
  207.     { GIMP_PDB_STRING, "from", "The email address for the From: field" },
  208.     { GIMP_PDB_STRING, "subject", "The subject" },
  209.     { GIMP_PDB_STRING, "comment", "The Comment" },
  210.     { GIMP_PDB_INT32,  "encapsulation", "Uuencode, MIME" }
  211.   };
  212.   static gint nargs = sizeof (args) / sizeof (args[0]);
  213.  
  214.   gimp_install_procedure ("plug_in_mail_image",
  215.               "pipe files to uuencode then mail them",
  216.               "You need to have uuencode and mail installed",
  217.               "Adrian Likins, Reagan Blundell",
  218.               "Adrian Likins, Reagan Blundell, Daniel Risacher, Spencer Kimball and Peter Mattis",
  219.               "1995-1997",
  220.               N_("<Image>/File/Mail Image..."),
  221.               "RGB*, GRAY*, INDEXED*",
  222.               GIMP_PLUGIN,
  223.               nargs, 0,
  224.               args, NULL);
  225. }
  226.  
  227. static void
  228. run (gchar      *name,
  229.      gint        nparams,
  230.      GimpParam  *param,
  231.      gint       *nreturn_vals,
  232.      GimpParam **return_vals)
  233. {
  234.   static GimpParam   values[2];
  235.   GimpRunModeType    run_mode;
  236.   GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
  237.   gint32  image_ID;
  238.   gint32  drawable_ID;
  239.  
  240.   INIT_I18N_UI();
  241.  
  242.   run_mode    = param[0].data.d_int32;
  243.   image_ID    = param[1].data.d_image;
  244.   drawable_ID = param[2].data.d_drawable;
  245.  
  246.   *nreturn_vals = 1;
  247.   *return_vals  = values;
  248.   values[0].type          = GIMP_PDB_STATUS;
  249.   values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
  250.  
  251.   if (strcmp (name, "plug_in_mail_image") == 0)
  252.     {
  253.       switch (run_mode)
  254.     {
  255.     case GIMP_RUN_INTERACTIVE:
  256.       gimp_get_data ("plug_in_mail_image", &mail_info);
  257.       if (!save_dialog ())
  258.         status = GIMP_PDB_CANCEL;
  259.       break;
  260.  
  261.     case GIMP_RUN_NONINTERACTIVE:
  262.       /*  Make sure all the arguments are there!  */
  263.       if (nparams != 9)
  264.         {
  265.           status = GIMP_PDB_CALLING_ERROR;
  266.         }
  267.       else
  268.         {
  269.           /* this hasnt been tested yet */
  270.           strncpy (mail_info.filename, param[3].data.d_string, BUFFER_SIZE);
  271.           strncpy (mail_info.receipt, param[4].data.d_string, BUFFER_SIZE);
  272.           strncpy (mail_info.receipt, param[5].data.d_string, BUFFER_SIZE);
  273.           strncpy (mail_info.subject, param[6].data.d_string, BUFFER_SIZE);
  274.           strncpy (mail_info.comment, param[7].data.d_string, BUFFER_SIZE);
  275.           mail_info.encapsulation = param[8].data.d_int32;
  276.         }
  277.       break;
  278.  
  279.     case GIMP_RUN_WITH_LAST_VALS:
  280.       gimp_get_data ("plug_in_mail_image", &mail_info);
  281.       break;
  282.       
  283.     default:
  284.       break;
  285.     }
  286.  
  287.       if (status == GIMP_PDB_SUCCESS)
  288.     {
  289.       status = save_image (mail_info.filename,
  290.                    image_ID,
  291.                    drawable_ID,
  292.                    run_mode);
  293.  
  294.       if (status == GIMP_PDB_SUCCESS)
  295.         gimp_set_data ("plug_in_mail_image", &mail_info, sizeof(m_info));
  296.     }
  297.     }
  298.   else
  299.     {
  300.       status = GIMP_PDB_CALLING_ERROR;
  301.     }
  302.  
  303.   values[0].data.d_status = status;
  304. }
  305.  
  306. static GimpPDBStatusType
  307. save_image (gchar  *filename,
  308.         gint32  image_ID,
  309.         gint32  drawable_ID,
  310.         gint32  run_mode)
  311. {
  312.   gchar  *ext;
  313.   gchar  *tmpname;
  314.   gchar   mailcmdline[512];
  315.   gint    pid;
  316.   gint    wpid;
  317.   gint    process_status;
  318.   FILE   *mailpipe;
  319.   FILE   *infile;
  320.  
  321.   if (NULL == (ext = find_extension (filename)))
  322.     return GIMP_PDB_CALLING_ERROR;
  323.  
  324.   /* get a temp name with the right extension and save into it. */
  325.   tmpname = gimp_temp_name (ext + 1);
  326.  
  327.   /* construct the "sendmail user@location" line */
  328.   strcpy (mailcmdline, MAILER);
  329.   strcat (mailcmdline, " ");
  330.   strcat (mailcmdline, mail_info.receipt);
  331.  
  332.   /* create a pipe to sendmail */
  333. #ifndef __EMX__
  334.   mailpipe = popen (mailcmdline, "w");
  335. #else
  336.   mailpipe = popen (mailcmdline, "wb");
  337. #endif
  338.   create_headers (mailpipe);
  339.   
  340.   /* This is necessary to make the comments and headers work correctly. Not real sure why */
  341.   fflush (mailpipe);      
  342.  
  343.   if (! (gimp_file_save (run_mode,
  344.              image_ID,
  345.              drawable_ID,
  346.              tmpname, 
  347.              tmpname) && valid_file (tmpname)) )
  348.     {
  349.       unlink (tmpname);
  350.       g_free (tmpname);
  351.       return GIMP_PDB_EXECUTION_ERROR;
  352.     }
  353.  
  354.   if (mail_info.encapsulation == ENCAPSULATION_UUENCODE)
  355.     {
  356. #ifndef __EMX__
  357.       /* fork off a uuencode process */
  358.       if ((pid = fork ()) < 0)
  359.     {
  360.       g_message ("mail: fork failed: %s\n", g_strerror (errno));
  361.       g_free (tmpname);
  362.       return GIMP_PDB_EXECUTION_ERROR;
  363.     }
  364.       else if (pid == 0)
  365.     {
  366.       if (-1 == dup2 (fileno (mailpipe), fileno (stdout)))
  367.         {
  368.           g_message ("mail: dup2 failed: %s\n", g_strerror (errno));
  369.         }
  370.  
  371.       execlp (UUENCODE, UUENCODE, tmpname, filename, NULL);
  372.       /* What are we doing here? exec must have failed */
  373.       g_message ("mail: exec failed: uuencode: %s\n", g_strerror (errno));
  374.  
  375.       /* close the pipe now */
  376.       pclose (mailpipe);
  377.       g_free (tmpname);
  378.       _exit (127);
  379.     }
  380.       else
  381. #else /* __EMX__ */
  382.       int tfd;
  383.       /* save fileno(stdout) */
  384.       tfd = dup (fileno (stdout));
  385.       if (dup2 (fileno (mailpipe), fileno (stdout)) == -1)
  386.     {
  387.       g_message ("mail: dup2 failed: %s\n", g_strerror (errno));
  388.       close (tfd);
  389.       g_free (tmpname);
  390.       return GIMP_PDB_EXECUTION_ERROR;
  391.     }
  392.       fcntl (tfd, F_SETFD, FD_CLOEXEC);
  393.       pid = spawnlp (P_NOWAIT, UUENCODE, UUENCODE, tmpname, filename, NULL);
  394.       /* restore fileno(stdout) */
  395.       dup2 (tfd, fileno (stdout));
  396.       close (tfd);
  397.       if (pid == -1)
  398.     {
  399.       g_message ("mail: spawn failed: %s\n", g_strerror (errno));
  400.       g_free (tmpname);
  401.       return GIMP_PDB_EXECUTION_ERROR;
  402.     }
  403. #endif
  404.         {
  405.       wpid = waitpid (pid, &process_status, 0);
  406.  
  407.       if ((wpid < 0)
  408.           || !WIFEXITED (process_status)
  409.           || (WEXITSTATUS (process_status) != 0))
  410.         {
  411.           g_message ("mail: mail didnt work or something on file %s\n", tmpname);
  412.           g_free (tmpname);
  413.           return GIMP_PDB_EXECUTION_ERROR;
  414.         }
  415.     }
  416.     }
  417.   else
  418.     {  /* This must be MIME stuff. Base64 away... */
  419.       infile = fopen(tmpname,"r");
  420.       to64(infile,mailpipe);
  421.       /* close off mime */
  422.       if( mail_info.encapsulation == ENCAPSULATION_MIME )
  423.     {
  424.       fprintf (mailpipe, "\n--GUMP-MIME-boundary--\n");
  425.     }
  426.     }
  427.  
  428.   /* delete the tmpfile that was generated */
  429.   unlink (tmpname);
  430.   g_free (tmpname);
  431.  
  432.   return GIMP_PDB_SUCCESS;
  433. }
  434.  
  435.  
  436. static gint
  437. save_dialog (void)
  438. {
  439.   GtkWidget *dlg;
  440.   GtkWidget *entry;
  441.   GtkWidget *table;
  442.   GtkWidget *table2;
  443.   GtkWidget *label;
  444.   GtkWidget *vbox;
  445.   GtkWidget *text;
  446.   GtkWidget *vscrollbar;
  447.  
  448.   gchar      buffer[BUFFER_SIZE];
  449.   gchar     *gump_from;
  450.  
  451.   gimp_ui_init ("mail", FALSE);
  452.  
  453.   /* check gimprc for a preffered "From:" address */
  454.   gump_from = gimp_gimprc_query ("gump-from");         
  455.  
  456.   if (gump_from)
  457.     {
  458.       strncpy (mail_info.from, gump_from, BUFFER_SIZE);
  459.       g_free (gump_from);
  460.     }
  461.  
  462.   dlg = gimp_dialog_new (_("Send to Mail"), "mail",
  463.              gimp_standard_help_func, "filters/mail.html",
  464.              GTK_WIN_POS_MOUSE,
  465.              FALSE, TRUE, FALSE,
  466.  
  467.              _("OK"), ok_callback,
  468.              NULL, NULL, NULL, TRUE, FALSE,
  469.              _("Cancel"), gtk_widget_destroy,
  470.              NULL, 1, NULL, FALSE, TRUE,
  471.  
  472.              NULL);
  473.  
  474.   gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
  475.               GTK_SIGNAL_FUNC (gtk_main_quit),
  476.               NULL);
  477.  
  478.   /* table */
  479.   table = gtk_table_new (7, 2, FALSE);
  480.   gtk_container_set_border_width (GTK_CONTAINER (table), 6);
  481.   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), table, TRUE, TRUE, 0);
  482.   gtk_widget_show (table);
  483.  
  484.   gtk_table_set_row_spacings (GTK_TABLE (table), 4);
  485.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  486.  
  487.   /* to: dialog */
  488.   entry = gtk_entry_new ();
  489.   gtk_widget_set_usize (entry, 200, 0);
  490.   g_snprintf (buffer, sizeof (buffer), "%s", mail_info.receipt);
  491.   gtk_entry_set_text (GTK_ENTRY (entry), buffer);
  492.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
  493.                  _("Recipient:"), 1.0, 0.5,
  494.                  entry, 1, FALSE);
  495.   gtk_signal_connect (GTK_OBJECT (entry), "changed",
  496.               GTK_SIGNAL_FUNC (mail_entry_callback),
  497.               &mail_info.receipt);
  498.  
  499.   /* From entry */
  500.   entry = gtk_entry_new ();
  501.   gtk_widget_set_usize (entry, 200, 0);
  502.   g_snprintf (buffer, sizeof (buffer), "%s", mail_info.from);
  503.   gtk_entry_set_text (GTK_ENTRY (entry), buffer);
  504.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
  505.                  _("Sender:"), 1.0, 0.5,
  506.                  entry, 1, FALSE);
  507.   gtk_signal_connect (GTK_OBJECT (entry), "changed",
  508.               GTK_SIGNAL_FUNC (mail_entry_callback),
  509.               &mail_info.from);
  510.  
  511.   /* Subject entry */
  512.   entry = gtk_entry_new ();
  513.   gtk_widget_set_usize (entry, 200, 0);
  514.   g_snprintf (buffer, sizeof (buffer), "%s", mail_info.subject);
  515.   gtk_entry_set_text (GTK_ENTRY (entry), buffer);
  516.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 2,
  517.                  _("Subject:"), 1.0, 0.5,
  518.                  entry, 1, FALSE);
  519.   gtk_signal_connect (GTK_OBJECT (entry), "changed",
  520.               GTK_SIGNAL_FUNC (mail_entry_callback),
  521.               &mail_info.subject);
  522.  
  523.   /* Comment entry */
  524.   entry = gtk_entry_new ();
  525.   gtk_widget_set_usize (entry, 200, 0);
  526.   g_snprintf (buffer, sizeof (buffer), "%s", mail_info.comment);
  527.   gtk_entry_set_text (GTK_ENTRY (entry), buffer);
  528.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 3,
  529.                  _("Comment:"), 1.0, 0.5,
  530.                  entry, 1, FALSE);
  531.   gtk_signal_connect (GTK_OBJECT (entry), "changed",
  532.               GTK_SIGNAL_FUNC (mail_entry_callback),
  533.               &mail_info.comment);
  534.  
  535.   /* Filename entry */
  536.   entry = gtk_entry_new ();
  537.   gtk_widget_set_usize (entry, 200, 0);
  538.   g_snprintf (buffer, sizeof (buffer), "%s", mail_info.filename);
  539.   gtk_entry_set_text (GTK_ENTRY (entry), buffer);
  540.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 4,
  541.                  _("Filename:"), 1.0, 0.5,
  542.                  entry, 1, FALSE);
  543.   gtk_signal_connect (GTK_OBJECT (entry), "changed",
  544.               GTK_SIGNAL_FUNC (mail_entry_callback),
  545.               &mail_info.filename);
  546.  
  547.   /* comment  */
  548.   table2 = gtk_table_new (2, 2, FALSE);
  549.   gtk_table_set_row_spacing (GTK_TABLE (table2), 0, 2);
  550.   gtk_table_set_col_spacing (GTK_TABLE (table2), 0, 2);
  551.  
  552.   gtk_table_attach (GTK_TABLE (table), table2, 
  553.             0, 2, 5, 6,
  554.             GTK_EXPAND | GTK_FILL,
  555.             GTK_EXPAND | GTK_FILL,
  556.             0, 0);
  557.  
  558.   text = gtk_text_new (NULL, NULL);
  559.   gtk_text_set_editable (GTK_TEXT (text), TRUE);
  560.   gtk_table_attach (GTK_TABLE (table2), text, 
  561.             0, 1, 0, 1,
  562.             GTK_EXPAND | GTK_FILL,
  563.             GTK_EXPAND | GTK_FILL,
  564.             0, 0);
  565.   gtk_widget_set_usize (text, 200, 100);
  566.   gtk_widget_show (text);
  567.   gtk_signal_connect (GTK_OBJECT (text), "changed",
  568.               GTK_SIGNAL_FUNC (mesg_body_callback),
  569.               mesg_body);
  570.   gtk_widget_show (table2);
  571.  
  572.   vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
  573.   gtk_table_attach (GTK_TABLE (table2), vscrollbar, 1, 2, 0, 1,
  574.             GTK_FILL, GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
  575.   gtk_widget_show (vscrollbar);
  576.  
  577.   /* Encapsulation label */
  578.   label = gtk_label_new (_("Encapsulation:"));
  579.   gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  580.   gtk_table_attach (GTK_TABLE (table), label, 0, 1, 6, 7,
  581.             GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  582.   gtk_widget_show (label);
  583.  
  584.   /* Encapsulation radiobuttons */
  585.   vbox = gimp_radio_group_new2 (FALSE, NULL,
  586.                 gimp_radio_button_update,
  587.                 &mail_info.encapsulation,
  588.                 (gpointer) mail_info.encapsulation,
  589.  
  590.                 _("Uuencode"),
  591.                 (gpointer) ENCAPSULATION_UUENCODE, NULL,
  592.                 _("MIME"),
  593.                 (gpointer) ENCAPSULATION_MIME, NULL,
  594.  
  595.                 NULL);
  596.   gtk_table_attach (GTK_TABLE (table), vbox, 1, 2, 6, 8,
  597.             GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  598.   gtk_widget_show (vbox);
  599.  
  600.   gtk_widget_show (dlg);
  601.  
  602.   gtk_main ();
  603.   gdk_flush ();
  604.  
  605.   return run_flag;
  606. }
  607.  
  608. static gint 
  609. valid_file (gchar *filename)
  610. {
  611.   int stat_res;
  612.   struct stat buf;
  613.  
  614.   stat_res = stat (filename, &buf);
  615.  
  616.   if ((0 == stat_res) && (buf.st_size > 0))
  617.     return 1;
  618.   else
  619.     return 0;
  620. }
  621.  
  622. static gchar *
  623. find_content_type (gchar *filename)
  624. {
  625.   /* This function returns a MIME Content-type: value based on the
  626.      filename it is given.  */
  627.   gchar *type_mappings[20] =
  628.   {
  629.     "gif" , "image/gif",
  630.     "jpg" , "image/jpeg",
  631.     "jpeg", "image/jpeg",
  632.     "tif" , "image/tiff",
  633.     "tiff", "image/tiff",
  634.     "png" , "image/png",
  635.     "g3"  , "image/g3fax",
  636.     "ps", "application/postscript",
  637.     "eps", "application/postscript",
  638.     NULL, NULL
  639.   };
  640.  
  641.   gchar *ext;
  642.   gchar *mimetype = malloc(100);
  643.   gint i=0;
  644.  
  645.   ext = find_extension (filename);
  646.   if (!ext)
  647.     {
  648.       strcpy (mimetype, "application/octet-stream");
  649.       return mimetype;
  650.     }
  651.  
  652.   while (type_mappings[i])
  653.     {
  654.       if (strcmp (ext+1, type_mappings[i]) == 0)
  655.     {
  656.       strcpy (mimetype, type_mappings[i + 1]);
  657.       return mimetype;
  658.     }
  659.       i += 2;
  660.     }
  661.   strcpy (mimetype, "image/x-");
  662.   strncat (mimetype, ext + 1, 91);
  663.   mimetype[99] = '\0';
  664.  
  665.   return mimetype;
  666. }
  667.  
  668. static gchar *
  669. find_extension (gchar *filename)
  670. {
  671.   gchar *filename_copy;
  672.   gchar *ext;
  673.  
  674.   /* this whole routine needs to be redone so it works for xccfgz and gz files*/
  675.   /* not real sure where to start......                                       */
  676.   /* right now saving for .xcfgz works but not .xcf.gz                        */
  677.   /* this is all pretty close to straight from gz. It needs to be changed to  */
  678.   /* work better for this plugin                                              */
  679.   /* ie, FIXME */
  680.  
  681.   /* we never free this copy - aren't we evil! */
  682.   filename_copy = malloc (strlen (filename) + 1);
  683.   strcpy (filename_copy, filename);
  684.  
  685.   /* find the extension, boy! */
  686.   ext = strrchr (filename_copy, '.');
  687.  
  688.   while (1)
  689.     {
  690.       if (!ext || ext[1] == 0 || strchr (ext, '/'))
  691.     {
  692.       g_message (_("mail: some sort of error with the file extension or lack thereof \n"));
  693.       
  694.       return NULL;
  695.     }
  696.       if (0 != strcmp(ext,".gz"))
  697.     { 
  698.       return ext;
  699.     }
  700.       else
  701.     {
  702.       /* we found somehting, loop back, and look again */
  703.       *ext = 0;
  704.       ext = strrchr (filename_copy, '.');
  705.     }
  706.     }
  707.   return ext;
  708. }
  709.  
  710. static void
  711. ok_callback (GtkWidget *widget,
  712.          gpointer   data)
  713. {
  714.   run_flag = TRUE;
  715.   
  716.   gtk_widget_destroy (GTK_WIDGET (data));
  717. }
  718.  
  719. static void
  720. mail_entry_callback (GtkWidget *widget,
  721.              gpointer   data)
  722. {
  723.   strncpy ((gchar *) data, gtk_entry_get_text (GTK_ENTRY (widget)), BUFFER_SIZE);
  724. }
  725.  
  726. static void 
  727. mesg_body_callback (GtkWidget *widget,
  728.             gpointer   data)
  729. {
  730.   mesg_body = gtk_editable_get_chars (GTK_EDITABLE (widget), 0,
  731.                       gtk_text_get_length (GTK_TEXT (widget)));
  732.  
  733. static void
  734. create_headers (FILE *mailpipe)
  735. {
  736.   /* create all the mail header stuff. Feel free to add your own */
  737.   /* It is advisable to leave the X-Mailer header though, as     */
  738.   /* there is a possibilty of a Gimp mail scanner/reader in the  */
  739.   /* future. It will probabaly need that header.                 */
  740.  
  741.   fprintf (mailpipe, "To: %s \n", mail_info.receipt);
  742.   fprintf (mailpipe, "Subject: %s \n", mail_info.subject);
  743.   fprintf (mailpipe, "From: %s \n", mail_info.from);
  744.   fprintf (mailpipe, "X-Mailer: GIMP Useless Mail Program v.85\n");
  745.  
  746.   if (mail_info.encapsulation == ENCAPSULATION_MIME )
  747.     {
  748.       fprintf (mailpipe, "MIME-Version: 1.0\n");
  749.       fprintf (mailpipe, "Content-type: multipart/mixed; boundary=GUMP-MIME-boundary\n");
  750.     }
  751.   fprintf (mailpipe, "\n\n");
  752.   if (mail_info.encapsulation == ENCAPSULATION_MIME )
  753.     {
  754.       fprintf (mailpipe, "--GUMP-MIME-boundary\n");
  755.       fprintf (mailpipe, "Content-type: text/plain; charset=US-ASCII\n\n");
  756.     }
  757.   fprintf (mailpipe, mail_info.comment);
  758.   fprintf (mailpipe, "\n\n");
  759.   fprintf (mailpipe, mesg_body); 
  760.   fprintf (mailpipe, "\n\n");
  761.   if (mail_info.encapsulation == ENCAPSULATION_MIME )
  762.     {
  763.       char *content;
  764.       content = find_content_type (mail_info.filename);
  765.       fprintf (mailpipe, "--GUMP-MIME-boundary\n");
  766.       fprintf (mailpipe, "Content-type: %s\n",content);
  767.       fprintf (mailpipe, "Content-transfer-encoding: base64\n");
  768.       fprintf (mailpipe, "Content-disposition: attachment; filename=\"%s\"\n",mail_info.filename);
  769.       fprintf (mailpipe, "Content-description: %s\n\n",mail_info.filename);
  770.       free(content);
  771.     }
  772. }
  773.  
  774. /*
  775.  * The following code taken from codes.c in the mpack-1.5 distribution
  776.  * by Carnegie Mellon University. 
  777.  * 
  778.  *
  779.  * (C) Copyright 1993,1994 by Carnegie Mellon University
  780.  * All Rights Reserved.
  781.  *
  782.  * Permission to use, copy, modify, distribute, and sell this software
  783.  * and its documentation for any purpose is hereby granted without
  784.  * fee, provided that the above copyright notice appear in all copies
  785.  * and that both that copyright notice and this permission notice
  786.  * appear in supporting documentation, and that the name of Carnegie
  787.  * Mellon University not be used in advertising or publicity
  788.  * pertaining to distribution of the software without specific,
  789.  * written prior permission.  Carnegie Mellon University makes no
  790.  * representations about the suitability of this software for any
  791.  * purpose.  It is provided "as is" without express or implied
  792.  * warranty.
  793.  *
  794.  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
  795.  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  796.  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
  797.  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  798.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  799.  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  800.  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  801.  * SOFTWARE.
  802.  */
  803. /*
  804. Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
  805.  
  806. Permission to use, copy, modify, and distribute this material 
  807. for any purpose and without fee is hereby granted, provided 
  808. that the above copyright notice and this permission notice 
  809. appear in all copies, and that the name of Bellcore not be 
  810. used in advertising or publicity pertaining to this 
  811. material without the specific, prior written permission 
  812. of an authorized representative of Bellcore.  BELLCORE 
  813. MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY 
  814. OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS", 
  815. WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.  */
  816.  
  817.  
  818. static gchar basis_64[] =
  819. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  820.  
  821. static gint
  822. to64 (FILE *infile,
  823.       FILE *outfile) 
  824. {
  825.   gint c1, c2, c3, ct = 0, written = 0;
  826.  
  827.   while ((c1 = getc (infile)) != EOF)
  828.     {
  829.       c2 = getc (infile);
  830.       if (c2 == EOF)
  831.     {
  832.       output64chunk (c1, 0, 0, 2, outfile);
  833.         }
  834.       else
  835.     {
  836.       c3 = getc (infile);
  837.       if (c3 == EOF)
  838.         {
  839.           output64chunk (c1, c2, 0, 1, outfile);
  840.             }
  841.       else
  842.         {
  843.           output64chunk (c1, c2, c3, 0, outfile);
  844.             }
  845.         }
  846.       ct += 4;
  847.       if (ct > 71)
  848.     {
  849.       putc ('\n', outfile);
  850.       written += 73;
  851.       ct = 0;
  852.         }
  853.     }
  854.   if (ct)
  855.     {
  856.       putc ('\n', outfile);
  857.       ct++;
  858.     }
  859.  
  860.   return written + ct;
  861. }
  862.  
  863. static void
  864. output64chunk (gint  c1,
  865.            gint  c2,
  866.            gint  c3,
  867.            gint  pads,
  868.            FILE *outfile)
  869. {
  870.   putc (basis_64[c1>>2], outfile);
  871.   putc (basis_64[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)], outfile);
  872.  
  873.   if (pads == 2)
  874.     {
  875.       putc ('=', outfile);
  876.       putc ('=', outfile);
  877.     }
  878.   else if (pads)
  879.     {
  880.       putc (basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);
  881.       putc ('=', outfile);
  882.     }
  883.   else
  884.     {
  885.       putc (basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);
  886.       putc (basis_64[c3 & 0x3F], outfile);
  887.     }
  888. }
  889.