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