IdReplyIMAP4.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.26 3/23/2005 3:01:56 PM DSiders
  18. Modified TIdReplyIMAP4.Destroy to call inherited destructor.
  19. Rev 1.25 20/01/2005 11:02:00 CCostelloe
  20. Now compiles, also updated to suit change in IdReply
  21. Rev 1.24 1/19/05 5:21:52 PM RLebeau
  22. added Destructor to free the FExtra object
  23. Removed label from SetFormattedReply()
  24. Rev 1.23 10/26/2004 10:39:54 PM JPMugaas
  25. Updated refs.
  26. Rev 1.22 6/11/2004 9:38:30 AM DSiders
  27. Added "Do not Localize" comments.
  28. Rev 1.21 5/17/04 9:53:00 AM RLebeau
  29. Changed TIdRepliesIMAP4 constructor to use 'reintroduce' instead
  30. Rev 1.20 5/16/04 5:31:24 PM RLebeau
  31. Added constructor to TIdRepliesIMAP4 class
  32. Rev 1.19 03/03/2004 01:16:56 CCostelloe
  33. Yet another check-in as part of continuing development
  34. Rev 1.18 26/02/2004 02:02:22 CCostelloe
  35. A few updates to support IdIMAP4Server development
  36. Rev 1.17 05/02/2004 00:26:06 CCostelloe
  37. Changes to support TIdIMAP4Server
  38. Rev 1.16 2/3/2004 4:12:34 PM JPMugaas
  39. Fixed up units so they should compile.
  40. Rev 1.15 2004.01.29 12:07:52 AM czhower
  41. .Net constructor problem fix.
  42. Rev 1.14 1/3/2004 8:05:48 PM JPMugaas
  43. Bug fix: Sometimes, replies will appear twice due to the way functionality
  44. was enherited.
  45. Rev 1.13 22/12/2003 00:45:40 CCostelloe
  46. .NET fixes
  47. Rev 1.12 03/12/2003 09:48:34 CCostelloe
  48. IsItANumber and IsItAValidSequenceNumber made public for use by TIdIMAP4.
  49. Rev 1.11 28/11/2003 21:02:46 CCostelloe
  50. Fixes for Courier IMAP
  51. Rev 1.10 22/10/2003 12:18:06 CCostelloe
  52. Split out DoesLineHaveExpectedResponse for use by other functions in IdIMAP4.
  53. Rev 1.9 10/19/2003 5:57:12 PM DSiders
  54. Added localization comments.
  55. Rev 1.8 18/10/2003 22:33:00 CCostelloe
  56. RemoveUnsolicitedResponses added.
  57. Rev 1.7 20/09/2003 19:36:42 CCostelloe
  58. Multiple changes to clear up older issues
  59. Rev 1.6 2003.09.20 10:38:40 AM czhower
  60. Bug fix to allow clearing code field (Return to default value)
  61. Rev 1.5 18/06/2003 21:57:00 CCostelloe
  62. Rewrote SetFormattedReply. Compiles and works. Needs tidying up, as does
  63. IdIMAP4.
  64. Rev 1.4 17/06/2003 01:38:12 CCostelloe
  65. Updated to suit LoginSASL changes. Compiles OK.
  66. Rev 1.3 15/06/2003 08:41:48 CCostelloe
  67. Bug fix: i was undefined in SetFormattedReply in posted version, changed to LN
  68. Rev 1.2 12/06/2003 10:26:14 CCostelloe
  69. Unfinished but compiles. Checked in to show problem with Get/SetNumericCode.
  70. Rev 1.1 6/5/2003 04:54:26 AM JPMugaas
  71. Reworkings and minor changes for new Reply exception framework.
  72. Rev 1.0 5/27/2003 03:03:54 AM JPMugaas
  73. 2003-Sep-26: CC2: Added Extra property.
  74. 2003-Oct-18: CC3: Added RemoveUnsolicitedResponses function.
  75. 2003-Nov-28: CC4: Fixes for Courier IMAP server.
  76. }
  77. unit IdReplyIMAP4;
  78. interface
  79. {$i IdCompilerDefines.inc}
  80. uses
  81. Classes,
  82. IdReply;
  83. const
  84. IMAP_OK = 'OK'; {Do not Localize}
  85. IMAP_NO = 'NO'; {Do not Localize}
  86. IMAP_BAD = 'BAD'; {Do not Localize}
  87. IMAP_PREAUTH = 'PREAUTH'; {Do not Localize}
  88. IMAP_BYE = 'BYE'; {Do not Localize}
  89. IMAP_CONT = '+'; {Do not Localize}
  90. VALID_TAGGEDREPLIES : array [0..5] of string =
  91. (IMAP_OK, IMAP_NO, IMAP_BAD, IMAP_PREAUTH, IMAP_BYE, IMAP_CONT);
  92. type
  93. TIdReplyIMAP4 = class(TIdReply)
  94. protected
  95. {CC: A tagged IMAP response is 'C41 OK Completed', where C41 is the
  96. command sequence number identifying the command you sent to get that
  97. response. An untagged one is '* OK Bad parameter'. The codes are
  98. the same, some just start with *.
  99. FSequenceNumber is either a *, C41 or '' (if the response line starts with
  100. a valid response code like OK)...}
  101. FSequenceNumber: string;
  102. {IMAP servers can send extra info after a command like "BAD Bad parameter".
  103. Keep these for error messages (may be more than one).
  104. Unsolicited responses from the server will also be put here.}
  105. FExtra: TStrings;
  106. function GetExtra: TStrings; //Added to get over .NET not calling TIdReplyIMAP4's constructor
  107. {You would think that we need to override IdReply's Get/SetNumericCode
  108. because they assume the code is like '32' whereas IMAP codes are text like
  109. 'OK' (when IdReply's StrToIntDef always returns 0), but Indy 10 has switched
  110. from numeric codes to string codes (i.e. we use 'OK' and never a
  111. numeric equivalent like 4).}
  112. {function GetNumericCode: Integer;
  113. procedure SetNumericCode(const AValue: Integer);}
  114. {Get/SetFormattedReply need to be overriden for IMAP4}
  115. function GetFormattedReply: TStrings; override;
  116. procedure SetFormattedReply(const AValue: TStrings); override;
  117. {CC: Need this also, otherwise the virtual one in IdReply uses
  118. TIdReplyRFC.CheckIfCodeIsValid which will only convert numeric
  119. codes like '22' to integer 22.}
  120. function CheckIfCodeIsValid(const ACode: string): Boolean; override;
  121. procedure AssignTo(ADest: TPersistent); override;
  122. public
  123. constructor CreateWithReplyTexts(
  124. ACollection: TCollection = nil;
  125. AReplyTexts: TIdReplies = nil
  126. ); override;
  127. destructor Destroy; override;
  128. procedure Clear; override;
  129. //
  130. //CLIENT-SIDE (TIdIMAP4) FUNCTIONS...
  131. procedure RaiseReplyError; override;
  132. procedure DoReplyError(ADescription: string; AnOffendingLine: string = ''); reintroduce;
  133. procedure RemoveUnsolicitedResponses(AExpectedResponses: array of String);
  134. function DoesLineHaveExpectedResponse(ALine: string; AExpectedResponses: array of string): Boolean;
  135. {CC: The following decides if AValue is a valid command sequence number
  136. like C41...}
  137. function IsItAValidSequenceNumber(const AValue: string): Boolean;
  138. //
  139. //SERVER-SIDE (TIdIMAP4Server) FUNCTIONS...
  140. function ParseRequest(ARequest: string): Boolean;
  141. //
  142. property NumericCode: Integer read GetNumericCode write SetNumericCode;
  143. property Extra: TStrings read GetExtra;
  144. property SequenceNumber: string read FSequenceNumber;
  145. //
  146. end;
  147. TIdRepliesIMAP4 = class(TIdReplies)
  148. public
  149. constructor Create(AOwner: TPersistent); reintroduce;
  150. end;
  151. //This error method came from the POP3 Protocol reply exceptions
  152. // SendCmd / GetResponse
  153. EIdReplyIMAP4Error = class(EIdReplyError);
  154. implementation
  155. uses
  156. IdGlobal, IdGlobalProtocols, SysUtils;
  157. { TIdReplyIMAP4 }
  158. procedure TIdReplyIMAP4.AssignTo(ADest: TPersistent);
  159. var
  160. LR: TIdReplyIMAP4;
  161. begin
  162. if ADest is TIdReplyIMAP4 then begin
  163. LR := TIdReplyIMAP4(ADest);
  164. //set code first as it possibly clears the reply
  165. LR.Code := Code;
  166. LR.FSequenceNumber := SequenceNumber;
  167. LR.Extra.Assign(Extra);
  168. LR.Text.Assign(Text);
  169. end else begin
  170. inherited AssignTo(ADest);
  171. end;
  172. end;
  173. function TIdReplyIMAP4.ParseRequest(ARequest: string): Boolean;
  174. begin
  175. FSequenceNumber := Fetch(ARequest);
  176. Result := IsItAValidSequenceNumber(FSequenceNumber);
  177. end;
  178. function TIdReplyIMAP4.GetExtra: TStrings;
  179. begin
  180. if not Assigned(FExtra) then begin
  181. FExtra := TStringList.Create;
  182. end;
  183. Result := FExtra;
  184. end;
  185. constructor TIdReplyIMAP4.CreateWithReplyTexts(
  186. ACollection: TCollection = nil;
  187. AReplyTexts: TIdReplies = nil
  188. );
  189. begin
  190. inherited CreateWithReplyTexts(ACollection, AReplyTexts);
  191. FExtra := TStringList.Create;
  192. Clear;
  193. end;
  194. destructor TIdReplyIMAP4.Destroy;
  195. begin
  196. FreeAndNil(FExtra);
  197. inherited Destroy;
  198. end;
  199. procedure TIdReplyIMAP4.Clear;
  200. begin
  201. inherited Clear;
  202. FSequenceNumber := '';
  203. Extra.Clear;
  204. end;
  205. procedure TIdReplyIMAP4.RaiseReplyError;
  206. begin
  207. raise EIdReplyIMAP4Error.Create(Extra.Text); {do not localize}
  208. end;
  209. {CC: The following decides if AValue is a valid command sequence number like C41...}
  210. function TIdReplyIMAP4.IsItAValidSequenceNumber(const AValue: string): Boolean;
  211. begin
  212. {CC: Cannot be a C or a digit on its own...}
  213. {CC: Must start with a C...}
  214. if (Length(AValue) >= 2) and CharEquals(AValue, 1, 'C') then begin
  215. {CC: Check if other characters are digits...}
  216. Result := IsNumeric(AValue, -1, 2);
  217. end else begin
  218. Result := False;
  219. end;
  220. end;
  221. function TIdReplyIMAP4.CheckIfCodeIsValid(const ACode: string): Boolean;
  222. var
  223. LOrd : Integer;
  224. begin
  225. LOrd := PosInStrArray(ACode, VALID_TAGGEDREPLIES, False);
  226. Result := (LOrd <> -1) or (Trim(ACode) = '');
  227. end;
  228. function TIdReplyIMAP4.GetFormattedReply: TStrings;
  229. begin
  230. {Used by TIdIMAP4Server to assemble a string reply from our fields...}
  231. FFormattedReply.Clear;
  232. Result := FFormattedReply;
  233. end;
  234. {CC: AValue may be in one of a few formats:
  235. 1) Many commands just give a simple result to the command issued:
  236. C41 OK Completed
  237. 2) Some commands give you data first, then the result:
  238. * LIST (\UnMarked) "/" INBOX
  239. * LIST (\UnMarked) "/" Junk
  240. * LIST (\UnMarked) "/" Junk/Subbox1
  241. C42 OK Completed
  242. 3) Some responses have a result but * instead of a command number (like C42):
  243. * OK CommuniGate Pro IMAP Server 3.5.7 ready
  244. 4) Some have neither a * nor command number, but start with a result:
  245. + Send the additional command text
  246. or:
  247. BAD Bad parameter
  248. Because you may get data first, which you need to put into Text, you need to
  249. accept all the above possibilities.
  250. In this function, we can assume that the last line of AValues has previously been
  251. identified (by GetResponse).
  252. For the Text parameter, data lines are added with the starting * stripped off.
  253. The last Text line is the response line (the OK, BAD, etc., line) with any *
  254. and response (OK, BAD) stripped out - this is usually just Completed or the
  255. error message.
  256. Set FSequenceNumber to C41 for cases (1) and (2) above, * for case (3), and
  257. empty '' for case 4. This tells the caller the context of the reply.
  258. }
  259. procedure TIdReplyIMAP4.SetFormattedReply(const AValue: TStrings);
  260. var
  261. LWord: string;
  262. LPos: integer;
  263. LBuf : String;
  264. LN: integer;
  265. LLine: string;
  266. begin
  267. Clear;
  268. LWord := '';
  269. if AValue.Count <= 0 then begin
  270. {Throw an exception. Something is badly messed up if we were called with
  271. an empty string list.}
  272. DoReplyError('Unexpected: Logic error, SetFormattedReply called with an empty list of parameters'); {do not localize}
  273. end;
  274. {CC: Any lines before the last one should be data lines, which begin with a * ...}
  275. for LN := 0 to AValue.Count - 2 do begin
  276. LLine := AValue[LN];
  277. if LLine <> '' then begin
  278. LWord := Trim(Fetch(LLine));
  279. LLine := Trim(LLine);
  280. if (LLine = '') then begin
  281. {Throw an exception: this line is a single word, not a valid data
  282. line since it does not have a * plus at least one word of data.}
  283. DoReplyError('Unexpected: Non-last response line (i.e. a data line) only contained one word, instead of a * followed by one or more words', AValue[LN]); {do not localize}
  284. end;
  285. if (LWord <> '*') then begin {Do not Localize}
  286. //Throw an exception: No * as first word of a data line.
  287. DoReplyError('Unexpected: Non-last response line (i.e. a data line) did not start with a *', AValue[LN]); {do not localize}
  288. end;
  289. Text.Add(LLine);
  290. end;
  291. end;
  292. {The response (OK, BAD, etc.) is in the LAST line received (or else the
  293. function that got the response, such as GetResponse, is broken).}
  294. LLine := AValue[AValue.Count-1];
  295. if LLine = '' then begin
  296. {Throw an exception: The previous function (GetResponse, or whatever)
  297. messed up and passed an empty line as the response (last) line...}
  298. DoReplyError('Unexpected: Response (last) line was empty instead of containing a line with a response code like OK, NO, BAD, etc'); {do not localize}
  299. end;
  300. LBuf := LLine;
  301. LWord := Trim(Fetch(LBuf));
  302. LBuf := Trim(LBuf);
  303. {We can assume, if the previous function (GetResponse) did its
  304. job, that either the first or the second word (if it exists) is the
  305. response code...}
  306. LPos := PosInStrArray(LWord, VALID_TAGGEDREPLIES); {Do not Localize}
  307. if LPos > -1 then begin
  308. {The first word is a valid response. Leave FSequenceNumber as ''
  309. because there was nothing before it.}
  310. FCode := LWord;
  311. Text.Add(LBuf);
  312. end
  313. else if LWord = '*' then begin {Do not Localize}
  314. if LBuf = '' then begin
  315. {Throw an exception: it is a line that is just '*'}
  316. DoReplyError('Unexpected: Response (last) line contained only a *'); {do not localize}
  317. end;
  318. FSequenceNumber := LWord; {Record that it is a * line}
  319. {The next word had better be a response...}
  320. LWord := Trim(Fetch(LBuf));
  321. LBuf := Trim(LBuf);
  322. if (LBuf = '') then begin
  323. {Should never get to here: LBuf should have been ''. Might as
  324. well throw an exception since we are down here anyway.}
  325. DoReplyError('Unexpected: Response (last) line contained only a * (type 2)'); {do not localize}
  326. end;
  327. LPos := PosInStrArray(LWord, VALID_TAGGEDREPLIES);
  328. if LPos = -1 then begin
  329. {A line beginning with * but no valid response code as the 2nd
  330. word. It is invalid, but maybe a data line that GetResponse
  331. missed. Throw an exception anyway.}
  332. DoReplyError('Unexpected: Response (last) line started with a * but next word was not a valid response like OK, BAD, etc', LLine); {do not localize}
  333. end;
  334. {A valid resonse code...}
  335. FCode := LWord;
  336. Text.Add(LBuf);
  337. end
  338. else if IsItAValidSequenceNumber(LWord) then begin
  339. if LBuf = '' then begin
  340. {Throw an exception: it is a line that is just 'C41' or whatever}
  341. DoReplyError('Unexpected: Response (last) line started with a command reference (like C41) but nothing else', LLine); {do not localize}
  342. end;
  343. FSequenceNumber := LWord; {Record that it is a C41 line}
  344. {The next word had better be a response...}
  345. LWord := Trim(Fetch(LBuf));
  346. LBuf := Trim(LBuf);
  347. if LBuf = '' then begin
  348. {Should never get to here: LBuf should have been ''. Might as
  349. well throw an exception since we are down here anyway.}
  350. DoReplyError('Unexpected: Logic error, line starts with a command reference (like C41) but nothing else, why was an exception not thrown earlier?', LLine); {do not localize}
  351. end;
  352. LPos := PosInStrArray(LWord, VALID_TAGGEDREPLIES);
  353. if LPos = -1 then begin
  354. {A line beginning with C41 but no valid response code as the 2nd
  355. word. Throw an exception.}
  356. DoReplyError('Unexpected: Line starts with a command reference (like C41) but next word was not a valid response like OK, BAD, etc', LLine); {do not localize}
  357. end;
  358. {A valid response code...}
  359. FCode := LWord;
  360. //CC4: LBuf will contain "SEARCH completed" if LLine was "C64 OK SEARCH completed".
  361. //Ditch LBuf, otherwise we will confuse the later parser that checks for
  362. //"expected response" keywords.
  363. Extra.Add(LBuf);
  364. end
  365. else begin
  366. {Not a response, * or command (e.g. C41). Throw an exception, as usual.}
  367. DoReplyError('Unexpected: Line does not start with a command reference (like C41), a *, or a valid response like OK, BAD, etc', LLine); {do not localize}
  368. end;
  369. if FCode = '' then begin
  370. {Did not get a valid response line, copy ALL of the last line we received
  371. into Text[] for error display. This is paranoid programming, we probably
  372. would have thrown an exception by now.}
  373. Text.Add(AValue[AValue.Count-1]);
  374. end;
  375. end;
  376. {CC3: This goes through the lines in Text and moves any that are not "expected" into
  377. Extra. Lines that are "expected" are those that have a command in one of the
  378. strings in AExpectedResponses, which has entries like "FETCH", "UID", "LIST".
  379. Unsolicited responses are typically lines like "* RECENT 3", which are sent by
  380. the server to tell you that new messages arrived. The problem is that they can
  381. be anywhere in a reply from the server, the RFC does not stipulate where, or
  382. what their format may be, but they wont be expected by the caller and will cause
  383. the caller's parsing to fail.
  384. The Text variable also has the bits stripped off from the final response, i.e.
  385. it will have "Completed" as the last entry, stripped from "C62 OK Completed".}
  386. procedure TIdReplyIMAP4.RemoveUnsolicitedResponses(AExpectedResponses: array of String);
  387. var
  388. LLine: string;
  389. LN, LIndex: integer;
  390. LLast: integer; {Need to calculate this outside the loop}
  391. begin
  392. {The (valid) lines are of one of two formats:
  393. * LIST BlahBlah
  394. * 53 FETCH BlahBlah
  395. The "53" arises with commands that reference a specific email, the server returns
  396. the relative message number in that case.
  397. Note the * has been stripped off before this procedure is called.}
  398. LLast := Text.Count-1;
  399. LIndex := 0;
  400. for LN := 0 to LLast do begin
  401. LLine := Text[LIndex];
  402. if LLine = '' then begin
  403. {Unlikely to happen, but paranoia is always a better approach...}
  404. Text.Delete(LIndex);
  405. end
  406. else begin
  407. if DoesLineHaveExpectedResponse(LLine, AExpectedResponses) then begin
  408. {We were expecting this word, so don't remove this line.}
  409. Inc(LIndex);
  410. Continue;
  411. end;
  412. {We were not expecting this response, it is an unsolicited response or
  413. something else we are not interested in. Transfer the UNSTRIPPED
  414. line to Extra (i.e. not LLine).}
  415. Extra.Add(Text[LIndex]);
  416. Text.Delete(LIndex);
  417. end;
  418. end;
  419. end;
  420. function TIdReplyIMAP4.DoesLineHaveExpectedResponse(ALine: string; AExpectedResponses: array of string): Boolean;
  421. var
  422. LWord: string;
  423. LPos: integer;
  424. begin
  425. Result := False;
  426. {Get the first word, it may be a relative message number like "53".
  427. CC4: Note the line may only consist of a single word, e.g. "SEARCH" with some
  428. servers (e.g. Courier) where there were no matches to the search.}
  429. LPos := Pos(' ', ALine); {Do not Localize}
  430. if LPos > 0 then begin
  431. if IsNumeric(ALine, LPos-1) then begin
  432. ALine := Copy(ALine, LPos+1, MaxInt);
  433. end;
  434. {If there was a relative message number, it is now stripped from LLine.}
  435. {The first word in LLine is the one that may hold our expected response.}
  436. LPos := Pos(' ', ALine); {Do not Localize}
  437. if LPos > 0 then begin
  438. LWord := Copy(ALine, 1, LPos-1);
  439. end else begin
  440. LWord := ALine;
  441. end;
  442. end else begin
  443. LWord := ALine;
  444. end;
  445. if PosInStrArray(LWord, AExpectedResponses) > -1 then begin
  446. {We were expecting this word...}
  447. Result := True;
  448. end;
  449. end;
  450. procedure TIdReplyIMAP4.DoReplyError(ADescription: string; AnOffendingLine: string);
  451. var
  452. LMsg: string;
  453. begin
  454. LMsg := ADescription;
  455. if AnOffendingLine <> '' then begin
  456. LMsg := LMsg + ', offending line: ' + AnOffendingLine; {do not localize}
  457. end;
  458. raise EIdReplyIMAP4Error.Create(LMsg);
  459. end;
  460. { TIdRepliesIMAP4 }
  461. constructor TIdRepliesIMAP4.Create(AOwner: TPersistent);
  462. begin
  463. inherited Create(AOwner, TIdReplyIMAP4);
  464. end;
  465. end.