5 * Revision 1.2 1995/03/27 12:51:05 adam
6 * New log level in use: GW_LOG_ERRNO.
8 * Revision 1.1 1995/03/27 08:24:00 adam
9 * First use of gip interface and gw-db.
10 * First work on eti program.
27 static char line_buf[LINE_MAX+1];
29 static char *module = "eti";
31 static int email_header (FILE *inf, char *from_str, char *subject_str)
35 while (fgets (line_buf, LINE_MAX, inf))
37 if (line_buf[0] == '\n')
39 if (strncmp (line_buf, "From ", 5) == 0)
40 sscanf (line_buf+4, "%s", from_str);
41 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
42 sscanf (line_buf+9, "%s", subject_str+1) == 1)
43 strcpy (subject_str, line_buf+9);
48 static int kernel_active (int id)
50 char active_name[1024];
53 sprintf (active_name, "kernel.pid.%d", id);
54 fd = open (active_name, O_RDONLY);
61 int main (int argc, char **argv)
63 char from_str[LINE_MAX+1];
64 char subject_str[LINE_MAX+1];
65 char line_str[LINE_MAX+1];
72 char fifo_client_name[1024];
73 char fifo_server_name[1024];
76 gw_log_level (GW_LOG_ALL);
77 gw_log_file (GW_LOG_ALL, "eti.log");
78 r = email_header (stdin, from_str, subject_str);
81 gw_log (GW_LOG_WARN, module, "No \"From\" in mail");
84 gw_log (GW_LOG_STAT, module, "Mail from %s", from_str);
85 user_db = gw_db_open ("user.db", 1);
87 r = gw_db_lookup (user_db, from_str, strlen(from_str),
88 &user_info, &info_length);
91 gw_log (GW_LOG_STAT, module, "New user");
92 id = gw_db_seq_no (user_db);
93 r = gw_db_insert (user_db, from_str, strlen(from_str),
95 gw_log (GW_LOG_STAT, module, "Added user with id %d", id);
99 if (sizeof(id) != info_length)
101 gw_log (GW_LOG_FATAL, module, "Bad id-size");
104 memcpy (&id, user_info, sizeof(id));
105 gw_log (GW_LOG_STAT, module, "Found with id %d", id);
109 gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
111 sprintf (fifo_server_name, "fifo.s.%d", id);
112 sprintf (fifo_client_name, "fifo.c.%d", id);
114 gip = gipc_initialize (fifo_client_name);
115 if (kernel_active (id))
117 gw_log (GW_LOG_DEBUG, module, "Kernel already active");
118 gipc_open (gip, fifo_server_name, 0);
125 char userid_option[20];
127 argv_p = malloc (sizeof(*argv_p)*(argc+2));
130 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "Malloc fail");
133 argv_p[0] = "kernel";
134 for (i = 1; i<argc; i++)
136 sprintf (userid_option, "-i%d", id);
137 argv_p[i++] = userid_option;
140 gw_log (GW_LOG_DEBUG, module, "Kernel not active");
144 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "fork");
149 execvp ("kernel", argv_p);
150 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, module, "execvp");
153 gipc_open (gip, fifo_server_name, 1);
155 /* deliver message ... */
156 gw_log (GW_LOG_DEBUG, module, "Deliver mail header");
157 gip_wline (gip, "mail\n");
158 gip_wline (gip, "From ");
159 gip_wline (gip, from_str);
160 gip_wline (gip, "\n");
163 gip_wline (gip, "Subject: ");
164 gip_wline (gip, subject_str);
165 gip_wline (gip, "\n");
167 gip_wline (gip, "\n");
168 gw_log (GW_LOG_DEBUG, module, "Deliver mail body");
169 while (fgets (line_str, LINE_MAX, stdin))
170 gip_wline (gip, line_str);
171 gip_wline (gip, "\001");
172 gw_log (GW_LOG_DEBUG, module, "Closing");
175 gw_db_close (user_db);