5 * Revision 1.17 1995/02/28 13:16:26 adam
6 * Configurable From: added.
8 * Revision 1.16 1995/02/23 10:08:20 adam
9 * Added logging of all user commands.
11 * Revision 1.15 1995/02/23 08:32:17 adam
14 * Revision 1.13 1995/02/22 16:54:42 adam
15 * Qualifiers of LOC target updated. More logging messages.
17 * Revision 1.12 1995/02/22 15:51:51 adam
18 * Bug fix: swap of parameter number and offset in function present.
20 * Revision 1.11 1995/02/22 15:22:33 adam
21 * Much more checking of run-time state. Show command never retrieves
22 * more records than indicated by the previous search request. Help
23 * command available. The maximum number of records retrieved can be
26 * Revision 1.10 1995/02/22 08:51:35 adam
27 * Output function can be customized in fml, which is used to print
28 * the reply to reply_fd.
30 * Revision 1.9 1995/02/21 17:46:21 adam
33 * Revision 1.8 1995/02/21 12:12:00 adam
34 * Diagnostic record with error info. observed.
36 * Revision 1.7 1995/02/20 21:16:20 adam
37 * FML support. Bug fixes. Profile for drewdb.
39 * Revision 1.6 1995/02/17 14:41:14 quinn
40 * Added simple display of records.
42 * Revision 1.5 1995/02/17 14:22:13 adam
43 * First steps of CCL show command. Not finished yet.
45 * Revision 1.4 1995/02/17 09:08:36 adam
46 * Reply with subject. CCL base command implemented.
48 * Revision 1.3 1995/02/16 18:35:09 adam
49 * First use of Zdist library. Search requests are supported.
50 * Present requests are not supported yet.
52 * Revision 1.2 1995/02/16 13:21:00 adam
53 * Organization of resource files for targets and conversion
54 * language implemented.
56 * Revision 1.1 1995/02/15 17:45:30 adam
57 * First version of email gateway kernel. Email requests are read
58 * from stdin. The output is transferred to an MTA if 'From' is
59 * found in the header - or stdout if absent. No Z39.50 client is used.
74 static void put_esc_str (const char *s)
79 if (*s == '\\' && s[1])
84 fputc ('\n', reply_fd);
87 fputc ('\t', reply_fd);
97 if (*s != ' ' || !escape_flag)
105 static int reopen_target (void)
109 gw_log (GW_LOG_WARN, "urp", "Zass free...");
110 info.zass = zass_open (info.hostname, info.port);
113 fprintf (reply_fd, "%s %s:%d\n",
114 gw_res_get (info.kernel_res, "gw.err.connect",
115 "Cannot connect to target"),
116 info.hostname, info.port);
119 v = gw_res_get (info.kernel_res, "gw.description", NULL);
121 fprintf (reply_fd, "%s\n", v);
122 fprintf (reply_fd, "%s %s:%d\n",
123 gw_res_get (info.kernel_res, "gw.msg.connect",
124 "Connection established to"),
125 info.hostname, info.port);
127 fprintf (reply_fd, "%s:\n%s\n",
128 gw_res_get (info.kernel_res, "gw.msg.databases",
129 "Available databases"),
134 static char line_buf[LINE_MAX+1];
136 static struct command_word {
138 char *resource_suffix;
146 { "continue", "continue" },
147 { "status", "status" },
148 { "cancel", "cancel" },
149 { "target", "target" },
153 static int command_search (struct command_word *tab, struct ccl_token *cmd,
154 const char *resource_prefix)
158 assert (resource_prefix);
161 while (tab->default_value)
163 char *cp, command_names[60];
164 char resource_name[60];
167 sprintf (resource_name, "%s%s", resource_prefix,
168 tab->resource_suffix);
169 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
171 strcpy (command_names, v);
177 if ((split = strchr (cp, ' ')))
179 if (cmd->len == strlen(cp) &&
180 !memcmp (cmd->name, cp, cmd->len))
192 static struct error_no_struct {
195 } error_ccl_tab[] = {
197 { CCL_ERR_TERM_EXPECTED, "term.expected" },
198 { CCL_ERR_RP_EXPECTED, "rp.expected" },
199 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
200 { CCL_ERR_OP_EXPECTED, "op.expected" },
201 { CCL_ERR_BAD_RP, "bad.rp" },
202 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
203 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
204 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
205 { CCL_ERR_BAD_RELATION, "bad.relation" },
206 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
207 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
208 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
212 static char *error_no_search (struct error_no_struct *tab, int no)
214 struct error_no_struct *p = tab;
215 while (p->resource_name)
218 return p->resource_name;
224 static int email_header (FILE *inf, char *from_str, char *subject_str)
228 while (fgets (line_buf, LINE_MAX, inf))
230 if (line_buf[0] == '\n')
232 if (strncmp (line_buf, "From ", 5) == 0)
233 sscanf (line_buf+4, "%s", from_str);
234 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
235 sscanf (line_buf+9, "%s", subject_str+1) == 1)
236 strcpy (subject_str, line_buf+9);
241 static void help_general (void)
243 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
244 "Commands available in this service:\n"));
247 static int exec_help (struct ccl_token *list)
252 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
253 "target <name> - selects a given target\n"));
255 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
256 "base <base>.. - selects databases\n"));
258 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
259 "find <query> - performs a search request\n"));
261 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
262 "show <spec> - retrieves and displays "
264 put_esc_str (gw_res_get (info.kernel_res, "gw.help.help",
265 "help - displays help\n"));
270 static int exec_find (struct ccl_token *list)
272 const struct zass_searchent *p;
273 struct gw_user_set *us;
275 struct ccl_rpn_node *rpn;
279 us = user_set_add ("Default", -1);
280 rpn = ccl_find (info.bibset, list, &error, &pos);
283 const char *v = NULL, *n;
286 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
288 n = error_no_search (error_ccl_tab, error);
291 sprintf (name, "gw.err.%s", n);
292 v = gw_res_get (info.kernel_res, name, NULL);
295 v = ccl_err_msg (error);
296 fprintf (reply_fd, "%s\n", v);
299 ccl_pr_tree (rpn, reply_fd);
300 fprintf (reply_fd, "\n");
302 if (!*info.databases)
304 fprintf (reply_fd, "%s\n",
305 gw_res_get (info.kernel_res, "gw.err.no.database",
306 "You must select database"));
309 gw_log (GW_LOG_DEBUG, "urp", "Searching in database %s",
312 p = zass_search (info.zass, rpn, "Default", info.databases);
315 if (p->errcode != -1)
317 fprintf (reply_fd, "%s %d: %s\n",
318 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
319 "Z39.50 error code"),
320 p->errcode, p->errstring);
323 fprintf (reply_fd, "%d %s\n", p->num,
324 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
329 static int exec_target (struct ccl_token *list)
332 if (list->kind == CCL_TOK_EOL)
335 memcpy (info.target, list->name, len);
336 info.target [len] = '\0';
339 return reopen_target ();
342 static int exec_base (struct ccl_token *list)
344 struct ccl_token *li = list;
348 if (list->kind == CCL_TOK_EOL)
350 free (info.databases);
351 while (li->kind != CCL_TOK_EOL)
355 if (li->kind == CCL_TOK_COMMA)
358 info.databases = malloc (len);
359 assert (info.databases);
362 while (li->kind != CCL_TOK_EOL)
364 memcpy (info.databases+len, li->name, li->len);
366 info.databases[len++] = ',';
368 if (li->kind == CCL_TOK_COMMA)
371 info.databases[len-1] = '\0';
375 struct command_word show_tab [] =
382 static void present (const char *set, int offset, int number,
383 struct ccl_token *format_token)
385 const struct zass_presentent *zp;
390 max_number = atoi (gw_res_get (info.kernel_res, "gw.max.show",
392 if (number > max_number)
394 gw_log (GW_LOG_DEBUG, "urp", "present in set %s", set);
395 gw_log (GW_LOG_DEBUG, "urp", "present of %d records from offset %d",
397 zp = zass_present(info.zass, (char *) set, offset, number);
403 fprintf (reply_fd, gw_res_get (info.kernel_res,
407 fprintf (reply_fd, "\n");
408 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
412 const char *arg_ar[5];
414 fprintf (reply_fd, "--- %d/%d ---\n",
415 i+offset, offset+zp->num-1);
416 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
418 if (pp->which == ZASS_REC_DIAG)
420 fprintf (reply_fd, "Record error %d: %s\n",
421 pp->errcode, pp->errstring);
424 else if (pp->which != ZASS_REC_USMARC)
426 fprintf (reply_fd, "Unknown record kind %d\n",
431 rec = iso2709_cvt (pp->record);
437 len = format_token->len;
438 memcpy (format_str, format_token->name, len);
439 format_str[len] = '\0';
441 if (info.fml && format_token &&
442 (!strcmp (format_str, "0") || !strcmp (format_str, "1")))
445 arg_ar[1] = format_str;
446 arg_ar[2] = " \\list";
447 arg_ar[3] = marc_to_str (info.fml, rec);
449 fml_exec_call_argv (info.fml, arg_ar);
452 iso2709_display (rec, reply_fd);
454 iso2709_display (rec, reply_fd);
459 fprintf (reply_fd, "Not a MARC record\n");
464 static int exec_show (struct ccl_token *list)
467 struct ccl_token *set_token = NULL;
468 struct ccl_token *format_token = NULL;
469 struct ccl_token *li = list;
470 int no_of_present = 0;
473 while (li->kind != CCL_TOK_EOL)
476 if (li->next->kind == CCL_TOK_EQ)
478 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
485 modifier_no = command_search (show_tab, li, "ccl.token.");
488 fprintf (reply_fd, "Unknown modifier in show\n");
492 if (modifier_no == 1) /* f = <name> ? */
494 else if (modifier_no == 2) /* p = <name> ? */
496 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
497 && li->next->kind == CCL_TOK_MINUS
498 && li->next->next != CCL_TOK_EOL)
504 fprintf (reply_fd, "%s\n", "Missing token after '='");
513 gw_log (GW_LOG_DEBUG, "urp", "Got set=%.*s", set_token->len,
516 gw_log (GW_LOG_DEBUG, "urp", "Got format=%.*s", format_token->len,
520 while (li->kind != CCL_TOK_EOL)
526 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
528 modifier_no = command_search (show_tab, li, "ccl.token.");
530 if (modifier_no == 2) /* p = <name> ? */
532 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
533 && li->next->kind == CCL_TOK_MINUS
534 && li->next->next != CCL_TOK_EOL)
537 memcpy (tmp_str, li->name, len);
538 tmp_str [len] = '\0';
539 offset = atoi (tmp_str);
543 memcpy (tmp_str, li->name, len);
544 tmp_str [len] = '\0';
545 number = atoi (tmp_str) - offset + 1;
550 memcpy (tmp_str, li->name, len);
551 tmp_str [len] = '\0';
552 offset = atoi (tmp_str);
561 memcpy (tmp_str, li->name, len);
563 number = atoi (tmp_str);
567 if (offset > 0 && number > 0)
569 struct gw_user_set *us;
573 len = set_token->len;
574 memcpy (tmp_str, set_token->name, len);
576 us = user_set_search (tmp_str);
579 us = user_set_search (NULL);
580 if (us && us->hits != -1) /* proper result-set? */
582 if (offset <= us->hits)
584 if (offset+number-1 > us->hits)
585 number = us->hits - offset+1;
586 present (us->name, offset, number, format_token);
589 else if (!no_of_present) /* display error message once! */
591 fprintf (reply_fd, "%s\n",
592 gw_res_get (info.kernel_res, "gw.err.no.set",
593 "No result-set generated"));
598 if (!no_of_present) /* no records shown so far? */
600 struct gw_user_set *us;
603 us = user_set_search (NULL);
604 if (us && us->hits != -1) /* proper result-set? */
606 default_show = atoi (gw_res_get (info.kernel_res,
607 "gw.default.show", "20"));
608 if (us->hits > default_show)
609 present (us->name, 1, default_show, format_token);
610 else if (us->hits > 0)
611 present (us->name, 1, us->hits, format_token);
613 else /* display error message */
615 fprintf (reply_fd, "%s\n",
616 gw_res_get (info.kernel_res, "gw.err.no.set",
617 "No result-set generated"));
624 static int exec_command (const char *str)
626 struct ccl_token *cmd = ccl_tokenize (str);
629 if (cmd->kind != CCL_TOK_EOL &&
630 (no = command_search (command_tab, cmd, "ccl.command.")))
632 if (!info.zass && no != 9 && no != 4)
634 fprintf (reply_fd, "\n> %s\n", str);
635 if (!info.zass && (no == 1 || no == 2 || no == 3))
637 fprintf (reply_fd, "%s\n",
638 gw_res_get (info.kernel_res, "gw.err.no.target",
639 "No connection established - "
646 return exec_find (cmd->next);
648 return exec_show (cmd->next);
650 return exec_base (cmd->next);
652 return exec_help (cmd->next);
654 return exec_target (cmd->next);
656 fprintf (reply_fd, "%s\n",
657 gw_res_get (info.kernel_res, "gw.err.unimplemented",
658 "Not implemented yet"));
663 fprintf (reply_fd, "\n> %s", str);
664 fprintf (reply_fd, " ^ %s\n",
665 gw_res_get (info.kernel_res, "gw.err.unknown.command",
667 "Use help to see list of commands"));
675 char subject_str[128];
677 char *reply_fname = NULL;
679 if (email_header (inf, from_str, subject_str))
681 gw_log (GW_LOG_WARN, "urp", "No message body");
686 reply_fname = tempnam (gw_res_get (info.kernel_res,
687 "gw.reply.tmp.dir", NULL),
688 gw_res_get (info.kernel_res,
689 "gw.reply.tmp.prefix", "gwr"));
691 reply_fd = fopen (reply_fname, "w");
694 gw_log (GW_LOG_FATAL, "urp", "Cannot create %s",
698 fprintf (reply_fd, "From: %s\n",
699 gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
700 fprintf (reply_fd, "Subject: ");
702 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
704 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
707 fprintf (reply_fd, "\n");
708 gw_log (GW_LOG_DEBUG, "urp", "Mail from %s", from_str);
711 gw_log (GW_LOG_WARN, "urp", "No From in email header");
712 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
713 "Email->Z39.50 gateway"));
714 while (fgets (line_buf, LINE_MAX, inf))
718 if (line_buf[0] == '\n')
720 if ((cp = strchr (line_buf, '\n')))
722 gw_log (GW_LOG_ACCT, "urp", "cmd: %s", line_buf);
723 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
724 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
725 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
726 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
727 if (isalpha (line_buf[0]))
728 exec_command (line_buf);
733 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
744 assert (reply_fname);
748 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
749 "/usr/lib/sendmail");
750 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
752 mta_code = system (cmd);
754 gw_log (GW_LOG_FATAL, "urp", "Reply '%s' got exit code %d",
756 unlink (reply_fname);
757 gw_log (GW_LOG_DEBUG, "urp", "Normal session for user %s", from_str);