nedmail.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580
  1. #include "common.h"
  2. #include <ctype.h>
  3. #include <plumb.h>
  4. typedef struct Message Message;
  5. typedef struct Ctype Ctype;
  6. typedef struct Cmd Cmd;
  7. char root[Pathlen];
  8. char mbname[Elemlen];
  9. int rootlen;
  10. int didopen;
  11. char *user;
  12. char wd[2048];
  13. String *mbpath;
  14. int natural;
  15. int doflush;
  16. int interrupted;
  17. struct Message {
  18. Message *next;
  19. Message *prev;
  20. Message *cmd;
  21. Message *child;
  22. Message *parent;
  23. String *path;
  24. int id;
  25. int len;
  26. int fileno; // number of directory
  27. String *info;
  28. char *from;
  29. char *to;
  30. char *cc;
  31. char *replyto;
  32. char *date;
  33. char *subject;
  34. char *type;
  35. char *disposition;
  36. char *filename;
  37. char deleted;
  38. char stored;
  39. };
  40. Message top;
  41. struct Ctype {
  42. char *type;
  43. char *ext;
  44. int display;
  45. char *plumbdest;
  46. Ctype *next;
  47. };
  48. Ctype ctype[] = {
  49. { "text/plain", "txt", 1, 0 },
  50. { "text/html", "htm", 1, 0 },
  51. { "text/html", "html", 1, 0 },
  52. { "text/tab-separated-values", "tsv", 1, 0 },
  53. { "text/richtext", "rtx", 1, 0 },
  54. { "text/rtf", "rtf", 1, 0 },
  55. { "text/calendar", "ics", 1, 0 },
  56. { "text", "txt", 1, 0 },
  57. { "message/rfc822", "msg", 0, 0 },
  58. { "image/bmp", "bmp", 0, "image" },
  59. { "image/jpeg", "jpg", 0, "image" },
  60. { "image/gif", "gif", 0, "image" },
  61. { "image/png", "png", 0, "image" },
  62. { "application/pdf", "pdf", 0, "postscript" },
  63. { "application/postscript", "ps", 0, "postscript" },
  64. { "application/", 0, 0, 0 },
  65. { "image/", 0, 0, 0 },
  66. { "multipart/", "mul", 0, 0 },
  67. };
  68. Message* acmd(Cmd*, Message*);
  69. Message* bcmd(Cmd*, Message*);
  70. Message* dcmd(Cmd*, Message*);
  71. Message* eqcmd(Cmd*, Message*);
  72. Message* hcmd(Cmd*, Message*);
  73. Message* Hcmd(Cmd*, Message*);
  74. Message* helpcmd(Cmd*, Message*);
  75. Message* icmd(Cmd*, Message*);
  76. Message* pcmd(Cmd*, Message*);
  77. Message* qcmd(Cmd*, Message*);
  78. Message* rcmd(Cmd*, Message*);
  79. Message* scmd(Cmd*, Message*);
  80. Message* ucmd(Cmd*, Message*);
  81. Message* wcmd(Cmd*, Message*);
  82. Message* xcmd(Cmd*, Message*);
  83. Message* ycmd(Cmd*, Message*);
  84. Message* pipecmd(Cmd*, Message*);
  85. Message* rpipecmd(Cmd*, Message*);
  86. Message* bangcmd(Cmd*, Message*);
  87. Message* Pcmd(Cmd*, Message*);
  88. Message* mcmd(Cmd*, Message*);
  89. Message* fcmd(Cmd*, Message*);
  90. Message* quotecmd(Cmd*, Message*);
  91. struct {
  92. char *cmd;
  93. int args;
  94. Message* (*f)(Cmd*, Message*);
  95. char *help;
  96. } cmdtab[] = {
  97. { "a", 1, acmd, "a reply to sender and recipients" },
  98. { "A", 1, acmd, "A reply to sender and recipients with copy" },
  99. { "b", 0, bcmd, "b print the next 10 headers" },
  100. { "d", 0, dcmd, "d mark for deletion" },
  101. { "f", 0, fcmd, "f file message by from address" },
  102. { "h", 0, hcmd, "h print elided message summary (,h for all)" },
  103. { "help", 0, helpcmd, "help print this info" },
  104. { "H", 0, Hcmd, "H print message's MIME structure " },
  105. { "i", 0, icmd, "i incorporate new mail" },
  106. { "m", 1, mcmd, "m addr forward mail" },
  107. { "M", 1, mcmd, "M addr forward mail with message" },
  108. { "p", 0, pcmd, "p print the processed message" },
  109. { "P", 0, Pcmd, "P print the raw message" },
  110. { "\"", 0, quotecmd, "\" print a quoted version of msg" },
  111. { "q", 0, qcmd, "q exit and remove all deleted mail" },
  112. { "r", 1, rcmd, "r [addr] reply to sender plus any addrs specified" },
  113. { "rf", 1, rcmd, "rf [addr]file message and reply" },
  114. { "R", 1, rcmd, "R [addr] reply including copy of message" },
  115. { "Rf", 1, rcmd, "Rf [addr]file message and reply with copy" },
  116. { "s", 1, scmd, "s file append raw message to file" },
  117. { "u", 0, ucmd, "u remove deletion mark" },
  118. { "w", 1, wcmd, "w file store message contents as file" },
  119. { "x", 0, xcmd, "x exit without flushing deleted messages" },
  120. { "y", 0, ycmd, "y synchronize with mail box" },
  121. { "=", 1, eqcmd, "= print current message number" },
  122. { "|", 1, pipecmd, "|cmd pipe message body to a command" },
  123. { "||", 1, rpipecmd, "||cmd pipe raw message to a command" },
  124. { "!", 1, bangcmd, "!cmd run a command" },
  125. { nil, 0, nil, nil },
  126. };
  127. enum
  128. {
  129. NARG= 32,
  130. };
  131. struct Cmd {
  132. Message *msgs;
  133. Message *(*f)(Cmd*, Message*);
  134. int an;
  135. char *av[NARG];
  136. int delete;
  137. };
  138. Biobuf out;
  139. int startedfs;
  140. int reverse;
  141. int longestfrom = 12;
  142. String* file2string(String*, char*);
  143. int dir2message(Message*, int);
  144. int filelen(String*, char*);
  145. String* extendpath(String*, char*);
  146. void snprintheader(char*, int, Message*);
  147. void cracktime(char*, char*, int);
  148. int cistrncmp(char*, char*, int);
  149. int cistrcmp(char*, char*);
  150. Reprog* parsesearch(char**);
  151. char* parseaddr(char**, Message*, Message*, Message*, Message**);
  152. char* parsecmd(char*, Cmd*, Message*, Message*);
  153. char* readline(char*, char*, int);
  154. void messagecount(Message*);
  155. void system(char*, char**, int);
  156. void mkid(String*, Message*);
  157. int switchmb(char*, char*);
  158. void closemb(void);
  159. int lineize(char*, char**, int);
  160. int rawsearch(Message*, Reprog*);
  161. Message* dosingleton(Message*, char*);
  162. String* rooted(String*);
  163. int plumb(Message*, Ctype*);
  164. String* addrecolon(char*);
  165. void exitfs(char*);
  166. Message* flushdeleted(Message*);
  167. void
  168. usage(void)
  169. {
  170. fprint(2, "usage: %s [-nr] [-f mailfile] [-s mailfile]\n", argv0);
  171. fprint(2, " %s -c dir\n", argv0);
  172. exits("usage");
  173. }
  174. void
  175. catchnote(void*, char *note)
  176. {
  177. if(strstr(note, "interrupt") != nil){
  178. interrupted = 1;
  179. noted(NCONT);
  180. }
  181. noted(NDFLT);
  182. }
  183. char *
  184. plural(int n)
  185. {
  186. if (n == 1)
  187. return "";
  188. return "s";
  189. }
  190. void
  191. main(int argc, char **argv)
  192. {
  193. Message *cur, *m, *x;
  194. char cmdline[4*1024];
  195. Cmd cmd;
  196. Ctype *cp;
  197. int n, cflag;
  198. char *av[4];
  199. String *prompt;
  200. char *err, *file, *singleton;
  201. quotefmtinstall();
  202. Binit(&out, 1, OWRITE);
  203. file = nil;
  204. singleton = nil;
  205. reverse = 1;
  206. cflag = 0;
  207. ARGBEGIN {
  208. case 'c':
  209. cflag = 1;
  210. break;
  211. case 'f':
  212. file = EARGF(usage());
  213. break;
  214. case 's':
  215. singleton = EARGF(usage());
  216. break;
  217. case 'r':
  218. reverse = 0;
  219. break;
  220. case 'n':
  221. natural = 1;
  222. reverse = 0;
  223. break;
  224. default:
  225. usage();
  226. break;
  227. } ARGEND;
  228. user = getlog();
  229. if(user == nil || *user == 0)
  230. sysfatal("can't read user name");
  231. if(cflag){
  232. if(argc > 0)
  233. creatembox(user, argv[0]);
  234. else
  235. creatembox(user, nil);
  236. exits(0);
  237. }
  238. if(argc)
  239. usage();
  240. if(access("/mail/fs/ctl", 0) < 0){
  241. startedfs = 1;
  242. av[0] = "fs";
  243. av[1] = "-p";
  244. av[2] = 0;
  245. system("/bin/upas/fs", av, -1);
  246. }
  247. switchmb(file, singleton);
  248. top.path = s_copy(root);
  249. for(cp = ctype; cp < ctype+nelem(ctype)-1; cp++)
  250. cp->next = cp+1;
  251. if(singleton != nil){
  252. cur = dosingleton(&top, singleton);
  253. if(cur == nil){
  254. Bprint(&out, "no message\n");
  255. exitfs(0);
  256. }
  257. pcmd(nil, cur);
  258. } else {
  259. cur = &top;
  260. n = dir2message(&top, reverse);
  261. if(n < 0)
  262. sysfatal("can't read %s", s_to_c(top.path));
  263. Bprint(&out, "%d message%s\n", n, plural(n));
  264. }
  265. notify(catchnote);
  266. prompt = s_new();
  267. for(;;){
  268. s_reset(prompt);
  269. if(cur == &top)
  270. s_append(prompt, ": ");
  271. else {
  272. mkid(prompt, cur);
  273. s_append(prompt, ": ");
  274. }
  275. // leave space at the end of cmd line in case parsecmd needs to
  276. // add a space after a '|' or '!'
  277. if(readline(s_to_c(prompt), cmdline, sizeof(cmdline)-1) == nil)
  278. break;
  279. err = parsecmd(cmdline, &cmd, top.child, cur);
  280. if(err != nil){
  281. Bprint(&out, "!%s\n", err);
  282. continue;
  283. }
  284. if(singleton != nil && cmd.f == icmd){
  285. Bprint(&out, "!illegal command\n");
  286. continue;
  287. }
  288. interrupted = 0;
  289. if(cmd.msgs == nil || cmd.msgs == &top){
  290. x = (*cmd.f)(&cmd, &top);
  291. if(x != nil)
  292. cur = x;
  293. } else for(m = cmd.msgs; m != nil; m = m->cmd){
  294. x = m;
  295. if(cmd.delete){
  296. dcmd(&cmd, x);
  297. // dp acts differently than all other commands
  298. // since its an old lesk idiom that people love.
  299. // it deletes the current message, moves the current
  300. // pointer ahead one and prints.
  301. if(cmd.f == pcmd){
  302. if(x->next == nil){
  303. Bprint(&out, "!address\n");
  304. cur = x;
  305. break;
  306. } else
  307. x = x->next;
  308. }
  309. }
  310. x = (*cmd.f)(&cmd, x);
  311. if(x != nil)
  312. cur = x;
  313. if(interrupted)
  314. break;
  315. if(singleton != nil && (cmd.delete || cmd.f == dcmd))
  316. qcmd(nil, nil);
  317. }
  318. if(doflush)
  319. cur = flushdeleted(cur);
  320. }
  321. qcmd(nil, nil);
  322. }
  323. //
  324. // read the message info
  325. //
  326. Message*
  327. file2message(Message *parent, char *name)
  328. {
  329. Message *m;
  330. String *path;
  331. char *f[10];
  332. m = mallocz(sizeof(Message), 1);
  333. if(m == nil)
  334. return nil;
  335. m->path = path = extendpath(parent->path, name);
  336. m->fileno = atoi(name);
  337. m->info = file2string(path, "info");
  338. lineize(s_to_c(m->info), f, nelem(f));
  339. m->from = f[0];
  340. m->to = f[1];
  341. m->cc = f[2];
  342. m->replyto = f[3];
  343. m->date = f[4];
  344. m->subject = f[5];
  345. m->type = f[6];
  346. m->disposition = f[7];
  347. m->filename = f[8];
  348. m->len = filelen(path, "raw");
  349. if(strstr(m->type, "multipart") != nil || strcmp(m->type, "message/rfc822") == 0)
  350. dir2message(m, 0);
  351. m->parent = parent;
  352. return m;
  353. }
  354. void
  355. freemessage(Message *m)
  356. {
  357. Message *nm, *next;
  358. for(nm = m->child; nm != nil; nm = next){
  359. next = nm->next;
  360. freemessage(nm);
  361. }
  362. s_free(m->path);
  363. s_free(m->info);
  364. free(m);
  365. }
  366. //
  367. // read a directory into a list of messages
  368. //
  369. int
  370. dir2message(Message *parent, int reverse)
  371. {
  372. int i, n, fd, highest, newmsgs;
  373. Dir *d;
  374. Message *first, *last, *m;
  375. fd = open(s_to_c(parent->path), OREAD);
  376. if(fd < 0)
  377. return -1;
  378. // count current entries
  379. first = parent->child;
  380. highest = newmsgs = 0;
  381. for(last = parent->child; last != nil && last->next != nil; last = last->next)
  382. if(last->fileno > highest)
  383. highest = last->fileno;
  384. if(last != nil)
  385. if(last->fileno > highest)
  386. highest = last->fileno;
  387. n = dirreadall(fd, &d);
  388. for(i = 0; i < n; i++){
  389. if((d[i].qid.type & QTDIR) == 0)
  390. continue;
  391. if(atoi(d[i].name) <= highest)
  392. continue;
  393. m = file2message(parent, d[i].name);
  394. if(m == nil)
  395. break;
  396. newmsgs++;
  397. if(reverse){
  398. m->next = first;
  399. if(first != nil)
  400. first->prev = m;
  401. first = m;
  402. } else {
  403. if(first == nil)
  404. first = m;
  405. else
  406. last->next = m;
  407. m->prev = last;
  408. last = m;
  409. }
  410. }
  411. free(d);
  412. close(fd);
  413. parent->child = first;
  414. // renumber and file longest from
  415. i = 1;
  416. longestfrom = 12;
  417. for(m = first; m != nil; m = m->next){
  418. m->id = natural ? m->fileno : i++;
  419. n = strlen(m->from);
  420. if(n > longestfrom)
  421. longestfrom = n;
  422. }
  423. return newmsgs;
  424. }
  425. //
  426. // point directly to a message
  427. //
  428. Message*
  429. dosingleton(Message *parent, char *path)
  430. {
  431. char *p, *np;
  432. Message *m;
  433. // walk down to message and read it
  434. if(strlen(path) < rootlen)
  435. return nil;
  436. if(path[rootlen] != '/')
  437. return nil;
  438. p = path+rootlen+1;
  439. np = strchr(p, '/');
  440. if(np != nil)
  441. *np = 0;
  442. m = file2message(parent, p);
  443. if(m == nil)
  444. return nil;
  445. parent->child = m;
  446. m->id = 1;
  447. // walk down to requested component
  448. while(np != nil){
  449. *np = '/';
  450. np = strchr(np+1, '/');
  451. if(np != nil)
  452. *np = 0;
  453. for(m = m->child; m != nil; m = m->next)
  454. if(strcmp(path, s_to_c(m->path)) == 0)
  455. return m;
  456. if(m == nil)
  457. return nil;
  458. }
  459. return m;
  460. }
  461. //
  462. // read a file into a string
  463. //
  464. String*
  465. file2string(String *dir, char *file)
  466. {
  467. String *s;
  468. int fd, n, m;
  469. s = extendpath(dir, file);
  470. fd = open(s_to_c(s), OREAD);
  471. s_grow(s, 512); /* avoid multiple reads on info files */
  472. s_reset(s);
  473. if(fd < 0)
  474. return s;
  475. for(;;){
  476. n = s->end - s->ptr;
  477. if(n == 0){
  478. s_grow(s, 128);
  479. continue;
  480. }
  481. m = read(fd, s->ptr, n);
  482. if(m <= 0)
  483. break;
  484. s->ptr += m;
  485. if(m < n)
  486. break;
  487. }
  488. s_terminate(s);
  489. close(fd);
  490. return s;
  491. }
  492. //
  493. // get the length of a file
  494. //
  495. int
  496. filelen(String *dir, char *file)
  497. {
  498. String *path;
  499. Dir *d;
  500. int rv;
  501. path = extendpath(dir, file);
  502. d = dirstat(s_to_c(path));
  503. if(d == nil){
  504. s_free(path);
  505. return -1;
  506. }
  507. s_free(path);
  508. rv = d->length;
  509. free(d);
  510. return rv;
  511. }
  512. //
  513. // walk the path name an element
  514. //
  515. String*
  516. extendpath(String *dir, char *name)
  517. {
  518. String *path;
  519. if(strcmp(s_to_c(dir), ".") == 0)
  520. path = s_new();
  521. else {
  522. path = s_copy(s_to_c(dir));
  523. s_append(path, "/");
  524. }
  525. s_append(path, name);
  526. return path;
  527. }
  528. int
  529. cistrncmp(char *a, char *b, int n)
  530. {
  531. while(n-- > 0){
  532. if(tolower(*a++) != tolower(*b++))
  533. return -1;
  534. }
  535. return 0;
  536. }
  537. int
  538. cistrcmp(char *a, char *b)
  539. {
  540. for(;;){
  541. if(tolower(*a) != tolower(*b++))
  542. return -1;
  543. if(*a++ == 0)
  544. break;
  545. }
  546. return 0;
  547. }
  548. char*
  549. nosecs(char *t)
  550. {
  551. char *p;
  552. p = strchr(t, ':');
  553. if(p == nil)
  554. return t;
  555. p = strchr(p+1, ':');
  556. if(p != nil)
  557. *p = 0;
  558. return t;
  559. }
  560. char *months[12] =
  561. {
  562. "jan", "feb", "mar", "apr", "may", "jun",
  563. "jul", "aug", "sep", "oct", "nov", "dec"
  564. };
  565. int
  566. month(char *m)
  567. {
  568. int i;
  569. for(i = 0; i < 12; i++)
  570. if(cistrcmp(m, months[i]) == 0)
  571. return i+1;
  572. return 1;
  573. }
  574. enum
  575. {
  576. Yearsecs= 365*24*60*60
  577. };
  578. void
  579. cracktime(char *d, char *out, int len)
  580. {
  581. char in[64];
  582. char *f[6];
  583. int n;
  584. Tm tm;
  585. long now, then;
  586. char *dtime;
  587. *out = 0;
  588. if(d == nil)
  589. return;
  590. strncpy(in, d, sizeof(in));
  591. in[sizeof(in)-1] = 0;
  592. n = getfields(in, f, 6, 1, " \t\r\n");
  593. if(n != 6){
  594. // unknown style
  595. snprint(out, 16, "%10.10s", d);
  596. return;
  597. }
  598. now = time(0);
  599. memset(&tm, 0, sizeof tm);
  600. if(strchr(f[0], ',') != nil && strchr(f[4], ':') != nil){
  601. // 822 style
  602. tm.year = atoi(f[3])-1900;
  603. tm.mon = month(f[2]);
  604. tm.mday = atoi(f[1]);
  605. dtime = nosecs(f[4]);
  606. then = tm2sec(&tm);
  607. } else if(strchr(f[3], ':') != nil){
  608. // unix style
  609. tm.year = atoi(f[5])-1900;
  610. tm.mon = month(f[1]);
  611. tm.mday = atoi(f[2]);
  612. dtime = nosecs(f[3]);
  613. then = tm2sec(&tm);
  614. } else {
  615. then = now;
  616. tm = *localtime(now);
  617. dtime = "";
  618. }
  619. if(now - then < Yearsecs/2)
  620. snprint(out, len, "%2d/%2.2d %s", tm.mon, tm.mday, dtime);
  621. else
  622. snprint(out, len, "%2d/%2.2d %4d", tm.mon, tm.mday, tm.year+1900);
  623. }
  624. Ctype*
  625. findctype(Message *m)
  626. {
  627. char *p;
  628. char ftype[128];
  629. int n, pfd[2];
  630. Ctype *a, *cp;
  631. static Ctype nulltype = { "", 0, 0, 0 };
  632. static Ctype bintype = { "application/octet-stream", "bin", 0, 0 };
  633. for(cp = ctype; cp; cp = cp->next)
  634. if(strncmp(cp->type, m->type, strlen(cp->type)) == 0)
  635. return cp;
  636. /* use file(1) for any unknown mimetypes
  637. *
  638. * if (strcmp(m->type, bintype.type) != 0)
  639. * return &nulltype;
  640. */
  641. if(pipe(pfd) < 0)
  642. return &bintype;
  643. *ftype = 0;
  644. switch(fork()){
  645. case -1:
  646. break;
  647. case 0:
  648. close(pfd[1]);
  649. close(0);
  650. dup(pfd[0], 0);
  651. close(1);
  652. dup(pfd[0], 1);
  653. execl("/bin/file", "file", "-m", s_to_c(extendpath(m->path, "body")), nil);
  654. exits(0);
  655. default:
  656. close(pfd[0]);
  657. n = read(pfd[1], ftype, sizeof(ftype));
  658. if(n > 0)
  659. ftype[n] = 0;
  660. close(pfd[1]);
  661. waitpid();
  662. break;
  663. }
  664. if (*ftype=='\0' || (p = strchr(ftype, '/')) == nil)
  665. return &bintype;
  666. *p++ = 0;
  667. a = mallocz(sizeof(Ctype), 1);
  668. a->type = strdup(ftype);
  669. a->ext = strdup(p);
  670. a->display = 0;
  671. a->plumbdest = strdup(ftype);
  672. for(cp = ctype; cp->next; cp = cp->next)
  673. continue;
  674. cp->next = a;
  675. a->next = nil;
  676. return a;
  677. }
  678. void
  679. mkid(String *s, Message *m)
  680. {
  681. char buf[32];
  682. if(m->parent != &top){
  683. mkid(s, m->parent);
  684. s_append(s, ".");
  685. }
  686. sprint(buf, "%d", m->id);
  687. s_append(s, buf);
  688. }
  689. void
  690. snprintheader(char *buf, int len, Message *m)
  691. {
  692. char timebuf[32];
  693. String *id;
  694. char *p, *q;
  695. // create id
  696. id = s_new();
  697. mkid(id, m);
  698. if(*m->from == 0){
  699. // no from
  700. snprint(buf, len, "%-3s %s %6d %s",
  701. s_to_c(id),
  702. m->type,
  703. m->len,
  704. m->filename);
  705. } else if(*m->subject){
  706. q = p = strdup(m->subject);
  707. while(*p == ' ')
  708. p++;
  709. if(strlen(p) > 50)
  710. p[50] = 0;
  711. cracktime(m->date, timebuf, sizeof(timebuf));
  712. snprint(buf, len, "%-3s %c%c%c %6d %11.11s %-*.*s %s",
  713. s_to_c(id),
  714. m->child ? 'H' : ' ',
  715. m->deleted ? 'd' : ' ',
  716. m->stored ? 's' : ' ',
  717. m->len,
  718. timebuf,
  719. longestfrom, longestfrom, m->from,
  720. p);
  721. free(q);
  722. } else {
  723. cracktime(m->date, timebuf, sizeof(timebuf));
  724. snprint(buf, len, "%-3s %c%c%c %6d %11.11s %s",
  725. s_to_c(id),
  726. m->child ? 'H' : ' ',
  727. m->deleted ? 'd' : ' ',
  728. m->stored ? 's' : ' ',
  729. m->len,
  730. timebuf,
  731. m->from);
  732. }
  733. s_free(id);
  734. }
  735. char *spaces = " ";
  736. void
  737. snprintHeader(char *buf, int len, int indent, Message *m)
  738. {
  739. String *id;
  740. char typeid[64];
  741. char *p, *e;
  742. // create id
  743. id = s_new();
  744. mkid(id, m);
  745. e = buf + len;
  746. snprint(typeid, sizeof typeid, "%s %s", s_to_c(id), m->type);
  747. if(indent < 6)
  748. p = seprint(buf, e, "%-32s %-6d ", typeid, m->len);
  749. else
  750. p = seprint(buf, e, "%-64s %-6d ", typeid, m->len);
  751. if(m->filename && *m->filename)
  752. p = seprint(p, e, "(file,%s)", m->filename);
  753. if(m->from && *m->from)
  754. p = seprint(p, e, "(from,%s)", m->from);
  755. if(m->subject && *m->subject)
  756. seprint(p, e, "(subj,%s)", m->subject);
  757. s_free(id);
  758. }
  759. char sstring[256];
  760. // cmd := range cmd ' ' arg-list ;
  761. // range := address
  762. // | address ',' address
  763. // | 'g' search ;
  764. // address := msgno
  765. // | search ;
  766. // msgno := number
  767. // | number '/' msgno ;
  768. // search := '/' string '/'
  769. // | '%' string '%' ;
  770. //
  771. Reprog*
  772. parsesearch(char **pp)
  773. {
  774. char *p, *np;
  775. int c, n;
  776. p = *pp;
  777. c = *p++;
  778. np = strchr(p, c);
  779. if(np != nil){
  780. *np++ = 0;
  781. *pp = np;
  782. } else {
  783. n = strlen(p);
  784. *pp = p + n;
  785. }
  786. if(*p == 0)
  787. p = sstring;
  788. else{
  789. strncpy(sstring, p, sizeof(sstring));
  790. sstring[sizeof(sstring)-1] = 0;
  791. }
  792. return regcomp(p);
  793. }
  794. static char *
  795. num2msg(Message **mp, int sign, int n, Message *first, Message *cur)
  796. {
  797. Message *m;
  798. m = nil;
  799. switch(sign){
  800. case 0:
  801. for(m = first; m != nil; m = m->next)
  802. if(m->id == n)
  803. break;
  804. break;
  805. case -1:
  806. if(cur != &top)
  807. for(m = cur; m != nil && n > 0; n--)
  808. m = m->prev;
  809. break;
  810. case 1:
  811. if(cur == &top){
  812. n--;
  813. cur = first;
  814. }
  815. for(m = cur; m != nil && n > 0; n--)
  816. m = m->next;
  817. break;
  818. }
  819. if(m == nil)
  820. return "address";
  821. *mp = m;
  822. return nil;
  823. }
  824. char*
  825. parseaddr(char **pp, Message *first, Message *cur, Message *unspec, Message **mp)
  826. {
  827. int n;
  828. Message *m;
  829. char *p, *err;
  830. Reprog *prog;
  831. int c, sign;
  832. char buf[256];
  833. *mp = nil;
  834. p = *pp;
  835. if(*p == '+'){
  836. sign = 1;
  837. p++;
  838. *pp = p;
  839. } else if(*p == '-'){
  840. sign = -1;
  841. p++;
  842. *pp = p;
  843. } else
  844. sign = 0;
  845. /*
  846. * TODO: verify & install this.
  847. * make + and - mean +1 and -1, as in ed. then -,.d won't
  848. * delete all messages up to the current one. - geoff
  849. */
  850. if(sign && (!isascii(*p) || !isdigit(*p))) {
  851. err = num2msg(mp, sign, 1, first, cur);
  852. if (err != nil)
  853. return err;
  854. }
  855. switch(*p){
  856. default:
  857. if(sign){
  858. n = 1;
  859. goto number;
  860. }
  861. *mp = unspec;
  862. break;
  863. case '0': case '1': case '2': case '3': case '4':
  864. case '5': case '6': case '7': case '8': case '9':
  865. n = strtoul(p, pp, 10);
  866. if(n == 0){
  867. if(sign)
  868. *mp = cur;
  869. else
  870. *mp = &top;
  871. break;
  872. }
  873. /* fall through */
  874. number:
  875. err = num2msg(mp, sign, n, first, cur);
  876. if (err != nil)
  877. return err;
  878. break;
  879. case '%':
  880. case '/':
  881. case '?':
  882. c = *p;
  883. prog = parsesearch(pp);
  884. if(prog == nil)
  885. return "badly formed regular expression";
  886. m = nil;
  887. switch(c){
  888. case '%':
  889. for(m = cur == &top ? first : cur->next; m != nil; m = m->next){
  890. if(rawsearch(m, prog))
  891. break;
  892. }
  893. break;
  894. case '/':
  895. for(m = cur == &top ? first : cur->next; m != nil; m = m->next){
  896. snprintheader(buf, sizeof(buf), m);
  897. if(regexec(prog, buf, nil, 0))
  898. break;
  899. }
  900. break;
  901. case '?':
  902. for(m = cur == &top ? nil : cur->prev; m != nil; m = m->prev){
  903. snprintheader(buf, sizeof(buf), m);
  904. if(regexec(prog, buf, nil, 0))
  905. break;
  906. }
  907. break;
  908. }
  909. if(m == nil)
  910. return "search";
  911. *mp = m;
  912. free(prog);
  913. break;
  914. case '$':
  915. for(m = first; m != nil && m->next != nil; m = m->next)
  916. ;
  917. *mp = m;
  918. *pp = p+1;
  919. break;
  920. case '.':
  921. *mp = cur;
  922. *pp = p+1;
  923. break;
  924. case ',':
  925. if (*mp == nil)
  926. *mp = first;
  927. *pp = p;
  928. break;
  929. }
  930. if(*mp != nil && **pp == '.'){
  931. (*pp)++;
  932. if((*mp)->child == nil)
  933. return "no sub parts";
  934. return parseaddr(pp, (*mp)->child, (*mp)->child, (*mp)->child, mp);
  935. }
  936. if(**pp == '+' || **pp == '-' || **pp == '/' || **pp == '%')
  937. return parseaddr(pp, first, *mp, *mp, mp);
  938. return nil;
  939. }
  940. //
  941. // search a message for a regular expression match
  942. //
  943. int
  944. rawsearch(Message *m, Reprog *prog)
  945. {
  946. char buf[4096+1];
  947. int i, fd, rv;
  948. String *path;
  949. path = extendpath(m->path, "raw");
  950. fd = open(s_to_c(path), OREAD);
  951. if(fd < 0)
  952. return 0;
  953. // march through raw message 4096 bytes at a time
  954. // with a 128 byte overlap to chain the re search.
  955. rv = 0;
  956. for(;;){
  957. i = read(fd, buf, sizeof(buf)-1);
  958. if(i <= 0)
  959. break;
  960. buf[i] = 0;
  961. if(regexec(prog, buf, nil, 0)){
  962. rv = 1;
  963. break;
  964. }
  965. if(i < sizeof(buf)-1)
  966. break;
  967. if(seek(fd, -128LL, 1) < 0)
  968. break;
  969. }
  970. close(fd);
  971. s_free(path);
  972. return rv;
  973. }
  974. char*
  975. parsecmd(char *p, Cmd *cmd, Message *first, Message *cur)
  976. {
  977. Reprog *prog;
  978. Message *m, *s, *e, **l, *last;
  979. char buf[256];
  980. char *err;
  981. int i, c;
  982. char *q;
  983. static char errbuf[Errlen];
  984. cmd->delete = 0;
  985. l = &cmd->msgs;
  986. *l = nil;
  987. // eat white space
  988. while(*p == ' ')
  989. p++;
  990. // null command is a special case (advance and print)
  991. if(*p == 0){
  992. if(cur == &top){
  993. // special case
  994. m = first;
  995. } else {
  996. // walk to the next message even if we have to go up
  997. m = cur->next;
  998. while(m == nil && cur->parent != nil){
  999. cur = cur->parent;
  1000. m = cur->next;
  1001. }
  1002. }
  1003. if(m == nil)
  1004. return "address";
  1005. *l = m;
  1006. m->cmd = nil;
  1007. cmd->an = 0;
  1008. cmd->f = pcmd;
  1009. return nil;
  1010. }
  1011. // global search ?
  1012. if(*p == 'g'){
  1013. p++;
  1014. // no search string means all messages
  1015. if(*p != '/' && *p != '%'){
  1016. for(m = first; m != nil; m = m->next){
  1017. *l = m;
  1018. l = &m->cmd;
  1019. *l = nil;
  1020. }
  1021. } else {
  1022. // mark all messages matching this search string
  1023. c = *p;
  1024. prog = parsesearch(&p);
  1025. if(prog == nil)
  1026. return "badly formed regular expression";
  1027. if(c == '%'){
  1028. for(m = first; m != nil; m = m->next){
  1029. if(rawsearch(m, prog)){
  1030. *l = m;
  1031. l = &m->cmd;
  1032. *l = nil;
  1033. }
  1034. }
  1035. } else {
  1036. for(m = first; m != nil; m = m->next){
  1037. snprintheader(buf, sizeof(buf), m);
  1038. if(regexec(prog, buf, nil, 0)){
  1039. *l = m;
  1040. l = &m->cmd;
  1041. *l = nil;
  1042. }
  1043. }
  1044. }
  1045. free(prog);
  1046. }
  1047. } else {
  1048. // parse an address
  1049. s = e = nil;
  1050. err = parseaddr(&p, first, cur, cur, &s);
  1051. if(err != nil)
  1052. return err;
  1053. if(*p == ','){
  1054. // this is an address range
  1055. if(s == &top)
  1056. s = first;
  1057. p++;
  1058. for(last = s; last != nil && last->next != nil; last = last->next)
  1059. ;
  1060. err = parseaddr(&p, first, cur, last, &e);
  1061. if(err != nil)
  1062. return err;
  1063. // select all messages in the range
  1064. for(; s != nil; s = s->next){
  1065. *l = s;
  1066. l = &s->cmd;
  1067. *l = nil;
  1068. if(s == e)
  1069. break;
  1070. }
  1071. if(s == nil)
  1072. return "null address range";
  1073. } else {
  1074. // single address
  1075. if(s != &top){
  1076. *l = s;
  1077. s->cmd = nil;
  1078. }
  1079. }
  1080. }
  1081. // insert a space after '!'s and '|'s
  1082. for(q = p; *q; q++)
  1083. if(*q != '!' && *q != '|')
  1084. break;
  1085. if(q != p && *q != ' '){
  1086. memmove(q+1, q, strlen(q)+1);
  1087. *q = ' ';
  1088. }
  1089. cmd->an = getfields(p, cmd->av, nelem(cmd->av) - 1, 1, " \t\r\n");
  1090. if(cmd->an == 0 || *cmd->av[0] == 0)
  1091. cmd->f = pcmd;
  1092. else {
  1093. // hack to allow all messages to start with 'd'
  1094. if(*(cmd->av[0]) == 'd' && *(cmd->av[0]+1) != 0){
  1095. cmd->delete = 1;
  1096. cmd->av[0]++;
  1097. }
  1098. // search command table
  1099. for(i = 0; cmdtab[i].cmd != nil; i++)
  1100. if(strcmp(cmd->av[0], cmdtab[i].cmd) == 0)
  1101. break;
  1102. if(cmdtab[i].cmd == nil)
  1103. return "illegal command";
  1104. if(cmdtab[i].args == 0 && cmd->an > 1){
  1105. snprint(errbuf, sizeof(errbuf), "%s doesn't take an argument", cmdtab[i].cmd);
  1106. return errbuf;
  1107. }
  1108. cmd->f = cmdtab[i].f;
  1109. }
  1110. return nil;
  1111. }
  1112. // inefficient read from standard input
  1113. char*
  1114. readline(char *prompt, char *line, int len)
  1115. {
  1116. char *p, *e;
  1117. int n;
  1118. retry:
  1119. interrupted = 0;
  1120. Bprint(&out, "%s", prompt);
  1121. Bflush(&out);
  1122. e = line + len;
  1123. for(p = line; p < e; p++){
  1124. n = read(0, p, 1);
  1125. if(n < 0){
  1126. if(interrupted)
  1127. goto retry;
  1128. return nil;
  1129. }
  1130. if(n == 0)
  1131. return nil;
  1132. if(*p == '\n')
  1133. break;
  1134. }
  1135. *p = 0;
  1136. return line;
  1137. }
  1138. void
  1139. messagecount(Message *m)
  1140. {
  1141. int i;
  1142. i = 0;
  1143. for(; m != nil; m = m->next)
  1144. i++;
  1145. Bprint(&out, "%d message%s\n", i, plural(i));
  1146. }
  1147. Message*
  1148. aichcmd(Message *m, int indent)
  1149. {
  1150. char hdr[256];
  1151. if(m == &top)
  1152. return nil;
  1153. snprintHeader(hdr, sizeof(hdr), indent, m);
  1154. Bprint(&out, "%s\n", hdr);
  1155. for(m = m->child; m != nil; m = m->next)
  1156. aichcmd(m, indent+1);
  1157. return nil;
  1158. }
  1159. Message*
  1160. Hcmd(Cmd*, Message *m)
  1161. {
  1162. if(m == &top)
  1163. return nil;
  1164. aichcmd(m, 0);
  1165. return nil;
  1166. }
  1167. Message*
  1168. hcmd(Cmd*, Message *m)
  1169. {
  1170. char hdr[256];
  1171. if(m == &top)
  1172. return nil;
  1173. snprintheader(hdr, sizeof(hdr), m);
  1174. Bprint(&out, "%s\n", hdr);
  1175. return nil;
  1176. }
  1177. Message*
  1178. bcmd(Cmd*, Message *m)
  1179. {
  1180. int i;
  1181. Message *om = m;
  1182. if(m == &top)
  1183. m = top.child;
  1184. for(i = 0; i < 10 && m != nil; i++){
  1185. hcmd(nil, m);
  1186. om = m;
  1187. m = m->next;
  1188. }
  1189. return om;
  1190. }
  1191. Message*
  1192. ncmd(Cmd*, Message *m)
  1193. {
  1194. if(m == &top)
  1195. return m->child;
  1196. return m->next;
  1197. }
  1198. /* turn crlfs into newlines */
  1199. int
  1200. decrlf(char *buf, int n)
  1201. {
  1202. char *nl;
  1203. int left;
  1204. for (nl = buf, left = n; left >= 2 &&
  1205. (nl = memchr(nl, '\r', left)) != nil; left = n - (nl - buf))
  1206. if (nl[1] == '\n'){
  1207. memmove(nl, nl+1, left-1); /* delete the cr */
  1208. --n;
  1209. }
  1210. return n;
  1211. }
  1212. int
  1213. printpart(String *s, char *part)
  1214. {
  1215. char buf[4096];
  1216. int n, fd, tot;
  1217. String *path;
  1218. path = extendpath(s, part);
  1219. fd = open(s_to_c(path), OREAD);
  1220. s_free(path);
  1221. if(fd < 0){
  1222. fprint(2, "!message disappeared\n");
  1223. return 0;
  1224. }
  1225. tot = 0;
  1226. while((n = read(fd, buf, sizeof(buf))) > 0){
  1227. if(interrupted)
  1228. break;
  1229. n = decrlf(buf, n);
  1230. if(Bwrite(&out, buf, n) <= 0)
  1231. break;
  1232. tot += n;
  1233. }
  1234. close(fd);
  1235. return tot;
  1236. }
  1237. int
  1238. printhtml(Message *m)
  1239. {
  1240. Cmd c;
  1241. c.an = 3;
  1242. c.av[1] = "/bin/htmlfmt";
  1243. c.av[2] = "-l 40 -cutf-8";
  1244. Bprint(&out, "!%s\n", c.av[1]);
  1245. Bflush(&out);
  1246. pipecmd(&c, m);
  1247. return 0;
  1248. }
  1249. Message*
  1250. Pcmd(Cmd*, Message *m)
  1251. {
  1252. if(m == &top)
  1253. return &top;
  1254. if(m->parent == &top)
  1255. printpart(m->path, "unixheader");
  1256. printpart(m->path, "raw");
  1257. return m;
  1258. }
  1259. void
  1260. compress(char *p)
  1261. {
  1262. char *np;
  1263. int last;
  1264. last = ' ';
  1265. for(np = p; *p; p++){
  1266. if(*p != ' ' || last != ' '){
  1267. last = *p;
  1268. *np++ = last;
  1269. }
  1270. }
  1271. *np = 0;
  1272. }
  1273. /*
  1274. * find the best alternative part.
  1275. *
  1276. * turkeys have started emitting empty text/plain parts,
  1277. * with the actual content in a text/html part, which complicates the choice.
  1278. *
  1279. * bigger turkeys emit a tiny base64-encoded text/plain part,
  1280. * a small base64-encoded text/html part, and the real content is in
  1281. * a text/calendar part.
  1282. *
  1283. * the magic lengths are empirically derived.
  1284. * as turkeys devolve and mutate, this will only get worse.
  1285. */
  1286. static Message*
  1287. bestalt(Message *m)
  1288. {
  1289. Message *nm;
  1290. Message *realplain, *realhtml, *realcal;
  1291. Ctype *cp;
  1292. realplain = realhtml = realcal = nil;
  1293. for(nm = m->child; nm != nil; nm = nm->next){
  1294. cp = findctype(nm);
  1295. if(cp->ext != nil)
  1296. if(strncmp(cp->ext, "txt", 3) == 0 && nm->len >= 88)
  1297. realplain = nm;
  1298. else if(strncmp(cp->ext, "html", 3) == 0 &&
  1299. nm->len >= 670)
  1300. realhtml = nm;
  1301. else if(strncmp(cp->ext, "ics", 3) == 0)
  1302. realcal = nm;
  1303. }
  1304. if(realplain == nil && realhtml == nil && realcal)
  1305. return realcal; /* super-turkey */
  1306. else if(realplain == nil && realhtml)
  1307. return realhtml; /* regular turkey */
  1308. else
  1309. return realplain;
  1310. }
  1311. static void
  1312. pralt(Message *m)
  1313. {
  1314. Message *nm;
  1315. Ctype *cp;
  1316. nm = bestalt(m);
  1317. if(nm == nil)
  1318. /* no winner. print the first displayable part. */
  1319. for(nm = m->child; nm != nil; nm = nm->next){
  1320. cp = findctype(nm);
  1321. if(cp->display)
  1322. break;
  1323. }
  1324. if(nm != nil)
  1325. pcmd(nil, nm);
  1326. else
  1327. hcmd(nil, m);
  1328. }
  1329. Message*
  1330. pcmd(Cmd*, Message *m)
  1331. {
  1332. Message *nm;
  1333. Ctype *cp;
  1334. String *s;
  1335. char buf[128];
  1336. if(m == &top)
  1337. return &top;
  1338. if(m->parent == &top)
  1339. printpart(m->path, "unixheader");
  1340. if(printpart(m->path, "header") > 0)
  1341. Bprint(&out, "\n");
  1342. cp = findctype(m);
  1343. if(cp->display){
  1344. if(strcmp(m->type, "text/html") == 0)
  1345. printhtml(m);
  1346. else
  1347. printpart(m->path, "body");
  1348. } else if(strcmp(m->type, "multipart/alternative") == 0)
  1349. pralt(m);
  1350. else if(strncmp(m->type, "multipart/", 10) == 0){
  1351. nm = m->child;
  1352. if(nm != nil){
  1353. // always print first part
  1354. pcmd(nil, nm);
  1355. for(nm = nm->next; nm != nil; nm = nm->next){
  1356. s = rooted(s_clone(nm->path));
  1357. cp = findctype(nm);
  1358. snprintHeader(buf, sizeof buf, -1, nm);
  1359. compress(buf);
  1360. if(strcmp(nm->disposition, "inline") == 0){
  1361. if(cp->ext != nil)
  1362. Bprint(&out, "\n--- %s %s/body.%s\n\n",
  1363. buf, s_to_c(s), cp->ext);
  1364. else
  1365. Bprint(&out, "\n--- %s %s/body\n\n",
  1366. buf, s_to_c(s));
  1367. pcmd(nil, nm);
  1368. } else {
  1369. if(cp->ext != nil)
  1370. Bprint(&out, "\n!--- %s %s/body.%s\n",
  1371. buf, s_to_c(s), cp->ext);
  1372. else
  1373. Bprint(&out, "\n!--- %s %s/body\n",
  1374. buf, s_to_c(s));
  1375. }
  1376. s_free(s);
  1377. }
  1378. } else {
  1379. hcmd(nil, m);
  1380. }
  1381. } else if(strcmp(m->type, "message/rfc822") == 0){
  1382. pcmd(nil, m->child);
  1383. } else if(plumb(m, cp) >= 0)
  1384. Bprint(&out, "\n!--- using plumber to display message of type %s\n", m->type);
  1385. else
  1386. Bprint(&out, "\n!--- cannot display messages of type %s\n", m->type);
  1387. return m;
  1388. }
  1389. void
  1390. printpartindented(String *s, char *part, char *indent)
  1391. {
  1392. char *p;
  1393. String *path;
  1394. Biobuf *b;
  1395. path = extendpath(s, part);
  1396. b = Bopen(s_to_c(path), OREAD);
  1397. s_free(path);
  1398. if(b == nil){
  1399. fprint(2, "!message disappeared\n");
  1400. return;
  1401. }
  1402. while((p = Brdline(b, '\n')) != nil){
  1403. if(interrupted)
  1404. break;
  1405. p[Blinelen(b)-1] = 0;
  1406. if(Bprint(&out, "%s%s\n", indent, p) < 0)
  1407. break;
  1408. }
  1409. Bprint(&out, "\n");
  1410. Bterm(b);
  1411. }
  1412. Message*
  1413. quotecmd(Cmd*, Message *m)
  1414. {
  1415. Message *nm;
  1416. Ctype *cp;
  1417. if(m == &top)
  1418. return &top;
  1419. Bprint(&out, "\n");
  1420. if(m->from != nil && *m->from)
  1421. Bprint(&out, "On %s, %s wrote:\n", m->date, m->from);
  1422. cp = findctype(m);
  1423. if(cp->display){
  1424. printpartindented(m->path, "body", "> ");
  1425. } else if(strcmp(m->type, "multipart/alternative") == 0){
  1426. for(nm = m->child; nm != nil; nm = nm->next){
  1427. cp = findctype(nm);
  1428. if(cp->ext != nil && strncmp(cp->ext, "txt", 3) == 0)
  1429. break;
  1430. }
  1431. if(nm == nil)
  1432. for(nm = m->child; nm != nil; nm = nm->next){
  1433. cp = findctype(nm);
  1434. if(cp->display)
  1435. break;
  1436. }
  1437. if(nm != nil)
  1438. quotecmd(nil, nm);
  1439. } else if(strncmp(m->type, "multipart/", 10) == 0){
  1440. nm = m->child;
  1441. if(nm != nil){
  1442. cp = findctype(nm);
  1443. if(cp->display || strncmp(m->type, "multipart/", 10) == 0)
  1444. quotecmd(nil, nm);
  1445. }
  1446. }
  1447. return m;
  1448. }
  1449. // really delete messages
  1450. Message*
  1451. flushdeleted(Message *cur)
  1452. {
  1453. Message *m, **l;
  1454. char buf[1024], *p, *e, *msg;
  1455. int deld, n, fd;
  1456. int i;
  1457. doflush = 0;
  1458. deld = 0;
  1459. fd = open("/mail/fs/ctl", ORDWR);
  1460. if(fd < 0){
  1461. fprint(2, "!can't delete mail, opening /mail/fs/ctl: %r\n");
  1462. exitfs(0);
  1463. }
  1464. e = &buf[sizeof(buf)];
  1465. p = seprint(buf, e, "delete %s", mbname);
  1466. n = 0;
  1467. for(l = &top.child; *l != nil;){
  1468. m = *l;
  1469. if(!m->deleted){
  1470. l = &(*l)->next;
  1471. continue;
  1472. }
  1473. // don't return a pointer to a deleted message
  1474. if(m == cur)
  1475. cur = m->next;
  1476. deld++;
  1477. msg = strrchr(s_to_c(m->path), '/');
  1478. if(msg == nil)
  1479. msg = s_to_c(m->path);
  1480. else
  1481. msg++;
  1482. if(e-p < 10){
  1483. write(fd, buf, p-buf);
  1484. n = 0;
  1485. p = seprint(buf, e, "delete %s", mbname);
  1486. }
  1487. p = seprint(p, e, " %s", msg);
  1488. n++;
  1489. // unchain and free
  1490. *l = m->next;
  1491. if(m->next)
  1492. m->next->prev = m->prev;
  1493. freemessage(m);
  1494. }
  1495. if(n)
  1496. write(fd, buf, p-buf);
  1497. close(fd);
  1498. if(deld)
  1499. Bprint(&out, "!%d message%s deleted\n", deld, plural(deld));
  1500. // renumber
  1501. i = 1;
  1502. for(m = top.child; m != nil; m = m->next)
  1503. m->id = natural ? m->fileno : i++;
  1504. // if we're out of messages, go back to first
  1505. // if no first, return the fake first
  1506. if(cur == nil){
  1507. if(top.child)
  1508. return top.child;
  1509. else
  1510. return &top;
  1511. }
  1512. return cur;
  1513. }
  1514. Message*
  1515. qcmd(Cmd*, Message*)
  1516. {
  1517. flushdeleted(nil);
  1518. if(didopen)
  1519. closemb();
  1520. Bflush(&out);
  1521. exitfs(0);
  1522. return nil; // not reached
  1523. }
  1524. Message*
  1525. ycmd(Cmd*, Message *m)
  1526. {
  1527. doflush = 1;
  1528. return icmd(nil, m);
  1529. }
  1530. Message*
  1531. xcmd(Cmd*, Message*)
  1532. {
  1533. exitfs(0);
  1534. return nil; // not reached
  1535. }
  1536. Message*
  1537. eqcmd(Cmd*, Message *m)
  1538. {
  1539. if(m == &top)
  1540. Bprint(&out, "0\n");
  1541. else
  1542. Bprint(&out, "%d\n", m->id);
  1543. return nil;
  1544. }
  1545. Message*
  1546. dcmd(Cmd*, Message *m)
  1547. {
  1548. if(m == &top){
  1549. Bprint(&out, "!address\n");
  1550. return nil;
  1551. }
  1552. while(m->parent != &top)
  1553. m = m->parent;
  1554. m->deleted = 1;
  1555. return m;
  1556. }
  1557. Message*
  1558. ucmd(Cmd*, Message *m)
  1559. {
  1560. if(m == &top)
  1561. return nil;
  1562. while(m->parent != &top)
  1563. m = m->parent;
  1564. if(m->deleted < 0)
  1565. Bprint(&out, "!can't undelete, already flushed\n");
  1566. m->deleted = 0;
  1567. return m;
  1568. }
  1569. Message*
  1570. icmd(Cmd*, Message *m)
  1571. {
  1572. int n;
  1573. n = dir2message(&top, reverse);
  1574. if(n > 0)
  1575. Bprint(&out, "%d new message%s\n", n, plural(n));
  1576. return m;
  1577. }
  1578. Message*
  1579. helpcmd(Cmd*, Message *m)
  1580. {
  1581. int i;
  1582. Bprint(&out, "Commands are of the form [<range>] <command> [args]\n");
  1583. Bprint(&out, "<range> := <addr> | <addr>','<addr>| 'g'<search>\n");
  1584. Bprint(&out, "<addr> := '.' | '$' | '^' | <number> | <search> | <addr>'+'<addr> | <addr>'-'<addr>\n");
  1585. Bprint(&out, "<search> := '/'<regexp>'/' | '?'<regexp>'?' | '%%'<regexp>'%%'\n");
  1586. Bprint(&out, "<command> :=\n");
  1587. for(i = 0; cmdtab[i].cmd != nil; i++)
  1588. Bprint(&out, "%s\n", cmdtab[i].help);
  1589. return m;
  1590. }
  1591. int
  1592. tomailer(char **av)
  1593. {
  1594. Waitmsg *w;
  1595. int pid, i;
  1596. // start the mailer and get out of the way
  1597. switch(pid = fork()){
  1598. case -1:
  1599. fprint(2, "can't fork: %r\n");
  1600. return -1;
  1601. case 0:
  1602. Bprint(&out, "!/bin/upas/marshal");
  1603. for(i = 1; av[i]; i++)
  1604. Bprint(&out, " %q", av[i]);
  1605. Bprint(&out, "\n");
  1606. Bflush(&out);
  1607. av[0] = "marshal";
  1608. chdir(wd);
  1609. exec("/bin/upas/marshal", av);
  1610. fprint(2, "couldn't exec /bin/upas/marshal\n");
  1611. exits(0);
  1612. default:
  1613. w = wait();
  1614. if(w == nil){
  1615. if(interrupted)
  1616. postnote(PNPROC, pid, "die");
  1617. waitpid();
  1618. return -1;
  1619. }
  1620. if(w->msg[0]){
  1621. fprint(2, "mailer failed: %s\n", w->msg);
  1622. free(w);
  1623. return -1;
  1624. }
  1625. free(w);
  1626. Bprint(&out, "!\n");
  1627. break;
  1628. }
  1629. return 0;
  1630. }
  1631. //
  1632. // like tokenize but obey "" quoting
  1633. //
  1634. int
  1635. tokenize822(char *str, char **args, int max)
  1636. {
  1637. int na;
  1638. int intok = 0, inquote = 0;
  1639. if(max <= 0)
  1640. return 0;
  1641. for(na=0; ;str++)
  1642. switch(*str) {
  1643. case ' ':
  1644. case '\t':
  1645. if(inquote)
  1646. goto Default;
  1647. /* fall through */
  1648. case '\n':
  1649. *str = 0;
  1650. if(!intok)
  1651. continue;
  1652. intok = 0;
  1653. if(na < max)
  1654. continue;
  1655. /* fall through */
  1656. case 0:
  1657. return na;
  1658. case '"':
  1659. inquote ^= 1;
  1660. /* fall through */
  1661. Default:
  1662. default:
  1663. if(intok)
  1664. continue;
  1665. args[na++] = str;
  1666. intok = 1;
  1667. }
  1668. }
  1669. /* return reply-to address & set *nmp to corresponding Message */
  1670. static char *
  1671. getreplyto(Message *m, Message **nmp)
  1672. {
  1673. Message *nm;
  1674. for(nm = m; nm != &top; nm = nm->parent)
  1675. if(*nm->replyto != 0)
  1676. break;
  1677. *nmp = nm;
  1678. return nm? nm->replyto: nil;
  1679. }
  1680. Message*
  1681. rcmd(Cmd *c, Message *m)
  1682. {
  1683. char *addr;
  1684. char *av[128];
  1685. int i, ai = 1;
  1686. String *from, *rpath, *path = nil, *subject = nil;
  1687. Message *nm;
  1688. if(m == &top){
  1689. Bprint(&out, "!address\n");
  1690. return nil;
  1691. }
  1692. addr = getreplyto(m, &nm);
  1693. if(addr == nil){
  1694. Bprint(&out, "!no reply address\n");
  1695. return nil;
  1696. }
  1697. if(nm == &top){
  1698. print("!noone to reply to\n");
  1699. return nil;
  1700. }
  1701. for(nm = m; nm != &top; nm = nm->parent){
  1702. if(*nm->subject){
  1703. av[ai++] = "-s";
  1704. subject = addrecolon(nm->subject);
  1705. av[ai++] = s_to_c(subject);
  1706. break;
  1707. }
  1708. }
  1709. av[ai++] = "-R";
  1710. rpath = rooted(s_clone(m->path));
  1711. av[ai++] = s_to_c(rpath);
  1712. if(strchr(c->av[0], 'f') != nil){
  1713. fcmd(c, m);
  1714. av[ai++] = "-F";
  1715. }
  1716. if(strchr(c->av[0], 'R') != nil){
  1717. av[ai++] = "-t";
  1718. av[ai++] = "message/rfc822";
  1719. av[ai++] = "-A";
  1720. path = rooted(extendpath(m->path, "raw"));
  1721. av[ai++] = s_to_c(path);
  1722. }
  1723. for(i = 1; i < c->an && ai < nelem(av)-1; i++)
  1724. av[ai++] = c->av[i];
  1725. from = s_copy(addr);
  1726. ai += tokenize822(s_to_c(from), &av[ai], nelem(av) - ai);
  1727. av[ai] = 0;
  1728. if(tomailer(av) < 0)
  1729. m = nil;
  1730. s_free(path);
  1731. s_free(rpath);
  1732. s_free(subject);
  1733. s_free(from);
  1734. return m;
  1735. }
  1736. Message*
  1737. mcmd(Cmd *c, Message *m)
  1738. {
  1739. char **av;
  1740. int i, ai;
  1741. String *path;
  1742. if(m == &top){
  1743. Bprint(&out, "!address\n");
  1744. return nil;
  1745. }
  1746. if(c->an < 2){
  1747. fprint(2, "!usage: M list-of addresses\n");
  1748. return nil;
  1749. }
  1750. ai = 1;
  1751. av = malloc(sizeof(char*)*(c->an + 8));
  1752. av[ai++] = "-t";
  1753. if(m->parent == &top)
  1754. av[ai++] = "message/rfc822";
  1755. else
  1756. av[ai++] = "mime";
  1757. av[ai++] = "-A";
  1758. path = rooted(extendpath(m->path, "raw"));
  1759. av[ai++] = s_to_c(path);
  1760. if(strchr(c->av[0], 'M') == nil)
  1761. av[ai++] = "-n";
  1762. for(i = 1; i < c->an; i++)
  1763. av[ai++] = c->av[i];
  1764. av[ai] = 0;
  1765. if(tomailer(av) < 0)
  1766. m = nil;
  1767. if(path != nil)
  1768. s_free(path);
  1769. free(av);
  1770. return m;
  1771. }
  1772. Message*
  1773. acmd(Cmd *c, Message *m)
  1774. {
  1775. char *av[128];
  1776. int i, ai = 1;
  1777. String *from, *rpath, *path = nil, *subject = nil;
  1778. String *to, *cc;
  1779. if(m == &top){
  1780. Bprint(&out, "!address\n");
  1781. return nil;
  1782. }
  1783. if(*m->subject){
  1784. av[ai++] = "-s";
  1785. subject = addrecolon(m->subject);
  1786. av[ai++] = s_to_c(subject);
  1787. }
  1788. av[ai++] = "-R";
  1789. rpath = rooted(s_clone(m->path));
  1790. av[ai++] = s_to_c(rpath);
  1791. if(strchr(c->av[0], 'A') != nil){
  1792. av[ai++] = "-t";
  1793. av[ai++] = "message/rfc822";
  1794. av[ai++] = "-A";
  1795. path = rooted(extendpath(m->path, "raw"));
  1796. av[ai++] = s_to_c(path);
  1797. }
  1798. for(i = 1; i < c->an && ai < nelem(av)-1; i++)
  1799. av[ai++] = c->av[i];
  1800. from = s_copy(m->from);
  1801. ai += tokenize822(s_to_c(from), &av[ai], nelem(av) - ai);
  1802. to = s_copy(m->to);
  1803. ai += tokenize822(s_to_c(to), &av[ai], nelem(av) - ai);
  1804. cc = s_copy(m->cc);
  1805. ai += tokenize822(s_to_c(cc), &av[ai], nelem(av) - ai);
  1806. av[ai] = 0;
  1807. if(tomailer(av) < 0)
  1808. m = nil;
  1809. s_free(path);
  1810. s_free(rpath);
  1811. s_free(subject);
  1812. s_free(from);
  1813. s_free(to);
  1814. s_free(cc);
  1815. return m;
  1816. }
  1817. String *
  1818. relpath(char *path, String *to)
  1819. {
  1820. if (*path=='/' || strncmp(path, "./", 2) == 0
  1821. || strncmp(path, "../", 3) == 0) {
  1822. to = s_append(to, path);
  1823. } else if(mbpath) {
  1824. to = s_append(to, s_to_c(mbpath));
  1825. to->ptr = strrchr(to->base, '/')+1;
  1826. s_append(to, path);
  1827. }
  1828. return to;
  1829. }
  1830. int
  1831. appendtofile(Message *m, char *part, char *base, int mbox)
  1832. {
  1833. String *file, *h;
  1834. int in, out, rv;
  1835. file = extendpath(m->path, part);
  1836. in = open(s_to_c(file), OREAD);
  1837. if(in < 0){
  1838. fprint(2, "!message disappeared\n");
  1839. return -1;
  1840. }
  1841. s_reset(file);
  1842. relpath(base, file);
  1843. if(sysisdir(s_to_c(file))){
  1844. s_append(file, "/");
  1845. if(m->filename && strchr(m->filename, '/') == nil)
  1846. s_append(file, m->filename);
  1847. else {
  1848. s_append(file, "att.XXXXXXXXXXX");
  1849. mktemp(s_to_c(file));
  1850. }
  1851. }
  1852. if(mbox)
  1853. out = open(s_to_c(file), OWRITE);
  1854. else
  1855. out = open(s_to_c(file), OWRITE|OTRUNC);
  1856. if(out < 0){
  1857. out = create(s_to_c(file), OWRITE, 0666);
  1858. if(out < 0){
  1859. fprint(2, "!can't open %s: %r\n", s_to_c(file));
  1860. close(in);
  1861. s_free(file);
  1862. return -1;
  1863. }
  1864. }
  1865. if(mbox)
  1866. seek(out, 0, 2);
  1867. // put on a 'From ' line
  1868. if(mbox){
  1869. while(m->parent != &top)
  1870. m = m->parent;
  1871. h = file2string(m->path, "unixheader");
  1872. fprint(out, "%s", s_to_c(h));
  1873. s_free(h);
  1874. }
  1875. // copy the message escaping what we have to ad adding newlines if we have to
  1876. if(mbox)
  1877. rv = appendfiletombox(in, out);
  1878. else
  1879. rv = appendfiletofile(in, out);
  1880. close(in);
  1881. close(out);
  1882. if(rv >= 0)
  1883. print("!saved in %s\n", s_to_c(file));
  1884. s_free(file);
  1885. return rv;
  1886. }
  1887. Message*
  1888. scmd(Cmd *c, Message *m)
  1889. {
  1890. char *file;
  1891. if(m == &top){
  1892. Bprint(&out, "!address\n");
  1893. return nil;
  1894. }
  1895. switch(c->an){
  1896. case 1:
  1897. file = "stored";
  1898. break;
  1899. case 2:
  1900. file = c->av[1];
  1901. break;
  1902. default:
  1903. fprint(2, "!usage: s filename\n");
  1904. return nil;
  1905. }
  1906. if(appendtofile(m, "raw", file, 1) < 0)
  1907. return nil;
  1908. m->stored = 1;
  1909. return m;
  1910. }
  1911. Message*
  1912. wcmd(Cmd *c, Message *m)
  1913. {
  1914. char *file;
  1915. if(m == &top){
  1916. Bprint(&out, "!address\n");
  1917. return nil;
  1918. }
  1919. switch(c->an){
  1920. case 2:
  1921. file = c->av[1];
  1922. break;
  1923. case 1:
  1924. if(*m->filename == 0){
  1925. fprint(2, "!usage: w filename\n");
  1926. return nil;
  1927. }
  1928. file = strrchr(m->filename, '/');
  1929. if(file != nil)
  1930. file++;
  1931. else
  1932. file = m->filename;
  1933. break;
  1934. default:
  1935. fprint(2, "!usage: w filename\n");
  1936. return nil;
  1937. }
  1938. if(appendtofile(m, "body", file, 0) < 0)
  1939. return nil;
  1940. m->stored = 1;
  1941. return m;
  1942. }
  1943. char *specialfile[] =
  1944. {
  1945. "pipeto",
  1946. "pipefrom",
  1947. "L.mbox",
  1948. "forward",
  1949. "names"
  1950. };
  1951. // return 1 if this is a special file
  1952. static int
  1953. special(String *s)
  1954. {
  1955. char *p;
  1956. int i;
  1957. p = strrchr(s_to_c(s), '/');
  1958. if(p == nil)
  1959. p = s_to_c(s);
  1960. else
  1961. p++;
  1962. for(i = 0; i < nelem(specialfile); i++)
  1963. if(strcmp(p, specialfile[i]) == 0)
  1964. return 1;
  1965. return 0;
  1966. }
  1967. // open the folder using the recipients account name
  1968. static String*
  1969. foldername(char *rcvr)
  1970. {
  1971. char *p;
  1972. int c;
  1973. String *file;
  1974. Dir *d;
  1975. int scarey;
  1976. file = s_new();
  1977. mboxpath("f", user, file, 0);
  1978. d = dirstat(s_to_c(file));
  1979. // if $mail/f exists, store there, otherwise in $mail
  1980. s_restart(file);
  1981. if(d && d->qid.type == QTDIR){
  1982. scarey = 0;
  1983. s_append(file, "f/");
  1984. } else {
  1985. scarey = 1;
  1986. }
  1987. free(d);
  1988. p = strrchr(rcvr, '!');
  1989. if(p != nil)
  1990. rcvr = p+1;
  1991. while(*rcvr && *rcvr != '@'){
  1992. c = *rcvr++;
  1993. if(c == '/')
  1994. c = '_';
  1995. s_putc(file, c);
  1996. }
  1997. s_terminate(file);
  1998. if(scarey && special(file)){
  1999. fprint(2, "!won't overwrite %s\n", s_to_c(file));
  2000. s_free(file);
  2001. return nil;
  2002. }
  2003. return file;
  2004. }
  2005. Message*
  2006. fcmd(Cmd *c, Message *m)
  2007. {
  2008. String *folder;
  2009. if(c->an > 1){
  2010. fprint(2, "!usage: f takes no arguments\n");
  2011. return nil;
  2012. }
  2013. if(m == &top){
  2014. Bprint(&out, "!address\n");
  2015. return nil;
  2016. }
  2017. folder = foldername(m->from);
  2018. if(folder == nil)
  2019. return nil;
  2020. if(appendtofile(m, "raw", s_to_c(folder), 1) < 0){
  2021. s_free(folder);
  2022. return nil;
  2023. }
  2024. s_free(folder);
  2025. m->stored = 1;
  2026. return m;
  2027. }
  2028. void
  2029. system(char *cmd, char **av, int in)
  2030. {
  2031. int pid;
  2032. switch(pid=fork()){
  2033. case -1:
  2034. return;
  2035. case 0:
  2036. if(in >= 0){
  2037. close(0);
  2038. dup(in, 0);
  2039. close(in);
  2040. }
  2041. if(wd[0] != 0)
  2042. chdir(wd);
  2043. exec(cmd, av);
  2044. fprint(2, "!couldn't exec %s\n", cmd);
  2045. exits(0);
  2046. default:
  2047. if(in >= 0)
  2048. close(in);
  2049. while(waitpid() < 0){
  2050. if(!interrupted)
  2051. break;
  2052. postnote(PNPROC, pid, "die");
  2053. continue;
  2054. }
  2055. break;
  2056. }
  2057. }
  2058. Message*
  2059. bangcmd(Cmd *c, Message *m)
  2060. {
  2061. char cmd[4*1024];
  2062. char *p, *e;
  2063. char *av[4];
  2064. int i;
  2065. cmd[0] = 0;
  2066. p = cmd;
  2067. e = cmd+sizeof(cmd);
  2068. for(i = 1; i < c->an; i++)
  2069. p = seprint(p, e, "%s ", c->av[i]);
  2070. av[0] = "rc";
  2071. av[1] = "-c";
  2072. av[2] = cmd;
  2073. av[3] = 0;
  2074. system("/bin/rc", av, -1);
  2075. Bprint(&out, "!\n");
  2076. return m;
  2077. }
  2078. Message*
  2079. xpipecmd(Cmd *c, Message *m, char *part)
  2080. {
  2081. char cmd[128];
  2082. char *p, *e;
  2083. char *av[4];
  2084. String *path;
  2085. int i, fd;
  2086. if(c->an < 2){
  2087. Bprint(&out, "!usage: | cmd\n");
  2088. return nil;
  2089. }
  2090. if(m == &top){
  2091. Bprint(&out, "!address\n");
  2092. return nil;
  2093. }
  2094. path = extendpath(m->path, part);
  2095. fd = open(s_to_c(path), OREAD);
  2096. s_free(path);
  2097. if(fd < 0){ // compatibility with older upas/fs
  2098. path = extendpath(m->path, "raw");
  2099. fd = open(s_to_c(path), OREAD);
  2100. s_free(path);
  2101. }
  2102. if(fd < 0){
  2103. fprint(2, "!message disappeared\n");
  2104. return nil;
  2105. }
  2106. p = cmd;
  2107. e = cmd+sizeof(cmd);
  2108. cmd[0] = 0;
  2109. for(i = 1; i < c->an; i++)
  2110. p = seprint(p, e, "%s ", c->av[i]);
  2111. av[0] = "rc";
  2112. av[1] = "-c";
  2113. av[2] = cmd;
  2114. av[3] = 0;
  2115. system("/bin/rc", av, fd); /* system closes fd */
  2116. Bprint(&out, "!\n");
  2117. return m;
  2118. }
  2119. Message*
  2120. pipecmd(Cmd *c, Message *m)
  2121. {
  2122. return xpipecmd(c, m, "body");
  2123. }
  2124. Message*
  2125. rpipecmd(Cmd *c, Message *m)
  2126. {
  2127. return xpipecmd(c, m, "rawunix");
  2128. }
  2129. void
  2130. closemb(void)
  2131. {
  2132. int fd;
  2133. fd = open("/mail/fs/ctl", ORDWR);
  2134. if(fd < 0)
  2135. sysfatal("can't open /mail/fs/ctl: %r");
  2136. // close current mailbox
  2137. if(*mbname && strcmp(mbname, "mbox") != 0)
  2138. fprint(fd, "close %s", mbname);
  2139. close(fd);
  2140. }
  2141. int
  2142. switchmb(char *file, char *singleton)
  2143. {
  2144. char *p;
  2145. int n, fd;
  2146. String *path;
  2147. char buf[256];
  2148. // if the user didn't say anything and there
  2149. // is an mbox mounted already, use that one
  2150. // so that the upas/fs -fdefault default is honored.
  2151. if(file
  2152. || (singleton && access(singleton, 0)<0)
  2153. || (!singleton && access("/mail/fs/mbox", 0)<0)){
  2154. if(file == nil)
  2155. file = "mbox";
  2156. // close current mailbox
  2157. closemb();
  2158. didopen = 1;
  2159. fd = open("/mail/fs/ctl", ORDWR);
  2160. if(fd < 0)
  2161. sysfatal("can't open /mail/fs/ctl: %r");
  2162. path = s_new();
  2163. // get an absolute path to the mail box
  2164. if(strncmp(file, "./", 2) == 0){
  2165. // resolve path here since upas/fs doesn't know
  2166. // our working directory
  2167. if(getwd(buf, sizeof(buf)-strlen(file)) == nil){
  2168. fprint(2, "!can't get working directory: %s\n", buf);
  2169. return -1;
  2170. }
  2171. s_append(path, buf);
  2172. s_append(path, file+1);
  2173. } else {
  2174. mboxpath(file, user, path, 0);
  2175. }
  2176. // make up a handle to use when talking to fs
  2177. p = strrchr(file, '/');
  2178. if(p == nil){
  2179. // if its in the mailbox directory, just use the name
  2180. strncpy(mbname, file, sizeof(mbname));
  2181. mbname[sizeof(mbname)-1] = 0;
  2182. } else {
  2183. // make up a mailbox name
  2184. p = strrchr(s_to_c(path), '/');
  2185. p++;
  2186. if(*p == 0){
  2187. fprint(2, "!bad mbox name");
  2188. return -1;
  2189. }
  2190. strncpy(mbname, p, sizeof(mbname));
  2191. mbname[sizeof(mbname)-1] = 0;
  2192. n = strlen(mbname);
  2193. if(n > Elemlen-12)
  2194. n = Elemlen-12;
  2195. sprint(mbname+n, "%ld", time(0));
  2196. }
  2197. if(fprint(fd, "open %s %s", s_to_c(path), mbname) < 0){
  2198. fprint(2, "!can't 'open %s %s': %r\n", file, mbname);
  2199. s_free(path);
  2200. return -1;
  2201. }
  2202. close(fd);
  2203. }else
  2204. if (singleton && access(singleton, 0)==0
  2205. && strncmp(singleton, "/mail/fs/", 9) == 0){
  2206. if ((p = strchr(singleton +10, '/')) == nil){
  2207. fprint(2, "!bad mbox name");
  2208. return -1;
  2209. }
  2210. n = p-(singleton+9);
  2211. strncpy(mbname, singleton+9, n);
  2212. mbname[n+1] = 0;
  2213. path = s_reset(nil);
  2214. mboxpath(mbname, user, path, 0);
  2215. }else{
  2216. path = s_reset(nil);
  2217. mboxpath("mbox", user, path, 0);
  2218. strcpy(mbname, "mbox");
  2219. }
  2220. snprint(root, sizeof root, "/mail/fs/%s", mbname);
  2221. if(getwd(wd, sizeof(wd)) == 0)
  2222. wd[0] = 0;
  2223. if(singleton == nil && chdir(root) >= 0)
  2224. strcpy(root, ".");
  2225. rootlen = strlen(root);
  2226. if(mbpath != nil)
  2227. s_free(mbpath);
  2228. mbpath = path;
  2229. return 0;
  2230. }
  2231. // like tokenize but for into lines
  2232. int
  2233. lineize(char *s, char **f, int n)
  2234. {
  2235. int i;
  2236. for(i = 0; *s && i < n; i++){
  2237. f[i] = s;
  2238. s = strchr(s, '\n');
  2239. if(s == nil)
  2240. break;
  2241. *s++ = 0;
  2242. }
  2243. return i;
  2244. }
  2245. String*
  2246. rooted(String *s)
  2247. {
  2248. static char buf[256];
  2249. if(strcmp(root, ".") != 0)
  2250. return s;
  2251. snprint(buf, sizeof(buf), "/mail/fs/%s/%s", mbname, s_to_c(s));
  2252. s_free(s);
  2253. return s_copy(buf);
  2254. }
  2255. int
  2256. plumb(Message *m, Ctype *cp)
  2257. {
  2258. String *s;
  2259. Plumbmsg *pm;
  2260. static int fd = -2;
  2261. if(cp->plumbdest == nil)
  2262. return -1;
  2263. if(fd < -1)
  2264. fd = plumbopen("send", OWRITE);
  2265. if(fd < 0)
  2266. return -1;
  2267. pm = mallocz(sizeof(Plumbmsg), 1);
  2268. pm->src = strdup("mail");
  2269. if(*cp->plumbdest)
  2270. pm->dst = strdup(cp->plumbdest);
  2271. pm->wdir = nil;
  2272. pm->type = strdup("text");
  2273. pm->ndata = -1;
  2274. s = rooted(extendpath(m->path, "body"));
  2275. if(cp->ext != nil){
  2276. s_append(s, ".");
  2277. s_append(s, cp->ext);
  2278. }
  2279. pm->data = strdup(s_to_c(s));
  2280. s_free(s);
  2281. plumbsend(fd, pm);
  2282. plumbfree(pm);
  2283. return 0;
  2284. }
  2285. void
  2286. regerror(char*)
  2287. {
  2288. }
  2289. String*
  2290. addrecolon(char *s)
  2291. {
  2292. String *str;
  2293. if(cistrncmp(s, "re:", 3) != 0){
  2294. str = s_copy("Re: ");
  2295. s_append(str, s);
  2296. } else
  2297. str = s_copy(s);
  2298. return str;
  2299. }
  2300. void
  2301. exitfs(char *rv)
  2302. {
  2303. if(startedfs)
  2304. unmount(nil, "/mail/fs");
  2305. chdir("/sys/src/cmd/upas/ned"); /* for profiling? */
  2306. exits(rv);
  2307. }