1 /* bb_wtmail.c - write mail to a BBoard */
3 static char Id[] = "@(#)$Id: bb_wtmail.c,v 1.3 1993/08/25 17:43:26 jromine Exp $";
25 channelinfo[LINESIZE],
32 extern char *qu_msgfile,
40 extern struct ll_struct *logptr;
44 char *index (), *rindex (), *sprintf ();
45 struct passwd *getpwnam ();
57 ll_log (logptr, LLOGBTR, "bb_init(chanptr=%s)", chanptr -> ch_spec);
60 channelname = chanptr -> ch_spec;
62 sprintf (channelinfo, "vmth%s*", channelname);
63 #else notdef /* the following is probably a BAD idea */
64 if (chanptr -> ch_host == NULL)
65 channelinfo[0] = NULL; /* local delivery ONLY */
67 sprintf (channelinfo, "vmth%s*", chanptr -> ch_host);
70 if ((pw = getpwnam (BBOARDS)) == NULL)
71 err_abrt (RP_BHST, "no passwd entry for '%s'", BBOARDS);
72 bbrduid = pw -> pw_uid;
73 bbrdgid = pw -> pw_gid;
74 sprintf (bbrdfrom, "%s@%s", pw -> pw_name, locname);
76 ll_log (logptr, LLOGGEN, "distributing as '%s'", bbrdfrom);
78 sprintf (bbrdhome, pw -> pw_dir);
81 err_abrt (RP_BHST, "setbbent() failed");
85 err_abrt (RP_BHST, "not running as root");
95 ll_log (logptr, LLOGBTR, "bb_end(result=0%o)", result);
105 ll_log (logptr, LLOGBTR, "bb_sbinit()");
114 ll_log (logptr, LLOGBTR, "bb_sbend()");
122 bb_winit (info, sender)
127 ll_log (logptr, LLOGBTR, "bb_winit(info='%s',sender='%s')",
154 ll_log (logptr, LLOGBTR, "bb_wtadr(host=%s,adr=%s)", host, adr);
157 make_lower (adr, adr);
158 if ((curbb = getbbnam (adr)) == NULL)
160 sprintf (bbrdaddr, "local-%s-request@%s", curbb -> bb_name, locname);
162 ll_log (logptr, LLOGGEN, "=> BBoard %s: file='%s' info='%s addr='%s'",
163 curbb -> bb_name, curbb -> bb_file, curbb -> bb_info, bbrdaddr);
166 if (curbb -> bb_file == NULL || *curbb -> bb_file == NULL)
169 ll_log (logptr, LLOGGEN, "begin local delivery...");
171 printx ("\r\nperforming local delivery to file %s...\n",
175 if ((qd = dup (qu_fileno ())) == NOTOK) {
176 ll_log (logptr, LLOGFAT, "unable to dup qu_fileno()");
180 if ((md = mbx_open (curbb -> bb_file, bbrduid, bbrdgid, BBMODE)) == NOTOK) {
185 if (rp_isbad (result = mbx_init ())) {
187 mbx_close (curbb -> bb_file, md);
191 pos = (long) lseek (md, (off_t)0, 1);
192 count = strlen (delim1);
193 if (write (md, delim1, count) != count) {
194 ll_log (logptr, LLOGTMP, "error writing delim1");
198 start = (long) lseek (md, (off_t)0, 1);
201 count = strlen (bbrdheader);
202 if (write (md, bbrdheader, count) != count) {
203 ll_log (logptr, LLOGTMP, "error writing BBoard information");
207 for (cp = bbrhdheader; *cp; cp++, size++)
213 while ((i = read (qd, buffer, sizeof buffer)) > 0) {
215 (offset = strindex (delim1, buffer)) >= 0;
219 (offset = strindex (delim2, buffer)) >= 0;
222 if (write (md, buffer, i) != i) {
223 ll_log (logptr, LLOGTMP, "error writing to file '%s'",
228 for (offset = 0, cp = buffer; offset < i; offset++, size++)
234 ll_log (logptr, LLOGTMP, "error reading from message file '%s'",
239 stop = (long) lseek (md, (off_t)0, 1);
240 count = strlen (delim2);
241 if (write (md, delim2, count) != count)
242 ll_log (logptr, LLOGTMP, "error writing delim2");
243 map_write (curbb -> bb_file, md, curbb -> bb_maxima, start, stop, pos,
247 ll_log (logptr, LLOGGEN, "end local delivery...");
250 result = mbx_close (curbb -> bb_file, md);
251 return (i < 0 ? RP_FIO : result);
260 ll_log (logptr, LLOGBTR, "bb_txtcpy()");
263 if (curbb -> bb_dist == NULL
264 || *curbb -> bb_dist == NULL
265 || channelinfo[0] == NULL)
268 ll_log (logptr, LLOGGEN, "begin distribution...");
270 if (curbb -> bb_file == NULL || *curbb -> bb_file == NULL)
272 printx("\rperforming remote distribution\n");
274 if (rp_isbad (result = dist_init ())
275 || rp_isbad (result = dist_adrs ())
276 || rp_isbad (result = dist_text ())
277 || rp_isbad (result = dist_end ()))
278 return dist_lose (result);
281 ll_log (logptr, LLOGGEN, "end distribution...");
286 printx ("\rmessage distributed\n");
293 /* dist_ BBoard distribution routines */
299 ll_log (logptr, LLOGBTR, "dist_init()");
302 if (rp_isbad (result = mm_init ()))
303 return dist_log (result, LLOGFAT, "mm_init() failed [%s]",
305 if (rp_isbad (result = mm_sbinit ()))
306 return dist_log (result, LLOGFAT, "mm_sbinit() failed [%s]",
308 if (rp_isbad (result = mm_winit (chnlname, chnlinfo, bbrdaddr)))
309 return dist_log (result, LLOGFAT,
310 "mm_winit('%s','%s','%s') failed [%s]",
311 chnlname, chnlinfo, bbrdaddr, rp_valstr (result));
322 ll_log (logptr, LLOGBTR, "dist_adrs()");
325 if (getbbdist (curbb, dist_address))
326 return dist_log (RP_NO, LLOGTMP, "getbbdist failed: %s", getbberr ());
328 if (rp_isbad (result = mm_waend ()))
329 return dist_log (result, LLOGFAT, "mm_waend() failed [%s]",
337 dist_address (addr, host)
343 struct rp_bufstruct reply;
346 ll_log (logptr, LLOGBTR, "dist_address(addr='%s',host='%s')", addr, host);
349 printx ("\rperforming distribution to %s@%s...\n", addr, host);
350 if (rp_isbad (result = mm_wadr (host, addr))) {
351 dist_log (result, LLOGFAT, "mm_wadr('%s','%s') failed [%s]",
352 host, addr, rp_valstr (result));
355 if (rp_isbad (result = mm_rrply (&reply, &len))) {
356 dist_log (result, LLOGFAT,
357 "mm_rrply() failed [%s] getting status of '%s@%s'",
358 rp_valstr (result), addr, host);
362 switch (rp_gval (reply.rp_val)) {
365 ll_log (logptr, LLOGGEN, "address '%s@%s' [%s] -- %s",
366 addr, host, rp_valstr (reply.rp_val), reply.rp_line);
375 dist_log (reply.rp_val, LLOGTMP, "address '%s@%s' [%s] -- %s",
376 addr, host, rp_valstr (reply.rp_val), reply.rp_line);
377 return OK; /* fail-soft */
380 dist_log (reply.rp_val, LLOGFAT, "unexpected reply [%s] -- %s",
381 rp_valstr (reply.rp_val), reply.rp_line);
395 ll_log (logptr, LLOGBTR, "dist_text()");
399 if ((qd = dup (qu_fileno ())) == NOTOK)
400 return dist_log (RP_LIO, LLOGFAT, "unable to dup qu_fileno()");
402 while ((i = read (qd, buffer, sizeof buffer)) > 0)
403 if (rp_isbad (result = mm_wtxt (buffer, i)))
404 return dist_log (result, LLOGFAT, "mm_wtxt() failed [%s]",
409 return dist_log (RP_FIO, LLOGTMP,
410 "error reading from message file '%s'", qu_msgfile);
412 if (rp_isbad (result = mm_wtend ()))
413 return dist_log (result, LLOGFAT, "mm_wtend() failed [%s]",
424 struct rp_bufstruct reply;
427 ll_log (logptr, LLOGBTR, "dist_end()");
430 if (rp_isbad (result = mm_rrply (&reply, &len)))
431 return dist_log (result, LLOGFAT,
432 "mm_rrply() failed [%s] getting final status",
435 switch (rp_gval (reply.rp_val)) {
439 ll_log (logptr, LLOGGEN, "message [%s] -- %s",
440 rp_valstr (reply.rp_val), reply.rp_line);
450 return dist_log (RP_NO, LLOGTMP, "not delivered [%s] -- %s",
451 rp_valstr (reply.rp_val), reply.rp_line);
454 return dist_log (RP_RPLY, LLOGFAT,
455 "unexpected final reply [%s] -- %s",
456 rp_valstr (reply.rp_val), reply.rp_line);
470 ll_log (logptr, LLOGBTR, "dist_lose(result=0%o)", result);
474 printx ("\rerrors during distribution: ");
477 sprintf (buffer, "ch_bboards(%d) distribution for %s failed [%s]\n",
478 getpid (), curbb -> bb_name, rp_valstr (result));
479 if (ml_init (NO, NO, sitesignature, "Re-distribution Failure") != OK
480 || ml_adr (bbrdaddr) != OK
482 || ml_adr (bbrdfrom) != OK
484 || ml_tinit () != OK)
488 if (err_fd != NOTOK) {
489 lseek (err_fd, (off_t)0, 0);
490 if ((qp = fdopen (err_fd, "r")) == NULL) {
491 ml_txt ("unable to fdopen() for diagnostic copy\n");
502 if ((qd = dup (qu_fileno ())) == NOTOK)
503 ml_txt ("unable to dup qu_fileno() for message copy\n");
505 if ((qp = fdopen (qd, "r")) == NULL) {
506 ml_txt ("unable to fdopen() for message copy\n");
510 ml_txt ("\n --Message Follows--\n");
515 if (ml_end (OK) != OK) {
519 if (cp = index (buffer, '\n'))
521 printx ("unable to post advisory.\n");
522 ll_log (logptr, LLOGFAT, "unable to post failure notice");
523 ll_log (logptr, LLOGFAT, "info: %s", buffer);
526 printx ("advisory posted.\n");
537 dist_log (result, level, fmt, a, b, c, d, e)
551 ll_log (logptr, level, fmt, a, b, c, d, e);
553 sprintf (buffer, fmt, a, b, c, d, e);
554 strcat (buffer, "\n");
556 printx ("\rerror: %s", buffer);
558 if (err_fd == NOTOK) {
559 unlink (mktemp (strcpy (tmpfil, "/tmp/bboardsXXXXXX")));
560 if ((err_fd = creat (tmpfil, 0600)) == NOTOK)
563 if ((err_fd = open (tmpfil, 2)) == NOTOK)
566 lseek (err_fd, (off_t)0, 0);
569 write (err_fd, buffer, i);
576 /* mbx_ local mailbox routines */
585 ll_log (logptr, LLOGBTR, "mbx_init()");
588 if ((fd = mbx_Xopen (curbb -> bb_info, bbrduid, bbrdgid, BBMODE, &clear))
590 if (errno == ETXTBSY) {
591 printx ("\runable to lock %s\n", curbb -> bb_info);
592 ll_err (logptr, LLOGTMP, "unable to lock %s",
596 printx ("\runable to open '%s'", curbb -> bb_info);
597 ll_log (logptr, LLOGTMP, "unable to open '%s'", curbb -> bb_info);
600 if ((fp = fdopen (fd, "w")) == (FILE *) NULL) {
601 printx ("\runable to fdopen '%s'", curbb -> bb_info);
602 ll_err (logptr, LLOGTMP, "unable to fdopen '%s'", curbb -> bb_info);
603 mbx_close (curbb -> bb_info, fd);
607 strcpy (name, curbb -> bb_name);
608 if ((curbb = getbbnam (name)) == NULL) {
609 printx ("\runable to get information on BBoard %s\n", name);
610 ll_err (logptr, LLOGFAT, "unable to get info on %s", name);
611 lkfclose (fp, curbb -> bb_info);
615 strcpy (bbrdtime, dtimenow ());
616 sprintf (bbrdheader, "BBoard-ID: %d\nBB-Posted: %s\n",
617 ++curbb -> bb_maxima, bbrdtime);
619 fprintf (fp, "%d\n%s\n", curbb -> bb_maxima, bbrdtime);
620 lkfclose (fp, curbb -> bb_info);