Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
5.6 KiB

  1. /************************************************************************************************
  2. Copyright (c) 2001 Microsoft Corporation
  3. File Name: MailBox.h
  4. Abstract: Defines the CMailBox class, as abstraction of mailbox storage
  5. Notes:
  6. History: 08/01/2001 Created by Hao Yu (haoyu)
  7. ************************************************************************************************/
  8. #ifndef __POP3_MAILBOX_H__
  9. #define __POP3_MAILBOX_H__
  10. #include <POP3Server.h>
  11. #include <IOContext.h>
  12. #include <vector>
  13. #define INIT_MAIL_COUNT 256
  14. #define MAIL_STATUS_NONE 0
  15. #define MAIL_STATUS_DEL 1
  16. #define NO_PENDING_OP 0
  17. #define DEL_PENDING 1
  18. #define MAX_MAIL_PER_DOWNLOAD 1024
  19. #define LOCAL_FILE_BUFFER_SIZE 4096
  20. #define ERROR_NO_FILE_ATTR 0xffffffff
  21. #define ERR_NO_SUCH_MSG 0xf0000001
  22. #define ERR_MSG_ALREADY_DELETED 0xf0000002
  23. #define ERR_CAN_NOT_OPEN_FILE 0xf0000003
  24. #define ERR_CAN_NOT_SET_FILE_CURSOR 0xf0000004
  25. #define RESP_END_OF_MULTILINE "\r\n.\r\n"
  26. #define DEFAULT_MAIL_VECTOR_SIZE 512
  27. #define LOCK_FILENAME_A "Lock"
  28. #define LOCK_FILENAME_W L"Lock"
  29. #define QUOTA_FILENAME_A "Quota"
  30. #define QUOTA_FILENAME_W L"Quota"
  31. #define MAILBOX_PREFIX_A "P3_"
  32. #define MAILBOX_PREFIX_W L"P3_"
  33. #define MAILBOX_EXTENSION_A ".mbx"
  34. #define MAILBOX_EXTENSION_W L".mbx"
  35. #define MAILBOX_EXTENSION2_A ".tmp"
  36. #define MAILBOX_EXTENSION2_W L".tmp"
  37. #ifdef UNICODE
  38. #define LOCK_FILENAME LOCK_FILENAME_W
  39. #define QUOTA_FILENAME QUOTA_FILENAME_W
  40. #define MAILBOX_PREFIX MAILBOX_PREFIX_W
  41. #define MAILBOX_EXTENSION MAILBOX_EXTENSION_W
  42. #define MAILBOX_EXTENSION2 MAILBOX_EXTENSION2_W
  43. #else
  44. #define LOCK_FILENAME LOCK_FILENAME_A
  45. #define QUOTA_FILENAME QUOTA_FILENAME_A
  46. #define MAILBOX_PREFIX MAILBOX_PREFIX_A
  47. #define MAILBOX_EXTENSION MAILBOX_EXTENSION_A
  48. #define MAILBOX_EXTENSION2 MAILBOX_EXTENSION2_A
  49. #endif // !UNICODE
  50. template<class _Ty>
  51. class RockallAllocator:public std::allocator<_Ty>
  52. {
  53. public:
  54. pointer allocate(size_type _N, const void *)
  55. {
  56. return (pointer) _Charalloc(_N * sizeof(_Ty));
  57. }
  58. char * _Charalloc(size_type _N)
  59. {
  60. return (new char[_N]);
  61. }
  62. void deallocate(void * _P, size_type)
  63. {
  64. delete[] _P;
  65. }
  66. };
  67. struct MAIL_ITEM
  68. {
  69. DWORD dwStatus;
  70. HANDLE hFile;
  71. DWORD dwFileSize;
  72. BSTR bstrFileName;
  73. };
  74. typedef MAIL_ITEM *PMAIL_ITEM;
  75. class CMailBox
  76. {
  77. DWORD m_dwSizeOfMailVector;
  78. PMAIL_ITEM *m_MailVector;
  79. DWORD m_cMailCount; // Mail acturally stored in the box
  80. DWORD m_dwShowMailCount; // Mail that not marked as to be deleted
  81. DWORD m_dwTotalSize;
  82. HANDLE m_hMailBoxLock;
  83. WCHAR m_wszMailBoxPath[POP3_MAX_PATH];
  84. BOOL m_bMailBoxOpened;
  85. static long m_lMailRootGuard;
  86. static WCHAR m_wszMailRoot[POP3_MAX_MAILROOT_LENGTH];
  87. public:
  88. CMailBox();
  89. ~CMailBox();
  90. // Mailbox
  91. DWORD GetTotalSize()
  92. {
  93. return m_dwTotalSize;
  94. }
  95. DWORD GetCurrentMailCount()
  96. {
  97. return m_dwShowMailCount;
  98. }
  99. DWORD GetMailCount()
  100. {
  101. return m_cMailCount;
  102. }
  103. void CloseMailBox();
  104. bool CreateMailBox(WCHAR *wszEmailAddr);
  105. LPWSTR GetMailboxFromStoreNameW( LPWSTR psStoreName );
  106. bool GetEncyptedPassword( LPBYTE pbBuffer, const DWORD dwBufferSize, LPDWORD pdwBytesRead );
  107. bool SetEncyptedPassword( LPBYTE pbBuffer, const DWORD dwBytesToWrite, LPDWORD pdwBytesWritten );
  108. //Check the number of mails in the mailbox
  109. BOOL EnumerateMailBox(DWORD dwMaxMsg=0);
  110. BOOL LockMailBox();
  111. bool isMailboxInUse();
  112. // Check existance of mailbox
  113. BOOL OpenMailBox(WCHAR *wszEmailAddr);
  114. BOOL RepairMailBox();
  115. void UnlockMailBox();
  116. // Mail
  117. //Close a newly create mail file and rename it
  118. //so the pop3 service and find it
  119. DWORD CloseMail(HANDLE hMailFile, DWORD dwFlagsAndAttributes = 0);
  120. //Create a new emplty mail file and return the name
  121. HANDLE CreateMail(LPWSTR wszTargetFileName, DWORD dwFlagsAndAttributes = 0 );
  122. DWORD DeleteMail(int iIndex);
  123. bool DeleteMail(LPWSTR wszTargetFileName); // Used to delete a file that was created by CreateMail but then was not successfully delivered
  124. bool GetMailFileName( int iIndex, LPWSTR psFilename, DWORD dwSize );
  125. DWORD ListMail(int iIndex, char *szBuf, DWORD dwSize);
  126. DWORD TransmitMail(IO_CONTEXT *pIoContext, int iIndex, int iLines=-1);
  127. DWORD TransmitMail(SOCKET hSocket, int iIndex);
  128. DWORD UidlMail(int iIndex, char *szBuf, DWORD dwSize);
  129. bool BuildFilePath( LPWSTR psFilePathBuffer, LPWSTR psFileName, DWORD dwSizeOfFilePathBuffer );
  130. // Other
  131. BOOL CommitAndClose();
  132. DWORD GetHashedPassword(char *strPswdBuf, DWORD *pcbBufSize);
  133. DWORD SetHashedPassword(char *strPswd, DWORD *pcbPswdSize);
  134. void QuitAndClose();
  135. void Reset();
  136. //Set the MailRoot dir, if szMailRoot is NULL,
  137. //the function searches the registry,
  138. //if the registry is empty, it returns FALSE
  139. static LPCWSTR GetMailRoot(){ if ( 0x0 == m_wszMailRoot[0] ) { SetMailRoot(); } return m_wszMailRoot; }
  140. static BOOL SetMailRoot(const WCHAR *wszMailRoot=NULL);
  141. protected:
  142. bool SetMailBoxPath(WCHAR *wszEmailAddr);
  143. bool ReadTopLines(int iLines, HANDLE hFile, DWORD *pdwBytesToRead);
  144. bool PushMailToVector(PMAIL_ITEM pMail);
  145. };
  146. #endif //__POP3_MAILBOX_H__