drv_imap.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388
  1. /*
  2. * mbsync - mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2006,2008,2010-2013 Oswald Buddenhagen <ossi@users.sf.net>
  5. * Copyright (C) 2004 Theodore Y. Ts'o <tytso@mit.edu>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * As a special exception, mbsync may be linked with the OpenSSL library,
  21. * despite that library's more restrictive license.
  22. */
  23. #include "driver.h"
  24. #include "socket.h"
  25. #include <assert.h>
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <stddef.h>
  30. #include <limits.h>
  31. #include <string.h>
  32. #include <ctype.h>
  33. #include <time.h>
  34. #include <sys/wait.h>
  35. typedef struct imap_server_conf {
  36. struct imap_server_conf *next;
  37. char *name;
  38. server_conf_t sconf;
  39. char *user;
  40. char *pass;
  41. char *pass_cmd;
  42. int max_in_progress;
  43. #ifdef HAVE_LIBSSL
  44. char require_ssl;
  45. char require_cram;
  46. #endif
  47. } imap_server_conf_t;
  48. typedef struct imap_store_conf {
  49. store_conf_t gen;
  50. imap_server_conf_t *server;
  51. char *delimiter;
  52. char use_namespace;
  53. } imap_store_conf_t;
  54. typedef struct imap_message {
  55. message_t gen;
  56. /* int seq; will be needed when expunges are tracked */
  57. } imap_message_t;
  58. #define NIL (void*)0x1
  59. #define LIST (void*)0x2
  60. typedef struct _list {
  61. struct _list *next, *child;
  62. char *val;
  63. int len;
  64. } list_t;
  65. #define MAX_LIST_DEPTH 5
  66. struct imap_store;
  67. typedef struct parse_list_state {
  68. list_t *head, **stack[MAX_LIST_DEPTH];
  69. int (*callback)( struct imap_store *ctx, list_t *list, char *cmd );
  70. int level, need_bytes;
  71. } parse_list_state_t;
  72. struct imap_cmd;
  73. typedef struct imap_store {
  74. store_t gen;
  75. const char *label; /* foreign */
  76. const char *prefix;
  77. int ref_count;
  78. /* trash folder's existence is not confirmed yet */
  79. enum { TrashUnknown, TrashChecking, TrashKnown } trashnc;
  80. unsigned got_namespace:1;
  81. char *delimiter; /* hierarchy delimiter */
  82. list_t *ns_personal, *ns_other, *ns_shared; /* NAMESPACE info */
  83. message_t **msgapp; /* FETCH results */
  84. unsigned caps; /* CAPABILITY results */
  85. parse_list_state_t parse_list_sts;
  86. /* command queue */
  87. int nexttag, num_in_progress;
  88. struct imap_cmd *pending, **pending_append;
  89. struct imap_cmd *in_progress, **in_progress_append;
  90. /* Used during sequential operations like connect */
  91. enum { GreetingPending = 0, GreetingBad, GreetingOk, GreetingPreauth } greeting;
  92. int canceling; /* imap_cancel() is in progress */
  93. union {
  94. void (*imap_open)( store_t *srv, void *aux );
  95. void (*imap_cancel)( void *aux );
  96. } callbacks;
  97. void *callback_aux;
  98. conn_t conn; /* this is BIG, so put it last */
  99. } imap_store_t;
  100. struct imap_cmd {
  101. struct imap_cmd *next;
  102. char *cmd;
  103. int tag;
  104. struct {
  105. /* Will be called on each continuation request until it resets this pointer.
  106. * Needs to invoke bad_callback and return -1 on error, otherwise return 0. */
  107. int (*cont)( imap_store_t *ctx, struct imap_cmd *cmd, const char *prompt );
  108. void (*done)( imap_store_t *ctx, struct imap_cmd *cmd, int response );
  109. char *data;
  110. int data_len;
  111. int uid; /* to identify fetch responses */
  112. char high_prio; /* if command is queued, put it at the front of the queue. */
  113. char to_trash; /* we are storing to trash, not current. */
  114. char create; /* create the mailbox if we get an error ... */
  115. char trycreate; /* ... but only if this is true or the server says so. */
  116. } param;
  117. };
  118. struct imap_cmd_simple {
  119. struct imap_cmd gen;
  120. void (*callback)( int sts, void *aux );
  121. void *callback_aux;
  122. };
  123. struct imap_cmd_fetch_msg {
  124. struct imap_cmd_simple gen;
  125. msg_data_t *msg_data;
  126. };
  127. struct imap_cmd_out_uid {
  128. struct imap_cmd gen;
  129. void (*callback)( int sts, int uid, void *aux );
  130. void *callback_aux;
  131. int out_uid;
  132. };
  133. struct imap_cmd_refcounted_state {
  134. void (*callback)( int sts, void *aux );
  135. void *callback_aux;
  136. int ref_count;
  137. int ret_val;
  138. };
  139. struct imap_cmd_refcounted {
  140. struct imap_cmd gen;
  141. struct imap_cmd_refcounted_state *state;
  142. };
  143. #define CAP(cap) (ctx->caps & (1 << (cap)))
  144. enum CAPABILITY {
  145. NOLOGIN = 0,
  146. #ifdef HAVE_LIBSSL
  147. CRAM,
  148. STARTTLS,
  149. #endif
  150. UIDPLUS,
  151. LITERALPLUS,
  152. MOVE,
  153. NAMESPACE
  154. };
  155. static const char *cap_list[] = {
  156. "LOGINDISABLED",
  157. #ifdef HAVE_LIBSSL
  158. "AUTH=CRAM-MD5",
  159. "STARTTLS",
  160. #endif
  161. "UIDPLUS",
  162. "LITERAL+",
  163. "MOVE",
  164. "NAMESPACE"
  165. };
  166. #define RESP_OK 0
  167. #define RESP_NO 1
  168. #define RESP_CANCEL 2
  169. static INLINE void imap_ref( imap_store_t *ctx ) { ++ctx->ref_count; }
  170. static int imap_deref( imap_store_t *ctx );
  171. static void imap_invoke_bad_callback( imap_store_t *ctx );
  172. static const char *Flags[] = {
  173. "Draft",
  174. "Flagged",
  175. "Answered",
  176. "Seen",
  177. "Deleted",
  178. };
  179. static struct imap_cmd *
  180. new_imap_cmd( int size )
  181. {
  182. struct imap_cmd *cmd = nfmalloc( size );
  183. memset( &cmd->param, 0, sizeof(cmd->param) );
  184. return cmd;
  185. }
  186. #define INIT_IMAP_CMD(type, cmdp, cb, aux) \
  187. cmdp = (struct type *)new_imap_cmd( sizeof(*cmdp) ); \
  188. cmdp->callback = cb; \
  189. cmdp->callback_aux = aux;
  190. #define INIT_IMAP_CMD_X(type, cmdp, cb, aux) \
  191. cmdp = (struct type *)new_imap_cmd( sizeof(*cmdp) ); \
  192. cmdp->gen.callback = cb; \
  193. cmdp->gen.callback_aux = aux;
  194. static void
  195. done_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd, int response )
  196. {
  197. cmd->param.done( ctx, cmd, response );
  198. free( cmd->param.data );
  199. free( cmd->cmd );
  200. free( cmd );
  201. }
  202. static int
  203. send_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd )
  204. {
  205. int bufl, litplus;
  206. const char *buffmt;
  207. char buf[1024];
  208. cmd->tag = ++ctx->nexttag;
  209. if (!cmd->param.data) {
  210. buffmt = "%d %s\r\n";
  211. litplus = 0;
  212. } else if ((cmd->param.to_trash && ctx->trashnc == TrashUnknown) || !CAP(LITERALPLUS)) {
  213. buffmt = "%d %s{%d}\r\n";
  214. litplus = 0;
  215. } else {
  216. buffmt = "%d %s{%d+}\r\n";
  217. litplus = 1;
  218. }
  219. bufl = nfsnprintf( buf, sizeof(buf), buffmt,
  220. cmd->tag, cmd->cmd, cmd->param.data_len );
  221. if (DFlags & VERBOSE) {
  222. if (ctx->num_in_progress)
  223. printf( "(%d in progress) ", ctx->num_in_progress );
  224. if (memcmp( cmd->cmd, "LOGIN", 5 ))
  225. printf( "%s>>> %s", ctx->label, buf );
  226. else
  227. printf( "%s>>> %d LOGIN <user> <pass>\n", ctx->label, cmd->tag );
  228. fflush( stdout );
  229. }
  230. if (socket_write( &ctx->conn, buf, bufl, KeepOwn ) < 0)
  231. goto bail;
  232. if (litplus) {
  233. char *p = cmd->param.data;
  234. cmd->param.data = 0;
  235. if (socket_write( &ctx->conn, p, cmd->param.data_len, GiveOwn ) < 0 ||
  236. socket_write( &ctx->conn, "\r\n", 2, KeepOwn ) < 0)
  237. goto bail;
  238. }
  239. if (cmd->param.to_trash && ctx->trashnc == TrashUnknown)
  240. ctx->trashnc = TrashChecking;
  241. cmd->next = 0;
  242. *ctx->in_progress_append = cmd;
  243. ctx->in_progress_append = &cmd->next;
  244. ctx->num_in_progress++;
  245. return 0;
  246. bail:
  247. done_imap_cmd( ctx, cmd, RESP_CANCEL );
  248. return -1;
  249. }
  250. static int
  251. cmd_submittable( imap_store_t *ctx, struct imap_cmd *cmd )
  252. {
  253. struct imap_cmd *cmdp;
  254. return !ctx->conn.write_buf &&
  255. !(ctx->in_progress &&
  256. (cmdp = (struct imap_cmd *)((char *)ctx->in_progress_append -
  257. offsetof(struct imap_cmd, next)), 1) &&
  258. (cmdp->param.cont || cmdp->param.data)) &&
  259. !(cmd->param.to_trash && ctx->trashnc == TrashChecking) &&
  260. ctx->num_in_progress < ((imap_store_conf_t *)ctx->gen.conf)->server->max_in_progress;
  261. }
  262. static int
  263. flush_imap_cmds( imap_store_t *ctx )
  264. {
  265. struct imap_cmd *cmd;
  266. while ((cmd = ctx->pending) && cmd_submittable( ctx, cmd )) {
  267. if (!(ctx->pending = cmd->next))
  268. ctx->pending_append = &ctx->pending;
  269. if (send_imap_cmd( ctx, cmd ) < 0)
  270. return -1;
  271. }
  272. return 0;
  273. }
  274. static void
  275. cancel_pending_imap_cmds( imap_store_t *ctx )
  276. {
  277. struct imap_cmd *cmd;
  278. while ((cmd = ctx->pending)) {
  279. if (!(ctx->pending = cmd->next))
  280. ctx->pending_append = &ctx->pending;
  281. done_imap_cmd( ctx, cmd, RESP_CANCEL );
  282. }
  283. }
  284. static void
  285. cancel_submitted_imap_cmds( imap_store_t *ctx )
  286. {
  287. struct imap_cmd *cmd;
  288. while ((cmd = ctx->in_progress)) {
  289. ctx->in_progress = cmd->next;
  290. /* don't update num_in_progress and in_progress_append - store is dead */
  291. done_imap_cmd( ctx, cmd, RESP_CANCEL );
  292. }
  293. }
  294. static int
  295. submit_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd )
  296. {
  297. assert( ctx );
  298. assert( ctx->gen.bad_callback );
  299. assert( cmd );
  300. assert( cmd->param.done );
  301. if ((ctx->pending && !cmd->param.high_prio) || !cmd_submittable( ctx, cmd )) {
  302. if (ctx->pending && cmd->param.high_prio) {
  303. cmd->next = ctx->pending;
  304. ctx->pending = cmd;
  305. } else {
  306. cmd->next = 0;
  307. *ctx->pending_append = cmd;
  308. ctx->pending_append = &cmd->next;
  309. }
  310. return 0;
  311. }
  312. return send_imap_cmd( ctx, cmd );
  313. }
  314. /* Minimal printf() replacement that supports an %\s format sequence to print backslash-escaped
  315. * string literals. Note that this does not automatically add quotes around the printed string,
  316. * so it is possible to concatenate multiple segments. */
  317. static char *
  318. imap_vprintf( const char *fmt, va_list ap )
  319. {
  320. const char *s, *es;
  321. char *d, *ed;
  322. int maxlen;
  323. char c;
  324. char buf[1024]; /* Minimal supported command buffer size per IMAP spec. */
  325. d = buf;
  326. ed = d + sizeof(buf);
  327. s = fmt;
  328. for (;;) {
  329. c = *fmt;
  330. if (!c || c == '%') {
  331. int l = fmt - s;
  332. if (d + l > ed)
  333. oob();
  334. memcpy( d, s, l );
  335. d += l;
  336. if (!c) {
  337. l = d - buf;
  338. ed = nfmalloc( l + 1 );
  339. memcpy( ed, buf, l );
  340. ed[l] = 0;
  341. return ed;
  342. }
  343. maxlen = INT_MAX;
  344. c = *++fmt;
  345. if (c == '\\') {
  346. c = *++fmt;
  347. if (c != 's') {
  348. fputs( "Fatal: unsupported escaped format specifier. Please report a bug.\n", stderr );
  349. abort();
  350. }
  351. s = va_arg( ap, const char * );
  352. while ((c = *s++)) {
  353. if (d + 2 > ed)
  354. oob();
  355. if (c == '\\' || c == '"')
  356. *d++ = '\\';
  357. *d++ = c;
  358. }
  359. } else { /* \\ cannot be combined with anything else. */
  360. if (c == '.') {
  361. c = *++fmt;
  362. if (c != '*') {
  363. fputs( "Fatal: unsupported string length specification. Please report a bug.\n", stderr );
  364. abort();
  365. }
  366. maxlen = va_arg( ap , int );
  367. c = *++fmt;
  368. }
  369. if (c == 'c') {
  370. if (d + 1 > ed)
  371. oob();
  372. *d++ = (char)va_arg( ap , int );
  373. } else if (c == 's') {
  374. s = va_arg( ap, const char * );
  375. es = memchr( s, 0, maxlen );
  376. l = es ? es - s : maxlen;
  377. if (d + l > ed)
  378. oob();
  379. memcpy( d, s, l );
  380. d += l;
  381. } else if (c == 'd') {
  382. d += nfsnprintf( d, ed - d, "%d", va_arg( ap , int ) );
  383. } else {
  384. fputs( "Fatal: unsupported format specifier. Please report a bug.\n", stderr );
  385. abort();
  386. }
  387. }
  388. s = ++fmt;
  389. } else {
  390. fmt++;
  391. }
  392. }
  393. }
  394. static int
  395. imap_exec( imap_store_t *ctx, struct imap_cmd *cmdp,
  396. void (*done)( imap_store_t *ctx, struct imap_cmd *cmd, int response ),
  397. const char *fmt, ... )
  398. {
  399. va_list ap;
  400. if (!cmdp)
  401. cmdp = new_imap_cmd( sizeof(*cmdp) );
  402. cmdp->param.done = done;
  403. va_start( ap, fmt );
  404. cmdp->cmd = imap_vprintf( fmt, ap );
  405. va_end( ap );
  406. return submit_imap_cmd( ctx, cmdp );
  407. }
  408. static void
  409. transform_box_response( int *response )
  410. {
  411. switch (*response) {
  412. case RESP_CANCEL: *response = DRV_CANCELED; break;
  413. case RESP_NO: *response = DRV_BOX_BAD; break;
  414. default: *response = DRV_OK; break;
  415. }
  416. }
  417. static void
  418. imap_done_simple_box( imap_store_t *ctx ATTR_UNUSED,
  419. struct imap_cmd *cmd, int response )
  420. {
  421. struct imap_cmd_simple *cmdp = (struct imap_cmd_simple *)cmd;
  422. transform_box_response( &response );
  423. cmdp->callback( response, cmdp->callback_aux );
  424. }
  425. static void
  426. transform_msg_response( int *response )
  427. {
  428. switch (*response) {
  429. case RESP_CANCEL: *response = DRV_CANCELED; break;
  430. case RESP_NO: *response = DRV_MSG_BAD; break;
  431. default: *response = DRV_OK; break;
  432. }
  433. }
  434. static void
  435. imap_done_simple_msg( imap_store_t *ctx ATTR_UNUSED,
  436. struct imap_cmd *cmd, int response )
  437. {
  438. struct imap_cmd_simple *cmdp = (struct imap_cmd_simple *)cmd;
  439. transform_msg_response( &response );
  440. cmdp->callback( response, cmdp->callback_aux );
  441. }
  442. static struct imap_cmd_refcounted_state *
  443. imap_refcounted_new_state( void (*cb)( int, void * ), void *aux )
  444. {
  445. struct imap_cmd_refcounted_state *sts = nfmalloc( sizeof(*sts) );
  446. sts->callback = cb;
  447. sts->callback_aux = aux;
  448. sts->ref_count = 1; /* so forced sync does not cause an early exit */
  449. sts->ret_val = DRV_OK;
  450. return sts;
  451. }
  452. static struct imap_cmd *
  453. imap_refcounted_new_cmd( struct imap_cmd_refcounted_state *sts )
  454. {
  455. struct imap_cmd_refcounted *cmd = (struct imap_cmd_refcounted *)new_imap_cmd( sizeof(*cmd) );
  456. cmd->state = sts;
  457. sts->ref_count++;
  458. return &cmd->gen;
  459. }
  460. static void
  461. imap_refcounted_done( struct imap_cmd_refcounted_state *sts )
  462. {
  463. if (!--sts->ref_count) {
  464. sts->callback( sts->ret_val, sts->callback_aux );
  465. free( sts );
  466. }
  467. }
  468. static void
  469. imap_refcounted_done_box( imap_store_t *ctx ATTR_UNUSED, struct imap_cmd *cmd, int response )
  470. {
  471. struct imap_cmd_refcounted_state *sts = ((struct imap_cmd_refcounted *)cmd)->state;
  472. switch (response) {
  473. case RESP_CANCEL:
  474. sts->ret_val = DRV_CANCELED;
  475. break;
  476. case RESP_NO:
  477. if (sts->ret_val == DRV_OK) /* Don't override cancelation. */
  478. sts->ret_val = DRV_BOX_BAD;
  479. break;
  480. }
  481. imap_refcounted_done( sts );
  482. }
  483. static const char *
  484. imap_strchr( const char *s, char tc )
  485. {
  486. for (;; s++) {
  487. char c = *s;
  488. if (c == '\\')
  489. c = *++s;
  490. if (!c)
  491. return 0;
  492. if (c == tc)
  493. return s;
  494. }
  495. }
  496. static char *
  497. next_arg( char **ps )
  498. {
  499. char *ret, *s, *d;
  500. char c;
  501. assert( ps );
  502. s = *ps;
  503. if (!s)
  504. return 0;
  505. while (isspace( (unsigned char)*s ))
  506. s++;
  507. if (!*s) {
  508. *ps = 0;
  509. return 0;
  510. }
  511. if (*s == '"') {
  512. s++;
  513. ret = d = s;
  514. while ((c = *s++) != '"') {
  515. if (c == '\\')
  516. c = *s++;
  517. if (!c) {
  518. *ps = 0;
  519. return 0;
  520. }
  521. *d++ = c;
  522. }
  523. *d = 0;
  524. } else {
  525. ret = s;
  526. while ((c = *s)) {
  527. if (isspace( (unsigned char)c )) {
  528. *s++ = 0;
  529. break;
  530. }
  531. s++;
  532. }
  533. }
  534. if (!*s)
  535. s = 0;
  536. *ps = s;
  537. return ret;
  538. }
  539. static int
  540. is_atom( list_t *list )
  541. {
  542. return list && list->val && list->val != NIL && list->val != LIST;
  543. }
  544. static int
  545. is_list( list_t *list )
  546. {
  547. return list && list->val == LIST;
  548. }
  549. static void
  550. free_list( list_t *list )
  551. {
  552. list_t *tmp;
  553. for (; list; list = tmp) {
  554. tmp = list->next;
  555. if (is_list( list ))
  556. free_list( list->child );
  557. else if (is_atom( list ))
  558. free( list->val );
  559. free( list );
  560. }
  561. }
  562. enum {
  563. LIST_OK,
  564. LIST_PARTIAL,
  565. LIST_BAD
  566. };
  567. static int
  568. parse_imap_list( imap_store_t *ctx, char **sp, parse_list_state_t *sts )
  569. {
  570. list_t *cur, **curp;
  571. char *s = *sp, *d, *p;
  572. int bytes;
  573. char c;
  574. assert( sts );
  575. assert( sts->level > 0 );
  576. curp = sts->stack[--sts->level];
  577. bytes = sts->need_bytes;
  578. if (bytes >= 0) {
  579. sts->need_bytes = -1;
  580. if (!bytes)
  581. goto getline;
  582. cur = (list_t *)((char *)curp - offsetof(list_t, next));
  583. s = cur->val + cur->len - bytes;
  584. goto getbytes;
  585. }
  586. if (!s)
  587. return LIST_BAD;
  588. for (;;) {
  589. while (isspace( (unsigned char)*s ))
  590. s++;
  591. if (sts->level && *s == ')') {
  592. s++;
  593. curp = sts->stack[--sts->level];
  594. goto next;
  595. }
  596. *curp = cur = nfmalloc( sizeof(*cur) );
  597. cur->val = 0; /* for clean bail */
  598. curp = &cur->next;
  599. *curp = 0; /* ditto */
  600. if (*s == '(') {
  601. /* sublist */
  602. if (sts->level == MAX_LIST_DEPTH)
  603. goto bail;
  604. s++;
  605. cur->val = LIST;
  606. sts->stack[sts->level++] = curp;
  607. curp = &cur->child;
  608. *curp = 0; /* for clean bail */
  609. goto next2;
  610. } else if (ctx && *s == '{') {
  611. /* literal */
  612. bytes = cur->len = strtol( s + 1, &s, 10 );
  613. if (*s != '}' || *++s)
  614. goto bail;
  615. s = cur->val = nfmalloc( cur->len + 1 );
  616. s[cur->len] = 0;
  617. getbytes:
  618. bytes -= socket_read( &ctx->conn, s, bytes );
  619. if (bytes > 0)
  620. goto postpone;
  621. if (DFlags & XVERBOSE) {
  622. printf( "%s=========\n", ctx->label );
  623. fwrite( cur->val, cur->len, 1, stdout );
  624. printf( "%s=========\n", ctx->label );
  625. fflush( stdout );
  626. }
  627. getline:
  628. if (!(s = socket_read_line( &ctx->conn )))
  629. goto postpone;
  630. if (DFlags & VERBOSE) {
  631. printf( "%s%s\n", ctx->label, s );
  632. fflush( stdout );
  633. }
  634. } else if (*s == '"') {
  635. /* quoted string */
  636. s++;
  637. p = d = s;
  638. while ((c = *s++) != '"') {
  639. if (c == '\\')
  640. c = *s++;
  641. if (!c)
  642. goto bail;
  643. *d++ = c;
  644. }
  645. cur->len = d - p;
  646. cur->val = nfmalloc( cur->len + 1 );
  647. memcpy( cur->val, p, cur->len );
  648. cur->val[cur->len] = 0;
  649. } else {
  650. /* atom */
  651. p = s;
  652. for (; *s && !isspace( (unsigned char)*s ); s++)
  653. if (sts->level && *s == ')')
  654. break;
  655. cur->len = s - p;
  656. if (cur->len == 3 && !memcmp ("NIL", p, 3))
  657. cur->val = NIL;
  658. else {
  659. cur->val = nfmalloc( cur->len + 1 );
  660. memcpy( cur->val, p, cur->len );
  661. cur->val[cur->len] = 0;
  662. }
  663. }
  664. next:
  665. if (!sts->level)
  666. break;
  667. next2:
  668. if (!*s)
  669. goto bail;
  670. }
  671. *sp = s;
  672. return LIST_OK;
  673. postpone:
  674. if (sts->level < MAX_LIST_DEPTH) {
  675. sts->stack[sts->level++] = curp;
  676. sts->need_bytes = bytes;
  677. return LIST_PARTIAL;
  678. }
  679. bail:
  680. free_list( sts->head );
  681. return LIST_BAD;
  682. }
  683. static void
  684. parse_list_init( parse_list_state_t *sts )
  685. {
  686. sts->need_bytes = -1;
  687. sts->level = 1;
  688. sts->head = 0;
  689. sts->stack[0] = &sts->head;
  690. }
  691. static int
  692. parse_list_continue( imap_store_t *ctx, char *s )
  693. {
  694. list_t *list;
  695. int resp;
  696. if ((resp = parse_imap_list( ctx, &s, &ctx->parse_list_sts )) != LIST_PARTIAL) {
  697. list = (resp == LIST_BAD) ? 0 : ctx->parse_list_sts.head;
  698. ctx->parse_list_sts.head = 0;
  699. resp = ctx->parse_list_sts.callback( ctx, list, s );
  700. }
  701. return resp;
  702. }
  703. static int
  704. parse_list( imap_store_t *ctx, char *s, int (*cb)( imap_store_t *ctx, list_t *list, char *s ) )
  705. {
  706. parse_list_init( &ctx->parse_list_sts );
  707. ctx->parse_list_sts.callback = cb;
  708. return parse_list_continue( ctx, s );
  709. }
  710. static int parse_namespace_rsp_p2( imap_store_t *, list_t *, char * );
  711. static int parse_namespace_rsp_p3( imap_store_t *, list_t *, char * );
  712. static int
  713. parse_namespace_rsp_fail( void )
  714. {
  715. error( "IMAP error: malformed NAMESPACE response\n" );
  716. return LIST_BAD;
  717. }
  718. static int
  719. parse_namespace_rsp( imap_store_t *ctx, list_t *list, char *s )
  720. {
  721. if (!(ctx->ns_personal = list))
  722. return parse_namespace_rsp_fail();
  723. return parse_list( ctx, s, parse_namespace_rsp_p2 );
  724. }
  725. static int
  726. parse_namespace_rsp_p2( imap_store_t *ctx, list_t *list, char *s )
  727. {
  728. if (!(ctx->ns_other = list))
  729. return parse_namespace_rsp_fail();
  730. return parse_list( ctx, s, parse_namespace_rsp_p3 );
  731. }
  732. static int
  733. parse_namespace_rsp_p3( imap_store_t *ctx, list_t *list, char *s ATTR_UNUSED )
  734. {
  735. if (!(ctx->ns_shared = list))
  736. return parse_namespace_rsp_fail();
  737. return LIST_OK;
  738. }
  739. static time_t
  740. parse_date( const char *str )
  741. {
  742. char *end;
  743. time_t date;
  744. int hours, mins;
  745. struct tm datetime;
  746. memset( &datetime, 0, sizeof(datetime) );
  747. if (!(end = strptime( str, "%d-%b-%Y %H:%M:%S ", &datetime )))
  748. return -1;
  749. if ((date = timegm( &datetime )) == -1)
  750. return -1;
  751. if (sscanf( end, "%3d%2d", &hours, &mins ) != 2)
  752. return -1;
  753. return date - (hours * 60 + mins) * 60;
  754. }
  755. static int
  756. parse_fetch_rsp( imap_store_t *ctx, list_t *list, char *s ATTR_UNUSED )
  757. {
  758. list_t *tmp, *flags;
  759. char *body = 0, *tuid = 0;
  760. imap_message_t *cur;
  761. msg_data_t *msgdata;
  762. struct imap_cmd *cmdp;
  763. int uid = 0, mask = 0, status = 0, size = 0;
  764. unsigned i;
  765. time_t date = 0;
  766. if (!is_list( list )) {
  767. error( "IMAP error: bogus FETCH response\n" );
  768. free_list( list );
  769. return LIST_BAD;
  770. }
  771. for (tmp = list->child; tmp; tmp = tmp->next) {
  772. if (is_atom( tmp )) {
  773. if (!strcmp( "UID", tmp->val )) {
  774. tmp = tmp->next;
  775. if (is_atom( tmp ))
  776. uid = atoi( tmp->val );
  777. else
  778. error( "IMAP error: unable to parse UID\n" );
  779. } else if (!strcmp( "FLAGS", tmp->val )) {
  780. tmp = tmp->next;
  781. if (is_list( tmp )) {
  782. for (flags = tmp->child; flags; flags = flags->next) {
  783. if (is_atom( flags )) {
  784. if (flags->val[0] == '\\') { /* ignore user-defined flags for now */
  785. if (!strcmp( "Recent", flags->val + 1)) {
  786. status |= M_RECENT;
  787. goto flagok;
  788. }
  789. for (i = 0; i < as(Flags); i++)
  790. if (!strcmp( Flags[i], flags->val + 1 )) {
  791. mask |= 1 << i;
  792. goto flagok;
  793. }
  794. if (flags->val[1] == 'X' && flags->val[2] == '-')
  795. goto flagok; /* ignore system flag extensions */
  796. error( "IMAP warning: unknown system flag %s\n", flags->val );
  797. }
  798. flagok: ;
  799. } else
  800. error( "IMAP error: unable to parse FLAGS list\n" );
  801. }
  802. status |= M_FLAGS;
  803. } else
  804. error( "IMAP error: unable to parse FLAGS\n" );
  805. } else if (!strcmp( "INTERNALDATE", tmp->val )) {
  806. tmp = tmp->next;
  807. if (is_atom( tmp )) {
  808. if ((date = parse_date( tmp->val )) == -1)
  809. error( "IMAP error: unable to parse INTERNALDATE format\n" );
  810. } else
  811. error( "IMAP error: unable to parse INTERNALDATE\n" );
  812. } else if (!strcmp( "RFC822.SIZE", tmp->val )) {
  813. tmp = tmp->next;
  814. if (is_atom( tmp ))
  815. size = atoi( tmp->val );
  816. else
  817. error( "IMAP error: unable to parse RFC822.SIZE\n" );
  818. } else if (!strcmp( "BODY[]", tmp->val )) {
  819. tmp = tmp->next;
  820. if (is_atom( tmp )) {
  821. body = tmp->val;
  822. tmp->val = 0; /* don't free together with list */
  823. size = tmp->len;
  824. } else
  825. error( "IMAP error: unable to parse BODY[]\n" );
  826. } else if (!strcmp( "BODY[HEADER.FIELDS", tmp->val )) {
  827. tmp = tmp->next;
  828. if (is_list( tmp )) {
  829. tmp = tmp->next;
  830. if (!is_atom( tmp ) || strcmp( tmp->val, "]" ))
  831. goto bfail;
  832. tmp = tmp->next;
  833. if (!is_atom( tmp ))
  834. goto bfail;
  835. if (!memcmp( tmp->val, "X-TUID: ", 8 ))
  836. tuid = tmp->val + 8;
  837. } else {
  838. bfail:
  839. error( "IMAP error: unable to parse BODY[HEADER.FIELDS ...]\n" );
  840. }
  841. }
  842. }
  843. }
  844. if (body) {
  845. for (cmdp = ctx->in_progress; cmdp; cmdp = cmdp->next)
  846. if (cmdp->param.uid == uid)
  847. goto gotuid;
  848. error( "IMAP error: unexpected FETCH response (UID %d)\n", uid );
  849. free_list( list );
  850. return LIST_BAD;
  851. gotuid:
  852. msgdata = ((struct imap_cmd_fetch_msg *)cmdp)->msg_data;
  853. msgdata->data = body;
  854. msgdata->len = size;
  855. msgdata->date = date;
  856. if (status & M_FLAGS)
  857. msgdata->flags = mask;
  858. } else if (uid) { /* ignore async flag updates for now */
  859. /* XXX this will need sorting for out-of-order (multiple queries) */
  860. cur = nfcalloc( sizeof(*cur) );
  861. *ctx->msgapp = &cur->gen;
  862. ctx->msgapp = &cur->gen.next;
  863. cur->gen.next = 0;
  864. cur->gen.uid = uid;
  865. cur->gen.flags = mask;
  866. cur->gen.status = status;
  867. cur->gen.size = size;
  868. cur->gen.srec = 0;
  869. if (tuid)
  870. strncpy( cur->gen.tuid, tuid, TUIDL );
  871. else
  872. cur->gen.tuid[0] = 0;
  873. if (ctx->gen.uidnext <= uid) /* in case the server sends no UIDNEXT */
  874. ctx->gen.uidnext = uid + 1;
  875. }
  876. free_list( list );
  877. return LIST_OK;
  878. }
  879. static void
  880. parse_capability( imap_store_t *ctx, char *cmd )
  881. {
  882. char *arg;
  883. unsigned i;
  884. ctx->caps = 0x80000000;
  885. while ((arg = next_arg( &cmd )))
  886. for (i = 0; i < as(cap_list); i++)
  887. if (!strcmp( cap_list[i], arg ))
  888. ctx->caps |= 1 << i;
  889. }
  890. static int
  891. parse_response_code( imap_store_t *ctx, struct imap_cmd *cmd, char *s )
  892. {
  893. char *arg, *earg, *p;
  894. if (!s || *s != '[')
  895. return RESP_OK; /* no response code */
  896. s++;
  897. if (!(p = strchr( s, ']' ))) {
  898. bad_resp:
  899. error( "IMAP error: malformed response code\n" );
  900. return RESP_CANCEL;
  901. }
  902. *p++ = 0;
  903. if (!(arg = next_arg( &s )))
  904. goto bad_resp;
  905. if (!strcmp( "UIDVALIDITY", arg )) {
  906. if (!(arg = next_arg( &s )) ||
  907. (ctx->gen.uidvalidity = strtoll( arg, &earg, 10 ), *earg))
  908. {
  909. error( "IMAP error: malformed UIDVALIDITY status\n" );
  910. return RESP_CANCEL;
  911. }
  912. } else if (!strcmp( "UIDNEXT", arg )) {
  913. if (!(arg = next_arg( &s )) || !(ctx->gen.uidnext = atoi( arg ))) {
  914. error( "IMAP error: malformed NEXTUID status\n" );
  915. return RESP_CANCEL;
  916. }
  917. } else if (!strcmp( "CAPABILITY", arg )) {
  918. parse_capability( ctx, s );
  919. } else if (!strcmp( "ALERT", arg )) {
  920. /* RFC2060 says that these messages MUST be displayed
  921. * to the user
  922. */
  923. for (; isspace( (unsigned char)*p ); p++);
  924. error( "*** IMAP ALERT *** %s\n", p );
  925. } else if (cmd && !strcmp( "APPENDUID", arg )) {
  926. if (!(arg = next_arg( &s )) ||
  927. (ctx->gen.uidvalidity = strtoll( arg, &earg, 10 ), *earg) ||
  928. !(arg = next_arg( &s )) ||
  929. !(((struct imap_cmd_out_uid *)cmd)->out_uid = atoi( arg )))
  930. {
  931. error( "IMAP error: malformed APPENDUID status\n" );
  932. return RESP_CANCEL;
  933. }
  934. }
  935. return RESP_OK;
  936. }
  937. static int parse_list_rsp_p2( imap_store_t *, list_t *, char * );
  938. static int
  939. parse_list_rsp( imap_store_t *ctx, list_t *list, char *cmd )
  940. {
  941. char *arg;
  942. list_t *lp;
  943. if (!is_list( list )) {
  944. free_list( list );
  945. bad_list:
  946. error( "IMAP error: malformed LIST response\n" );
  947. return LIST_BAD;
  948. }
  949. for (lp = list->child; lp; lp = lp->next)
  950. if (is_atom( lp ) && !strcasecmp( lp->val, "\\NoSelect" )) {
  951. free_list( list );
  952. return LIST_OK;
  953. }
  954. free_list( list );
  955. if (!(arg = next_arg( &cmd )))
  956. goto bad_list;
  957. if (!ctx->delimiter)
  958. ctx->delimiter = nfstrdup( arg );
  959. return parse_list( ctx, cmd, parse_list_rsp_p2 );
  960. }
  961. static int
  962. is_inbox( imap_store_t *ctx, const char *arg )
  963. {
  964. int i;
  965. char c;
  966. if (memcmp( arg, "INBOX", 5 ))
  967. return 0;
  968. if (arg[5])
  969. for (i = 0; (c = ctx->delimiter[i]); i++)
  970. if (arg[i + 5] != c)
  971. return 0;
  972. return 1;
  973. }
  974. static int
  975. parse_list_rsp_p2( imap_store_t *ctx, list_t *list, char *cmd ATTR_UNUSED )
  976. {
  977. string_list_t *narg;
  978. char *arg;
  979. int l;
  980. if (!is_atom( list )) {
  981. error( "IMAP error: malformed LIST response\n" );
  982. free_list( list );
  983. return LIST_BAD;
  984. }
  985. arg = list->val;
  986. if (!is_inbox( ctx, arg )) {
  987. l = strlen( ctx->gen.conf->path );
  988. if (memcmp( arg, ctx->gen.conf->path, l ))
  989. goto skip;
  990. arg += l;
  991. if (is_inbox( ctx, arg )) {
  992. if (!arg[5])
  993. warn( "IMAP warning: ignoring INBOX in %s\n", ctx->gen.conf->path );
  994. goto skip;
  995. }
  996. }
  997. if ((l = strlen( arg )) >= 5 && !memcmp( arg + l - 5, ".lock", 5 )) /* workaround broken servers */
  998. goto skip;
  999. if (map_name( arg, (char **)&narg, offsetof(string_list_t, string), ctx->delimiter, "/") < 0) {
  1000. warn( "IMAP warning: ignoring mailbox %s (reserved character '/' in name)\n", arg );
  1001. goto skip;
  1002. }
  1003. narg->next = ctx->gen.boxes;
  1004. ctx->gen.boxes = narg;
  1005. skip:
  1006. free_list( list );
  1007. return LIST_OK;
  1008. }
  1009. static int
  1010. prepare_name( char **buf, const imap_store_t *ctx, const char *prefix, const char *name )
  1011. {
  1012. int pl = strlen( prefix );
  1013. switch (map_name( name, buf, pl, "/", ctx->delimiter )) {
  1014. case -1:
  1015. error( "IMAP error: mailbox name %s contains server's hierarchy delimiter\n", name );
  1016. return -1;
  1017. case -2:
  1018. error( "IMAP error: server's hierarchy delimiter not known\n" );
  1019. return -1;
  1020. default:
  1021. memcpy( *buf, prefix, pl );
  1022. return 0;
  1023. }
  1024. }
  1025. static int
  1026. prepare_box( char **buf, const imap_store_t *ctx )
  1027. {
  1028. const char *name = ctx->gen.name;
  1029. return prepare_name( buf, ctx,
  1030. (!memcmp( name, "INBOX", 5 ) && (!name[5] || name[5] == '/')) ? "" : ctx->prefix, name );
  1031. }
  1032. static int
  1033. prepare_trash( char **buf, const imap_store_t *ctx )
  1034. {
  1035. return prepare_name( buf, ctx, ctx->prefix, ctx->gen.conf->trash );
  1036. }
  1037. struct imap_cmd_trycreate {
  1038. struct imap_cmd gen;
  1039. struct imap_cmd *orig_cmd;
  1040. };
  1041. static void imap_open_store_greeted( imap_store_t * );
  1042. static void get_cmd_result_p2( imap_store_t *, struct imap_cmd *, int );
  1043. static void
  1044. imap_socket_read( void *aux )
  1045. {
  1046. imap_store_t *ctx = (imap_store_t *)aux;
  1047. struct imap_cmd *cmdp, **pcmdp;
  1048. char *cmd, *arg, *arg1, *p;
  1049. int resp, resp2, tag, greeted;
  1050. greeted = ctx->greeting;
  1051. for (;;) {
  1052. if (ctx->parse_list_sts.level) {
  1053. resp = parse_list_continue( ctx, 0 );
  1054. listret:
  1055. if (resp == LIST_PARTIAL)
  1056. return;
  1057. if (resp == LIST_BAD)
  1058. break;
  1059. continue;
  1060. }
  1061. if (!(cmd = socket_read_line( &ctx->conn )))
  1062. return;
  1063. if (DFlags & VERBOSE) {
  1064. printf( "%s%s\n", ctx->label, cmd );
  1065. fflush( stdout );
  1066. }
  1067. arg = next_arg( &cmd );
  1068. if (!arg) {
  1069. error( "IMAP error: empty response\n" );
  1070. break;
  1071. }
  1072. if (*arg == '*') {
  1073. arg = next_arg( &cmd );
  1074. if (!arg) {
  1075. error( "IMAP error: malformed untagged response\n" );
  1076. break;
  1077. }
  1078. if (!strcmp( "NAMESPACE", arg )) {
  1079. resp = parse_list( ctx, cmd, parse_namespace_rsp );
  1080. goto listret;
  1081. } else if (ctx->greeting == GreetingPending && !strcmp( "PREAUTH", arg )) {
  1082. ctx->greeting = GreetingPreauth;
  1083. parse_response_code( ctx, 0, cmd );
  1084. } else if (!strcmp( "OK", arg )) {
  1085. ctx->greeting = GreetingOk;
  1086. parse_response_code( ctx, 0, cmd );
  1087. } else if (!strcmp( "BAD", arg ) || !strcmp( "NO", arg ) || !strcmp( "BYE", arg )) {
  1088. ctx->greeting = GreetingBad;
  1089. parse_response_code( ctx, 0, cmd );
  1090. } else if (!strcmp( "CAPABILITY", arg )) {
  1091. parse_capability( ctx, cmd );
  1092. } else if (!strcmp( "LIST", arg )) {
  1093. resp = parse_list( ctx, cmd, parse_list_rsp );
  1094. goto listret;
  1095. } else if ((arg1 = next_arg( &cmd ))) {
  1096. if (!strcmp( "EXISTS", arg1 ))
  1097. ctx->gen.count = atoi( arg );
  1098. else if (!strcmp( "RECENT", arg1 ))
  1099. ctx->gen.recent = atoi( arg );
  1100. else if(!strcmp ( "FETCH", arg1 )) {
  1101. resp = parse_list( ctx, cmd, parse_fetch_rsp );
  1102. goto listret;
  1103. }
  1104. } else {
  1105. error( "IMAP error: unrecognized untagged response '%s'\n", arg );
  1106. break; /* this may mean anything, so prefer not to spam the log */
  1107. }
  1108. if (greeted == GreetingPending) {
  1109. imap_ref( ctx );
  1110. imap_open_store_greeted( ctx );
  1111. if (imap_deref( ctx ))
  1112. return;
  1113. }
  1114. continue;
  1115. } else if (!ctx->in_progress) {
  1116. error( "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : "" );
  1117. break; /* this may mean anything, so prefer not to spam the log */
  1118. } else if (*arg == '+') {
  1119. /* This can happen only with the last command underway, as
  1120. it enforces a round-trip. */
  1121. cmdp = ctx->in_progress;
  1122. if (cmdp->param.data) {
  1123. if (cmdp->param.to_trash)
  1124. ctx->trashnc = TrashKnown; /* Can't get NO [TRYCREATE] any more. */
  1125. p = cmdp->param.data;
  1126. cmdp->param.data = 0;
  1127. if (socket_write( &ctx->conn, p, cmdp->param.data_len, GiveOwn ) < 0)
  1128. return;
  1129. } else if (cmdp->param.cont) {
  1130. if (cmdp->param.cont( ctx, cmdp, cmd ))
  1131. return;
  1132. } else {
  1133. error( "IMAP error: unexpected command continuation request\n" );
  1134. break;
  1135. }
  1136. if (socket_write( &ctx->conn, "\r\n", 2, KeepOwn ) < 0)
  1137. return;
  1138. } else {
  1139. tag = atoi( arg );
  1140. for (pcmdp = &ctx->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
  1141. if (cmdp->tag == tag)
  1142. goto gottag;
  1143. error( "IMAP error: unexpected tag %s\n", arg );
  1144. break;
  1145. gottag:
  1146. if (!(*pcmdp = cmdp->next))
  1147. ctx->in_progress_append = pcmdp;
  1148. ctx->num_in_progress--;
  1149. arg = next_arg( &cmd );
  1150. if (!arg) {
  1151. error( "IMAP error: malformed tagged response\n" );
  1152. break;
  1153. }
  1154. if (!strcmp( "OK", arg )) {
  1155. if (cmdp->param.to_trash)
  1156. ctx->trashnc = TrashKnown; /* Can't get NO [TRYCREATE] any more. */
  1157. resp = RESP_OK;
  1158. } else {
  1159. if (!strcmp( "NO", arg )) {
  1160. if (cmdp->param.create &&
  1161. (cmdp->param.trycreate ||
  1162. (cmd && !memcmp( cmd, "[TRYCREATE]", 11 ))))
  1163. { /* SELECT, APPEND or UID COPY */
  1164. struct imap_cmd_trycreate *cmd2 =
  1165. (struct imap_cmd_trycreate *)new_imap_cmd( sizeof(*cmd2) );
  1166. cmd2->orig_cmd = cmdp;
  1167. cmd2->gen.param.high_prio = 1;
  1168. p = strchr( cmdp->cmd, '"' );
  1169. if (imap_exec( ctx, &cmd2->gen, get_cmd_result_p2,
  1170. "CREATE %.*s", imap_strchr( p + 1, '"' ) - p + 1, p ) < 0)
  1171. return;
  1172. continue;
  1173. }
  1174. resp = RESP_NO;
  1175. } else /*if (!strcmp( "BAD", arg ))*/
  1176. resp = RESP_CANCEL;
  1177. error( "IMAP command '%s' returned an error: %s %s\n",
  1178. memcmp( cmdp->cmd, "LOGIN", 5 ) ? cmdp->cmd : "LOGIN <user> <pass>",
  1179. arg, cmd ? cmd : "" );
  1180. }
  1181. if ((resp2 = parse_response_code( ctx, cmdp, cmd )) > resp)
  1182. resp = resp2;
  1183. imap_ref( ctx );
  1184. if (resp == RESP_CANCEL)
  1185. imap_invoke_bad_callback( ctx );
  1186. done_imap_cmd( ctx, cmdp, resp );
  1187. if (imap_deref( ctx ))
  1188. return;
  1189. if (ctx->canceling && !ctx->in_progress) {
  1190. ctx->canceling = 0;
  1191. ctx->callbacks.imap_cancel( ctx->callback_aux );
  1192. return;
  1193. }
  1194. }
  1195. if (flush_imap_cmds( ctx ) < 0)
  1196. return;
  1197. }
  1198. imap_invoke_bad_callback( ctx );
  1199. }
  1200. static void
  1201. get_cmd_result_p2( imap_store_t *ctx, struct imap_cmd *cmd, int response )
  1202. {
  1203. struct imap_cmd_trycreate *cmdp = (struct imap_cmd_trycreate *)cmd;
  1204. struct imap_cmd *ocmd = cmdp->orig_cmd;
  1205. if (response != RESP_OK) {
  1206. done_imap_cmd( ctx, ocmd, response );
  1207. } else {
  1208. ctx->gen.uidnext = 1;
  1209. if (ocmd->param.to_trash)
  1210. ctx->trashnc = TrashKnown;
  1211. ocmd->param.create = 0;
  1212. ocmd->param.high_prio = 1;
  1213. submit_imap_cmd( ctx, ocmd );
  1214. }
  1215. }
  1216. /******************* imap_cancel_store *******************/
  1217. static void
  1218. imap_cancel_store( store_t *gctx )
  1219. {
  1220. imap_store_t *ctx = (imap_store_t *)gctx;
  1221. socket_close( &ctx->conn );
  1222. cancel_submitted_imap_cmds( ctx );
  1223. cancel_pending_imap_cmds( ctx );
  1224. free_generic_messages( ctx->gen.msgs );
  1225. free_string_list( ctx->gen.boxes );
  1226. free_list( ctx->ns_personal );
  1227. free_list( ctx->ns_other );
  1228. free_list( ctx->ns_shared );
  1229. free( ctx->delimiter );
  1230. imap_deref( ctx );
  1231. }
  1232. static int
  1233. imap_deref( imap_store_t *ctx )
  1234. {
  1235. if (!--ctx->ref_count) {
  1236. free( ctx );
  1237. return -1;
  1238. }
  1239. return 0;
  1240. }
  1241. static void
  1242. imap_invoke_bad_callback( imap_store_t *ctx )
  1243. {
  1244. ctx->gen.bad_callback( ctx->gen.bad_callback_aux );
  1245. }
  1246. /******************* imap_disown_store *******************/
  1247. static store_t *unowned;
  1248. static void
  1249. imap_cancel_unowned( void *gctx )
  1250. {
  1251. store_t *store, **storep;
  1252. for (storep = &unowned; (store = *storep); storep = &store->next)
  1253. if (store == gctx) {
  1254. *storep = store->next;
  1255. break;
  1256. }
  1257. imap_cancel_store( gctx );
  1258. }
  1259. static void
  1260. imap_disown_store( store_t *gctx )
  1261. {
  1262. free_generic_messages( gctx->msgs );
  1263. gctx->msgs = 0;
  1264. set_bad_callback( gctx, imap_cancel_unowned, gctx );
  1265. gctx->next = unowned;
  1266. unowned = gctx;
  1267. }
  1268. /******************* imap_cleanup *******************/
  1269. static void imap_cleanup_p2( imap_store_t *, struct imap_cmd *, int );
  1270. static void
  1271. imap_cleanup( void )
  1272. {
  1273. store_t *ctx, *nctx;
  1274. for (ctx = unowned; ctx; ctx = nctx) {
  1275. nctx = ctx->next;
  1276. set_bad_callback( ctx, (void (*)(void *))imap_cancel_store, ctx );
  1277. imap_exec( (imap_store_t *)ctx, 0, imap_cleanup_p2, "LOGOUT" );
  1278. }
  1279. }
  1280. static void
  1281. imap_cleanup_p2( imap_store_t *ctx,
  1282. struct imap_cmd *cmd ATTR_UNUSED, int response )
  1283. {
  1284. if (response != RESP_CANCEL)
  1285. imap_cancel_store( &ctx->gen );
  1286. }
  1287. /******************* imap_open_store *******************/
  1288. #ifdef HAVE_LIBSSL
  1289. static int
  1290. do_cram_auth( imap_store_t *ctx, struct imap_cmd *cmdp, const char *prompt )
  1291. {
  1292. imap_server_conf_t *srvc = ((imap_store_conf_t *)ctx->gen.conf)->server;
  1293. char *resp;
  1294. int l;
  1295. cmdp->param.cont = 0;
  1296. cram( prompt, srvc->user, srvc->pass, &resp, &l );
  1297. if (DFlags & VERBOSE) {
  1298. printf( "%s>+> %s\n", ctx->label, resp );
  1299. fflush( stdout );
  1300. }
  1301. if (socket_write( &ctx->conn, resp, l, GiveOwn ) < 0)
  1302. return -1;
  1303. return socket_write( &ctx->conn, "\r\n", 2, KeepOwn );
  1304. }
  1305. #endif
  1306. static void imap_open_store_connected( int, void * );
  1307. #ifdef HAVE_LIBSSL
  1308. static void imap_open_store_tlsstarted1( int, void * );
  1309. #endif
  1310. static void imap_open_store_p2( imap_store_t *, struct imap_cmd *, int );
  1311. static void imap_open_store_authenticate( imap_store_t * );
  1312. #ifdef HAVE_LIBSSL
  1313. static void imap_open_store_authenticate_p2( imap_store_t *, struct imap_cmd *, int );
  1314. static void imap_open_store_tlsstarted2( int, void * );
  1315. static void imap_open_store_authenticate_p3( imap_store_t *, struct imap_cmd *, int );
  1316. #endif
  1317. static void imap_open_store_authenticate2( imap_store_t * );
  1318. static void imap_open_store_authenticate2_p2( imap_store_t *, struct imap_cmd *, int );
  1319. static void imap_open_store_namespace( imap_store_t * );
  1320. static void imap_open_store_namespace_p2( imap_store_t *, struct imap_cmd *, int );
  1321. static void imap_open_store_namespace2( imap_store_t * );
  1322. static void imap_open_store_finalize( imap_store_t * );
  1323. #ifdef HAVE_LIBSSL
  1324. static void imap_open_store_ssl_bail( imap_store_t * );
  1325. #endif
  1326. static void imap_open_store_bail( imap_store_t * );
  1327. static void
  1328. imap_open_store( store_conf_t *conf, const char *label,
  1329. void (*cb)( store_t *srv, void *aux ), void *aux )
  1330. {
  1331. imap_store_conf_t *cfg = (imap_store_conf_t *)conf;
  1332. imap_server_conf_t *srvc = cfg->server;
  1333. imap_store_t *ctx;
  1334. store_t **ctxp;
  1335. for (ctxp = &unowned; (ctx = (imap_store_t *)*ctxp); ctxp = &ctx->gen.next)
  1336. if (ctx->gen.conf == conf) {
  1337. *ctxp = ctx->gen.next;
  1338. ctx->label = label;
  1339. cb( &ctx->gen, aux );
  1340. return;
  1341. }
  1342. for (ctxp = &unowned; (ctx = (imap_store_t *)*ctxp); ctxp = &ctx->gen.next)
  1343. if (((imap_store_conf_t *)ctx->gen.conf)->server == srvc) {
  1344. *ctxp = ctx->gen.next;
  1345. ctx->label = label;
  1346. /* One could ping the server here, but given that the idle timeout
  1347. * is at least 30 minutes, this sounds pretty pointless. */
  1348. free_string_list( ctx->gen.boxes );
  1349. ctx->gen.boxes = 0;
  1350. ctx->gen.listed = 0;
  1351. ctx->gen.conf = conf;
  1352. free( ctx->delimiter );
  1353. ctx->delimiter = 0;
  1354. ctx->callbacks.imap_open = cb;
  1355. ctx->callback_aux = aux;
  1356. set_bad_callback( &ctx->gen, (void (*)(void *))imap_open_store_bail, ctx );
  1357. imap_open_store_namespace( ctx );
  1358. return;
  1359. }
  1360. ctx = nfcalloc( sizeof(*ctx) );
  1361. ctx->gen.conf = conf;
  1362. ctx->label = label;
  1363. ctx->ref_count = 1;
  1364. ctx->callbacks.imap_open = cb;
  1365. ctx->callback_aux = aux;
  1366. set_bad_callback( &ctx->gen, (void (*)(void *))imap_open_store_bail, ctx );
  1367. ctx->in_progress_append = &ctx->in_progress;
  1368. ctx->pending_append = &ctx->pending;
  1369. socket_init( &ctx->conn, &srvc->sconf,
  1370. (void (*)( void * ))imap_invoke_bad_callback,
  1371. imap_socket_read, (int (*)(void *))flush_imap_cmds, ctx );
  1372. socket_connect( &ctx->conn, imap_open_store_connected );
  1373. }
  1374. static void
  1375. imap_open_store_connected( int ok, void *aux )
  1376. {
  1377. imap_store_t *ctx = (imap_store_t *)aux;
  1378. #ifdef HAVE_LIBSSL
  1379. imap_store_conf_t *cfg = (imap_store_conf_t *)ctx->gen.conf;
  1380. imap_server_conf_t *srvc = cfg->server;
  1381. #endif
  1382. if (!ok)
  1383. imap_open_store_bail( ctx );
  1384. #ifdef HAVE_LIBSSL
  1385. else if (srvc->sconf.use_imaps)
  1386. socket_start_tls( &ctx->conn, imap_open_store_tlsstarted1 );
  1387. #endif
  1388. }
  1389. #ifdef HAVE_LIBSSL
  1390. static void
  1391. imap_open_store_tlsstarted1( int ok, void *aux )
  1392. {
  1393. imap_store_t *ctx = (imap_store_t *)aux;
  1394. if (!ok)
  1395. imap_open_store_ssl_bail( ctx );
  1396. }
  1397. #endif
  1398. static void
  1399. imap_open_store_greeted( imap_store_t *ctx )
  1400. {
  1401. if (ctx->greeting == GreetingBad) {
  1402. error( "IMAP error: unknown greeting response\n" );
  1403. imap_open_store_bail( ctx );
  1404. return;
  1405. }
  1406. if (!ctx->caps)
  1407. imap_exec( ctx, 0, imap_open_store_p2, "CAPABILITY" );
  1408. else
  1409. imap_open_store_authenticate( ctx );
  1410. }
  1411. static void
  1412. imap_open_store_p2( imap_store_t *ctx, struct imap_cmd *cmd ATTR_UNUSED, int response )
  1413. {
  1414. if (response == RESP_NO)
  1415. imap_open_store_bail( ctx );
  1416. else if (response == RESP_OK)
  1417. imap_open_store_authenticate( ctx );
  1418. }
  1419. static void
  1420. imap_open_store_authenticate( imap_store_t *ctx )
  1421. {
  1422. if (ctx->greeting != GreetingPreauth) {
  1423. #ifdef HAVE_LIBSSL
  1424. imap_store_conf_t *cfg = (imap_store_conf_t *)ctx->gen.conf;
  1425. imap_server_conf_t *srvc = cfg->server;
  1426. if (!srvc->sconf.use_imaps &&
  1427. (srvc->sconf.use_sslv2 || srvc->sconf.use_sslv3 || srvc->sconf.use_tlsv1)) {
  1428. /* always try to select SSL support if available */
  1429. if (CAP(STARTTLS)) {
  1430. imap_exec( ctx, 0, imap_open_store_authenticate_p2, "STARTTLS" );
  1431. return;
  1432. } else {
  1433. if (srvc->require_ssl) {
  1434. error( "IMAP error: SSL support not available\n" );
  1435. imap_open_store_bail( ctx );
  1436. return;
  1437. } else {
  1438. warn( "IMAP warning: SSL support not available\n" );
  1439. }
  1440. }
  1441. }
  1442. #endif
  1443. imap_open_store_authenticate2( ctx );
  1444. } else {
  1445. imap_open_store_namespace( ctx );
  1446. }
  1447. }
  1448. #ifdef HAVE_LIBSSL
  1449. static void
  1450. imap_open_store_authenticate_p2( imap_store_t *ctx, struct imap_cmd *cmd ATTR_UNUSED, int response )
  1451. {
  1452. if (response == RESP_NO)
  1453. imap_open_store_bail( ctx );
  1454. else if (response == RESP_OK)
  1455. socket_start_tls( &ctx->conn, imap_open_store_tlsstarted2 );
  1456. }
  1457. static void
  1458. imap_open_store_tlsstarted2( int ok, void *aux )
  1459. {
  1460. imap_store_t *ctx = (imap_store_t *)aux;
  1461. if (!ok)
  1462. imap_open_store_ssl_bail( ctx );
  1463. else
  1464. imap_exec( ctx, 0, imap_open_store_authenticate_p3, "CAPABILITY" );
  1465. }
  1466. static void
  1467. imap_open_store_authenticate_p3( imap_store_t *ctx, struct imap_cmd *cmd ATTR_UNUSED, int response )
  1468. {
  1469. if (response == RESP_NO)
  1470. imap_open_store_bail( ctx );
  1471. else if (response == RESP_OK)
  1472. imap_open_store_authenticate2( ctx );
  1473. }
  1474. #endif
  1475. static void
  1476. imap_open_store_authenticate2( imap_store_t *ctx )
  1477. {
  1478. imap_store_conf_t *cfg = (imap_store_conf_t *)ctx->gen.conf;
  1479. imap_server_conf_t *srvc = cfg->server;
  1480. char *arg;
  1481. info ("Logging in...\n");
  1482. if (!srvc->user) {
  1483. error( "Skipping account %s, no user\n", srvc->name );
  1484. goto bail;
  1485. }
  1486. if (srvc->pass_cmd) {
  1487. FILE *fp;
  1488. int ret;
  1489. char buffer[80];
  1490. if (!(fp = popen( srvc->pass_cmd, "r" ))) {
  1491. pipeerr:
  1492. sys_error( "Skipping account %s, password command failed", srvc->name );
  1493. goto bail;
  1494. }
  1495. if (!fgets( buffer, sizeof(buffer), fp ))
  1496. buffer[0] = 0;
  1497. if ((ret = pclose( fp )) < 0)
  1498. goto pipeerr;
  1499. if (ret) {
  1500. if (WIFSIGNALED( ret ))
  1501. error( "Skipping account %s, password command crashed\n", srvc->name );
  1502. else
  1503. error( "Skipping account %s, password command exited with status %d\n", srvc->name, WEXITSTATUS( ret ) );
  1504. goto bail;
  1505. }
  1506. if (!buffer[0]) {
  1507. error( "Skipping account %s, password command produced no output\n", srvc->name );
  1508. goto bail;
  1509. }
  1510. buffer[strcspn( buffer, "\n" )] = 0; /* Strip trailing newline */
  1511. free( srvc->pass ); /* From previous runs */
  1512. srvc->pass = nfstrdup( buffer );
  1513. } else if (!srvc->pass) {
  1514. char prompt[80];
  1515. sprintf( prompt, "Password (%s): ", srvc->name );
  1516. arg = getpass( prompt );
  1517. if (!arg) {
  1518. perror( "getpass" );
  1519. exit( 1 );
  1520. }
  1521. if (!*arg) {
  1522. error( "Skipping account %s, no password\n", srvc->name );
  1523. goto bail;
  1524. }
  1525. /*
  1526. * getpass() returns a pointer to a static buffer. make a copy
  1527. * for long term storage.
  1528. */
  1529. srvc->pass = nfstrdup( arg );
  1530. }
  1531. #ifdef HAVE_LIBSSL
  1532. if (CAP(CRAM)) {
  1533. struct imap_cmd *cmd = new_imap_cmd( sizeof(*cmd) );
  1534. info( "Authenticating with CRAM-MD5\n" );
  1535. cmd->param.cont = do_cram_auth;
  1536. imap_exec( ctx, cmd, imap_open_store_authenticate2_p2, "AUTHENTICATE CRAM-MD5" );
  1537. return;
  1538. }
  1539. if (srvc->require_cram) {
  1540. error( "IMAP error: CRAM-MD5 authentication is not supported by server\n" );
  1541. goto bail;
  1542. }
  1543. #endif
  1544. if (CAP(NOLOGIN)) {
  1545. error( "Skipping account %s, server forbids LOGIN\n", srvc->name );
  1546. goto bail;
  1547. }
  1548. #ifdef HAVE_LIBSSL
  1549. if (!ctx->conn.ssl)
  1550. #endif
  1551. warn( "*** IMAP Warning *** Password is being sent in the clear\n" );
  1552. imap_exec( ctx, 0, imap_open_store_authenticate2_p2,
  1553. "LOGIN \"%\\s\" \"%\\s\"", srvc->user, srvc->pass );
  1554. return;
  1555. bail:
  1556. imap_open_store_bail( ctx );
  1557. }
  1558. static void
  1559. imap_open_store_authenticate2_p2( imap_store_t *ctx, struct imap_cmd *cmd ATTR_UNUSED, int response )
  1560. {
  1561. if (response == RESP_NO)
  1562. imap_open_store_bail( ctx );
  1563. else if (response == RESP_OK)
  1564. imap_open_store_namespace( ctx );
  1565. }
  1566. static void
  1567. imap_open_store_namespace( imap_store_t *ctx )
  1568. {
  1569. imap_store_conf_t *cfg = (imap_store_conf_t *)ctx->gen.conf;
  1570. ctx->prefix = cfg->gen.path;
  1571. ctx->delimiter = cfg->delimiter ? nfstrdup( cfg->delimiter ) : 0;
  1572. if (((!*ctx->prefix && cfg->use_namespace) || !cfg->delimiter) && CAP(NAMESPACE)) {
  1573. /* get NAMESPACE info */
  1574. if (!ctx->got_namespace)
  1575. imap_exec( ctx, 0, imap_open_store_namespace_p2, "NAMESPACE" );
  1576. else
  1577. imap_open_store_namespace2( ctx );
  1578. return;
  1579. }
  1580. imap_open_store_finalize( ctx );
  1581. }
  1582. static void
  1583. imap_open_store_namespace_p2( imap_store_t *ctx, struct imap_cmd *cmd ATTR_UNUSED, int response )
  1584. {
  1585. if (response == RESP_NO) {
  1586. imap_open_store_bail( ctx );
  1587. } else if (response == RESP_OK) {
  1588. ctx->got_namespace = 1;
  1589. imap_open_store_namespace2( ctx );
  1590. }
  1591. }
  1592. static void
  1593. imap_open_store_namespace2( imap_store_t *ctx )
  1594. {
  1595. imap_store_conf_t *cfg = (imap_store_conf_t *)ctx->gen.conf;
  1596. list_t *nsp, *nsp_1st, *nsp_1st_ns, *nsp_1st_dl;
  1597. /* XXX for now assume 1st personal namespace */
  1598. if (is_list( (nsp = ctx->ns_personal) ) &&
  1599. is_list( (nsp_1st = nsp->child) ) &&
  1600. is_atom( (nsp_1st_ns = nsp_1st->child) ) &&
  1601. is_atom( (nsp_1st_dl = nsp_1st_ns->next) ))
  1602. {
  1603. if (!*ctx->prefix && cfg->use_namespace)
  1604. ctx->prefix = nsp_1st_ns->val;
  1605. if (!ctx->delimiter)
  1606. ctx->delimiter = nfstrdup( nsp_1st_dl->val );
  1607. }
  1608. imap_open_store_finalize( ctx );
  1609. }
  1610. static void
  1611. imap_open_store_finalize( imap_store_t *ctx )
  1612. {
  1613. set_bad_callback( &ctx->gen, 0, 0 );
  1614. ctx->trashnc = TrashUnknown;
  1615. ctx->callbacks.imap_open( &ctx->gen, ctx->callback_aux );
  1616. }
  1617. #ifdef HAVE_LIBSSL
  1618. static void
  1619. imap_open_store_ssl_bail( imap_store_t *ctx )
  1620. {
  1621. /* This avoids that we try to send LOGOUT to an unusable socket. */
  1622. socket_close( &ctx->conn );
  1623. imap_open_store_bail( ctx );
  1624. }
  1625. #endif
  1626. static void
  1627. imap_open_store_bail( imap_store_t *ctx )
  1628. {
  1629. void (*cb)( store_t *srv, void *aux ) = ctx->callbacks.imap_open;
  1630. void *aux = ctx->callback_aux;
  1631. imap_cancel_store( &ctx->gen );
  1632. cb( 0, aux );
  1633. }
  1634. /******************* imap_prepare_opts *******************/
  1635. static void
  1636. imap_prepare_opts( store_t *gctx, int opts )
  1637. {
  1638. gctx->opts = opts;
  1639. }
  1640. /******************* imap_select *******************/
  1641. static void
  1642. imap_select( store_t *gctx, int create,
  1643. void (*cb)( int sts, void *aux ), void *aux )
  1644. {
  1645. imap_store_t *ctx = (imap_store_t *)gctx;
  1646. struct imap_cmd_simple *cmd;
  1647. char *buf;
  1648. free_generic_messages( gctx->msgs );
  1649. gctx->msgs = 0;
  1650. ctx->msgapp = &gctx->msgs;
  1651. if (prepare_box( &buf, ctx ) < 0) {
  1652. cb( DRV_BOX_BAD, aux );
  1653. return;
  1654. }
  1655. ctx->gen.uidnext = 0;
  1656. INIT_IMAP_CMD(imap_cmd_simple, cmd, cb, aux)
  1657. cmd->gen.param.create = create;
  1658. cmd->gen.param.trycreate = 1;
  1659. imap_exec( ctx, &cmd->gen, imap_done_simple_box,
  1660. "SELECT \"%\\s\"", buf );
  1661. free( buf );
  1662. }
  1663. /******************* imap_load *******************/
  1664. static int imap_submit_load( imap_store_t *, const char *, int, struct imap_cmd_refcounted_state * );
  1665. static void
  1666. imap_load( store_t *gctx, int minuid, int maxuid, int newuid, int *excs, int nexcs,
  1667. void (*cb)( int sts, void *aux ), void *aux )
  1668. {
  1669. imap_store_t *ctx = (imap_store_t *)gctx;
  1670. int i, j, bl;
  1671. char buf[1000];
  1672. if (!ctx->gen.count) {
  1673. free( excs );
  1674. cb( DRV_OK, aux );
  1675. } else {
  1676. struct imap_cmd_refcounted_state *sts = imap_refcounted_new_state( cb, aux );
  1677. sort_ints( excs, nexcs );
  1678. for (i = 0; i < nexcs; ) {
  1679. for (bl = 0; i < nexcs && bl < 960; i++) {
  1680. if (bl)
  1681. buf[bl++] = ',';
  1682. bl += sprintf( buf + bl, "%d", excs[i] );
  1683. j = i;
  1684. for (; i + 1 < nexcs && excs[i + 1] == excs[i] + 1; i++) {}
  1685. if (i != j)
  1686. bl += sprintf( buf + bl, ":%d", excs[i] );
  1687. }
  1688. if (imap_submit_load( ctx, buf, 0, sts ) < 0)
  1689. goto done;
  1690. }
  1691. if (maxuid == INT_MAX)
  1692. maxuid = ctx->gen.uidnext ? ctx->gen.uidnext - 1 : 1000000000;
  1693. if (maxuid >= minuid) {
  1694. if ((ctx->gen.opts & OPEN_FIND) && minuid < newuid) {
  1695. sprintf( buf, "%d:%d", minuid, newuid - 1 );
  1696. if (imap_submit_load( ctx, buf, 0, sts ) < 0)
  1697. goto done;
  1698. if (newuid > maxuid)
  1699. goto done;
  1700. sprintf( buf, "%d:%d", newuid, maxuid );
  1701. } else {
  1702. sprintf( buf, "%d:%d", minuid, maxuid );
  1703. }
  1704. imap_submit_load( ctx, buf, (ctx->gen.opts & OPEN_FIND), sts );
  1705. }
  1706. done:
  1707. free( excs );
  1708. imap_refcounted_done( sts );
  1709. }
  1710. }
  1711. static int
  1712. imap_submit_load( imap_store_t *ctx, const char *buf, int tuids, struct imap_cmd_refcounted_state *sts )
  1713. {
  1714. return imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_refcounted_done_box,
  1715. "UID FETCH %s (UID%s%s%s)", buf,
  1716. (ctx->gen.opts & OPEN_FLAGS) ? " FLAGS" : "",
  1717. (ctx->gen.opts & OPEN_SIZE) ? " RFC822.SIZE" : "",
  1718. tuids ? " BODY.PEEK[HEADER.FIELDS (X-TUID)]" : "");
  1719. }
  1720. /******************* imap_fetch_msg *******************/
  1721. static void imap_fetch_msg_p2( imap_store_t *ctx, struct imap_cmd *gcmd, int response );
  1722. static void
  1723. imap_fetch_msg( store_t *ctx, message_t *msg, msg_data_t *data,
  1724. void (*cb)( int sts, void *aux ), void *aux )
  1725. {
  1726. struct imap_cmd_fetch_msg *cmd;
  1727. INIT_IMAP_CMD_X(imap_cmd_fetch_msg, cmd, cb, aux)
  1728. cmd->gen.gen.param.uid = msg->uid;
  1729. cmd->msg_data = data;
  1730. data->data = 0;
  1731. imap_exec( (imap_store_t *)ctx, &cmd->gen.gen, imap_fetch_msg_p2,
  1732. "UID FETCH %d (%s%sBODY.PEEK[])", msg->uid,
  1733. !(msg->status & M_FLAGS) ? "FLAGS " : "",
  1734. (data->date== -1) ? "INTERNALDATE " : "" );
  1735. }
  1736. static void
  1737. imap_fetch_msg_p2( imap_store_t *ctx, struct imap_cmd *gcmd, int response )
  1738. {
  1739. struct imap_cmd_fetch_msg *cmd = (struct imap_cmd_fetch_msg *)gcmd;
  1740. if (response == RESP_OK && !cmd->msg_data->data) {
  1741. /* The FETCH succeeded, but there is no message with this UID. */
  1742. response = RESP_NO;
  1743. }
  1744. imap_done_simple_msg( ctx, gcmd, response );
  1745. }
  1746. /******************* imap_set_flags *******************/
  1747. static void imap_set_flags_p2( imap_store_t *, struct imap_cmd *, int );
  1748. static int
  1749. imap_make_flags( int flags, char *buf )
  1750. {
  1751. const char *s;
  1752. unsigned i, d;
  1753. for (i = d = 0; i < as(Flags); i++)
  1754. if (flags & (1 << i)) {
  1755. buf[d++] = ' ';
  1756. buf[d++] = '\\';
  1757. for (s = Flags[i]; *s; s++)
  1758. buf[d++] = *s;
  1759. }
  1760. buf[0] = '(';
  1761. buf[d++] = ')';
  1762. return d;
  1763. }
  1764. static int
  1765. imap_flags_helper( imap_store_t *ctx, int uid, char what, int flags,
  1766. struct imap_cmd_refcounted_state *sts )
  1767. {
  1768. char buf[256];
  1769. buf[imap_make_flags( flags, buf )] = 0;
  1770. return imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_set_flags_p2,
  1771. "UID STORE %d %cFLAGS.SILENT %s", uid, what, buf );
  1772. }
  1773. static void
  1774. imap_set_flags( store_t *gctx, message_t *msg, int uid, int add, int del,
  1775. void (*cb)( int sts, void *aux ), void *aux )
  1776. {
  1777. imap_store_t *ctx = (imap_store_t *)gctx;
  1778. if (msg) {
  1779. uid = msg->uid;
  1780. add &= ~msg->flags;
  1781. del &= msg->flags;
  1782. msg->flags |= add;
  1783. msg->flags &= ~del;
  1784. }
  1785. if (add || del) {
  1786. struct imap_cmd_refcounted_state *sts = imap_refcounted_new_state( cb, aux );
  1787. if ((add && imap_flags_helper( ctx, uid, '+', add, sts ) < 0) ||
  1788. (del && imap_flags_helper( ctx, uid, '-', del, sts ) < 0)) {}
  1789. imap_refcounted_done( sts );
  1790. } else {
  1791. cb( DRV_OK, aux );
  1792. }
  1793. }
  1794. static void
  1795. imap_set_flags_p2( imap_store_t *ctx ATTR_UNUSED, struct imap_cmd *cmd, int response )
  1796. {
  1797. struct imap_cmd_refcounted_state *sts = ((struct imap_cmd_refcounted *)cmd)->state;
  1798. switch (response) {
  1799. case RESP_CANCEL:
  1800. sts->ret_val = DRV_CANCELED;
  1801. break;
  1802. case RESP_NO:
  1803. if (sts->ret_val == DRV_OK) /* Don't override cancelation. */
  1804. sts->ret_val = DRV_MSG_BAD;
  1805. break;
  1806. }
  1807. imap_refcounted_done( sts );
  1808. }
  1809. /******************* imap_close *******************/
  1810. static void
  1811. imap_close( store_t *gctx,
  1812. void (*cb)( int sts, void *aux ), void *aux )
  1813. {
  1814. imap_store_t *ctx = (imap_store_t *)gctx;
  1815. if (ctx->gen.conf->trash && CAP(UIDPLUS)) {
  1816. struct imap_cmd_refcounted_state *sts = imap_refcounted_new_state( cb, aux );
  1817. message_t *msg, *fmsg, *nmsg;
  1818. int bl;
  1819. char buf[1000];
  1820. for (msg = ctx->gen.msgs; ; ) {
  1821. for (bl = 0; msg && bl < 960; msg = msg->next) {
  1822. if (!(msg->flags & F_DELETED))
  1823. continue;
  1824. if (bl)
  1825. buf[bl++] = ',';
  1826. bl += sprintf( buf + bl, "%d", msg->uid );
  1827. fmsg = msg;
  1828. for (; (nmsg = msg->next) && (nmsg->flags & F_DELETED); msg = nmsg) {}
  1829. if (msg != fmsg)
  1830. bl += sprintf( buf + bl, ":%d", msg->uid );
  1831. }
  1832. if (!bl)
  1833. break;
  1834. if (imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_refcounted_done_box,
  1835. "UID EXPUNGE %s", buf ) < 0)
  1836. break;
  1837. }
  1838. imap_refcounted_done( sts );
  1839. } else {
  1840. /* This is inherently racy: it may cause messages which other clients
  1841. * marked as deleted to be expunged without being trashed. */
  1842. struct imap_cmd_simple *cmd;
  1843. INIT_IMAP_CMD(imap_cmd_simple, cmd, cb, aux)
  1844. imap_exec( ctx, &cmd->gen, imap_done_simple_box, "CLOSE" );
  1845. }
  1846. }
  1847. /******************* imap_trash_msg *******************/
  1848. static void
  1849. imap_trash_msg( store_t *gctx, message_t *msg,
  1850. void (*cb)( int sts, void *aux ), void *aux )
  1851. {
  1852. imap_store_t *ctx = (imap_store_t *)gctx;
  1853. struct imap_cmd_simple *cmd;
  1854. char *buf;
  1855. INIT_IMAP_CMD(imap_cmd_simple, cmd, cb, aux)
  1856. cmd->gen.param.create = 1;
  1857. cmd->gen.param.to_trash = 1;
  1858. if (prepare_trash( &buf, ctx ) < 0) {
  1859. cb( DRV_BOX_BAD, aux );
  1860. return;
  1861. }
  1862. imap_exec( ctx, &cmd->gen, imap_done_simple_msg,
  1863. CAP(MOVE) ? "UID MOVE %d \"%\\s\"" : "UID COPY %d \"%\\s\"", msg->uid, buf );
  1864. free( buf );
  1865. }
  1866. /******************* imap_store_msg *******************/
  1867. static void imap_store_msg_p2( imap_store_t *, struct imap_cmd *, int );
  1868. static size_t
  1869. my_strftime( char *s, size_t max, const char *fmt, const struct tm *tm )
  1870. {
  1871. return strftime( s, max, fmt, tm );
  1872. }
  1873. static void
  1874. imap_store_msg( store_t *gctx, msg_data_t *data, int to_trash,
  1875. void (*cb)( int sts, int uid, void *aux ), void *aux )
  1876. {
  1877. imap_store_t *ctx = (imap_store_t *)gctx;
  1878. struct imap_cmd_out_uid *cmd;
  1879. char *buf;
  1880. int d;
  1881. char flagstr[128], datestr[64];
  1882. d = 0;
  1883. if (data->flags) {
  1884. d = imap_make_flags( data->flags, flagstr );
  1885. flagstr[d++] = ' ';
  1886. }
  1887. flagstr[d] = 0;
  1888. INIT_IMAP_CMD(imap_cmd_out_uid, cmd, cb, aux)
  1889. cmd->gen.param.data_len = data->len;
  1890. cmd->gen.param.data = data->data;
  1891. cmd->out_uid = -2;
  1892. if (to_trash) {
  1893. cmd->gen.param.create = 1;
  1894. cmd->gen.param.to_trash = 1;
  1895. if (prepare_trash( &buf, ctx ) < 0) {
  1896. cb( DRV_BOX_BAD, -1, aux );
  1897. return;
  1898. }
  1899. } else {
  1900. if (prepare_box( &buf, ctx ) < 0) {
  1901. cb( DRV_BOX_BAD, -1, aux );
  1902. return;
  1903. }
  1904. }
  1905. if (data->date) {
  1906. /* configure ensures that %z actually works. */
  1907. my_strftime( datestr, sizeof(datestr), "%d-%b-%Y %H:%M:%S %z", localtime( &data->date ) );
  1908. imap_exec( ctx, &cmd->gen, imap_store_msg_p2,
  1909. "APPEND \"%\\s\" %s\"%\\s\" ", buf, flagstr, datestr );
  1910. } else {
  1911. imap_exec( ctx, &cmd->gen, imap_store_msg_p2,
  1912. "APPEND \"%\\s\" %s", buf, flagstr );
  1913. }
  1914. free( buf );
  1915. }
  1916. static void
  1917. imap_store_msg_p2( imap_store_t *ctx ATTR_UNUSED, struct imap_cmd *cmd, int response )
  1918. {
  1919. struct imap_cmd_out_uid *cmdp = (struct imap_cmd_out_uid *)cmd;
  1920. transform_msg_response( &response );
  1921. cmdp->callback( response, cmdp->out_uid, cmdp->callback_aux );
  1922. }
  1923. /******************* imap_find_new_msgs *******************/
  1924. static void imap_find_new_msgs_p2( imap_store_t *, struct imap_cmd *, int );
  1925. static void
  1926. imap_find_new_msgs( store_t *gctx,
  1927. void (*cb)( int sts, void *aux ), void *aux )
  1928. {
  1929. imap_store_t *ctx = (imap_store_t *)gctx;
  1930. struct imap_cmd_simple *cmd;
  1931. INIT_IMAP_CMD(imap_cmd_simple, cmd, cb, aux)
  1932. imap_exec( (imap_store_t *)ctx, &cmd->gen, imap_find_new_msgs_p2, "CHECK" );
  1933. }
  1934. static void
  1935. imap_find_new_msgs_p2( imap_store_t *ctx, struct imap_cmd *gcmd, int response )
  1936. {
  1937. struct imap_cmd_simple *cmdp = (struct imap_cmd_simple *)gcmd, *cmd;
  1938. if (response != RESP_OK) {
  1939. imap_done_simple_box( ctx, gcmd, response );
  1940. return;
  1941. }
  1942. INIT_IMAP_CMD(imap_cmd_simple, cmd, cmdp->callback, cmdp->callback_aux)
  1943. imap_exec( (imap_store_t *)ctx, &cmd->gen, imap_done_simple_box,
  1944. "UID FETCH %d:1000000000 (UID BODY.PEEK[HEADER.FIELDS (X-TUID)])", ctx->gen.uidnext );
  1945. }
  1946. /******************* imap_list *******************/
  1947. static void
  1948. imap_list( store_t *gctx, int flags,
  1949. void (*cb)( int sts, void *aux ), void *aux )
  1950. {
  1951. imap_store_t *ctx = (imap_store_t *)gctx;
  1952. struct imap_cmd_refcounted_state *sts = imap_refcounted_new_state( cb, aux );
  1953. if (((flags & LIST_PATH) &&
  1954. imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_refcounted_done_box,
  1955. "LIST \"\" \"%\\s*\"", ctx->prefix ) < 0) ||
  1956. ((flags & LIST_INBOX) && (!(flags & LIST_PATH) || *ctx->prefix) &&
  1957. imap_exec( ctx, imap_refcounted_new_cmd( sts ), imap_refcounted_done_box,
  1958. "LIST \"\" INBOX*" ) < 0))
  1959. {}
  1960. imap_refcounted_done( sts );
  1961. }
  1962. /******************* imap_cancel *******************/
  1963. static void
  1964. imap_cancel( store_t *gctx,
  1965. void (*cb)( void *aux ), void *aux )
  1966. {
  1967. imap_store_t *ctx = (imap_store_t *)gctx;
  1968. cancel_pending_imap_cmds( ctx );
  1969. if (ctx->in_progress) {
  1970. ctx->canceling = 1;
  1971. ctx->callbacks.imap_cancel = cb;
  1972. ctx->callback_aux = aux;
  1973. } else {
  1974. cb( aux );
  1975. }
  1976. }
  1977. /******************* imap_commit *******************/
  1978. static void
  1979. imap_commit( store_t *gctx )
  1980. {
  1981. (void)gctx;
  1982. }
  1983. /******************* imap_parse_store *******************/
  1984. imap_server_conf_t *servers, **serverapp = &servers;
  1985. static int
  1986. imap_parse_store( conffile_t *cfg, store_conf_t **storep )
  1987. {
  1988. imap_store_conf_t *store;
  1989. imap_server_conf_t *server, *srv, sserver;
  1990. int acc_opt = 0;
  1991. if (!strcasecmp( "IMAPAccount", cfg->cmd )) {
  1992. server = nfcalloc( sizeof(*server) );
  1993. server->name = nfstrdup( cfg->val );
  1994. *serverapp = server;
  1995. serverapp = &server->next;
  1996. store = 0;
  1997. *storep = 0;
  1998. } else if (!strcasecmp( "IMAPStore", cfg->cmd )) {
  1999. store = nfcalloc( sizeof(*store) );
  2000. store->gen.driver = &imap_driver;
  2001. store->gen.name = nfstrdup( cfg->val );
  2002. store->use_namespace = 1;
  2003. *storep = &store->gen;
  2004. memset( &sserver, 0, sizeof(sserver) );
  2005. server = &sserver;
  2006. } else
  2007. return 0;
  2008. #ifdef HAVE_LIBSSL
  2009. /* this will probably annoy people, but its the best default just in
  2010. * case people forget to turn it on
  2011. */
  2012. server->require_ssl = 1;
  2013. server->sconf.use_tlsv1 = 1;
  2014. #endif
  2015. server->max_in_progress = INT_MAX;
  2016. while (getcline( cfg ) && cfg->cmd) {
  2017. if (!strcasecmp( "Host", cfg->cmd )) {
  2018. /* The imap[s]: syntax is just a backwards compat hack. */
  2019. #ifdef HAVE_LIBSSL
  2020. if (!memcmp( "imaps:", cfg->val, 6 )) {
  2021. cfg->val += 6;
  2022. server->sconf.use_imaps = 1;
  2023. server->sconf.use_sslv2 = 1;
  2024. server->sconf.use_sslv3 = 1;
  2025. } else
  2026. #endif
  2027. {
  2028. if (!memcmp( "imap:", cfg->val, 5 ))
  2029. cfg->val += 5;
  2030. }
  2031. if (!memcmp( "//", cfg->val, 2 ))
  2032. cfg->val += 2;
  2033. server->sconf.host = nfstrdup( cfg->val );
  2034. }
  2035. else if (!strcasecmp( "User", cfg->cmd ))
  2036. server->user = nfstrdup( cfg->val );
  2037. else if (!strcasecmp( "Pass", cfg->cmd ))
  2038. server->pass = nfstrdup( cfg->val );
  2039. else if (!strcasecmp( "PassCmd", cfg->cmd ))
  2040. server->pass_cmd = nfstrdup( cfg->val );
  2041. else if (!strcasecmp( "Port", cfg->cmd ))
  2042. server->sconf.port = parse_int( cfg );
  2043. else if (!strcasecmp( "PipelineDepth", cfg->cmd )) {
  2044. if ((server->max_in_progress = parse_int( cfg )) < 1) {
  2045. error( "%s:%d: PipelineDepth must be at least 1\n", cfg->file, cfg->line );
  2046. cfg->err = 1;
  2047. }
  2048. }
  2049. #ifdef HAVE_LIBSSL
  2050. else if (!strcasecmp( "CertificateFile", cfg->cmd )) {
  2051. server->sconf.cert_file = expand_strdup( cfg->val );
  2052. if (access( server->sconf.cert_file, R_OK )) {
  2053. sys_error( "%s:%d: CertificateFile '%s'",
  2054. cfg->file, cfg->line, server->sconf.cert_file );
  2055. cfg->err = 1;
  2056. }
  2057. } else if (!strcasecmp( "RequireSSL", cfg->cmd ))
  2058. server->require_ssl = parse_bool( cfg );
  2059. else if (!strcasecmp( "UseIMAPS", cfg->cmd ))
  2060. server->sconf.use_imaps = parse_bool( cfg );
  2061. else if (!strcasecmp( "UseSSLv2", cfg->cmd ))
  2062. server->sconf.use_sslv2 = parse_bool( cfg );
  2063. else if (!strcasecmp( "UseSSLv3", cfg->cmd ))
  2064. server->sconf.use_sslv3 = parse_bool( cfg );
  2065. else if (!strcasecmp( "UseTLSv1", cfg->cmd ))
  2066. server->sconf.use_tlsv1 = parse_bool( cfg );
  2067. else if (!strcasecmp( "UseTLSv1.1", cfg->cmd ))
  2068. server->sconf.use_tlsv11 = parse_bool( cfg );
  2069. else if (!strcasecmp( "UseTLSv1.2", cfg->cmd ))
  2070. server->sconf.use_tlsv12 = parse_bool( cfg );
  2071. else if (!strcasecmp( "RequireCRAM", cfg->cmd ))
  2072. server->require_cram = parse_bool( cfg );
  2073. #endif
  2074. else if (!strcasecmp( "Tunnel", cfg->cmd ))
  2075. server->sconf.tunnel = nfstrdup( cfg->val );
  2076. else if (store) {
  2077. if (!strcasecmp( "Account", cfg->cmd )) {
  2078. for (srv = servers; srv; srv = srv->next)
  2079. if (srv->name && !strcmp( srv->name, cfg->val ))
  2080. goto gotsrv;
  2081. error( "%s:%d: unknown IMAP account '%s'\n", cfg->file, cfg->line, cfg->val );
  2082. cfg->err = 1;
  2083. continue;
  2084. gotsrv:
  2085. store->server = srv;
  2086. } else if (!strcasecmp( "UseNamespace", cfg->cmd ))
  2087. store->use_namespace = parse_bool( cfg );
  2088. else if (!strcasecmp( "Path", cfg->cmd ))
  2089. store->gen.path = nfstrdup( cfg->val );
  2090. else if (!strcasecmp( "PathDelimiter", cfg->cmd ))
  2091. store->delimiter = nfstrdup( cfg->val );
  2092. else
  2093. parse_generic_store( &store->gen, cfg );
  2094. continue;
  2095. } else {
  2096. error( "%s:%d: unknown/misplaced keyword '%s'\n", cfg->file, cfg->line, cfg->cmd );
  2097. cfg->err = 1;
  2098. continue;
  2099. }
  2100. acc_opt = 1;
  2101. }
  2102. if (!store || !store->server) {
  2103. if (!server->sconf.tunnel && !server->sconf.host) {
  2104. if (store)
  2105. error( "IMAP store '%s' has incomplete/missing connection details\n", store->gen.name );
  2106. else
  2107. error( "IMAP account '%s' has incomplete/missing connection details\n", server->name );
  2108. cfg->err = 1;
  2109. return 1;
  2110. }
  2111. if (server->pass && server->pass_cmd) {
  2112. if (store)
  2113. error( "IMAP store '%s' has both Pass and PassCmd\n", store->gen.name );
  2114. else
  2115. error( "IMAP account '%s' has both Pass and PassCmd\n", server->name );
  2116. cfg->err = 1;
  2117. return 1;
  2118. }
  2119. }
  2120. if (store) {
  2121. if (!store->server) {
  2122. store->server = nfmalloc( sizeof(sserver) );
  2123. memcpy( store->server, &sserver, sizeof(sserver) );
  2124. store->server->name = store->gen.name;
  2125. } else if (acc_opt) {
  2126. error( "IMAP store '%s' has both Account and account-specific options\n", store->gen.name );
  2127. cfg->err = 1;
  2128. }
  2129. }
  2130. return 1;
  2131. }
  2132. struct driver imap_driver = {
  2133. DRV_CRLF | DRV_VERBOSE,
  2134. imap_parse_store,
  2135. imap_cleanup,
  2136. imap_open_store,
  2137. imap_disown_store,
  2138. imap_cancel_store,
  2139. imap_list,
  2140. imap_prepare_opts,
  2141. imap_select,
  2142. imap_load,
  2143. imap_fetch_msg,
  2144. imap_store_msg,
  2145. imap_find_new_msgs,
  2146. imap_set_flags,
  2147. imap_trash_msg,
  2148. imap_close,
  2149. imap_cancel,
  2150. imap_commit,
  2151. };