home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / metamail / contrib / ServiceMail / src / mesh / startjob.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-18  |  4.8 KB  |  147 lines

  1. /*
  2.  * startjob.c -- functions to invoke services
  3.  *
  4.  * 3-17-93   weber@eitech.com added service override from command line
  5.  * 2-18-93   weber@eitech.com added check for env vars MESHINIT and MESHCONFIG
  6.  * 2-18-93   weber@eitech.com added DATE and SPLITSIZE fields to envelope
  7.  * 1-21-93   weber@eitech.com added TO field to envelope
  8.  * 9-8-92    ekr@eitech.com                          v1.1
  9.  * 27-Jun-92 weber@eitech.com marked ServiceMail(tm) v1.0
  10.  * 25-May-92 weber@eitech.com created
  11.  *
  12.  * Copyright (c)  1992 Enterprise Integration Technologies Corporation
  13.  *
  14.  * Permission to use, copy, modify, distribute, and sell this software and 
  15.  * its documentation for any purpose is hereby granted without fee, provided
  16.  * that (i) the above copyright notices and this permission notice appear in
  17.  * all copies of the software and related documentation, and (ii) the name of
  18.  * Enterprise Integration Technologies Corporation may not be used in any 
  19.  * advertising or publicity relating to the software without the specific, 
  20.  * prior written permission of Enterprise Integration Technologies Corporation.
  21.  * 
  22.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  23.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  24.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  25.  *
  26.  * IN NO EVENT SHALL ENTERPRISE INTEGRATION TECHNOLOGIES CORPORATION  BE
  27.  * LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
  28.  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  29.  * PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY
  30.  * THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  31.  * PERFORMANCE OF THIS SOFTWARE.
  32.  */
  33.  
  34. #include <tcl.h>
  35.  
  36. #include "mesh.h"
  37. extern char scratchdir[];
  38. #ifndef MESHINIT
  39. #define MESHINIT "~/src/mesh/init.tcl"
  40. #endif
  41. #ifndef MESHCONFIG
  42. #define MESHCONFIG "~/src/mesh/config.tcl"
  43. #endif
  44.  
  45. extern char *service;
  46.  
  47. StartJob(m)
  48.      struct MessageInfo *m;
  49. {
  50.   int i;
  51.   char *s, *cmd, *switches, *meshi, *meshc;
  52.   char buf[1024], env[1024];     /* this really should be malloc'ed and realloc'ed */
  53.   char *strtok(), *getenv();
  54.   int mailout();
  55.   
  56.   Tcl_Interp *interp, *Tcl_CreateInterp();
  57.   
  58.   s = m->service ? m->service : m->subject;
  59.   sprintf(env, "{TO \"%s\" FROM \"%s\" REPLYTO \"%s\" SERVICE \"%s\" ",
  60.       m->to, m->from, m->reply_to ? m->reply_to : m->from,
  61.       s ? s : "unknown");
  62.   if (m->id) {
  63.     strcat(env, "MESSAGEID \"");
  64.     strcat(env, m->id);
  65.     strcat(env, "\" ");
  66.   }
  67.   if (m->subject) {
  68.     strcat(env, "SUBJECT \"");
  69.     strcat(env, m->subject);
  70.     strcat(env, "\" ");
  71.   }
  72.   if (m->date) {
  73.     strcat(env, "DATE \"");
  74.     strcat(env, m->date);
  75.     strcat(env, "\" ");
  76.   }
  77.   if (m->splitsize) {
  78.     strcat(env, "SPLITSIZE \"");
  79.     strcat(env, m->splitsize);
  80.     strcat(env, "\" ");
  81.   }
  82.   strcat(env, "}");
  83.  
  84.   s = strtok(s, " \t\n");
  85.   if (service) s = service;
  86.   
  87.   interp = Tcl_CreateInterp();
  88.   Tcl_CreateCommand(interp, "mailout", mailout, (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);
  89.   meshi = getenv("MESHINIT");
  90.   meshc = getenv("MESHCONFIG");
  91.   if (Tcl_EvalFile(interp, meshi ? meshi : MESHINIT) != TCL_OK) {
  92.     fputs(interp->result, stderr); fputc('\n', stderr);
  93.     ErrorExit("initialization problem");
  94.   }
  95.   else if (Tcl_EvalFile(interp, meshc ? meshc : MESHCONFIG) != TCL_OK) {
  96.     fputs(interp->result, stderr); fputc('\n', stderr);
  97.     ErrorExit("services configuration problem");
  98.   }
  99.   else {
  100.     switches = strtok(NULL, "[\n"); /* the bracket is for security */
  101.     BuildTclBodyRep(m, buf);
  102.     if (Tcl_VarEval(interp, "invoke-service ", s?s:"\"\"", " {",
  103.          switches ? switches : "\"\"", "} ", env, " ", buf, (char *)NULL) != TCL_OK) {
  104.       fputs(interp->result, stderr); fputc('\n', stderr);
  105.       ErrorExit("unknown service or error in service execution");
  106.     }
  107.   }
  108.   Tcl_DeleteInterp(interp);
  109. }
  110.  
  111. BuildTclBodyRep(m, buf)
  112.      struct MessageInfo *m;
  113.      char *buf;
  114. {
  115.   int i;
  116.   
  117.   sprintf(buf, "{ TYPE \"%s\" SUBTYPE \"%s\" PARAMS {", m->content.type, m->content.format);
  118.   while (*buf) buf++;
  119.   for (i=0; i < m->content.numparms; i++) {
  120.     sprintf(buf, "\"%s\" \"%s\"", m->content.parms[i].name,
  121.         m->content.parms[i].value);
  122.     while (*buf) buf++;
  123.   }
  124.   *buf++ = '}';
  125.   *buf++ = ' ';
  126.   if (strcasecmp(m->content.type, "multipart")) {
  127.     sprintf(buf, "FILE \"%s/%s\" ", scratchdir,m->body.fname);
  128.     while (*buf) buf++;
  129.     sprintf(buf, "ID \"%s\" ", m->content.id ? m->content.id : "");
  130.     while (*buf) buf++;
  131.     sprintf(buf, "DESCRIPTION \"%s\"", m->subject ? m->subject : "");
  132.     while (*buf) buf++;
  133.   }
  134.   else {
  135.     strcpy(buf, "PARTS {");
  136.     for (i=0; i < m->body.multipart.numparts; i++) {
  137.       while (*buf) buf++;
  138.       *buf++ = ' ';
  139.       BuildTclBodyRep(m->body.multipart.parts[i], buf);
  140.     }
  141.     while (*buf) buf++;
  142.     *buf++ = '}';
  143.   }
  144.   *buf++ = '}';
  145.   *buf = 0;
  146. }
  147.