5 * Revision 1.3 1995/02/16 18:35:09 adam
6 * First use of Zdist library. Search requests are supported.
7 * Present requests are not supported yet.
9 * Revision 1.2 1995/02/16 13:21:00 adam
10 * Organization of resource files for targets and conversion
11 * language implemented.
13 * Revision 1.1 1995/02/15 17:45:30 adam
14 * First version of email gateway kernel. Email requests are read
15 * from stdin. The output is transferred to an MTA if 'From' is
16 * found in the header - or stdout if absent. No Z39.50 client is used.
30 static int reopen_target (void)
34 gw_log (GW_LOG_WARN, "urp", "Zass free...");
35 info.zass = zass_open (info.hostname, info.port);
38 fprintf (reply_fd, "%s %s:%d\n",
39 gw_res_get (info.kernel_res, "gw.err.connect",
40 "Cannot connect to target"),
41 info.hostname, info.port);
44 v = gw_res_get (info.kernel_res, "gw.description", NULL);
46 fprintf (reply_fd, "%s\n", v);
47 fprintf (reply_fd, "%s %s:%d\n %s\n",
48 gw_res_get (info.kernel_res, "gw.msg.databases",
49 "Available databases on"),
50 info.hostname, info.port, info.databases);
54 static char line_buf[LINE_MAX+1];
56 static struct command_word {
58 char *resource_suffix;
66 { "continue", "continue" },
67 { "status", "status" },
68 { "cancel", "cancel" },
69 { "target", "target" },
73 static int command_search (struct command_word *tab, struct ccl_token *cmd,
74 const char *resource_prefix)
78 assert (resource_prefix);
81 while (tab->default_value)
83 char *cp, command_names[60];
84 char resource_name[60];
87 sprintf (resource_name, "%s%s", resource_prefix,
88 tab->resource_suffix);
89 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
91 strcpy (command_names, v);
97 if ((split = strchr (cp, ' ')))
99 if (cmd->len == strlen(cp) &&
100 !memcmp (cmd->name, cp, cmd->len))
112 static struct error_no_struct {
115 } error_ccl_tab[] = {
117 { CCL_ERR_TERM_EXPECTED, "term.expected" },
118 { CCL_ERR_RP_EXPECTED, "rp.expected" },
119 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
120 { CCL_ERR_OP_EXPECTED, "op.expected" },
121 { CCL_ERR_BAD_RP, "bad.rp" },
122 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
123 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
124 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
125 { CCL_ERR_BAD_RELATION, "bad.relation" },
126 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
127 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
128 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
132 static char *error_no_search (struct error_no_struct *tab, int no)
134 struct error_no_struct *p = tab;
135 while (p->resource_name)
138 return p->resource_name;
144 static int email_header (FILE *inf, char *from_str)
147 while (fgets (line_buf, LINE_MAX, inf))
149 if (line_buf[0] == '\n')
151 if (strncmp (line_buf, "From ", 5) == 0)
152 sscanf (line_buf+4, "%s", from_str);
157 static int exec_find (struct ccl_token *list)
159 const struct zass_searchent *p;
161 struct ccl_rpn_node *rpn;
165 rpn = ccl_find (info.bibset, list, &error, &pos);
168 const char *v = NULL, *n;
171 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
173 n = error_no_search (error_ccl_tab, error);
176 sprintf (name, "gw.err.%s", n);
177 v = gw_res_get (info.kernel_res, name, NULL);
180 v = ccl_err_msg (error);
181 fprintf (reply_fd, "%s\n", v);
184 ccl_pr_tree (rpn, reply_fd);
185 fprintf (reply_fd, "\n");
189 p = zass_search (info.zass, rpn, "Default", info.databases);
192 fprintf (reply_fd, "%d %s\n", p->num,
193 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
194 if (p->errcode != -1)
195 fprintf (reply_fd, "%s %d: %s\n",
196 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
197 "Z39.50 error code"),
198 p->errcode, p->errstring);
202 static int exec_target (struct ccl_token *list)
204 if (list->kind == CCL_TOK_EOL)
206 memcpy (info.target, list->name, list->len);
207 info.target [list->len] = '\0';
210 return reopen_target ();
213 static int exec_command (const char *str)
215 struct ccl_token *cmd = ccl_tokenize (str);
218 if (cmd->kind != CCL_TOK_EOL &&
219 (no = command_search (command_tab, cmd, "ccl.command.")))
221 if (!info.zass && no != 9)
223 fprintf (reply_fd, "\n> %s", str);
227 return exec_find (cmd->next);
230 return exec_target (cmd->next);
233 fprintf (reply_fd, "%s\n",
234 gw_res_get (info.kernel_res, "gw.err.unimplemented",
235 "Not implemented yet"));
240 fprintf (reply_fd, " ^ %s\n",
241 gw_res_get (info.kernel_res, "gw.err.unknown.command",
251 char *reply_fname = NULL;
253 if (email_header (inf, from_str))
255 gw_log (GW_LOG_WARN, "urp", "No message body");
260 reply_fname = tempnam (gw_res_get (info.kernel_res,
261 "gw.reply.tmp.dir", NULL),
262 gw_res_get (info.kernel_res,
263 "gw.reply.tmp.prefix", "gwr"));
265 reply_fd = fopen (reply_fname, "w");
268 gw_log (GW_LOG_FATAL, "urp", "Cannot create %s",
274 gw_log (GW_LOG_WARN, "urp", "No From in email header");
275 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
276 "Email->Z39.50 gateway"));
277 while (fgets (line_buf, LINE_MAX, inf))
279 if (line_buf[0] == '\n')
281 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
282 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
283 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
284 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
285 if (isalpha (line_buf[0]))
286 exec_command (line_buf);
290 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
299 assert (reply_fname);
303 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
304 "/usr/lib/sendmail");
305 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
307 mta_code = system (cmd);
309 gw_log (GW_LOG_FATAL, "urp", "Reply '%s' got exit code %d",
311 unlink (reply_fname);