1 /* Gateway Email Transport Interface
5 * Revision 1.7 1995/04/19 16:01:57 adam
6 * Some hacks to get the FIFO communication work!! Isn't reliable.
7 * Resource gw.account added - default account info.
9 * Revision 1.6 1995/04/17 09:34:27 adam
10 * Timeout (idletime) adjustable. Minor changes in kernel.
12 * Revision 1.5 1995/03/31 09:43:13 adam
13 * Removed unused variable.
15 * Revision 1.4 1995/03/28 11:42:34 adam
16 * First use of string-queue utility.
18 * Revision 1.3 1995/03/28 08:01:23 adam
19 * FIFO existence is used to test for a running kernel.
21 * Revision 1.2 1995/03/27 12:51:05 adam
22 * New log level in use: GW_LOG_ERRNO.
24 * Revision 1.1 1995/03/27 08:24:00 adam
25 * First use of gip interface and gw-db.
26 * First work on eti program.
47 static char *module = "eti";
48 static jmp_buf retry_jmp;
50 static void pipe_handle (int dummy)
52 longjmp (retry_jmp, 1);
55 static int email_header (struct str_queue *sq,
56 char *from_str, char *subject_str)
63 while ((msg = str_queue_get (sq, index++)))
67 if (memcmp (msg, "From ", 5) == 0)
68 sscanf (msg+4, "%s", from_str);
69 if (memcmp (msg, "Subject: ", 9) == 0 &&
70 sscanf (msg+9, "%s", subject_str+1) == 1)
71 strcpy (subject_str, msg+9);
76 static void start_kernel (int argc, char **argv, int id)
81 char userid_option[20];
83 argv_p = malloc (sizeof(*argv_p)*(argc+2));
86 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "malloc fail");
90 for (i = 1; i<argc; i++)
92 sprintf (userid_option, "-i%d", id);
93 argv_p[i++] = userid_option;
96 gw_log (GW_LOG_DEBUG, module, "Starting kernel");
100 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
105 execvp ("kernel", argv_p);
106 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
111 static void deliver (struct str_queue *sq, GIP gip)
116 gip_wline (gip, "mail\n");
117 while ((msg = str_queue_get (sq, index++)))
118 gip_wline (gip, msg);
119 gip_wline (gip, "\001");
122 int main (int argc, char **argv)
124 char from_str[LINE_MAX+1];
125 char subject_str[LINE_MAX+1];
126 char line_str[LINE_MAX+1];
134 char fifo_client_name[1024];
135 char fifo_server_name[1024];
136 struct str_queue *queue;
139 gw_log_level (GW_LOG_ALL);
140 gw_log_file (GW_LOG_ALL, "eti.log");
142 if (!(queue = str_queue_mk ()))
144 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "str_queue_mk");
147 while (fgets (line_str, LINE_MAX, stdin))
148 str_queue_enq (queue, line_str);
150 r = email_header (queue, from_str, subject_str);
153 gw_log (GW_LOG_WARN, module, "No \"From\" in mail");
156 gw_log (GW_LOG_STAT, module, "Mail from %s", from_str);
157 user_db = gw_db_open ("user.db", 1);
159 r = gw_db_lookup (user_db, from_str, strlen(from_str),
160 &user_info, &info_length);
163 gw_log (GW_LOG_STAT, module, "New user");
164 id = gw_db_seq_no (user_db);
165 r = gw_db_insert (user_db, from_str, strlen(from_str),
167 gw_log (GW_LOG_STAT, module, "Added user with id %d", id);
171 if (sizeof(id) != info_length)
173 gw_log (GW_LOG_FATAL, module, "Bad id-size");
176 memcpy (&id, user_info, sizeof(id));
177 gw_log (GW_LOG_STAT, module, "Found with id %d", id);
181 gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
184 sprintf (fifo_server_name, "fifo.s.%d", id);
185 sprintf (fifo_client_name, "fifo.c.%d", id);
187 gip = gipc_initialize (fifo_client_name);
189 signal (SIGPIPE, pipe_handle);
192 signal (SIGPIPE, pipe_handle);
193 gw_log (GW_LOG_DEBUG, module, "Pass %d", pass);
195 r = gipc_open (gip, fifo_server_name, 0);
202 unlink (fifo_server_name);
203 unlink (fifo_client_name);
204 gip = gipc_initialize (fifo_client_name);
205 start_kernel (argc, argv, id);
207 r = gipc_open (gip, fifo_server_name, 1);
211 gw_log (GW_LOG_FATAL, module, "Cannot start kernel");
217 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-2");
218 longjmp (retry_jmp, 1);
222 gw_log (GW_LOG_DEBUG|GW_LOG_ERRNO, module, "r==-1");
223 longjmp (retry_jmp, 1);
227 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "gipc_open");
230 /* deliver message ... */
231 gw_log (GW_LOG_DEBUG, module, "Delivering mail");
232 deliver (queue, gip);
233 gw_log (GW_LOG_DEBUG, module, "Closing");
236 gw_db_close (user_db);
237 gw_log (GW_LOG_DEBUG, module, "Exiting");