5 * Revision 1.20 1995/03/03 17:19:17 adam
6 * Smarter presentation. Bug fix in email header interpretation.
8 * Revision 1.19 1995/03/02 09:32:11 adam
9 * New presentation formats. f0=full, f1=brief, f2=mid
11 * Revision 1.18 1995/03/01 14:32:26 adam
12 * Better diagnostics. Default is, that only one database selected when
15 * Revision 1.17 1995/02/28 13:16:26 adam
16 * Configurable From: added.
18 * Revision 1.16 1995/02/23 10:08:20 adam
19 * Added logging of all user commands.
21 * Revision 1.15 1995/02/23 08:32:17 adam
24 * Revision 1.13 1995/02/22 16:54:42 adam
25 * Qualifiers of LOC target updated. More logging messages.
27 * Revision 1.12 1995/02/22 15:51:51 adam
28 * Bug fix: swap of parameter number and offset in function present.
30 * Revision 1.11 1995/02/22 15:22:33 adam
31 * Much more checking of run-time state. Show command never retrieves
32 * more records than indicated by the previous search request. Help
33 * command available. The maximum number of records retrieved can be
36 * Revision 1.10 1995/02/22 08:51:35 adam
37 * Output function can be customized in fml, which is used to print
38 * the reply to reply_fd.
40 * Revision 1.9 1995/02/21 17:46:21 adam
43 * Revision 1.8 1995/02/21 12:12:00 adam
44 * Diagnostic record with error info. observed.
46 * Revision 1.7 1995/02/20 21:16:20 adam
47 * FML support. Bug fixes. Profile for drewdb.
49 * Revision 1.6 1995/02/17 14:41:14 quinn
50 * Added simple display of records.
52 * Revision 1.5 1995/02/17 14:22:13 adam
53 * First steps of CCL show command. Not finished yet.
55 * Revision 1.4 1995/02/17 09:08:36 adam
56 * Reply with subject. CCL base command implemented.
58 * Revision 1.3 1995/02/16 18:35:09 adam
59 * First use of Zdist library. Search requests are supported.
60 * Present requests are not supported yet.
62 * Revision 1.2 1995/02/16 13:21:00 adam
63 * Organization of resource files for targets and conversion
64 * language implemented.
66 * Revision 1.1 1995/02/15 17:45:30 adam
67 * First version of email gateway kernel. Email requests are read
68 * from stdin. The output is transferred to an MTA if 'From' is
69 * found in the header - or stdout if absent. No Z39.50 client is used.
85 static void put_esc_str (const char *s)
91 static int reopen_target (void)
95 gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
96 info.zass = zass_open (info.hostname, info.port);
99 fprintf (reply_fd, "%s %s:%d\n",
100 gw_res_get (info.kernel_res, "gw.err.connect",
101 "Cannot connect to target"),
102 info.hostname, info.port);
105 v = gw_res_get (info.kernel_res, "gw.description", NULL);
109 fprintf (reply_fd, "\n");
111 fprintf (reply_fd, "%s %s:%d\n",
112 gw_res_get (info.kernel_res, "gw.msg.connect",
113 "Connection established to"),
114 info.hostname, info.port);
116 fprintf (reply_fd, "%s:\n%s\n",
117 gw_res_get (info.kernel_res, "gw.msg.databases",
118 "Available databases"),
121 fprintf (reply_fd, "%s:\n%s\n",
122 gw_res_get (info.kernel_res, "gw.msg.database",
123 "Selected databases"),
128 static char line_buf[LINE_MAX+1];
130 static struct command_word {
132 char *resource_suffix;
140 { "continue", "continue" },
141 { "status", "status" },
142 { "cancel", "cancel" },
143 { "target", "target" },
147 static int command_search (struct command_word *tab, struct ccl_token *cmd,
148 const char *resource_prefix)
152 assert (resource_prefix);
155 while (tab->default_value)
157 char *cp, command_names[60];
158 char resource_name[60];
161 sprintf (resource_name, "%s%s", resource_prefix,
162 tab->resource_suffix);
163 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
165 strcpy (command_names, v);
171 if ((split = strchr (cp, ' ')))
173 if (cmd->len == strlen(cp) &&
174 !memcmp (cmd->name, cp, cmd->len))
186 static struct error_no_struct {
189 } error_ccl_tab[] = {
191 { CCL_ERR_TERM_EXPECTED, "term.expected" },
192 { CCL_ERR_RP_EXPECTED, "rp.expected" },
193 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
194 { CCL_ERR_OP_EXPECTED, "op.expected" },
195 { CCL_ERR_BAD_RP, "bad.rp" },
196 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
197 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
198 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
199 { CCL_ERR_BAD_RELATION, "bad.relation" },
200 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
201 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
202 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
206 static char *error_no_search (struct error_no_struct *tab, int no)
208 struct error_no_struct *p = tab;
209 while (p->resource_name)
212 return p->resource_name;
218 static int email_header (FILE *inf, char *from_str, char *subject_str)
222 while (fgets (line_buf, LINE_MAX, inf))
224 if (line_buf[0] == '\n')
226 if (strncmp (line_buf, "From ", 5) == 0)
227 sscanf (line_buf+4, "%s", from_str);
228 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
229 sscanf (line_buf+9, "%s", subject_str+1) == 1)
230 strcpy (subject_str, line_buf+9);
235 static void help_general (void)
237 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
238 "Commands available in this service:\n"));
241 static int exec_help (struct ccl_token *list)
246 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
247 "target <name> - selects a given target\n"));
249 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
250 "base <base>.. - selects databases\n"));
252 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
253 "find <query> - performs a search request\n"));
255 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
256 "show <spec> - retrieves and displays "
262 static void display_diag_error (int code, const char *addinfo)
266 sprintf (str, "gw.bib1.diag.%d", code);
267 fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
268 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
271 gw_res_get (info.kernel_res, str, ""), addinfo);
274 static int exec_find (struct ccl_token *list)
276 const struct zass_searchent *p;
277 struct gw_user_set *us;
279 struct ccl_rpn_node *rpn;
283 us = user_set_add ("Default", -1);
284 rpn = ccl_find (info.bibset, list, &error, &pos);
287 const char *v = NULL, *n;
290 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
292 n = error_no_search (error_ccl_tab, error);
295 sprintf (name, "gw.err.%s", n);
296 v = gw_res_get (info.kernel_res, name, NULL);
299 v = ccl_err_msg (error);
300 fprintf (reply_fd, "%s\n", v);
303 ccl_pr_tree (rpn, reply_fd);
304 fprintf (reply_fd, "\n");
306 if (!*info.database )
308 fprintf (reply_fd, "%s\n",
309 gw_res_get (info.kernel_res, "gw.err.no.database",
310 "You must select database"));
313 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
316 p = zass_search (info.zass, rpn, "Default", info.database );
319 if (p->errcode != -1)
321 display_diag_error (p->errcode, p->errstring);
324 fprintf (reply_fd, "%d %s\n", p->num,
325 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
330 static int exec_target (struct ccl_token *list)
333 if (list->kind == CCL_TOK_EOL)
336 memcpy (info.target, list->name, len);
337 info.target [len] = '\0';
340 return reopen_target ();
343 static int exec_base (struct ccl_token *list)
345 struct ccl_token *li = list;
349 if (list->kind == CCL_TOK_EOL)
351 free (info.database);
352 while (li->kind != CCL_TOK_EOL)
356 if (li->kind == CCL_TOK_COMMA)
359 info.database = malloc (len);
360 assert (info.database );
363 while (li->kind != CCL_TOK_EOL)
365 memcpy (info.database+len, li->name, li->len);
367 info.database[len++] = ',';
369 if (li->kind == CCL_TOK_COMMA)
372 info.database[len-1] = '\0';
376 struct command_word show_tab [] =
383 static void present (const char *set, int offset, int number,
384 struct ccl_token *format_token)
386 const struct zass_presentent *zp;
391 max_number = atoi (gw_res_get (info.kernel_res, "gw.max.show",
393 if (number > max_number)
395 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
396 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
398 zp = zass_present(info.zass, (char *) set, offset, number);
404 int record_log_fd = -1;
405 const char *record_log_name;
407 record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
411 sprintf (path, "%s/%s", gw_res_get (info.kernel_res,
414 record_log_fd = open (path, O_WRONLY|O_CREAT|O_APPEND, 0666);
415 if (record_log_fd == -1)
416 gw_log (GW_LOG_WARN, "Cannot open %s", path);
418 fprintf (reply_fd, gw_res_get (info.kernel_res,
422 fprintf (reply_fd, "\n");
423 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
427 const char *arg_ar[5];
429 fprintf (reply_fd, "--- %d/%d ---\n",
430 i+offset, offset+zp->num-1);
431 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
433 if (pp->which == ZASS_REC_DIAG)
435 display_diag_error (pp->errcode, pp->errstring);
438 else if (pp->which != ZASS_REC_USMARC)
440 fprintf (reply_fd, "Unknown record kind %d\n",
445 if (record_log_fd != -1)
446 write (record_log_fd, pp->record, strlen(pp->record));
447 rec = iso2709_cvt (pp->record);
453 len = format_token->len;
454 if (len >= sizeof(format_str))
455 len = sizeof(format_str)-1;
456 memcpy (format_str, format_token->name, len);
457 format_str[len] = '\0';
459 if (info.fml && format_token &&
460 (!strcmp (format_str, "0") || !strcmp (format_str, "1")
461 || !strcmp(format_str, "2")))
464 arg_ar[1] = format_str;
465 arg_ar[2] = " \\list";
466 arg_ar[3] = marc_to_str (info.fml, rec);
468 fml_exec_call_argv (info.fml, arg_ar);
471 iso2709_display (rec, reply_fd);
473 iso2709_display (rec, reply_fd);
478 fprintf (reply_fd, "Not a MARC record\n");
480 if (record_log_fd != -1)
481 close (record_log_fd);
485 static int exec_show (struct ccl_token *list)
488 struct ccl_token *set_token = NULL;
489 struct ccl_token *format_token = NULL;
490 struct ccl_token *li = list;
491 int no_of_present = 0;
494 while (li->kind != CCL_TOK_EOL)
497 if (li->next->kind == CCL_TOK_EQ)
499 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
506 modifier_no = command_search (show_tab, li, "ccl.token.");
509 fprintf (reply_fd, "Unknown modifier in show\n");
513 if (modifier_no == 1) /* f = <name> ? */
515 else if (modifier_no == 2) /* p = <name> ? */
517 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
518 && li->next->kind == CCL_TOK_MINUS
519 && li->next->next != CCL_TOK_EOL)
525 fprintf (reply_fd, "%s\n", "Missing token after '='");
534 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
537 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
541 while (li->kind != CCL_TOK_EOL)
547 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
549 modifier_no = command_search (show_tab, li, "ccl.token.");
551 if (modifier_no == 2) /* p = <name> ? */
553 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
554 && li->next->kind == CCL_TOK_MINUS
555 && li->next->next != CCL_TOK_EOL)
558 memcpy (tmp_str, li->name, len);
559 tmp_str [len] = '\0';
560 offset = atoi (tmp_str);
564 memcpy (tmp_str, li->name, len);
565 tmp_str [len] = '\0';
566 number = atoi (tmp_str) - offset + 1;
571 memcpy (tmp_str, li->name, len);
572 tmp_str [len] = '\0';
573 offset = atoi (tmp_str);
582 memcpy (tmp_str, li->name, len);
584 number = atoi (tmp_str);
588 if (offset > 0 && number > 0)
590 struct gw_user_set *us;
594 len = set_token->len;
595 memcpy (tmp_str, set_token->name, len);
597 us = user_set_search (tmp_str);
600 us = user_set_search (NULL);
601 if (us && us->hits != -1) /* proper result-set? */
603 if (offset <= us->hits)
605 if (offset+number-1 > us->hits)
606 number = us->hits - offset+1;
607 present (us->name, offset, number, format_token);
610 else if (!no_of_present) /* display error message once! */
612 fprintf (reply_fd, "%s\n",
613 gw_res_get (info.kernel_res, "gw.err.no.set",
614 "No result-set generated"));
619 if (!no_of_present) /* no records shown so far? */
621 struct gw_user_set *us;
624 us = user_set_search (NULL);
625 if (us && us->hits != -1) /* proper result-set? */
627 default_show = atoi (gw_res_get (info.kernel_res,
628 "gw.default.show", "20"));
629 if (us->hits > default_show)
630 present (us->name, 1, default_show, format_token);
631 else if (us->hits > 0)
632 present (us->name, 1, us->hits, format_token);
634 else /* display error message */
636 fprintf (reply_fd, "%s\n",
637 gw_res_get (info.kernel_res, "gw.err.no.set",
638 "No result-set generated"));
645 static int exec_command (const char *str)
647 struct ccl_token *cmd = ccl_tokenize (str);
650 if (cmd->kind != CCL_TOK_EOL &&
651 (no = command_search (command_tab, cmd, "ccl.command.")))
653 if (!info.zass && no != 9 && no != 4)
655 fprintf (reply_fd, "\n> %s\n", str);
656 if (!info.zass && (no == 1 || no == 2 || no == 3))
658 fprintf (reply_fd, "%s\n",
659 gw_res_get (info.kernel_res, "gw.err.no.target",
660 "No connection established - "
667 return exec_find (cmd->next);
669 return exec_show (cmd->next);
671 return exec_base (cmd->next);
673 return exec_help (cmd->next);
675 return exec_target (cmd->next);
677 fprintf (reply_fd, "%s\n",
678 gw_res_get (info.kernel_res, "gw.err.unimplemented",
679 "Not implemented yet"));
684 fprintf (reply_fd, "\n> %s\n", str);
685 fprintf (reply_fd, " ^ %s\n",
686 gw_res_get (info.kernel_res, "gw.err.unknown.command",
688 "Use help to see list of commands"));
696 char subject_str[128];
698 char *reply_fname = NULL;
700 if (email_header (inf, from_str, subject_str))
702 gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
705 tty_init (stdout, 40, 70);
708 reply_fname = tempnam (gw_res_get (info.kernel_res,
709 "gw.reply.tmp.dir", NULL),
710 gw_res_get (info.kernel_res,
711 "gw.reply.tmp.prefix", "gwr"));
713 reply_fd = fopen (reply_fname, "w");
716 gw_log (GW_LOG_FATAL, KERNEL_LOG, "Cannot create %s",
720 tty_init (reply_fd, 0, 0);
721 fprintf (reply_fd, "From: %s\n",
722 gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
723 fprintf (reply_fd, "Subject: ");
725 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
727 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
730 fprintf (reply_fd, "\n");
731 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", from_str);
734 gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
735 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
736 "Email->Z39.50 gateway"));
737 while (fgets (line_buf, LINE_MAX, inf))
741 if (line_buf[0] == '\n')
746 if ((cp = strchr (line_buf, '\n')))
748 gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
749 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
750 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
751 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
752 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
753 if (isalpha (line_buf[0]))
754 exec_command (line_buf);
759 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
770 assert (reply_fname);
774 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
775 "/usr/lib/sendmail");
776 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
778 mta_code = system (cmd);
780 gw_log (GW_LOG_FATAL, KERNEL_LOG, "Reply '%s' got exit code %d",
782 unlink (reply_fname);
783 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", from_str);