740d43e54c2a40d4a4da757ed9024e4b321630c9
[egate.git] / kernel / urp.c
1 /* Gateway kernel
2  * Europagate, 1995
3  *
4  * $Log: urp.c,v $
5  * Revision 1.8  1995/02/21 12:12:00  adam
6  * Diagnostic record with error info. observed.
7  *
8  * Revision 1.7  1995/02/20  21:16:20  adam
9  * FML support. Bug fixes. Profile for drewdb.
10  *
11  * Revision 1.6  1995/02/17  14:41:14  quinn
12  * Added simple display of records.
13  *
14  * Revision 1.5  1995/02/17  14:22:13  adam
15  * First steps of CCL show command. Not finished yet.
16  *
17  * Revision 1.4  1995/02/17  09:08:36  adam
18  * Reply with subject. CCL base command implemented.
19  *
20  * Revision 1.3  1995/02/16  18:35:09  adam
21  * First use of Zdist library. Search requests are supported.
22  * Present requests are not supported yet.
23  *
24  * Revision 1.2  1995/02/16  13:21:00  adam
25  * Organization of resource files for targets and conversion
26  * language implemented.
27  *
28  * Revision 1.1  1995/02/15  17:45:30  adam
29  * First version of email gateway kernel. Email requests are read
30  * from stdin. The output is transferred to an MTA if 'From' is
31  * found in the header - or stdout if absent. No Z39.50 client is used.
32  *
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <assert.h>
38 #include <ctype.h>
39 #include <string.h>
40 #include <unistd.h>
41
42 #include "kernel.h"
43
44 #define LINE_MAX 256
45
46 static int reopen_target (void)
47 {
48     const char *v;
49     if (info.zass)
50         gw_log (GW_LOG_WARN, "urp", "Zass free...");
51     info.zass = zass_open (info.hostname, info.port);
52     if (!info.zass)
53     {
54         fprintf (reply_fd, "%s %s:%d\n", 
55                  gw_res_get (info.kernel_res, "gw.err.connect",
56                              "Cannot connect to target"),
57                  info.hostname, info.port);
58         return -1;
59     }
60     v = gw_res_get (info.kernel_res, "gw.description", NULL);
61     if (v)
62         fprintf (reply_fd, "%s\n", v);
63     fprintf (reply_fd, "%s %s:%d\n",
64              gw_res_get (info.kernel_res, "gw.msg.connect",
65                          "Connection established to"),
66              info.hostname, info.port);
67     if (*info.databases)
68         fprintf (reply_fd, "%s:\n%s\n",
69                  gw_res_get (info.kernel_res, "gw.msg.databases",
70                              "Available databases"),
71                  info.databases);
72     return 0;
73 }
74
75 static char line_buf[LINE_MAX+1];
76
77 static struct command_word {
78     char *default_value;
79     char *resource_suffix;
80 } command_tab [] = 
81 {
82 {   "find", "find"},
83 {   "show", "show"},
84 {   "base", "base" },
85 {   "help", "help" },
86 {   "info", "info" },
87 {   "continue", "continue" },
88 {   "status", "status" },
89 {   "cancel", "cancel" },
90 {   "target", "target" },
91 {   NULL, NULL }
92 };
93
94 static int command_search (struct command_word *tab, struct ccl_token *cmd,
95 const char *resource_prefix)
96 {
97     int no = 1;
98
99     assert (resource_prefix);
100     assert (tab);
101     assert (cmd);
102     while (tab->default_value)
103     {
104         char *cp, command_names[60];
105         char resource_name[60];
106         const char *v;
107
108         sprintf (resource_name, "%s%s", resource_prefix,
109                  tab->resource_suffix);
110         v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
111         assert (v);
112         strcpy (command_names, v);
113         cp = command_names;
114         while (1)
115         {
116             char *split;
117
118             if ((split = strchr (cp, ' ')))
119                 *split = '\0';
120             if (cmd->len == strlen(cp) &&
121                 !memcmp (cmd->name, cp, cmd->len))
122                 return no;
123             if (!split)
124                 break;
125             cp = split+1;
126         }        
127         no++;
128         tab++;
129     }
130     return 0;
131 }
132
133 static struct error_no_struct {
134     int no;
135     char *resource_name;
136 } error_ccl_tab[] = {
137 {  CCL_ERR_OK, "ok"},
138 {  CCL_ERR_TERM_EXPECTED, "term.expected" },
139 {  CCL_ERR_RP_EXPECTED, "rp.expected" },
140 {  CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
141 {  CCL_ERR_OP_EXPECTED, "op.expected" },
142 {  CCL_ERR_BAD_RP, "bad.rp" },
143 {  CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
144 {  CCL_ERR_DOUBLE_QUAL, "double.qual" },
145 {  CCL_ERR_EQ_EXPECTED, "eq.expected" },
146 {  CCL_ERR_BAD_RELATION, "bad.relation" },
147 {  CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
148 {  CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
149 {  CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
150 {  0, NULL }
151 };
152
153 static char *error_no_search (struct error_no_struct *tab, int no)
154 {
155     struct error_no_struct *p = tab;
156     while (p->resource_name)
157     {
158         if (no == p->no)
159             return p->resource_name;
160         p++;
161     }
162     return NULL;
163 }
164
165 static int email_header (FILE *inf, char *from_str, char *subject_str)
166 {
167     *from_str = '\0';
168     *subject_str = '\0';
169     while (fgets (line_buf, LINE_MAX, inf))
170     {
171         if (line_buf[0] == '\n')
172             return 0;
173         if (strncmp (line_buf, "From ", 5) == 0)
174             sscanf (line_buf+4, "%s", from_str);
175         if (strncmp (line_buf, "Subject: ", 9) == 0 &&
176             sscanf (line_buf+9, "%s", subject_str+1) == 1)
177             strcpy (subject_str, line_buf+9);
178     }
179     return 1;
180 }
181
182 static int exec_find (struct ccl_token *list)
183 {
184     const struct zass_searchent *p;
185
186     struct ccl_rpn_node *rpn;
187     int error;
188     const char *pos;
189
190     rpn = ccl_find (info.bibset, list, &error, &pos);
191     if (!rpn)
192     {
193         const char *v = NULL, *n;
194         char name[128];
195
196         fprintf (reply_fd, "  %*s^ - ", pos - line_buf, " ");
197
198         n = error_no_search (error_ccl_tab, error);
199         if (n)
200         {
201             sprintf (name, "gw.err.%s", n);
202             v = gw_res_get (info.kernel_res, name, NULL);
203         }
204         if (!v)
205             v = ccl_err_msg (error);
206         fprintf (reply_fd, "%s\n", v);
207         return -1;
208     }
209     ccl_pr_tree (rpn, reply_fd);
210     fprintf (reply_fd, "\n");
211
212     if (!info.zass)
213         return -2;
214     if (!*info.databases)
215     {
216         fprintf (reply_fd, "%s\n",
217                  gw_res_get (info.kernel_res, "gw.err.no.database",
218                              "You must select database"));
219         return -3;
220     }
221     gw_log (GW_LOG_DEBUG, "urp", "Searching in database %s",
222             info.databases);
223     p = zass_search (info.zass, rpn, "Default", info.databases);
224     if (!p)
225         return -1;
226     if (p->errcode != -1)
227         fprintf (reply_fd, "%s %d: %s\n",
228                  gw_res_get (info.kernel_res, "gw.msg.z39errcode",
229                              "Z39.50 error code"),
230                  p->errcode, p->errstring);
231     else
232         fprintf (reply_fd, "%d %s\n", p->num,
233                  gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
234     return 0;
235 }
236
237 static int exec_target (struct ccl_token *list)
238 {
239     int len;
240     if (list->kind == CCL_TOK_EOL)
241         return -1;
242     len = list->len;
243     memcpy (info.target, list->name, len);
244     info.target [len] = '\0';
245
246     read_kernel_res ();
247     return reopen_target ();
248 }
249
250 static int exec_base (struct ccl_token *list)
251 {
252     struct ccl_token *li = list;
253     int len = 0;
254
255     if (list->kind == CCL_TOK_EOL)
256         return -1;
257     free (info.databases);
258     while (li->kind != CCL_TOK_EOL)
259     {
260         len += li->len + 1;
261         li = li->next;
262         if (li->kind == CCL_TOK_COMMA)
263             li = li->next;
264     }
265     info.databases = malloc (len);
266     assert (info.databases);
267     len = 0;
268     li = list;
269     while (li->kind != CCL_TOK_EOL)
270     {
271         memcpy (info.databases+len, li->name, li->len);
272         len += li->len;
273         info.databases[len++] = ',';
274         li = li->next;
275         if (li->kind == CCL_TOK_COMMA)
276             li = li->next;
277     }
278     info.databases[len-1] = '\0';
279     return 0;
280 }
281
282 struct command_word show_tab [] = 
283 {
284 {   "f", "format"},
285 {   "p", "position"},
286 {   NULL, NULL }
287 };
288
289 static int exec_show (struct ccl_token *list)
290 {
291     const struct zass_presentent *zp;
292     char num_str[20];
293     struct ccl_token *set_token = NULL;
294     struct ccl_token *format_token = NULL;
295     struct ccl_token *li = list;
296
297     if (list->kind == CCL_TOK_EOL)
298         return -1;
299     if (!info.zass)
300         return -2;
301     while (li->kind != CCL_TOK_EOL)
302     {
303         int modifier_no = 0;
304         if (li->next->kind == CCL_TOK_EQ)
305         {
306             if (li->kind == CCL_TOK_SET)    /* set = <name> ? */
307             {
308                 li = li->next->next;
309                 set_token = li;
310             }
311             else 
312             {
313                 modifier_no = command_search (show_tab, li, "ccl.token.");
314                 if (!modifier_no)
315                 {
316                     fprintf (reply_fd, "Unknown modifier in show\n");
317                     return -1;
318                 }
319                 li = li->next->next;
320                 if (modifier_no == 1)       /* f = <name> ? */
321                     format_token = li;
322                 else if (modifier_no == 2)  /* p = <name> ? */
323                 {
324                     if (li->kind != CCL_TOK_EOL   /* p = <name> - <name> ? */
325                         && li->next->kind == CCL_TOK_MINUS
326                         && li->next->next != CCL_TOK_EOL)
327                         li = li->next->next;
328                 }
329             }
330             li = li->next;
331         }
332         else
333             li = li->next;
334     }
335     if (set_token)
336         gw_log (GW_LOG_DEBUG, "urp", "Got set=%.*s", set_token->len,
337                 set_token->name);
338     if (format_token)
339         gw_log (GW_LOG_DEBUG, "urp", "Got format=%.*s", format_token->len,
340                 format_token->name);
341
342     li = list;
343     while (li->kind != CCL_TOK_EOL)
344     {
345         int modifier_no = 0;
346         int offset = 0;
347         int number = 0;
348         int len;
349         if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
350         {
351             modifier_no = command_search (show_tab, li, "ccl.token.");
352             li = li->next->next;
353             if (modifier_no == 2)  /* p = <name> ? */
354             {
355                 if (li->kind != CCL_TOK_EOL   /* p = <name> - <name> ? */
356                     && li->next->kind == CCL_TOK_MINUS
357                     && li->next->next != CCL_TOK_EOL)
358                 {
359                     len = li->len;
360                     memcpy (num_str, li->name, len);
361                     num_str [len] = '\0';
362                     offset = atoi (num_str);
363                     li = li->next->next;
364
365                     len = li->len;
366                     memcpy (num_str, li->name, len);
367                     num_str [len] = '\0';
368                     number = atoi (num_str) - offset + 1;
369                 }
370                 else
371                 {
372                     len = li->len;
373                     memcpy (num_str, li->name, len);
374                     num_str [len] = '\0';
375                     offset = atoi (num_str);
376                     number = 1;
377                 }
378             }
379             li = li->next;
380         }
381         else
382         {
383             len = li->len;
384             memcpy (num_str, li->name, len);
385             num_str[len] = '\0';
386             number = atoi (num_str);
387             offset = 1;
388             li = li->next;
389         }
390         if (offset > 0 && number > 0)
391         {
392             if (set_token)
393             {
394                 len = set_token->len;
395                 memcpy (num_str, set_token->name, len);
396                 num_str[len] = '\0';
397             }
398             else
399                 strcpy (num_str, "Default");
400             gw_log (GW_LOG_DEBUG, "urp", "zass_present of %d records from"
401                     " offset %d in set %s", number, offset, num_str);
402             zp = zass_present(info.zass, num_str, offset, number);
403             if (zp)
404             {
405                 int i;
406                 zass_record *pp;
407                 
408                 fprintf (reply_fd, gw_res_get (info.kernel_res,
409                                                "gw.msg.records",
410                                                "Got %d records"),
411                          zp->num);
412                 fprintf (reply_fd, "\n");
413                 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
414                 {
415                     Iso2709Rec rec;
416 #if USE_FML
417                     const char *arg_ar[5];
418 #endif
419                     fprintf (reply_fd, "--- %d/%d ---\n",
420                              i+offset, offset+zp->num);
421                     if (pp->which == ZASS_REC_DIAG)
422                     {
423                         fprintf (reply_fd, "Record error %d: %s\n",
424                                  pp->errcode, pp->errstring);
425                         continue;
426                     }
427                     else if (pp->which != ZASS_REC_USMARC)
428                     {
429                         fprintf (reply_fd, "Unknown record kind %d\n",
430                                  pp->which);
431                         continue;
432                     }
433                     rec = iso2709_cvt (pp->record);
434 #if USE_FML
435                     if (format_token)
436                     {
437                         len = format_token->len;
438                         memcpy (num_str, format_token->name, len);
439                         num_str[len] = '\0';
440                     }
441                     if (format_token && 
442                         (!strcmp (num_str, "0") || !strcmp (num_str, "1")))
443                     {
444                         arg_ar[0] = "\\f";
445                         arg_ar[1] = num_str;
446                         arg_ar[2] = " \\list";
447                         arg_ar[3] = marc_to_str (info.fml, rec);
448                         arg_ar[4] = NULL;
449                         fml_exec_call_argv (info.fml, arg_ar);
450                     }
451                     else
452                         iso2709_display (rec, reply_fd);
453 #else
454                     iso2709_display (rec, reply_fd);
455 #endif
456                     iso2709_rm (rec);
457                 }
458             }
459         }
460     }
461 #if 0
462     len = list->len;
463     memcpy (num_str, list->name, len);
464     num_str[len] = '\0';
465
466     num = atoi (num_str);
467     if (!num)
468         return -3;
469     gw_log (GW_LOG_DEBUG, "urp", "zass_present of %d records", num);
470     zp = zass_present(info.zass, "Default", 1, num);
471     if (zp)
472     {
473         int i;
474         zass_record *pp;
475
476         fprintf (reply_fd, gw_res_get (info.kernel_res,
477                                        "gw.msg.records", "Got %d records"),
478                  zp->num);
479         fprintf (reply_fd, "\n");
480         for (i = 1, pp = zp->records; pp; pp = pp->next, i++)
481         {
482 #if USE_FML
483             const char *arg_ar[3];
484 #endif
485             Iso2709Rec rec = iso2709_cvt (pp->record);
486
487             fprintf (reply_fd, "--- %d/%d ---\n", i, zp->num);
488 #if USE_FML
489             arg_ar[0] = "\\f0 \\list";
490             arg_ar[1] = marc_to_str (info.fml, rec);
491             arg_ar[2] = NULL;
492             fml_exec_call_argv (info.fml, arg_ar);
493 #else
494             iso2709_display (rec, reply_fd);
495 #endif
496             iso2709_rm (rec);
497         }
498     }
499 #endif
500     return 0;
501 }
502
503 static int exec_command (const char *str)
504 {
505     struct ccl_token *cmd = ccl_tokenize (str);
506     int no;
507
508     if (cmd->kind != CCL_TOK_EOL &&
509         (no = command_search (command_tab, cmd, "ccl.command.")))
510     {
511         if (!info.zass && no != 9)
512             reopen_target ();
513         fprintf (reply_fd, "\n> %s", str);
514         switch (no)
515         {
516         case 1:
517             return exec_find (cmd->next);
518         case 2:
519             return exec_show (cmd->next);
520         case 3:
521             return exec_base (cmd->next);
522         case 9:
523             return exec_target (cmd->next);
524         default:
525             fprintf (reply_fd, "%s\n",
526                      gw_res_get (info.kernel_res, "gw.err.unimplemented",
527                                  "Not implemented yet"));
528         }
529     }
530     else
531     {
532         fprintf (reply_fd, "\n> %s", str);
533         fprintf (reply_fd, "  ^ %s\n", 
534                  gw_res_get (info.kernel_res, "gw.err.unknown.command",
535                              "unknown command"));
536     }
537     return 0;
538 }
539
540 int urp (FILE *inf)
541 {
542     char from_str[128];
543     char subject_str[128];
544     int command_no = 0;
545     char *reply_fname = NULL;
546
547     if (email_header (inf, from_str, subject_str))
548     {
549         gw_log (GW_LOG_WARN, "urp", "No message body");
550         return -1;
551     }
552     if (*from_str)
553     {
554         reply_fname = tempnam (gw_res_get (info.kernel_res,
555                                            "gw.reply.tmp.dir", NULL),
556                                gw_res_get (info.kernel_res,
557                                            "gw.reply.tmp.prefix", "gwr"));
558                                                  
559         reply_fd = fopen (reply_fname, "w");
560         if (!reply_fd)
561         {
562             gw_log (GW_LOG_FATAL, "urp", "Cannot create %s",
563                     reply_fname);
564             return -1;
565         }
566         fprintf (reply_fd, "Subject: ");
567         if (*subject_str)
568             fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
569         else
570             fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
571                                                    "gw.msg.subject",
572                                                    "Your Query"));
573         fprintf (reply_fd, "\n");
574     }
575     else
576         gw_log (GW_LOG_WARN, "urp", "No From in email header");
577     fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
578                                            "Email->Z39.50 gateway"));
579     while (fgets (line_buf, LINE_MAX, inf))
580     {
581         if (line_buf[0] == '\n')
582             break;
583         ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
584         ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
585         ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
586         ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
587         if (isalpha (line_buf[0]))
588             exec_command (line_buf);
589         command_no++;
590     }
591     if (!command_no)
592         fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
593                                                "gw.err.nullbody",
594                                                "No body"));
595     if (*from_str)
596     {
597         const char *mta;
598         char cmd[256];
599         int mta_code;
600
601         assert (reply_fname);
602         fclose (reply_fd);
603         reply_fd = stdout;
604
605         mta = gw_res_get (info.kernel_res, "gw.reply.mta",
606                           "/usr/lib/sendmail");
607         sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
608         
609         mta_code = system (cmd);
610         if (mta_code)
611             gw_log (GW_LOG_FATAL, "urp", "Reply '%s' got exit code %d",
612                     cmd, mta_code);
613         unlink (reply_fname);        
614     }
615     return 0;
616 }