5 * Revision 1.2 1995/02/16 13:21:00 adam
6 * Organization of resource files for targets and conversion
7 * language implemented.
9 * Revision 1.1 1995/02/15 17:45:30 adam
10 * First version of email gateway kernel. Email requests are read
11 * from stdin. The output is transferred to an MTA if 'From' is
12 * found in the header - or stdout if absent. No Z39.50 client is used.
26 static char line_buf[LINE_MAX+1];
28 static struct command_word {
30 char *resource_suffix;
38 { "continue", "continue" },
39 { "status", "status" },
40 { "cancel", "cancel" },
41 { "target", "target" },
45 static int command_search (struct command_word *tab, struct ccl_token *cmd,
46 const char *resource_prefix)
50 assert (resource_prefix);
53 while (tab->default_value)
55 char *cp, command_names[60];
56 char resource_name[60];
59 sprintf (resource_name, "%s%s", resource_prefix,
60 tab->resource_suffix);
61 v = gw_res_get (kernel_res, resource_name, tab->default_value);
63 strcpy (command_names, v);
69 if ((split = strchr (cp, ' ')))
71 if (cmd->len == strlen(cp) &&
72 !memcmp (cmd->name, cp, cmd->len))
84 static struct error_no_struct {
89 { CCL_ERR_TERM_EXPECTED, "term.expected" },
90 { CCL_ERR_RP_EXPECTED, "rp.expected" },
91 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
92 { CCL_ERR_OP_EXPECTED, "op.expected" },
93 { CCL_ERR_BAD_RP, "bad.rp" },
94 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
95 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
96 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
97 { CCL_ERR_BAD_RELATION, "bad.relation" },
98 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
99 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
100 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
104 static char *error_no_search (struct error_no_struct *tab, int no)
106 struct error_no_struct *p = tab;
107 while (p->resource_name)
110 return p->resource_name;
116 static int email_header (FILE *inf, char *from_str)
119 while (fgets (line_buf, LINE_MAX, inf))
121 if (line_buf[0] == '\n')
123 if (strncmp (line_buf, "From ", 5) == 0)
124 sscanf (line_buf+4, "%s", from_str);
129 static int exec_find (struct ccl_token *list)
131 struct ccl_rpn_node *rpn;
135 rpn = ccl_find (bibset, list, &error, &pos);
138 const char *v = NULL, *n;
141 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
143 n = error_no_search (error_ccl_tab, error);
146 sprintf (name, "gw.err.%s", n);
147 v = gw_res_get (kernel_res, name, NULL);
150 v = ccl_err_msg (error);
151 fprintf (reply_fd, "%s\n", v);
154 ccl_pr_tree (rpn, reply_fd);
155 fprintf (reply_fd, "\n");
159 static int exec_command (const char *str)
161 struct ccl_token *cmd = ccl_tokenize (str);
164 fprintf (reply_fd, "> %s", str);
165 if (cmd->kind != CCL_TOK_EOL &&
166 (no = command_search (command_tab, cmd, "ccl.command.")))
171 return exec_find (cmd->next);
174 fprintf (reply_fd, " %s\n",
175 gw_res_get (kernel_res, "gw.err.unimplemented",
176 "Not implemented yet"));
181 fprintf (reply_fd, " ^ %s\n",
182 gw_res_get (kernel_res, "gw.err.unknown.command",
192 char *reply_fname = NULL;
194 if (email_header (inf, from_str))
196 gw_log (GW_LOG_WARN, "urp", "No message body");
201 reply_fname = tempnam (gw_res_get (kernel_res,
202 "gw.reply.tmp.dir", NULL),
203 gw_res_get (kernel_res,
204 "gw.reply.tmp.prefix", "gwr"));
206 reply_fd = fopen (reply_fname, "w");
209 gw_log (GW_LOG_FATAL, "urp", "Cannot create %s",
215 gw_log (GW_LOG_WARN, "urp", "No From in email header");
216 fprintf (reply_fd, "%s\n", gw_res_get (kernel_res, "gw.msg.greeting",
217 "Email->Z39.50 gateway"));
218 while (fgets (line_buf, LINE_MAX, inf))
220 if (line_buf[0] == '\n')
222 ccl_token_and = gw_res_get (kernel_res, "ccl.token.and", "and");
223 ccl_token_or = gw_res_get (kernel_res, "ccl.token.or", "or");
224 ccl_token_not = gw_res_get (kernel_res, "ccl.token.not", "not");
225 ccl_token_set = gw_res_get (kernel_res, "ccl.token.set", "set");
226 if (isalpha (line_buf[0]))
227 exec_command (line_buf);
231 fprintf (reply_fd, "%s\n", gw_res_get (kernel_res, "gw.err.nullbody",
239 assert (reply_fname);
243 mta = gw_res_get (kernel_res, "gw.reply.mta", "/usr/lib/sendmail");
244 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
246 mta_code = system (cmd);
248 gw_log (GW_LOG_FATAL, "urp", "Reply '%s' got exit code %d",
250 unlink (reply_fname);