5 * Revision 1.1 1995/02/15 17:45:30 adam
6 * First version of email gateway kernel. Email requests are read
7 * from stdin. The output is transferred to an MTA if 'From' is
8 * found in the header - or stdout if absent. No Z39.50 client is used.
22 static char line_buf[LINE_MAX+1];
24 static struct command_word {
26 char *resource_suffix;
34 { "continue", "continue" },
35 { "status", "status" },
36 { "cancel", "cancel" },
40 static int command_search (struct command_word *tab, struct ccl_token *cmd,
41 const char *resource_prefix)
45 assert (resource_prefix);
48 while (tab->default_value)
50 char *cp, command_names[60];
51 char resource_name[60];
54 sprintf (resource_name, "%s%s", resource_prefix,
55 tab->resource_suffix);
56 v = gw_res_get (kernel_res, resource_name, tab->default_value);
58 strcpy (command_names, v);
64 if ((split = strchr (cp, ' ')))
66 if (cmd->len == strlen(cp) &&
67 !memcmp (cmd->name, cp, cmd->len))
79 static int email_header (FILE *inf, char *from_str)
82 while (fgets (line_buf, LINE_MAX, inf))
84 if (line_buf[0] == '\n')
86 if (strncmp (line_buf, "From ", 5) == 0)
87 sscanf (line_buf+4, "%s", from_str);
92 static int exec_find (struct ccl_token *list)
94 struct ccl_rpn_node *rpn;
98 rpn = ccl_find (bibset, list, &error, &pos);
101 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
102 fprintf (reply_fd, "%s\n", ccl_err_msg (error));
105 ccl_pr_tree (rpn, reply_fd);
106 fprintf (reply_fd, "\n");
110 static int exec_command (const char *str)
112 struct ccl_token *cmd = ccl_tokenize (str);
115 ccl_token_and = gw_res_get (kernel_res, "ccl.token.and", "and");
116 ccl_token_or = gw_res_get (kernel_res, "ccl.token.or", "or");
117 ccl_token_not = gw_res_get (kernel_res, "ccl.token.not", "not");
118 ccl_token_set = gw_res_get (kernel_res, "ccl.token.set", "set");
120 fprintf (reply_fd, "> %s", str);
121 if (cmd->kind == CCL_TOK_TERM &&
122 (no = command_search (command_tab, cmd, "ccl.command.")))
127 return exec_find (cmd->next);
130 fprintf (reply_fd, " show found\n");
133 fprintf (reply_fd, " unimplemented command\n");
137 fprintf (reply_fd, " ^ unknown command\n");
145 char *reply_fname = NULL;
147 if (email_header (inf, from_str))
149 gw_log (GW_LOG_WARN, "urp", "No message body");
154 reply_fname = tempnam (gw_res_get (kernel_res,
155 "gw.reply.tmp.dir", NULL),
156 gw_res_get (kernel_res,
157 "gw.reply.tmp.prefix", "gwr"));
159 reply_fd = fopen (reply_fname, "w");
162 gw_log (GW_LOG_FATAL, "urp", "Cannot create %s",
167 fprintf (reply_fd, "%s\n", gw_res_get (kernel_res, "gw.msg.greeting",
168 "Email->Z39.50 gateway"));
169 while (fgets (line_buf, LINE_MAX, inf))
171 if (line_buf[0] == '\n')
173 if (isalpha (line_buf[0]))
174 exec_command (line_buf);
178 fprintf (reply_fd, "%s\n", gw_res_get (kernel_res, "gw.err.nullbody",
186 assert (reply_fname);
190 mta = gw_res_get (kernel_res, "gw.reply.mta", "/usr/lib/sendmail");
191 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
193 mta_code = system (cmd);
195 gw_log (GW_LOG_FATAL, "urp", "Reply '%s' got exit code %d",
197 unlink (reply_fname);