Smarter presentation. Bug fix in email header interpretation.
[egate.git] / kernel / urp.c
1 /* Gateway kernel
2  * Europagate, 1995
3  *
4  * $Log: urp.c,v $
5  * Revision 1.20  1995/03/03 17:19:17  adam
6  * Smarter presentation. Bug fix in email header interpretation.
7  *
8  * Revision 1.19  1995/03/02  09:32:11  adam
9  * New presentation formats. f0=full, f1=brief, f2=mid
10  *
11  * Revision 1.18  1995/03/01  14:32:26  adam
12  * Better diagnostics. Default is, that only one database selected when
13  * several are known.
14  *
15  * Revision 1.17  1995/02/28  13:16:26  adam
16  * Configurable From: added.
17  *
18  * Revision 1.16  1995/02/23  10:08:20  adam
19  * Added logging of all user commands.
20  *
21  * Revision 1.15  1995/02/23  08:32:17  adam
22  * Changed header.
23  *
24  * Revision 1.13  1995/02/22  16:54:42  adam
25  * Qualifiers of LOC target updated. More logging messages.
26  *
27  * Revision 1.12  1995/02/22  15:51:51  adam
28  * Bug fix: swap of parameter number and offset in function present.
29  *
30  * Revision 1.11  1995/02/22  15:22:33  adam
31  * Much more checking of run-time state. Show command never retrieves
32  * more records than indicated by the previous search request. Help
33  * command available. The maximum number of records retrieved can be
34  * controlled now.
35  *
36  * Revision 1.10  1995/02/22  08:51:35  adam
37  * Output function can be customized in fml, which is used to print
38  * the reply to reply_fd.
39  *
40  * Revision 1.9  1995/02/21  17:46:21  adam
41  * Minor changes.
42  *
43  * Revision 1.8  1995/02/21  12:12:00  adam
44  * Diagnostic record with error info. observed.
45  *
46  * Revision 1.7  1995/02/20  21:16:20  adam
47  * FML support. Bug fixes. Profile for drewdb.
48  *
49  * Revision 1.6  1995/02/17  14:41:14  quinn
50  * Added simple display of records.
51  *
52  * Revision 1.5  1995/02/17  14:22:13  adam
53  * First steps of CCL show command. Not finished yet.
54  *
55  * Revision 1.4  1995/02/17  09:08:36  adam
56  * Reply with subject. CCL base command implemented.
57  *
58  * Revision 1.3  1995/02/16  18:35:09  adam
59  * First use of Zdist library. Search requests are supported.
60  * Present requests are not supported yet.
61  *
62  * Revision 1.2  1995/02/16  13:21:00  adam
63  * Organization of resource files for targets and conversion
64  * language implemented.
65  *
66  * Revision 1.1  1995/02/15  17:45:30  adam
67  * First version of email gateway kernel. Email requests are read
68  * from stdin. The output is transferred to an MTA if 'From' is
69  * found in the header - or stdout if absent. No Z39.50 client is used.
70  *
71  */
72
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <assert.h>
76 #include <ctype.h>
77 #include <string.h>
78 #include <unistd.h>
79 #include <fcntl.h>
80
81 #include "kernel.h"
82
83 #define LINE_MAX 256
84
85 static void put_esc_str (const char *s)
86 {
87     while (*s)
88         tty_emit (*s++);
89 }
90
91 static int reopen_target (void)
92 {
93     const char *v;
94     if (info.zass)
95         gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
96     info.zass = zass_open (info.hostname, info.port);
97     if (!info.zass)
98     {
99         fprintf (reply_fd, "%s %s:%d\n", 
100                  gw_res_get (info.kernel_res, "gw.err.connect",
101                              "Cannot connect to target"),
102                  info.hostname, info.port);
103         return -1;
104     }
105     v = gw_res_get (info.kernel_res, "gw.description", NULL);
106     if (v)
107     {
108         put_esc_str (v);
109         fprintf (reply_fd, "\n");
110     }
111     fprintf (reply_fd, "%s %s:%d\n",
112              gw_res_get (info.kernel_res, "gw.msg.connect",
113                          "Connection established to"),
114              info.hostname, info.port);
115     if (*info.databases)
116         fprintf (reply_fd, "%s:\n%s\n",
117                  gw_res_get (info.kernel_res, "gw.msg.databases",
118                              "Available databases"),
119                  info.databases);
120     if (*info.database)
121         fprintf (reply_fd, "%s:\n%s\n",
122                  gw_res_get (info.kernel_res, "gw.msg.database",
123                              "Selected databases"),
124                  info.database);
125     return 0;
126 }
127
128 static char line_buf[LINE_MAX+1];
129
130 static struct command_word {
131     char *default_value;
132     char *resource_suffix;
133 } command_tab [] = 
134 {
135 {   "find", "find"},
136 {   "show", "show"},
137 {   "base", "base" },
138 {   "help", "help" },
139 {   "info", "info" },
140 {   "continue", "continue" },
141 {   "status", "status" },
142 {   "cancel", "cancel" },
143 {   "target", "target" },
144 {   NULL, NULL }
145 };
146
147 static int command_search (struct command_word *tab, struct ccl_token *cmd,
148 const char *resource_prefix)
149 {
150     int no = 1;
151
152     assert (resource_prefix);
153     assert (tab);
154     assert (cmd);
155     while (tab->default_value)
156     {
157         char *cp, command_names[60];
158         char resource_name[60];
159         const char *v;
160
161         sprintf (resource_name, "%s%s", resource_prefix,
162                  tab->resource_suffix);
163         v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
164         assert (v);
165         strcpy (command_names, v);
166         cp = command_names;
167         while (1)
168         {
169             char *split;
170
171             if ((split = strchr (cp, ' ')))
172                 *split = '\0';
173             if (cmd->len == strlen(cp) &&
174                 !memcmp (cmd->name, cp, cmd->len))
175                 return no;
176             if (!split)
177                 break;
178             cp = split+1;
179         }        
180         no++;
181         tab++;
182     }
183     return 0;
184 }
185
186 static struct error_no_struct {
187     int no;
188     char *resource_name;
189 } error_ccl_tab[] = {
190 {  CCL_ERR_OK, "ok"},
191 {  CCL_ERR_TERM_EXPECTED, "term.expected" },
192 {  CCL_ERR_RP_EXPECTED, "rp.expected" },
193 {  CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
194 {  CCL_ERR_OP_EXPECTED, "op.expected" },
195 {  CCL_ERR_BAD_RP, "bad.rp" },
196 {  CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
197 {  CCL_ERR_DOUBLE_QUAL, "double.qual" },
198 {  CCL_ERR_EQ_EXPECTED, "eq.expected" },
199 {  CCL_ERR_BAD_RELATION, "bad.relation" },
200 {  CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
201 {  CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
202 {  CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
203 {  0, NULL }
204 };
205
206 static char *error_no_search (struct error_no_struct *tab, int no)
207 {
208     struct error_no_struct *p = tab;
209     while (p->resource_name)
210     {
211         if (no == p->no)
212             return p->resource_name;
213         p++;
214     }
215     return NULL;
216 }
217
218 static int email_header (FILE *inf, char *from_str, char *subject_str)
219 {
220     *from_str = '\0';
221     *subject_str = '\0';
222     while (fgets (line_buf, LINE_MAX, inf))
223     {
224         if (line_buf[0] == '\n')
225             return 0;
226         if (strncmp (line_buf, "From ", 5) == 0)
227             sscanf (line_buf+4, "%s", from_str);
228         if (strncmp (line_buf, "Subject: ", 9) == 0 &&
229             sscanf (line_buf+9, "%s", subject_str+1) == 1)
230             strcpy (subject_str, line_buf+9);
231     }
232     return 1;
233 }
234
235 static void help_general (void)
236 {
237     put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
238                              "Commands available in this service:\n"));
239 }
240
241 static int exec_help (struct ccl_token *list)
242 {
243     help_general ();
244
245 #if 1
246     put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
247                              "target <name> - selects a given target\n"));
248
249     put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
250                              "base <base>..  - selects databases\n"));
251
252     put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
253                              "find <query>   - performs a search request\n"));
254
255     put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
256                              "show <spec>    - retrieves and displays "
257                              "records\n"));
258 #endif
259     return 0;
260 }
261
262 static void display_diag_error (int code, const char *addinfo)
263 {
264     static char str[20];
265
266     sprintf (str, "gw.bib1.diag.%d", code);
267     fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
268              gw_res_get (info.kernel_res, "gw.msg.z39errcode", 
269                          "Z39.50 Error"),
270              code,
271              gw_res_get (info.kernel_res, str, ""), addinfo);
272 }
273
274 static int exec_find (struct ccl_token *list)
275 {
276     const struct zass_searchent *p;
277     struct gw_user_set *us;
278
279     struct ccl_rpn_node *rpn;
280     int error;
281     const char *pos;
282
283     us = user_set_add ("Default", -1);
284     rpn = ccl_find (info.bibset, list, &error, &pos);
285     if (!rpn)
286     {
287         const char *v = NULL, *n;
288         char name[128];
289
290         fprintf (reply_fd, "  %*s^ - ", pos - line_buf, " ");
291
292         n = error_no_search (error_ccl_tab, error);
293         if (n)
294         {
295             sprintf (name, "gw.err.%s", n);
296             v = gw_res_get (info.kernel_res, name, NULL);
297         }
298         if (!v)
299             v = ccl_err_msg (error);
300         fprintf (reply_fd, "%s\n", v);
301         return -1;
302     }
303     ccl_pr_tree (rpn, reply_fd);
304     fprintf (reply_fd, "\n");
305
306     if (!*info.database )
307     {
308         fprintf (reply_fd, "%s\n",
309                  gw_res_get (info.kernel_res, "gw.err.no.database",
310                              "You must select database"));
311         return -3;
312     }
313     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
314             info.database );
315     assert (info.zass);
316     p = zass_search (info.zass, rpn, "Default", info.database );
317     if (!p)
318         return -1;
319     if (p->errcode != -1)
320     {
321         display_diag_error (p->errcode, p->errstring);
322         return -2;
323     }
324     fprintf (reply_fd, "%d %s\n", p->num,
325              gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
326     us->hits = p->num;
327     return 0;
328 }
329
330 static int exec_target (struct ccl_token *list)
331 {
332     int len;
333     if (list->kind == CCL_TOK_EOL)
334         return -1;
335     len = list->len;
336     memcpy (info.target, list->name, len);
337     info.target [len] = '\0';
338
339     read_kernel_res ();
340     return reopen_target ();
341 }
342
343 static int exec_base (struct ccl_token *list)
344 {
345     struct ccl_token *li = list;
346     int len = 0;
347
348     assert (info.zass);
349     if (list->kind == CCL_TOK_EOL)
350         return -1;
351     free (info.database);
352     while (li->kind != CCL_TOK_EOL)
353     {
354         len += li->len + 1;
355         li = li->next;
356         if (li->kind == CCL_TOK_COMMA)
357             li = li->next;
358     }
359     info.database  = malloc (len);
360     assert (info.database );
361     len = 0;
362     li = list;
363     while (li->kind != CCL_TOK_EOL)
364     {
365         memcpy (info.database+len, li->name, li->len);
366         len += li->len;
367         info.database[len++] = ',';
368         li = li->next;
369         if (li->kind == CCL_TOK_COMMA)
370             li = li->next;
371     }
372     info.database[len-1] = '\0';
373     return 0;
374 }
375
376 struct command_word show_tab [] = 
377 {
378 {   "f", "format"},
379 {   "p", "position"},
380 {   NULL, NULL }
381 };
382
383 static void present (const char *set, int offset, int number,
384                      struct ccl_token *format_token)
385 {
386     const struct zass_presentent *zp;
387     int len;
388     int max_number;
389     char format_str[16];
390     
391     max_number = atoi (gw_res_get (info.kernel_res, "gw.max.show", 
392                                    "200"));
393     if (number > max_number)
394         number = max_number;
395     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
396     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
397             number, offset);
398     zp = zass_present(info.zass, (char *) set, offset, number);
399     if (zp)
400     {
401         int i;
402         zass_record *pp;
403         char path[128];
404         int  record_log_fd = -1;
405         const char *record_log_name;
406
407         record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
408                                       NULL);
409         if (record_log_name)
410         {
411             sprintf (path, "%s/%s", gw_res_get (info.kernel_res,
412                                                 "gw.path", "."),
413                      record_log_name );
414             record_log_fd = open (path, O_WRONLY|O_CREAT|O_APPEND, 0666);
415             if (record_log_fd == -1)
416                 gw_log (GW_LOG_WARN, "Cannot open %s", path);
417         }
418         fprintf (reply_fd, gw_res_get (info.kernel_res,
419                                        "gw.msg.records",
420                                        "Got %d records"),
421                  zp->num);
422         fprintf (reply_fd, "\n");
423         for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
424         {
425             Iso2709Rec rec;
426 #if USE_FML
427             const char *arg_ar[5];
428 #endif
429             fprintf (reply_fd, "--- %d/%d ---\n",
430                      i+offset, offset+zp->num-1);
431             if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
432             {
433                 if (pp->which == ZASS_REC_DIAG)
434                 {
435                     display_diag_error (pp->errcode, pp->errstring);
436                     continue;
437                 }
438                 else if (pp->which != ZASS_REC_USMARC)
439                 {
440                     fprintf (reply_fd, "Unknown record kind %d\n",
441                              pp->which);
442                     continue;
443                 }
444             }
445             if (record_log_fd != -1)
446                 write (record_log_fd, pp->record, strlen(pp->record));
447             rec = iso2709_cvt (pp->record);
448             if (rec)
449             {
450 #if USE_FML
451                 if (format_token)
452                 {
453                     len = format_token->len;
454                     if (len >= sizeof(format_str))
455                         len = sizeof(format_str)-1;
456                     memcpy (format_str, format_token->name, len);
457                     format_str[len] = '\0';
458                 }
459                 if (info.fml && format_token && 
460                     (!strcmp (format_str, "0") || !strcmp (format_str, "1")
461                      || !strcmp(format_str, "2")))
462                 {
463                     arg_ar[0] = "\\f";
464                     arg_ar[1] = format_str;
465                     arg_ar[2] = " \\list";
466                     arg_ar[3] = marc_to_str (info.fml, rec);
467                     arg_ar[4] = NULL;
468                     fml_exec_call_argv (info.fml, arg_ar);
469                 }
470                 else
471                     iso2709_display (rec, reply_fd);
472 #else
473                 iso2709_display (rec, reply_fd);
474 #endif
475                 iso2709_rm (rec);
476             }
477             else
478                 fprintf (reply_fd, "Not a MARC record\n");
479         }
480         if (record_log_fd != -1)
481             close (record_log_fd);
482     }
483 }
484
485 static int exec_show (struct ccl_token *list)
486 {
487     char tmp_str[20];
488     struct ccl_token *set_token = NULL;
489     struct ccl_token *format_token = NULL;
490     struct ccl_token *li = list;
491     int no_of_present = 0;
492
493     assert (info.zass);
494     while (li->kind != CCL_TOK_EOL)
495     {
496         int modifier_no = 0;
497         if (li->next->kind == CCL_TOK_EQ)
498         {
499             if (li->kind == CCL_TOK_SET)    /* set = <name> ? */
500             {
501                 li = li->next->next;
502                 set_token = li;
503             }
504             else 
505             {
506                 modifier_no = command_search (show_tab, li, "ccl.token.");
507                 if (!modifier_no)
508                 {
509                     fprintf (reply_fd, "Unknown modifier in show\n");
510                     return -1;
511                 }
512                 li = li->next->next;
513                 if (modifier_no == 1)       /* f = <name> ? */
514                     format_token = li;
515                 else if (modifier_no == 2)  /* p = <name> ? */
516                 {
517                     if (li->kind != CCL_TOK_EOL   /* p = <name> - <name> ? */
518                         && li->next->kind == CCL_TOK_MINUS
519                         && li->next->next != CCL_TOK_EOL)
520                         li = li->next->next;
521                 }
522             }
523             if (!li->next)
524             {
525                 fprintf (reply_fd, "%s\n", "Missing token after '='");
526                 return -2;
527             }
528             li = li->next;
529         }
530         else
531             li = li->next;
532     }
533     if (set_token)
534         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
535                 set_token->name);
536     if (format_token)
537         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
538                 format_token->name);
539
540     li = list;
541     while (li->kind != CCL_TOK_EOL)
542     {
543         int modifier_no = 0;
544         int offset = 0;
545         int number = 0;
546         int len;
547         if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
548         {
549             modifier_no = command_search (show_tab, li, "ccl.token.");
550             li = li->next->next;
551             if (modifier_no == 2)  /* p = <name> ? */
552             {
553                 if (li->kind != CCL_TOK_EOL   /* p = <name> - <name> ? */
554                     && li->next->kind == CCL_TOK_MINUS
555                     && li->next->next != CCL_TOK_EOL)
556                 {
557                     len = li->len;
558                     memcpy (tmp_str, li->name, len);
559                     tmp_str [len] = '\0';
560                     offset = atoi (tmp_str);
561                     li = li->next->next;
562
563                     len = li->len;
564                     memcpy (tmp_str, li->name, len);
565                     tmp_str [len] = '\0';
566                     number = atoi (tmp_str) - offset + 1;
567                 }
568                 else
569                 {
570                     len = li->len;
571                     memcpy (tmp_str, li->name, len);
572                     tmp_str [len] = '\0';
573                     offset = atoi (tmp_str);
574                     number = 1;
575                 }
576             }
577             li = li->next;
578         }
579         else
580         {
581             len = li->len;
582             memcpy (tmp_str, li->name, len);
583             tmp_str[len] = '\0';
584             number = atoi (tmp_str);
585             offset = 1;
586             li = li->next;
587         }
588         if (offset > 0 && number > 0)
589         {
590             struct gw_user_set *us;
591
592             if (set_token)
593             {
594                 len = set_token->len;
595                 memcpy (tmp_str, set_token->name, len);
596                 tmp_str[len] = '\0';
597                 us = user_set_search (tmp_str);
598             }
599             else
600                 us = user_set_search (NULL);
601             if (us && us->hits != -1) /* proper result-set? */
602             {
603                 if (offset <= us->hits)
604                 {
605                     if (offset+number-1 > us->hits)
606                         number = us->hits - offset+1;
607                     present (us->name, offset, number, format_token);
608                 }
609             }
610             else if (!no_of_present) /* display error message once! */
611             {
612                 fprintf (reply_fd, "%s\n",
613                          gw_res_get (info.kernel_res, "gw.err.no.set",
614                                      "No result-set generated"));
615             }
616             no_of_present++;
617         }
618     }
619     if (!no_of_present) /* no records shown so far? */
620     {
621         struct gw_user_set *us;
622         int default_show;
623         
624         us = user_set_search (NULL);
625         if (us && us->hits != -1)    /* proper result-set? */
626         {
627             default_show = atoi (gw_res_get (info.kernel_res,
628                                              "gw.default.show", "20"));
629             if (us->hits > default_show)
630                 present (us->name, 1, default_show, format_token);
631             else if (us->hits > 0)
632                 present (us->name, 1, us->hits, format_token);
633         }
634         else                         /* display error message */
635         {
636             fprintf (reply_fd, "%s\n",
637                      gw_res_get (info.kernel_res, "gw.err.no.set",
638                                  "No result-set generated"));
639             return -3;
640         }        
641     }
642     return 0;
643 }
644
645 static int exec_command (const char *str)
646 {
647     struct ccl_token *cmd = ccl_tokenize (str);
648     int no;
649
650     if (cmd->kind != CCL_TOK_EOL &&
651         (no = command_search (command_tab, cmd, "ccl.command.")))
652     {
653         if (!info.zass && no != 9 && no != 4)
654             reopen_target ();
655         fprintf (reply_fd, "\n> %s\n", str);
656         if (!info.zass && (no == 1 || no == 2 || no == 3))
657         {
658             fprintf (reply_fd, "%s\n",
659                      gw_res_get (info.kernel_res, "gw.err.no.target",
660                                  "No connection established - "
661                                  "command ignored"));
662             return 0;
663         }
664         switch (no)
665         {
666         case 1:
667             return exec_find (cmd->next);
668         case 2:
669             return exec_show (cmd->next);
670         case 3:
671             return exec_base (cmd->next);
672         case 4:
673             return exec_help (cmd->next);
674         case 9:
675             return exec_target (cmd->next);
676         default:
677             fprintf (reply_fd, "%s\n",
678                      gw_res_get (info.kernel_res, "gw.err.unimplemented",
679                                  "Not implemented yet"));
680         }
681     }
682     else
683     {
684         fprintf (reply_fd, "\n> %s\n", str);
685         fprintf (reply_fd, "  ^ %s\n", 
686                  gw_res_get (info.kernel_res, "gw.err.unknown.command",
687                              "unknown command. "
688                              "Use help to see list of commands"));
689     }
690     return 0;
691 }
692
693 int urp (FILE *inf)
694 {
695     char from_str[128];
696     char subject_str[128];
697     int command_no = 0;
698     char *reply_fname = NULL;
699
700     if (email_header (inf, from_str, subject_str))
701     {
702         gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
703         return -1;
704     }
705     tty_init (stdout, 40, 70);
706     if (*from_str)
707     {
708         reply_fname = tempnam (gw_res_get (info.kernel_res,
709                                            "gw.reply.tmp.dir", NULL),
710                                gw_res_get (info.kernel_res,
711                                            "gw.reply.tmp.prefix", "gwr"));
712                                                  
713         reply_fd = fopen (reply_fname, "w");
714         if (!reply_fd)
715         {
716             gw_log (GW_LOG_FATAL, KERNEL_LOG, "Cannot create %s",
717                     reply_fname);
718             return -1;
719         }
720         tty_init (reply_fd, 0, 0);
721         fprintf (reply_fd, "From: %s\n",
722                  gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
723         fprintf (reply_fd, "Subject: ");
724         if (*subject_str)
725             fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
726         else
727             fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
728                                                    "gw.msg.subject",
729                                                    "Your Query"));
730         fprintf (reply_fd, "\n");
731         gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", from_str);
732     }
733     else
734         gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
735     fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
736                                            "Email->Z39.50 gateway"));
737     while (fgets (line_buf, LINE_MAX, inf))
738     {
739         char *cp;
740
741         if (line_buf[0] == '\n')
742             if (command_no)
743                 break;
744             else 
745                 continue;
746         if ((cp = strchr (line_buf, '\n')))
747             *cp = '\0';
748         gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
749         ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
750         ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
751         ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
752         ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
753         if (isalpha (line_buf[0]))
754             exec_command (line_buf);
755         command_no++;
756     }
757     if (!command_no)
758     {
759         fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
760                                                "gw.err.nullbody",
761                                                "No body"));
762         help_general ();
763     }
764     if (*from_str)
765     {
766         const char *mta;
767         char cmd[256];
768         int mta_code;
769
770         assert (reply_fname);
771         fclose (reply_fd);
772         reply_fd = stdout;
773
774         mta = gw_res_get (info.kernel_res, "gw.reply.mta",
775                           "/usr/lib/sendmail");
776         sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
777         
778         mta_code = system (cmd);
779         if (mta_code)
780             gw_log (GW_LOG_FATAL, KERNEL_LOG, "Reply '%s' got exit code %d",
781                     cmd, mta_code);
782         unlink (reply_fname);        
783         gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", from_str);
784     }
785     return 0;
786 }