URI:
       treply.c - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       treply.c (11632B)
       ---
            1 #include <u.h>
            2 #include <libc.h>
            3 #include <bio.h>
            4 #include <thread.h>
            5 #include <ctype.h>
            6 #include <plumb.h>
            7 #include <9pclient.h>
            8 #include "dat.h"
            9 
           10 static int        replyid;
           11 
           12 int
           13 quote(Message *m, CFid *fid, char *dir, char *quotetext)
           14 {
           15         char *body, *type;
           16         int i, n, nlines;
           17         char **lines;
           18 
           19         if(quotetext){
           20                 body = quotetext;
           21                 n = strlen(body);
           22                 type = nil;
           23         }else{
           24                 /* look for first textual component to quote */
           25                 type = readfile(dir, "type", &n);
           26                 if(type == nil){
           27                         print("no type in %s\n", dir);
           28                         return 0;
           29                 }
           30                 if(strncmp(type, "multipart/", 10)==0 || strncmp(type, "message/", 8)==0){
           31                         dir = estrstrdup(dir, "1/");
           32                         if(quote(m, fid, dir, nil)){
           33                                 free(type);
           34                                 free(dir);
           35                                 return 1;
           36                         }
           37                         free(dir);
           38                 }
           39                 if(strncmp(type, "text", 4) != 0){
           40                         free(type);
           41                         return 0;
           42                 }
           43                 body = readbody(m->type, dir, &n);
           44                 if(body == nil)
           45                         return 0;
           46         }
           47         nlines = 0;
           48         for(i=0; i<n; i++)
           49                 if(body[i] == '\n')
           50                         nlines++;
           51         nlines++;
           52         lines = emalloc(nlines*sizeof(char*));
           53         nlines = getfields(body, lines, nlines, 0, "\n");
           54         /* delete leading and trailing blank lines */
           55         i = 0;
           56         while(i<nlines && lines[i][0]=='\0')
           57                 i++;
           58         while(i<nlines && lines[nlines-1][0]=='\0')
           59                 nlines--;
           60         while(i < nlines){
           61                 fsprint(fid, ">%s%s\n", lines[i][0]=='>'? "" : " ", lines[i]);
           62                 i++;
           63         }
           64         free(lines);
           65         free(body);        /* will free quotetext if non-nil */
           66         free(type);
           67         return 1;
           68 }
           69 
           70 void
           71 mkreply(Message *m, char *label, char *to, Plumbattr *attr, char *quotetext)
           72 {
           73         char buf[100];
           74         CFid *fd;
           75         Message *r;
           76         char *dir, *t;
           77         int quotereply;
           78         Plumbattr *a;
           79 
           80         quotereply = (label[0] == 'Q');
           81 
           82         if(quotereply && m && m->replywinid > 0){
           83                 snprint(buf, sizeof buf, "%d/body", m->replywinid);
           84                 if((fd = fsopen(acmefs, buf, OWRITE)) != nil){
           85                         dir = estrstrdup(mbox.name, m->name);
           86                         quote(m, fd, dir, quotetext);
           87                         free(dir);
           88                         return;
           89                 }
           90         }
           91 
           92         r = emalloc(sizeof(Message));
           93         r->isreply = 1;
           94         if(m != nil)
           95                 r->replyname = estrdup(m->name);
           96         r->next = replies.head;
           97         r->prev = nil;
           98         if(replies.head != nil)
           99                 replies.head->prev = r;
          100         replies.head = r;
          101         if(replies.tail == nil)
          102                 replies.tail = r;
          103         r->name = emalloc(strlen(mbox.name)+strlen(label)+10);
          104         sprint(r->name, "%s%s%d", mbox.name, label, ++replyid);
          105         r->w = newwindow();
          106         if(m)
          107                 m->replywinid = r->w->id;
          108         winname(r->w, r->name);
          109         ctlprint(r->w->ctl, "cleartag");
          110         wintagwrite(r->w, "fmt Look Post Undo", 4+5+5+4);
          111         r->tagposted = 1;
          112         threadcreate(mesgctl, r, STACK);
          113         winopenbody(r->w, OWRITE);
          114         if(to!=nil && to[0]!='\0')
          115                 fsprint(r->w->body, "%s\n", to);
          116         for(a=attr; a; a=a->next)
          117                 fsprint(r->w->body, "%s: %s\n", a->name, a->value);
          118         dir = nil;
          119         if(m != nil){
          120                 dir = estrstrdup(mbox.name, m->name);
          121                 if(to == nil && attr == nil){
          122                         /* Reply goes to replyto; Reply all goes to From and To and CC */
          123                         if(strstr(label, "all") == nil)
          124                                 fsprint(r->w->body, "To: %s\n", m->replyto);
          125                         else{        /* Replyall */
          126                                 if(strlen(m->from) > 0)
          127                                         fsprint(r->w->body, "To: %s\n", m->from);
          128                                 if(strlen(m->to) > 0)
          129                                         fsprint(r->w->body, "To: %s\n", m->to);
          130                                 if(strlen(m->cc) > 0)
          131                                         fsprint(r->w->body, "CC: %s\n", m->cc);
          132                         }
          133                 }
          134                 if(strlen(m->subject) > 0){
          135                         t = "Subject: Re: ";
          136                         if(strlen(m->subject) >= 3)
          137                                 if(tolower(m->subject[0])=='r' && tolower(m->subject[1])=='e' && m->subject[2]==':')
          138                                         t = "Subject: ";
          139                         fsprint(r->w->body, "%s%s\n", t, m->subject);
          140                 }
          141                 if(!quotereply){
          142                         fsprint(r->w->body, "Include: %sraw\n", dir);
          143                         free(dir);
          144                 }
          145         }
          146         fsprint(r->w->body, "\n");
          147         if(m == nil)
          148                 fsprint(r->w->body, "\n");
          149         else if(quotereply){
          150                 quote(m, r->w->body, dir, quotetext);
          151                 free(dir);
          152         }
          153         winclosebody(r->w);
          154         if(m==nil && (to==nil || to[0]=='\0'))
          155                 winselect(r->w, "0", 0);
          156         else
          157                 winselect(r->w, "$", 0);
          158         winclean(r->w);
          159         windormant(r->w);
          160 }
          161 
          162 void
          163 delreply(Message *m)
          164 {
          165         if(m->next == nil)
          166                 replies.tail = m->prev;
          167         else
          168                 m->next->prev = m->prev;
          169         if(m->prev == nil)
          170                 replies.head = m->next;
          171         else
          172                 m->prev->next = m->next;
          173         mesgfreeparts(m);
          174         free(m);
          175 }
          176 
          177 /* copy argv to stack and free the incoming strings, so we don't leak argument vectors */
          178 void
          179 buildargv(char **inargv, char *argv[NARGS+1], char args[NARGCHAR])
          180 {
          181         int i, n;
          182         char *s, *a;
          183 
          184         s = args;
          185         for(i=0; i<NARGS; i++){
          186                 a = inargv[i];
          187                 if(a == nil)
          188                         break;
          189                 n = strlen(a)+1;
          190                 if((s-args)+n >= NARGCHAR)        /* too many characters */
          191                         break;
          192                 argv[i] = s;
          193                 memmove(s, a, n);
          194                 s += n;
          195                 free(a);
          196         }
          197         argv[i] = nil;
          198 }
          199 
          200 void
          201 execproc(void *v)
          202 {
          203         struct Exec *e;
          204         int p[2], q[2];
          205         char *prog;
          206         char *argv[NARGS+1], args[NARGCHAR];
          207         int fd[3];
          208 
          209         e = v;
          210         p[0] = e->p[0];
          211         p[1] = e->p[1];
          212         q[0] = e->q[0];
          213         q[1] = e->q[1];
          214         prog = e->prog;        /* known not to be malloc'ed */
          215 
          216         fd[0] = dup(p[0], -1);
          217         if(q[0])
          218                 fd[1] = dup(q[1], -1);
          219         else
          220                 fd[1] = dup(1, -1);
          221         fd[2] = dup(2, -2);
          222         sendul(e->sync, 1);
          223         buildargv(e->argv, argv, args);
          224         free(e->argv);
          225         chanfree(e->sync);
          226         free(e);
          227 
          228         threadexec(nil, fd, prog, argv);
          229         close(fd[0]);
          230         close(fd[1]);
          231         close(fd[2]);
          232 
          233         fprint(2, "Mail: can't exec %s: %r\n", prog);
          234         threadexits("can't exec");
          235 }
          236 
          237 enum{
          238         ATTACH,
          239         BCC,
          240         CC,
          241         FROM,
          242         INCLUDE,
          243         TO
          244 };
          245 
          246 char *headers[] = {
          247         "attach:",
          248         "bcc:",
          249         "cc:",
          250         "from:",
          251         "include:",
          252         "to:",
          253         nil
          254 };
          255 
          256 int
          257 whichheader(char *h)
          258 {
          259         int i;
          260 
          261         for(i=0; headers[i]!=nil; i++)
          262                 if(cistrcmp(h, headers[i]) == 0)
          263                         return i;
          264         return -1;
          265 }
          266 
          267 char *tolist[200];
          268 char        *cclist[200];
          269 char        *bcclist[200];
          270 int ncc, nbcc, nto;
          271 char        *attlist[200];
          272 char        included[200];
          273 
          274 int
          275 addressed(char *name)
          276 {
          277         int i;
          278 
          279         for(i=0; i<nto; i++)
          280                 if(strcmp(name, tolist[i]) == 0)
          281                         return 1;
          282         for(i=0; i<ncc; i++)
          283                 if(strcmp(name, cclist[i]) == 0)
          284                         return 1;
          285         for(i=0; i<nbcc; i++)
          286                 if(strcmp(name, bcclist[i]) == 0)
          287                         return 1;
          288         return 0;
          289 }
          290 
          291 char*
          292 skipbl(char *s, char *e)
          293 {
          294         while(s < e){
          295                 if(*s!=' ' && *s!='\t' && *s!=',')
          296                         break;
          297                 s++;
          298         }
          299         return s;
          300 }
          301 
          302 char*
          303 findbl(char *s, char *e)
          304 {
          305         while(s < e){
          306                 if(*s==' ' || *s=='\t' || *s==',')
          307                         break;
          308                 s++;
          309         }
          310         return s;
          311 }
          312 
          313 /*
          314  * comma-separate possibly blank-separated strings in line; e points before newline
          315  */
          316 void
          317 commas(char *s, char *e)
          318 {
          319         char *t;
          320 
          321         /* may have initial blanks */
          322         s = skipbl(s, e);
          323         while(s < e){
          324                 s = findbl(s, e);
          325                 if(s == e)
          326                         break;
          327                 t = skipbl(s, e);
          328                 if(t == e)        /* no more words */
          329                         break;
          330                 /* patch comma */
          331                 *s++ = ',';
          332                 while(s < t)
          333                         *s++ = ' ';
          334         }
          335 }
          336 
          337 int
          338 print2(int fd, int ofd, char *fmt, ...)
          339 {
          340         int m, n;
          341         char *s;
          342         va_list arg;
          343 
          344         va_start(arg, fmt);
          345         s = vsmprint(fmt, arg);
          346         va_end(arg);
          347         if(s == nil)
          348                 return -1;
          349         m = strlen(s);
          350         n = write(fd, s, m);
          351         if(ofd > 0)
          352                 write(ofd, s, m);
          353         return n;
          354 }
          355 
          356 void
          357 write2(int fd, int ofd, char *buf, int n, int nofrom)
          358 {
          359         char *from, *p;
          360         int m;
          361 
          362         write(fd, buf, n);
          363 
          364         if(ofd <= 0)
          365                 return;
          366 
          367         if(nofrom == 0){
          368                 write(ofd, buf, n);
          369                 return;
          370         }
          371 
          372         /* need to escape leading From lines to avoid corrupting 'outgoing' mailbox */
          373         for(p=buf; *p; p+=m){
          374                 from = cistrstr(p, "from");
          375                 if(from == nil)
          376                         m = n;
          377                 else
          378                         m = from - p;
          379                 if(m > 0)
          380                         write(ofd, p, m);
          381                 if(from){
          382                         if(p==buf || from[-1]=='\n')
          383                                 write(ofd, " ", 1);        /* escape with space if From is at start of line */
          384                         write(ofd, from, 4);
          385                         m += 4;
          386                 }
          387                 n -= m;
          388         }
          389 }
          390 
          391 void
          392 mesgsend(Message *m)
          393 {
          394         char *s, *body, *to;
          395         int i, j, h, n, natt, p[2];
          396         struct Exec *e;
          397         Channel *sync;
          398         int first, nfld, delit, ofd;
          399         char *copy, *fld[100], *now;
          400 
          401         body = winreadbody(m->w, &n);
          402         /* assemble to: list from first line, to: line, and cc: line */
          403         nto = 0;
          404         natt = 0;
          405         ncc = 0;
          406         nbcc = 0;
          407         first = 1;
          408         to = body;
          409         for(;;){
          410                 for(s=to; *s!='\n'; s++)
          411                         if(*s == '\0'){
          412                                 free(body);
          413                                 return;
          414                         }
          415                 if(s++ == to)        /* blank line */
          416                         break;
          417                 /* make copy of line to tokenize */
          418                 copy = emalloc(s-to);
          419                 memmove(copy, to, s-to);
          420                 copy[s-to-1] = '\0';
          421                 nfld = tokenizec(copy, fld, nelem(fld), ", \t");
          422                 if(nfld == 0){
          423                         free(copy);
          424                         break;
          425                 }
          426                 n -= s-to;
          427                 switch(h = whichheader(fld[0])){
          428                 case TO:
          429                 case FROM:
          430                         delit = 1;
          431                         commas(to+strlen(fld[0]), s-1);
          432                         for(i=1; i<nfld && nto<nelem(tolist); i++)
          433                                 if(!addressed(fld[i]))
          434                                         tolist[nto++] = estrdup(fld[i]);
          435                         break;
          436                 case BCC:
          437                         delit = 1;
          438                         commas(to+strlen(fld[0]), s-1);
          439                         for(i=1; i<nfld && nbcc<nelem(bcclist); i++)
          440                                 if(!addressed(fld[i]))
          441                                         bcclist[nbcc++] = estrdup(fld[i]);
          442                         break;
          443                 case CC:
          444                         delit = 1;
          445                         commas(to+strlen(fld[0]), s-1);
          446                         for(i=1; i<nfld && ncc<nelem(cclist); i++)
          447                                 if(!addressed(fld[i]))
          448                                         cclist[ncc++] = estrdup(fld[i]);
          449                         break;
          450                 case ATTACH:
          451                 case INCLUDE:
          452                         delit = 1;
          453                         for(i=1; i<nfld && natt<nelem(attlist); i++){
          454                                 attlist[natt] = estrdup(fld[i]);
          455                                 included[natt++] = (h == INCLUDE);
          456                         }
          457                         break;
          458                 default:
          459                         if(first){
          460                                 delit = 1;
          461                                 for(i=0; i<nfld && nto<nelem(tolist); i++)
          462                                         tolist[nto++] = estrdup(fld[i]);
          463                         }else        /* ignore it */
          464                                 delit = 0;
          465                         break;
          466                 }
          467                 if(delit){
          468                         /* delete line from body */
          469                         memmove(to, s, n+1);
          470                 }else
          471                         to = s;
          472                 free(copy);
          473                 first = 0;
          474         }
          475 
          476         ofd = open(outgoing, OWRITE|OCEXEC);        /* no error check necessary */
          477         if(ofd > 0){
          478                 /* From dhog Fri Aug 24 22:13:00 EDT 2001 */
          479                 now = ctime(time(0));
          480                 seek(ofd, 0, 2);
          481                 fprint(ofd, "From %s %s", user, now);
          482                 fprint(ofd, "From: %s\n", user);
          483                 fprint(ofd, "Date: %s", now);
          484                 for(i=0; i<natt; i++)
          485                         if(included[i])
          486                                 fprint(ofd, "Include: %s\n", attlist[i]);
          487                         else
          488                                 fprint(ofd, "Attach: %s\n", attlist[i]);
          489                 /* needed because mail is by default Latin-1 */
          490                 fprint(ofd, "Content-Type: text/plain; charset=\"UTF-8\"\n");
          491                 fprint(ofd, "Content-Transfer-Encoding: 8bit\n");
          492         }
          493 
          494         e = emalloc(sizeof(struct Exec));
          495         if(pipe(p) < 0)
          496                 error("can't create pipe: %r");
          497         e->p[0] = p[0];
          498         e->p[1] = p[1];
          499         e->prog = unsharp("#9/bin/upas/marshal");
          500         e->argv = emalloc((1+1+2+4*natt+1)*sizeof(char*));
          501         e->argv[0] = estrdup("marshal");
          502         e->argv[1] = estrdup("-8");
          503         j = 2;
          504         if(m->replyname){
          505                 e->argv[j++] = estrdup("-R");
          506                 e->argv[j++] = estrstrdup(mbox.name, m->replyname);
          507         }
          508         for(i=0; i<natt; i++){
          509                 if(included[i])
          510                         e->argv[j++] = estrdup("-A");
          511                 else
          512                         e->argv[j++] = estrdup("-a");
          513                 e->argv[j++] = estrdup(attlist[i]);
          514         }
          515         sync = chancreate(sizeof(int), 0);
          516         e->sync = sync;
          517         proccreate(execproc, e, EXECSTACK);
          518         recvul(sync);
          519         /* close(p[0]); */
          520 
          521         /* using marshal -8, so generate rfc822 headers */
          522         if(nto > 0){
          523                 print2(p[1], ofd, "To: ");
          524                 for(i=0; i<nto-1; i++)
          525                         print2(p[1], ofd, "%s, ", tolist[i]);
          526                 print2(p[1], ofd, "%s\n", tolist[i]);
          527         }
          528         if(ncc > 0){
          529                 print2(p[1], ofd, "CC: ");
          530                 for(i=0; i<ncc-1; i++)
          531                         print2(p[1], ofd, "%s, ", cclist[i]);
          532                 print2(p[1], ofd, "%s\n", cclist[i]);
          533         }
          534         if(nbcc > 0){
          535                 print2(p[1], ofd, "BCC: ");
          536                 for(i=0; i<nbcc-1; i++)
          537                         print2(p[1], ofd, "%s, ", bcclist[i]);
          538                 print2(p[1], ofd, "%s\n", bcclist[i]);
          539         }
          540 
          541         i = strlen(body);
          542         if(i > 0)
          543                 write2(p[1], ofd, body, i, 1);
          544 
          545         /* guarantee a blank line, to ensure attachments are separated from body */
          546         if(i==0 || body[i-1]!='\n')
          547                 write2(p[1], ofd, "\n\n", 2, 0);
          548         else if(i>1 && body[i-2]!='\n')
          549                 write2(p[1], ofd, "\n", 1, 0);
          550 
          551         /* these look like pseudo-attachments in the "outgoing" box */
          552         if(ofd>0 && natt>0){
          553                 for(i=0; i<natt; i++)
          554                         if(included[i])
          555                                 fprint(ofd, "=====> Include: %s\n", attlist[i]);
          556                         else
          557                                 fprint(ofd, "=====> Attach: %s\n", attlist[i]);
          558         }
          559         if(ofd > 0)
          560                 write(ofd, "\n", 1);
          561 
          562         for(i=0; i<natt; i++)
          563                 free(attlist[i]);
          564         close(ofd);
          565         close(p[1]);
          566         free(body);
          567 
          568         if(m->replyname != nil)
          569                 mesgmenumark(mbox.w, m->replyname, "\t[replied]");
          570         if(m->name[0] == '/')
          571                 s = estrdup(m->name);
          572         else
          573                 s = estrstrdup(mbox.name, m->name);
          574         s = egrow(s, "-R", nil);
          575         winname(m->w, s);
          576         free(s);
          577         winclean(m->w);
          578         /* mark message unopened because it's no longer the original message */
          579         m->opened = 0;
          580 }