📄 idimap4.pas
字号:
function GetNewCmdCounter: String;
property LastCmdCounter: String read GetCmdCounter;
property NewCmdCounter: String read GetNewCmdCounter;
{ General Functions }
function ArrayToNumberStr (const AMsgNumList: array of Integer): String;
function MessageFlagSetToStr (const AFlags: TIdMessageFlagsSet): String;
//This function is needed because when using the regular DateToStr with dd/MMM/yyyy
//(which is the IMAP needed convension) may give the month as the local language
//three letter month instead of the English month needed.
function DateToIMAPDateStr (const ADate: TDateTime): String;
procedure StripCRLFs(var AText: string); overload; virtual; //Allow users to optimise
procedure StripCRLFs(ASourceStream, ADestStream: TStringStream); overload;
{ General Functions }
{ Parser Functions }
{CCC: new attempt...}
procedure ParseImapPart(ABodyStructure: string;
AImapParts: TIdImapMessageParts; AThisImapPart: TIdImapMessagePart; AParentImapPart: TIdImapMessagePart;
APartNumber: integer);
procedure ParseMessagePart(ABodyStructure: string;
AMessageParts: TIdMessageParts; AThisMessagePart: TIdMessagePart; AParentMessagePart: TIdMessagePart;
APartNumber: integer);
{CC2: ParseBodyStructureResult added to support individual part retreival...}
procedure ParseBodyStructureResult(ABodyStructure: string; ATheParts: TIdMessageParts; AImapParts: TIdImapMessageParts);
{CC3: ParseBodyStructurePart added to support individual part retreival...}
{CC7: TIdImapSubSection added to ParseBodyStructurePart to support multisection parts...}
procedure ParseBodyStructurePart(APartString: string; AThePart: TIdMessagePart; AImapPart: TIdImapMessagePart{; AImapSubSection: TIdImapSubSection});
procedure ParseTheLine(ALine: string; APartsList: TIdStringList);
procedure ParseIntoParts(APartString: string; AParams: TIdStringList);
procedure ParseIntoBrackettedQuotedAndUnquotedParts(APartString: string; AParams: TIdStringList; AKeepBrackets: Boolean);
procedure BreakApartParamsInQuotes(const AParam: string; var AParsedList: TIdStringList);
function GetNextWord(AParam: string): string;
function GetNextQuotedParam(AParam: string; ARemoveQuotes: Boolean): string;
procedure ParseExpungeResult (AMB: TIdMailBox; ACmdResultDetails: TIdStrings);
procedure ParseListResult (AMBList: TIdStringList; ACmdResultDetails: TIdStrings);
procedure ParseLSubResult(AMBList: TIdStringList; ACmdResultDetails: TIdStrings);
{CCA: InternalParseListResult added to resolve NIL mailbox separator and
rationalise code between ParseLisTresult and ParseLSubResult}
procedure InternalParseListResult(ACmd: string; AMBList: TIdStringList; ACmdResultDetails: TIdStrings);
procedure ParseMailBoxAttributeString(AAttributesList: String; var AAttributes: TIdMailBoxAttributesSet);
procedure ParseMessageFlagString (AFlagsList: String; var AFlags: TIdMessageFlagsSet);
procedure ParseSelectResult (AMB: TIdMailBox; ACmdResultDetails: TIdStrings);
procedure ParseStatusResult (AMB: TIdMailBox; ACmdResultDetails: TIdStrings);
procedure ParseSearchResult (AMB: TIdMailBox; ACmdResultDetails: TIdStrings);
procedure ParseEnvelopeResult (AMsg: TIdMessage; ACmdResultStr: String);
function ParseLastCmdResult(ALine: string; AExpectedCommand: string; AExpectedIMAPFunction: array of string): Boolean;
procedure ParseLastCmdResultButAppendInfo(ALine: string);
{CC8: Following added to combine the (UID)Retrieve(Peek) functions...}
function InternalRetrieve(const AMsgNum: Integer; AUseUID: Boolean; AUsePeek: Boolean; ANoDecode: Boolean; AMsg: TIdMessage): Boolean;
{CC2: Following added for retrieving individual parts of a message...}
function InternalRetrievePart(const AMsgNum: Integer; const APartNum: {Integer} string;
AUseUID: Boolean; AUsePeek: Boolean;
{$IFDEF DOTNET}
var ABuffer: TIdBytes;
{$ELSE}
var ABuffer: PChar;
{$ENDIF}
var ABufferLength: Integer; {NOTE: var args cannot have default params}
ADestFileNameAndPath: string = ''; {Do not Localize}
AContentTransferEncoding: string = 'text'): Boolean; {Do not Localize}
function ParseBodyStructureSectionAsEquates(AParam: string): string;
function ParseBodyStructureSectionAsEquates2(AParam: string): string;
{CC3: Following added for retrieving the text-only part of a message...}
function InternalRetrieveText(const AMsgNum: Integer; var AText: string;
AUseUID: Boolean; AUsePeek: Boolean; AUseFirstPartInsteadOfText: Boolean): Boolean;
{CC3: Following added for TLS support..}
function IsCapabilityListed(ACapability: string):Boolean;
{CC6: Added to support RetrieveEnvelopeRaw...}
function InternalRetrieveEnvelope(const AMsgNum: Integer; AMsg: TIdMessage; ADestList: TIdStringList): Boolean;
{CC6: Added to support UIDRetrieveEnvelopeRaw...}
function UIDInternalRetrieveEnvelope(const AMsgUID: String; AMsg: TIdMessage; ADestList: TIdStringList): Boolean;
{CCD: For getting the header of a part...}
function InternalRetrievePartHeader(const AMsgNum: Integer; const APartNum: string; const AUseUID: Boolean; AHeaders: TIdHeaderList): Boolean;
{CC: ReceiveHeader in IdMessageClient seems to have a very rare bug, maybe it
is missing the end marker occassionally. Moved up to here to add
debugging code, plus it can be converted to an IMAP byte-count retrieval
method if necessary.}
function ReceiveHeader(AMsg: TIdMessage; const AAltTerm: string = ''): string; override;
{CC3: Need to validate message numbers (relative and UIDs) and part numbers, because otherwise
the routines wait for a response that never arrives and so functions never return.
Also used for validating part numbers.}
function IsNumberValid(const ANumber: Integer): Boolean;
function IsUIDValid(const AUID: string): Boolean;
function IsImapPartNumberValid(const AUID: string): Boolean;
function IsItDigitsAndOptionallyPeriod(const AStr: string; AAllowPeriod: Boolean): Boolean;
{CC6: Override IdMessageClient's ReceiveBody due to the responses from some
servers...}
procedure ReceiveBody(AMsg: TIdMessage; const ADelim: string = '.'); override; {Do not Localize}
procedure InitComponent; override;
public
{ TIdIMAP4 Commands }
//Requests a listing of capabilities that the server supports.
function Capability(ASlCapability: TIdStrings): Boolean; overload;
function FindHowServerCreatesFolders: TIdIMAP4FolderTreatment;
procedure DoAlert (const AMsg: String);
property ConnectionState: TIdIMAP4ConnectionState read FConnectionState;
property MailBox: TIdMailBox read FMailBox write SetMailBox;
{CC7: Two versions of AppendMsg are provided. The first is the normal one you
would use. The second allows you to specify an alternative header list which
will be used in place of AMsg.Headers.
An email client may need the second type if it sends an email via IdSMTP and wants
to copy it to a "Sent" IMAP folder. In Indy 9, IdSMTP internally generates and
transmits the headers but does not keep them, so what you may need to do is to
subclass IdSMTP, override SendHeader so that the TIdHeaderList is returned (and
also override both SendMsg and Send to get it back to you), then use the
second version of AppendMsg to use the returned TIdHeaderList. In Indy 10,
IdSMTP puts the generated headers in the LastGeneratedHeaders field, so you
can use the second version of AppendMsg, passing it AMsg.LastGeneratedHeaders as
the AAlternativeHeaders field. Note that IdSMTP puts both the Headers and
the ExtraHeaders fields in LastGeneratedHeaders.}
function AppendMsg (const AMBName: String; AMsg: TIdMessage; const AFlags: TIdMessageFlagsSet = []): Boolean; overload;
function AppendMsg (const AMBName: String; AMsg: TIdMessage; AAlternativeHeaders: TIdHeaderList; const AFlags: TIdMessageFlagsSet = []): Boolean; overload;
function AppendMsgNoEncodeFromFile (const AMBName: String; ASourceFile: string; const AFlags: TIdMessageFlagsSet = []): Boolean;
function AppendMsgNoEncodeFromStream (const AMBName: String; AStream: TStream; const AFlags: TIdMessageFlagsSet = []): Boolean;
//The following are used for raw (unparsed) messages in a file or stream...
//Requests a checkpoint of the currently selected mailbox. Does NOTHING on most servers.
function CheckMailBox: Boolean;
//Checks if the message was read or not.
function CheckMsgSeen (const AMsgNum: Integer): Boolean;
//Method for logging in manually if you didn't login at connect
procedure Login; virtual;
//Connects and logins to the IMAP4 account.
procedure Connect(const AAndLogin: boolean = true); reintroduce; virtual;
//Closes the current selected mailbox in the account. {Do not Localize}
function CloseMailBox: Boolean;
//Creates a new mailbox with the specified name in the account. {Do not Localize}
function CreateMailBox (const AMBName: String): Boolean;
//Deletes the specified mailbox from the account. {Do not Localize}
function DeleteMailBox (const AMBName: String): Boolean;
//Marks messages for deletion, it will be deleted when the mailbox will be purged.
function DeleteMsgs(const AMsgNumList: array of Integer): Boolean;
destructor Destroy; override;
//Logouts and disconnects from the IMAP account.
procedure Disconnect; overload;
//Disconnect with a parameter for raising a Not Connected exception
procedure Disconnect(AImmediate: Boolean; const ARaiseExceptionIfNotCon : Boolean); reintroduce; overload;
//Examines the specified mailbox and inserts the results to the TIdMailBox provided. {Do not Localize}
function ExamineMailBox (const AMBName: String; AMB: TIdMailBox): Boolean;
//Expunges (deletes the marked files) the current selected mailbox in the account. {Do not Localize}
function ExpungeMailBox: Boolean;
//Sends a NOOP (No Operation) to keep the account connection with the server alive.
procedure KeepAlive;
//Returns a list of all the child mailboxes (one level down) to the mailbox supplied.
//This should be used when you fear that there are too many mailboxes and the listing of
//all of them could be time consuming, so this should be used to retrieve specific mailboxes.
function ListInferiorMailBoxes (AMailBoxList, AInferiorMailBoxList: TIdStringList): Boolean;
//Returns a list of all the mailboxes in the user account.
function ListMailBoxes (AMailBoxList: TIdStringList): Boolean;
//Returns a list of all the subscribed mailboxes in the user account.
function ListSubscribedMailBoxes (AMailBoxList: TIdStringList): Boolean;
//Renames the specified mailbox in the account. {Do not Localize}
function RenameMailBox (const AOldMBName, ANewMBName: String): Boolean;
//Searches the current selected mailbox for messages matching the SearchRec and
//returnes the results to the mailbox SearchResults array.
function SearchMailBox (const ASearchInfo: array of TIdIMAP4SearchRec): Boolean;
//Selects the current a mailbox in the account. {Do not Localize}
function SelectMailBox (const AMBName: String): Boolean;
//Retrieves the status of the indicated mailbox.
{CC2: It is pointless calling StatusMailBox with AStatusDataItems set to []
because you are asking the IMAP server to update none of the status flags.
Instead, if called with no AStatusDataItems specified, use the standard flags
returned by SelectMailBox, which allows the user to easily check if the mailbox
has changed. Overload the functions, since AStatusDataItems cannot be set
to nil.}
function StatusMailBox (const AMBName: String; AMB: TIdMailBox): Boolean; overload;
function StatusMailBox (const AMBName: String; AMB: TIdMailBox; const AStatusDataItems: array of TIdIMAP4StatusDataItem): Boolean; overload;
//Changes (adds or removes) message flags.
function StoreFlags (const AMsgNumList: array of Integer;
const AStoreMethod: TIdIMAP4StoreDataItem; const AFlags: TIdMessageFlagsSet): Boolean;
//Adds the specified mailbox name to the server's set of "active" or "subscribed" {Do not Localize}
//mailboxes as returned by the LSUB command.
function SubscribeMailBox (const AMBName: String): Boolean;
{CC8: Added CopyMsg, should have always been there...}
function CopyMsg (const AMsgNum: Integer; const AMBName: String): Boolean;
//Copies a message from the current selected mailbox to the specified mailbox. {Do not Localize}
function CopyMsgs (const AMsgNumList: array of Integer; const AMBName: String): Boolean;
//Retrieves a whole message while marking it read.
function Retrieve (const AMsgNum: Integer; AMsg: TIdMessage): Boolean;
//Retrieves a whole message "raw" and saves it to file, while marking it read.
function RetrieveNoDecodeToFile (const AMsgNum: Integer; ADestFile: string): Boolean;
function RetrieveNoDecodeToStream (const AMsgNum: Integer; AStream: TStream): Boolean;
//Retrieves all envelope of the selected mailbox to the specified TIdMessageCollection.
function RetrieveAllEnvelopes (AMsgList: TIdMessageCollection): Boolean;
//Retrieves all headers of the selected mailbox to the specified TIdMessageCollection.
function RetrieveAllHeaders (AMsgList: TIdMessageCollection): Boolean;
//Retrieves all messages of the selected mailbox to the specified TIdMessageCollection.
function RetrieveAllMsgs (AMsgList: TIdMessageCollection): Boolean;
//Retrieves the message envelope, parses it, and discards the envelope.
function RetrieveEnvelope (const AMsgNum: Integer; AMsg: TIdMessage): Boolean;
//Retrieves the message envelope into a TIdStringList but does NOT parse it.
function RetrieveEnvelopeRaw(const AMsgNum: Integer; ADestList: TIdStringList): Boolean;
//Returnes the message flag values.
function RetrieveFlags (const AMsgNum: Integer; var AFlags: TIdMessageFlagsSet): Boolean;
{CC2: Following added for retrieving individual parts of a message...}
function InternalRetrieveStructure(const AMsgNum: Integer; AMsg: TIdMessage; AParts: TIdImapMessageParts): Boolean;
//Retrieve only the message structure (this tells you what parts are in the message).
function RetrieveStructure(const AMsgNum: Integer; AMsg: TIdMessage): Boolean; overload;
function RetrieveStructure(const AMsgNum: Integer; AParts: TIdImapMessageParts): Boolean; overload;
{CC2: Following added for retrieving individual parts of a message...}
{Retrieve a specific individual part of a message where part is an integer (for backward compatibility)...}
function RetrievePart(const AMsgNum: Integer; const APartNum: Integer;
{$IFDEF DOTNET}
var ABuffer: TIdBytes;
{$ELSE}
var ABuffer: PChar;
{$ENDIF}
var ABufferLength: Integer; AContentTransferEncoding: string = 'text'): Boolean; overload; {Do not Localize}
{Retrieve a specific individual part of a message where part is an integer or sub-part like '2.3'...}
function RetrievePart(const AMsgNum: Integer; const APartNum: string;
{$IFDEF DOTNET}
var ABuffer: TIdBytes;
{$ELSE}
var ABuffer: PChar;
{$ENDIF}
var ABufferLength: Integer; AContentTransferEncoding: string = 'text'): Boolean; overload; {Do not Localize}
{CC2: Following added for retrieving individual parts of a message...}
{Retrieve a specific individual part of a message where part is an integer (for backward compatibility)...}
function RetrievePartPeek(const AMsgNum: Integer; const APartNum: Integer;
{$IFDEF DOTNET}
var ABuffer: TIdBytes;
{$ELSE}
var ABuffer: PChar;
{$ENDIF}
var ABufferLength: Integer; AContentTransferEncoding: string = 'text'): Boolean; overload; {Do not Localize}
{Retrieve a specific individual part of a message where part is an integer or sub-part like '2.3'...}
function RetrievePartPeek(const AMsgNum: Integer; const APartNum: string;
{$IFDEF DOTNET}
var ABuffer: TIdBytes;
{$ELSE}
var ABuffer: PChar;
{$ENDIF}
var ABufferLength: Integer; AContentTransferEncoding: string = 'text'): Boolean; overload; {Do not Localize}
{CC2: Following added for retrieving individual parts of a message...}
{Retrieve a specific individual part of a message where part is an integer (for backward compatibility)...}
function RetrievePartToFile(const AMsgNum: Integer; const APartNum: Integer;
ALength: Integer; ADestFileNameAndPath: string; AContentTransferEncoding: string): Boolean; overload;
{Retrieve a specific individual part of a message where part is an integer or sub-part like '2.3'...}
function RetrievePartToFile(const AMsgNum: Integer; const APartNum: string;
ALength: Integer; ADestFileNameAndPath: string; AContentTransferEncoding: string): Boolean; overload;
{CC2: Following added for retrieving individual parts of a message...}
{Retrieve a specific individual part of a message where part is an integer (for backward compatibility)...}
function RetrievePartToFilePeek(const AMsgNum: Integer; const APartNum: Integer;
ALength: Integer; ADestFileNameAndPath: string; AContentTransferEncoding: string): Boolean; overload;
{Retrieve a specific individual part of a message where part is an integer or sub-part like '2.3'...}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -