5 * Revision 1.4 1995/02/17 09:08:36 adam
6 * Reply with subject. CCL base command implemented.
8 * Revision 1.3 1995/02/16 18:35:09 adam
9 * First use of Zdist library. Search requests are supported.
10 * Present requests are not supported yet.
12 * Revision 1.2 1995/02/16 13:21:00 adam
13 * Organization of resource files for targets and conversion
14 * language implemented.
16 * Revision 1.1 1995/02/15 17:45:30 adam
17 * First version of email gateway kernel. Email requests are read
18 * from stdin. The output is transferred to an MTA if 'From' is
19 * found in the header - or stdout if absent. No Z39.50 client is used.
34 static int reopen_target (void)
38 gw_log (GW_LOG_WARN, "urp", "Zass free...");
39 info.zass = zass_open (info.hostname, info.port);
42 fprintf (reply_fd, "%s %s:%d\n",
43 gw_res_get (info.kernel_res, "gw.err.connect",
44 "Cannot connect to target"),
45 info.hostname, info.port);
48 v = gw_res_get (info.kernel_res, "gw.description", NULL);
50 fprintf (reply_fd, "%s\n", v);
51 fprintf (reply_fd, "%s %s:%d\n %s\n",
52 gw_res_get (info.kernel_res, "gw.msg.databases",
53 "Available databases on"),
54 info.hostname, info.port, info.databases);
58 static char line_buf[LINE_MAX+1];
60 static struct command_word {
62 char *resource_suffix;
70 { "continue", "continue" },
71 { "status", "status" },
72 { "cancel", "cancel" },
73 { "target", "target" },
77 static int command_search (struct command_word *tab, struct ccl_token *cmd,
78 const char *resource_prefix)
82 assert (resource_prefix);
85 while (tab->default_value)
87 char *cp, command_names[60];
88 char resource_name[60];
91 sprintf (resource_name, "%s%s", resource_prefix,
92 tab->resource_suffix);
93 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
95 strcpy (command_names, v);
101 if ((split = strchr (cp, ' ')))
103 if (cmd->len == strlen(cp) &&
104 !memcmp (cmd->name, cp, cmd->len))
116 static struct error_no_struct {
119 } error_ccl_tab[] = {
121 { CCL_ERR_TERM_EXPECTED, "term.expected" },
122 { CCL_ERR_RP_EXPECTED, "rp.expected" },
123 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
124 { CCL_ERR_OP_EXPECTED, "op.expected" },
125 { CCL_ERR_BAD_RP, "bad.rp" },
126 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
127 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
128 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
129 { CCL_ERR_BAD_RELATION, "bad.relation" },
130 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
131 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
132 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
136 static char *error_no_search (struct error_no_struct *tab, int no)
138 struct error_no_struct *p = tab;
139 while (p->resource_name)
142 return p->resource_name;
148 static int email_header (FILE *inf, char *from_str, char *subject_str)
152 while (fgets (line_buf, LINE_MAX, inf))
154 if (line_buf[0] == '\n')
156 if (strncmp (line_buf, "From ", 5) == 0)
157 sscanf (line_buf+4, "%s", from_str);
158 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
159 sscanf (line_buf+9, "%s", subject_str+1) == 1)
160 strcpy (subject_str, line_buf+9);
165 static int exec_find (struct ccl_token *list)
167 const struct zass_searchent *p;
169 struct ccl_rpn_node *rpn;
173 rpn = ccl_find (info.bibset, list, &error, &pos);
176 const char *v = NULL, *n;
179 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
181 n = error_no_search (error_ccl_tab, error);
184 sprintf (name, "gw.err.%s", n);
185 v = gw_res_get (info.kernel_res, name, NULL);
188 v = ccl_err_msg (error);
189 fprintf (reply_fd, "%s\n", v);
192 ccl_pr_tree (rpn, reply_fd);
193 fprintf (reply_fd, "\n");
197 p = zass_search (info.zass, rpn, "Default", info.databases);
200 fprintf (reply_fd, "%d %s\n", p->num,
201 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
202 if (p->errcode != -1)
203 fprintf (reply_fd, "%s %d: %s\n",
204 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
205 "Z39.50 error code"),
206 p->errcode, p->errstring);
210 static int exec_target (struct ccl_token *list)
213 if (list->kind == CCL_TOK_EOL)
216 memcpy (info.target, list->name, len);
217 info.target [len] = '\0';
220 return reopen_target ();
223 static int exec_base (struct ccl_token *list)
225 struct ccl_token *li = list;
228 if (list->kind == CCL_TOK_EOL)
230 free (info.databases);
231 while (li->kind != CCL_TOK_EOL)
235 if (li->kind == CCL_TOK_COMMA)
238 info.databases = malloc (len);
239 assert (info.databases);
242 while (li->kind != CCL_TOK_EOL)
244 memcpy (info.databases+len, li->name, li->len);
246 info.databases[len++] = ',';
248 if (li->kind == CCL_TOK_COMMA)
251 info.databases[len-1] = '\0';
255 static int exec_command (const char *str)
257 struct ccl_token *cmd = ccl_tokenize (str);
260 if (cmd->kind != CCL_TOK_EOL &&
261 (no = command_search (command_tab, cmd, "ccl.command.")))
263 if (!info.zass && no != 9)
265 fprintf (reply_fd, "\n> %s", str);
269 return exec_find (cmd->next);
271 return exec_base (cmd->next);
273 return exec_target (cmd->next);
275 fprintf (reply_fd, "%s\n",
276 gw_res_get (info.kernel_res, "gw.err.unimplemented",
277 "Not implemented yet"));
282 fprintf (reply_fd, " ^ %s\n",
283 gw_res_get (info.kernel_res, "gw.err.unknown.command",
292 char subject_str[128];
294 char *reply_fname = NULL;
296 if (email_header (inf, from_str, subject_str))
298 gw_log (GW_LOG_WARN, "urp", "No message body");
303 reply_fname = tempnam (gw_res_get (info.kernel_res,
304 "gw.reply.tmp.dir", NULL),
305 gw_res_get (info.kernel_res,
306 "gw.reply.tmp.prefix", "gwr"));
308 reply_fd = fopen (reply_fname, "w");
311 gw_log (GW_LOG_FATAL, "urp", "Cannot create %s",
315 fprintf (reply_fd, "Subject: ");
317 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
319 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
321 "Your Z39.50 Query"));
322 fprintf (reply_fd, "\n");
325 gw_log (GW_LOG_WARN, "urp", "No From in email header");
326 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
327 "Email->Z39.50 gateway"));
328 while (fgets (line_buf, LINE_MAX, inf))
330 if (line_buf[0] == '\n')
332 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
333 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
334 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
335 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
336 if (isalpha (line_buf[0]))
337 exec_command (line_buf);
341 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
350 assert (reply_fname);
354 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
355 "/usr/lib/sendmail");
356 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
358 mta_code = system (cmd);
360 gw_log (GW_LOG_FATAL, "urp", "Reply '%s' got exit code %d",
362 unlink (reply_fname);