5 * Revision 1.1 1995/03/27 08:24:00 adam
6 * First use of gip interface and gw-db.
7 * First work on eti program.
24 static char line_buf[LINE_MAX+1];
26 static char *module = "eti";
28 static int email_header (FILE *inf, char *from_str, char *subject_str)
32 while (fgets (line_buf, LINE_MAX, inf))
34 if (line_buf[0] == '\n')
36 if (strncmp (line_buf, "From ", 5) == 0)
37 sscanf (line_buf+4, "%s", from_str);
38 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
39 sscanf (line_buf+9, "%s", subject_str+1) == 1)
40 strcpy (subject_str, line_buf+9);
45 static int kernel_active (int id)
47 char active_name[1024];
50 sprintf (active_name, "kernel.pid.%d", id);
51 fd = open (active_name, O_RDONLY);
58 int main (int argc, char **argv)
60 char from_str[LINE_MAX+1];
61 char subject_str[LINE_MAX+1];
62 char line_str[LINE_MAX+1];
69 char fifo_client_name[1024];
70 char fifo_server_name[1024];
73 gw_log_level (GW_LOG_ALL);
74 gw_log_file (GW_LOG_ALL, "eti.log");
75 r = email_header (stdin, from_str, subject_str);
78 gw_log (GW_LOG_WARN, module, "No \"From\" in mail");
81 gw_log (GW_LOG_STAT, module, "Mail from %s", from_str);
82 user_db = gw_db_open ("user.db", 1);
84 r = gw_db_lookup (user_db, from_str, strlen(from_str),
85 &user_info, &info_length);
88 gw_log (GW_LOG_STAT, module, "New user");
89 id = gw_db_seq_no (user_db);
90 r = gw_db_insert (user_db, from_str, strlen(from_str),
92 gw_log (GW_LOG_STAT, module, "Added user with id %d", id);
96 if (sizeof(id) != info_length)
98 gw_log (GW_LOG_FATAL, module, "Bad id-size");
101 memcpy (&id, user_info, sizeof(id));
102 gw_log (GW_LOG_STAT, module, "Found with id %d", id);
106 gw_log (GW_LOG_FATAL, module, "gw_db_lookup fail");
108 sprintf (fifo_server_name, "fifo.s.%d", id);
109 sprintf (fifo_client_name, "fifo.c.%d", id);
111 gip = gipc_initialize (fifo_client_name);
112 if (kernel_active (id))
114 gw_log (GW_LOG_DEBUG, module, "Kernel already active");
115 gipc_open (gip, fifo_server_name, 0);
122 char userid_option[20];
124 argv_p = malloc (sizeof(*argv_p)*(argc+2));
127 gw_log (GW_LOG_FATAL, module, "Malloc fail");
130 argv_p[0] = "kernel";
131 for (i = 1; i<argc; i++)
133 sprintf (userid_option, "-i%d", id);
134 argv_p[i++] = userid_option;
137 gw_log (GW_LOG_DEBUG, module, "Kernel not active");
141 gw_log (GW_LOG_FATAL, module, "fork");
146 execvp ("kernel", argv_p);
147 gw_log (GW_LOG_FATAL, module, "execvp");
150 gipc_open (gip, fifo_server_name, 1);
152 /* deliver message ... */
153 gw_log (GW_LOG_DEBUG, module, "Deliver mail header");
154 gip_wline (gip, "mail\n");
155 gip_wline (gip, "From ");
156 gip_wline (gip, from_str);
157 gip_wline (gip, "\n");
160 gip_wline (gip, "Subject: ");
161 gip_wline (gip, subject_str);
162 gip_wline (gip, "\n");
164 gip_wline (gip, "\n");
165 gw_log (GW_LOG_DEBUG, module, "Deliver mail body");
166 while (fgets (line_str, LINE_MAX, stdin))
167 gip_wline (gip, line_str);
168 gip_wline (gip, "\001");
169 gw_log (GW_LOG_DEBUG, module, "Closing");
172 gw_db_close (user_db);