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