int main(int argc, char* argv[]) { obuf bufout; obuf* out; const char* recipient; const char* sender; parse_args(argc, argv); if (opt_starttime && now < opt_starttime) ignore("Autoresponder is not active yet"); if (opt_endtime && now > opt_endtime) ignore("Autoresponder is no longer active"); recipient = getenv("RECIPIENT"); if(!recipient) usage("RECIPIENT is not set, must be run from qmail."); sender = getenv("SENDER"); if(!sender) usage("SENDER is not set, must be run from qmail."); check_sender(sender); str_copys(&subject, "Your mail"); read_parse_headers(); // Check rate that SENDER has sent if(!count_history(sender)) { log_sender(sender, 0); ignore("SENDER has sent too many messages"); } if(opt_nosend) out = &outbuf; else { int msgfd = qmail_start(); if (!obuf_init(&bufout, msgfd, 0, IOBUF_NEEDSCLOSE, 0)) fail_temp("Could not initialize output buffer."); out = &bufout; } obuf_put3s(out, "To: <", sender, ">\n"); if(opt_subject_prefix) obuf_put4s(out, "Subject: ", opt_subject_prefix, subject.s, "\n"); if((!opt_no_inreplyto) && (message_id.len != 0)) obuf_put3s(out, "In-Reply-To: ", message_id.s, "\n"); build_response(out, sender, recipient); if (!obuf_close(out)) fail_temp("Could not close output."); if (!opt_nosend) qmail_finish(sender); log_sender(sender, 1); return 0; }
int main(int argc, char **argv) { int argn; FILE *tmpfp; struct rfc2045 *rfcp; struct mimeautoreply_s replyinfo; const char *subj=0; const char *txtfile=0, *mimefile=0; const char *mimedsn=0; int nosend=0; const char *replymode="reply"; int replytoenvelope=0; int donotquote=0; const char *forwardsep="--- Forwarded message ---"; const char *replysalut="%F writes:"; struct rfc2045src *src; setlocale(LC_ALL, ""); charset=unicode_default_chset(); sender=NULL; for (argn=1; argn < argc; argn++) { char optc; char *optarg; if (argv[argn][0] != '-') break; if (strcmp(argv[argn], "--") == 0) { ++argn; break; } optc=argv[argn][1]; optarg=argv[argn]+2; if (!*optarg) optarg=NULL; switch (optc) { case 'c': if (!optarg && argn+1 < argc) optarg=argv[++argn]; if (optarg && *optarg) { char *p=libmail_u_convert_tobuf("", optarg, libmail_u_ucs4_native, NULL); if (!p) { fprintf(stderr, "Unknown charset: %s\n", charset); exit(1); } free(p); charset=optarg; } continue; case 't': if (!optarg && argn+1 < argc) optarg=argv[++argn]; txtfile=optarg; continue; case 'm': if (!optarg && argn+1 < argc) optarg=argv[++argn]; mimefile=optarg; continue; case 'r': if (!optarg && argn+1 < argc) optarg=argv[++argn]; recips=optarg; continue; case 'M': if (!optarg && argn+1 < argc) optarg=argv[++argn]; mimedsn=optarg; continue; case 'd': if (!optarg && argn+1 < argc) optarg=argv[++argn]; dbfile=optarg; continue; case 'e': replytoenvelope=1; continue; case 'T': if (!optarg && argn+1 < argc) optarg=argv[++argn]; if (optarg && *optarg) replymode=optarg; continue; case 'N': donotquote=1; continue; case 'F': if (!optarg && argn+1 < argc) optarg=argv[++argn]; if (optarg && *optarg) forwardsep=optarg; continue; case 'S': if (!optarg && argn+1 < argc) optarg=argv[++argn]; if (optarg && *optarg) replysalut=optarg; continue; case 'D': if (!optarg && argn+1 < argc) optarg=argv[++argn]; interval=optarg ? atoi(optarg):1; continue; case 'A': if (!optarg && argn+1 < argc) optarg=argv[++argn]; if (optarg) { struct header **h; for (h= &extra_headers; *h; h= &(*h)->next) ; if ((*h=malloc(sizeof(struct header))) == 0 || ((*h)->buf=strdup(optarg)) == 0) { perror("malloc"); exit(EX_TEMPFAIL); } (*h)->next=0; } continue; case 's': if (!optarg && argn+1 < argc) optarg=argv[++argn]; subj=optarg; continue; case 'f': if (optarg && *optarg) { sender=strdup(optarg); } else { sender=getenv("SENDER"); if (!sender) continue; sender=strdup(sender); } if (sender == NULL) { perror("malloc"); exit(1); } continue; case 'n': nosend=1; continue; default: usage(); } } if (!txtfile && !mimefile) usage(); if (txtfile && mimefile) usage(); tmpfp=tmpfile(); if (!tmpfp) { perror("tmpfile"); exit(1); } rfcp=savemessage(tmpfp); if (fseek(tmpfp, 0L, SEEK_SET) < 0) { perror("fseek(tempfile)"); exit(1); } read_headers(tmpfp); if (sender == NULL || *sender == 0) check_sender(); check_dsn(); check_recips(); #ifdef DbObj check_db(); #endif src=rfc2045src_init_fd(fileno(tmpfp)); memset(&replyinfo, 0, sizeof(replyinfo)); replyinfo.info.src=src; replyinfo.info.rfc2045partp=rfcp; replyinfo.info.voidarg=&replyinfo; replyinfo.info.write_func=mimeautoreply_write_func; replyinfo.info.writesig_func=mimeautoreply_writesig_func; replyinfo.info.myaddr_func=mimeautoreply_myaddr_func; replyinfo.info.replymode=replymode; replyinfo.info.replytoenvelope=replytoenvelope; replyinfo.info.donotquote=donotquote; replyinfo.info.replysalut=replysalut; replyinfo.info.forwarddescr="Forwarded message"; replyinfo.info.mailinglists=""; replyinfo.info.charset=charset; replyinfo.info.subject=subj; replyinfo.info.forwardsep=forwardsep; if (mimedsn && *mimedsn) { replyinfo.info.dsnfrom=mimedsn; replyinfo.info.replymode="replydsn"; } if (mimefile) { if ((replyinfo.contentf=fopen(mimefile, "r")) == NULL) { perror(mimefile); exit(1); } { struct rfc2045 *rfcp=rfc2045_alloc(); static const char mv[]="Mime-Version: 1.0\n"; char buf[BUFSIZ]; int l; const char *content_type; const char *content_transfer_encoding; const char *charset; rfc2045_parse(rfcp, mv, sizeof(mv)-1); while ((l=fread(buf, 1, sizeof(buf), replyinfo.contentf) ) > 0) { rfc2045_parse(rfcp, buf, l); } if (l < 0 || fseek(replyinfo.contentf, 0L, SEEK_SET) < 0) { perror(mimefile); exit(1); } rfc2045_mimeinfo(rfcp, &content_type, &content_transfer_encoding, &charset); if (strcasecmp(content_type, "text/plain")) { fprintf(stderr, "%s must specify text/plain MIME type\n", mimefile); exit(1); } { char *p=NULL; if (charset) p=libmail_u_convert_tobuf("", charset, libmail_u_ucs4_native, NULL); if (!p) { fprintf(stderr, "Unknown charset in %s\n", mimefile); exit(1); } free(p); replyinfo.info.charset=strdup(charset); } rfc2045_free(rfcp); } replyinfo.info.content_set_charset=copy_headers; replyinfo.info.content_specify=copy_body; } else if (txtfile) { if ((replyinfo.contentf=fopen(txtfile, "r")) == NULL) { perror(mimefile); exit(1); } replyinfo.info.content_specify=copy_body; } if (replyinfo.contentf) fcntl(fileno(replyinfo.contentf), F_SETFD, FD_CLOEXEC); if (nosend) replyinfo.outf=stdout; else { replyinfo.outf=tmpfile(); if (replyinfo.outf == NULL) { perror("tmpfile"); exit(1); } } { struct header *h; for (h=extra_headers; h; h=h->next) fprintf(replyinfo.outf, "%s\n", h->buf); } fprintf(replyinfo.outf, "Precedence: junk\n" "Auto-Submitted: auto-replied\n"); if (rfc2045_makereply(&replyinfo.info) < 0 || fflush(replyinfo.outf) < 0 || ferror(replyinfo.outf) || (!nosend && ( fseek(replyinfo.outf, 0L, SEEK_SET) < 0 || (close(0), dup(fileno(replyinfo.outf))) < 0) )) { perror("tempfile"); exit(1); } fclose(replyinfo.outf); fcntl(0, F_SETFD, 0); rfc2045_free(rfcp); rfc2045src_deinit(src); if (!nosend) opensendmail(argn, argc, argv); return (0); }