1 /* Gateway Email Transport Interface
5 * Revision 1.6 1995/04/17 09:34:27 adam
6 * Timeout (idletime) adjustable. Minor changes in kernel.
8 * Revision 1.5 1995/03/31 09:43:13 adam
9 * Removed unused variable.
11 * Revision 1.4 1995/03/28 11:42:34 adam
12 * First use of string-queue utility.
14 * Revision 1.3 1995/03/28 08:01:23 adam
15 * FIFO existence is used to test for a running kernel.
17 * Revision 1.2 1995/03/27 12:51:05 adam
18 * New log level in use: GW_LOG_ERRNO.
20 * Revision 1.1 1995/03/27 08:24:00 adam
21 * First use of gip interface and gw-db.
22 * First work on eti program.
43 static char *module = "eti";
44 static jmp_buf retry_jmp;
46 static void pipe_handle (int dummy)
48 longjmp (retry_jmp, 1);
51 static int email_header (struct str_queue *sq,
52 char *from_str, char *subject_str)
59 while ((msg = str_queue_get (sq, index++)))
63 if (memcmp (msg, "From ", 5) == 0)
64 sscanf (msg+4, "%s", from_str);
65 if (memcmp (msg, "Subject: ", 9) == 0 &&
66 sscanf (msg+9, "%s", subject_str+1) == 1)
67 strcpy (subject_str, msg+9);
72 static void start_kernel (int argc, char **argv, int id)
77 char userid_option[20];
79 argv_p = malloc (sizeof(*argv_p)*(argc+2));
82 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "malloc fail");
86 for (i = 1; i<argc; i++)
88 sprintf (userid_option, "-i%d", id);
89 argv_p[i++] = userid_option;
92 gw_log (GW_LOG_DEBUG, module, "Starting kernel");
96 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
101 execvp ("kernel", argv_p);
102 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
107 static void deliver (struct str_queue *sq, GIP gip)
112 gip_wline (gip, "mail\n");
113 while ((msg = str_queue_get (sq, index++)))
114 gip_wline (gip, msg);
115 gip_wline (gip, "\001");
118 int main (int argc, char **argv)
120 char from_str[LINE_MAX+1];
121 char subject_str[LINE_MAX+1];
122 char line_str[LINE_MAX+1];
130 char fifo_client_name[1024];
131 char fifo_server_name[1024];
132 struct str_queue *queue;
135 gw_log_level (GW_LOG_ALL);
136 gw_log_file (GW_LOG_ALL, "eti.log");
138 if (!(queue = str_queue_mk ()))
140 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "str_queue_mk");
143 while (fgets (line_str, LINE_MAX, stdin))
144 str_queue_enq (queue, line_str);
146 r = email_header (queue, from_str, subject_str);
149 gw_log (GW_LOG_WARN, module, "No \"From\" in mail");
152 gw_log (GW_LOG_STAT, module, "Mail from %s", from_str);
153 user_db = gw_db_open ("user.db", 1);
155 r = gw_db_lookup (user_db, from_str, strlen(from_str),
156 &user_info, &info_length);
159 gw_log (GW_LOG_STAT, module, "New user");
160 id = gw_db_seq_no (user_db);
161 r = gw_db_insert (user_db, from_str, strlen(from_str),
163 gw_log (GW_LOG_STAT, module, "Added user with id %d", id);
167 if (sizeof(id) != info_length)
169 gw_log (GW_LOG_FATAL, module, "Bad id-size");
172 memcpy (&id, user_info, sizeof(id));
173 gw_log (GW_LOG_STAT, module, "Found with id %d", id);
177 gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
180 sprintf (fifo_server_name, "fifo.s.%d", id);
181 sprintf (fifo_client_name, "fifo.c.%d", id);
183 gip = gipc_initialize (fifo_client_name);
185 signal (SIGPIPE, pipe_handle);
188 gw_log (GW_LOG_DEBUG, module, "Pass %d", pass);
190 r = gipc_open (gip, fifo_server_name, 0);
194 start_kernel (argc, argv, id);
195 r = gipc_open (gip, fifo_server_name, 1);
199 gw_log (GW_LOG_FATAL, module, "Cannot start kernel");
205 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-2");
206 longjmp (retry_jmp, 1);
210 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-1");
211 longjmp (retry_jmp, 1);
215 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "gipc_open");
218 /* deliver message ... */
219 gw_log (GW_LOG_DEBUG, module, "Delivering mail");
220 deliver (queue, gip);
221 gw_log (GW_LOG_DEBUG, module, "Closing");
224 gw_db_close (user_db);
225 gw_log (GW_LOG_DEBUG, module, "Exiting");