IMAPServer.C 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. *+SNOTICE
  25. *
  26. * $TOG: IMAPServer.C /main/7 1998/11/10 17:08:32 mgreess $
  27. *
  28. * RESTRICTED CONFIDENTIAL INFORMATION:
  29. *
  30. * The information in this document is subject to special
  31. * restrictions in a confidential disclosure agreement between
  32. * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
  33. * document outside HP, IBM, Sun, USL, SCO, or Univel without
  34. * Sun's specific written approval. This document and all copies
  35. * and derivative works thereof must be returned or destroyed at
  36. * Sun's request.
  37. *
  38. * Copyright 1993, 1995, 1995 Sun Microsystems, Inc. All rights reserved.
  39. *
  40. *+ENOTICE
  41. */
  42. /*
  43. * Common Desktop Environment
  44. *
  45. * (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
  46. * (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
  47. * (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
  48. * (c) Copyright 1993, 1994, 1995 Novell, Inc.
  49. * (c) Copyright 1995 Digital Equipment Corp.
  50. * (c) Copyright 1995 Fujitsu Limited
  51. * (c) Copyright 1995 Hitachi, Ltd.
  52. *
  53. *
  54. * RESTRICTED RIGHTS LEGEND
  55. *
  56. *Use, duplication, or disclosure by the U.S. Government is subject to
  57. *restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  58. *Technical Data and Computer Software clause in DFARS 252.227-7013. Rights
  59. *for non-DOD U.S. Government Departments and Agencies are as set forth in
  60. *FAR 52.227-19(c)(1,2).
  61. *Hewlett-Packard Company, 3000 Hanover Street, Palo Alto, CA 94304 U.S.A.
  62. *International Business Machines Corp., Route 100, Somers, NY 10589 U.S.A.
  63. *Sun Microsystems, Inc., 2550 Garcia Avenue, Mountain View, CA 94043 U.S.A.
  64. *Novell, Inc., 190 River Road, Summit, NJ 07901 U.S.A.
  65. *Digital Equipment Corp., 111 Powdermill Road, Maynard, MA 01754, U.S.A.
  66. *Fujitsu Limited, 1015, Kamikodanaka Nakahara-Ku, Kawasaki 211, Japan
  67. *Hitachi, Ltd., 6, Kanda Surugadai 4-Chome, Chiyoda-ku, Tokyo 101, Japan
  68. */
  69. #include <stdio.h>
  70. #include <string.h>
  71. #include <ctype.h>
  72. #include <stdlib.h>
  73. #include <DtMail/DtMailServer.hh>
  74. #include <DtMail/IO.hh>
  75. #define dtmasTAGGET() (_transtag)
  76. IMAPServer::IMAPServer(
  77. char *folder,
  78. DtMail::Session *session,
  79. DtMail::MailBox *mailbox,
  80. DtMailAppendCallback append_mailbox_cb,
  81. void *append_mailbox_cb_data)
  82. : DtMailServer(folder, session, mailbox,
  83. append_mailbox_cb, append_mailbox_cb_data)
  84. {
  85. _append_mailbox_cb = append_mailbox_cb;
  86. _append_mailbox_cb_data = append_mailbox_cb_data;
  87. _count = 0;
  88. _imap4 = 0;
  89. _recent = 0;
  90. _seen = 0;
  91. _unseen = 0;
  92. }
  93. IMAPServer::~IMAPServer()
  94. {
  95. }
  96. //
  97. // Set delete flag for given message.
  98. //
  99. DTMailError_t
  100. IMAPServer::ptrans_delete(int msg)
  101. {
  102. // Use SILENT if possible as a minor throughput optimization.
  103. if (_imap4)
  104. return do_transaction("STORE %d +FLAGS.SILENT (\\Deleted)", msg);
  105. else
  106. return do_transaction("STORE %d +FLAGS (\\Deleted)", msg);
  107. }
  108. //
  109. // Apply for connection authorization
  110. //
  111. DTMailError_t
  112. IMAPServer::ptrans_authorize(char*)
  113. {
  114. /* try to get authorized */
  115. DTMailError_t ok;
  116. ok = do_transaction("LOGIN %s \"%s\"", _username, _password);
  117. if (DTME_NoError != ok) return DTME_MailServerAccess_AuthorizationFailed;
  118. // probe to see if we're running IMAP4 and can use RFC822.PEEK
  119. _imap4 = ((do_transaction("CAPABILITY")) == 0);
  120. return DTME_NoError;
  121. }
  122. //
  123. // Get range of messages to be fetched
  124. //
  125. DTMailError_t
  126. IMAPServer::ptrans_fldstate_read(int *countp, int *newp)
  127. {
  128. DTMailError_t ok;
  129. /* find out how many messages are waiting */
  130. _recent = _unseen = 0;
  131. ok = do_transaction("SELECT %s", _folder);
  132. if (DTME_NoError != ok) return ok;
  133. *countp = _count;
  134. if (_unseen) // Optional response, but better if we see it.
  135. *newp = _unseen;
  136. else if (_recent)
  137. *newp = _recent; // Mandatory
  138. else
  139. *newp = -1; // Should never happen, RECENT is mandatory.
  140. return DTME_NoError;
  141. }
  142. //
  143. // Capture the sizes of all messages.
  144. //
  145. DTMailError_t
  146. IMAPServer::ptrans_msgsizes(int count, int *sizes)
  147. {
  148. char buf[DTMAS_POPBUFSIZE+1];
  149. DTMailError_t ok = DTME_NoError;
  150. ok = do_send("FETCH 1:%d RFC822.SIZE", count);
  151. if (DTME_NoError != ok) return ok;
  152. while (SockGets(buf, sizeof(buf), _sockfp))
  153. {
  154. int num, size;
  155. if (buf[strlen(buf)-1] == '\n')
  156. buf[strlen(buf)-1] = '\0';
  157. if (buf[strlen(buf)-1] == '\r')
  158. buf[strlen(buf)-1] = '\r';
  159. if (_protologging)
  160. _logger.logError(DTM_FALSE, "%s< %s", proto_name(), buf);
  161. if (strstr(buf, "OK"))
  162. break;
  163. else if (sscanf(buf, "* %d FETCH (RFC822.SIZE %d)", &num, &size) == 2)
  164. sizes[num - 1] = size;
  165. else
  166. sizes[num - 1] = -1;
  167. }
  168. return DTME_NoError;
  169. }
  170. //
  171. // Is the given message old?
  172. //
  173. int
  174. IMAPServer::ptrans_msgisold(int num)
  175. {
  176. DTMailError_t ok;
  177. ok = do_transaction("FETCH %d FLAGS", num);
  178. if (DTME_NoError != ok) return 0;
  179. return _seen;
  180. }
  181. //
  182. // request nth message
  183. //
  184. DTMailError_t
  185. IMAPServer::ptrans_retrieve_start(int msg, int *lenp)
  186. {
  187. char buf[DTMAS_POPBUFSIZE+1];
  188. DTMailError_t ok = DTME_NoError;
  189. int num;
  190. //
  191. // If we're using IMAP4, we can fetch the message without setting its
  192. // seen flag. This is good! It means that if the protocol exchange
  193. // craps out during the message, it will still be marked `unseen' on
  194. // the server.
  195. //
  196. // However...*don't* do this if we're using keep to suppress deletion!
  197. // In that case, marking the seen flag is the only way to prevent the
  198. // message from being re-fetched on subsequent runs.
  199. //
  200. if (_imap4 && _removeafterdelivery)
  201. ok = do_send("FETCH %d RFC822.PEEK", msg);
  202. else
  203. ok = do_send("FETCH %d RFC822", msg);
  204. if (DTME_NoError != ok) return ok;
  205. // looking for FETCH response
  206. do
  207. {
  208. if (! SockGets(buf, sizeof(buf), _sockfp))
  209. {
  210. _logger.logError(DTM_FALSE, "Socket Error fetching message");
  211. return DTME_MailServerAccess_SocketIOError;
  212. }
  213. if (_protologging)
  214. _logger.logError(DTM_FALSE, "%s< %s", proto_name(), buf);
  215. } while (sscanf(buf+2, "%d FETCH (RFC822 {%d}", &num, lenp) != 2);
  216. if (num != msg)
  217. {
  218. _logger.logError(DTM_FALSE, "Protocol Error fetching message");
  219. return DTME_MailServerAccess_Error;
  220. }
  221. else
  222. {
  223. return DTME_NoError;
  224. }
  225. }
  226. //
  227. // Parse command response
  228. //
  229. DTMailError_t
  230. IMAPServer::ptrans_parse_response(char *argbuf)
  231. {
  232. char buf[DTMAS_POPBUFSIZE+1];
  233. _seen = 0;
  234. do
  235. {
  236. if (! SockGets(buf, sizeof(buf), _sockfp))
  237. {
  238. _logger.logError(DTM_FALSE, "Socket Error reading response");
  239. return DTME_MailServerAccess_SocketIOError;
  240. }
  241. if (buf[strlen(buf)-1] == '\n')
  242. buf[strlen(buf)-1] = '\0';
  243. if (buf[strlen(buf)-1] == '\r')
  244. buf[strlen(buf)-1] = '\r';
  245. if (_protologging)
  246. _logger.logError(DTM_FALSE, "%s< %s", proto_name(), buf);
  247. /* interpret untagged status responses */
  248. if (strstr(buf, "EXISTS"))
  249. _count = atoi(buf+2);
  250. if (strstr(buf, "RECENT"))
  251. _recent = atoi(buf+2);
  252. if (strstr(buf, "UNSEEN"))
  253. _unseen = atoi(buf+2);
  254. if (strstr(buf, "FLAGS"))
  255. _seen = (strstr(buf, "Seen") != (char *)NULL);
  256. } while (strlen(dtmasTAGGET()) &&
  257. strncmp(buf, dtmasTAGGET(), strlen(dtmasTAGGET())));
  258. if (! strlen(dtmasTAGGET()))
  259. {
  260. strcpy(argbuf, buf);
  261. return DTME_NoError;
  262. }
  263. else
  264. {
  265. char *cp;
  266. /* skip the tag */
  267. for (cp = buf; !isspace(*cp); cp++)
  268. continue;
  269. while (isspace(*cp))
  270. cp++;
  271. if (strncmp(cp, "OK", 2) == 0)
  272. {
  273. strcpy(argbuf, cp);
  274. return DTME_NoError;
  275. }
  276. else if (strncmp(cp, "BAD", 2) == 0)
  277. {
  278. _logger.logError(DTM_FALSE, "Protocol Error reading response");
  279. return DTME_MailServerAccess_Error;
  280. }
  281. else
  282. {
  283. _logger.logError(DTM_FALSE, "Protocol Violation reading response");
  284. return DTME_MailServerAccess_ProtocolViolation;
  285. }
  286. }
  287. }
  288. //
  289. // Retrieve messages using IMAP Version 2bis or Version 4.
  290. //
  291. void
  292. IMAPServer::retrieve_messages(DtMailEnv &error)
  293. {
  294. DtMailServer::retrieve_messages(error);
  295. }
  296. //
  297. // Discard tail of FETCH response after reading message text.
  298. //
  299. DTMailError_t
  300. IMAPServer::ptrans_retrieve_end(int)
  301. {
  302. char buf [DTMAS_POPBUFSIZE+1];
  303. if (! SockGets(buf, sizeof(buf), _sockfp))
  304. {
  305. _logger.logError(DTM_FALSE, "Socket Error reading trail");
  306. return DTME_MailServerAccess_SocketIOError;
  307. }
  308. return DTME_NoError;
  309. }