Source code of Windows XP (NT5)
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.

363 lines
9.2 KiB

  1. // user.cpp : Implementation of CSmtpadmApp and UserL registration.
  2. #include "stdafx.h"
  3. #include "smtpadm.h"
  4. #include "user.h"
  5. #include "oleutil.h"
  6. #include <lmapibuf.h>
  7. #include "smtpcmn.h"
  8. // Must define THIS_FILE_* macros to use SmtpCreateException()
  9. #define THIS_FILE_HELP_CONTEXT 0
  10. #define THIS_FILE_PROG_ID _T("Smtpadm.User.1")
  11. #define THIS_FILE_IID IID_ISmtpAdminUser
  12. #define DEFAULT_NEWSGROUP_NAME _T("")
  13. #define DEFAULT_NEWSGROUP_DESCRIPTION _T("")
  14. #define DEFAULT_NEWSGROUP_MODERATOR _T("")
  15. #define DEFAULT_NEWSGROUP_READONLY FALSE
  16. /////////////////////////////////////////////////////////////////////////////
  17. //
  18. //
  19. // Use a macro to define all the default methods
  20. //
  21. DECLARE_METHOD_IMPLEMENTATION_FOR_STANDARD_EXTENSION_INTERFACES(SmtpAdminUser, CSmtpAdminUser, IID_ISmtpAdminUser)
  22. STDMETHODIMP CSmtpAdminUser::InterfaceSupportsErrorInfo(REFIID riid)
  23. {
  24. static const IID* arr[] =
  25. {
  26. &IID_ISmtpAdminUser,
  27. };
  28. for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
  29. {
  30. if (InlineIsEqualGUID(*arr[i],riid))
  31. return S_OK;
  32. }
  33. return S_FALSE;
  34. }
  35. CSmtpAdminUser::CSmtpAdminUser ()
  36. // CComBSTR's are initialized to NULL by default.
  37. {
  38. m_lInboxSizeInMemory = 0;
  39. m_lInboxSizeInMsgNumber = 0;
  40. m_fAutoForward = FALSE;
  41. m_fLocal = TRUE;
  42. m_iadsImpl.SetService ( MD_SERVICE_NAME );
  43. m_iadsImpl.SetName ( _T("User") );
  44. m_iadsImpl.SetClass ( _T("IIsSmtpUser") );
  45. }
  46. CSmtpAdminUser::~CSmtpAdminUser ()
  47. {
  48. // All CComBSTR's are freed automatically.
  49. }
  50. //
  51. // IADs methods:
  52. //
  53. DECLARE_SIMPLE_IADS_IMPLEMENTATION(CSmtpAdminUser,m_iadsImpl)
  54. //////////////////////////////////////////////////////////////////////
  55. // Properties:
  56. //////////////////////////////////////////////////////////////////////
  57. // user property
  58. STDMETHODIMP CSmtpAdminUser::get_EmailId ( BSTR * pstrEmailId )
  59. {
  60. return StdPropertyGet ( m_strEmailId, pstrEmailId );
  61. }
  62. STDMETHODIMP CSmtpAdminUser::put_EmailId ( BSTR strEmailId )
  63. {
  64. return StdPropertyPut ( &m_strEmailId, strEmailId );
  65. }
  66. STDMETHODIMP CSmtpAdminUser::get_Domain ( BSTR * pstrDomain )
  67. {
  68. return StdPropertyGet ( m_strDomain, pstrDomain );
  69. }
  70. STDMETHODIMP CSmtpAdminUser::put_Domain ( BSTR strDomain )
  71. {
  72. return StdPropertyPut ( &m_strDomain, strDomain );
  73. }
  74. STDMETHODIMP CSmtpAdminUser::get_MailRoot ( BSTR * pstrMailRoot )
  75. {
  76. return StdPropertyGet ( m_strMailRoot, pstrMailRoot );
  77. }
  78. STDMETHODIMP CSmtpAdminUser::put_MailRoot ( BSTR strMailRoot )
  79. {
  80. return StdPropertyPut ( &m_strMailRoot, strMailRoot );
  81. }
  82. STDMETHODIMP CSmtpAdminUser::get_InboxSizeInMemory ( long * plInboxSizeInMemory )
  83. {
  84. return StdPropertyGet ( m_lInboxSizeInMemory, plInboxSizeInMemory );
  85. }
  86. STDMETHODIMP CSmtpAdminUser::put_InboxSizeInMemory ( long lInboxSizeInMemory )
  87. {
  88. return StdPropertyPut ( &m_lInboxSizeInMemory, lInboxSizeInMemory );
  89. }
  90. STDMETHODIMP CSmtpAdminUser::get_InboxSizeInMsgNumber ( long * plInboxSizeInMsgNumber )
  91. {
  92. return StdPropertyGet ( m_lInboxSizeInMsgNumber, plInboxSizeInMsgNumber );
  93. }
  94. STDMETHODIMP CSmtpAdminUser::put_InboxSizeInMsgNumber ( long lInboxSizeInMsgNumber )
  95. {
  96. return StdPropertyPut ( &m_lInboxSizeInMsgNumber, lInboxSizeInMsgNumber );
  97. }
  98. STDMETHODIMP CSmtpAdminUser::get_AutoForward ( BOOL * pfAutoForward )
  99. {
  100. return StdPropertyGet ( m_fAutoForward, pfAutoForward );
  101. }
  102. STDMETHODIMP CSmtpAdminUser::put_AutoForward ( BOOL fAutoForward )
  103. {
  104. return StdPropertyPut ( &m_fAutoForward, fAutoForward );
  105. }
  106. STDMETHODIMP CSmtpAdminUser::get_ForwardEmail ( BSTR * pstrForwardEmail )
  107. {
  108. return StdPropertyGet ( m_strForwardEmail, pstrForwardEmail );
  109. }
  110. STDMETHODIMP CSmtpAdminUser::put_ForwardEmail ( BSTR strForwardEmail )
  111. {
  112. return StdPropertyPut ( &m_strForwardEmail, strForwardEmail );
  113. }
  114. //////////////////////////////////////////////////////////////////////
  115. // Methods:
  116. //////////////////////////////////////////////////////////////////////
  117. STDMETHODIMP CSmtpAdminUser::Default ( )
  118. {
  119. TraceFunctEnter ( "CSmtpAdminUser::Default" );
  120. /*
  121. m_strNewsgroup = DEFAULT_NEWSGROUP_NAME;
  122. m_strDescription = DEFAULT_NEWSGROUP_DESCRIPTION;
  123. m_strModerator = DEFAULT_NEWSGROUP_MODERATOR;
  124. m_fReadOnly = DEFAULT_NEWSGROUP_READONLY;
  125. if (
  126. !m_strNewsgroup ||
  127. !m_strDescription ||
  128. !m_strModerator
  129. ) {
  130. FatalTrace ( (LPARAM) this, "Out of memory" );
  131. return E_OUTOFMEMORY;
  132. }
  133. */
  134. TraceFunctLeave ();
  135. return NOERROR;
  136. }
  137. STDMETHODIMP CSmtpAdminUser::Create ( )
  138. {
  139. TraceFunctEnter ( "CSmtpAdminUser::Create" );
  140. HRESULT hr = NOERROR;
  141. DWORD dwErr = NOERROR;
  142. if( !m_strEmailId || !m_strDomain )
  143. {
  144. hr = SmtpCreateException ( IDS_SMTPEXCEPTION_INVALID_ADDRESS );
  145. goto Exit;
  146. }
  147. WCHAR szUserName[512];
  148. WCHAR* lpForward;
  149. wsprintfW( szUserName, L"%s@%s", (LPWSTR) m_strEmailId, (LPWSTR) m_strDomain );
  150. if( m_strForwardEmail && m_strForwardEmail[0] )
  151. {
  152. lpForward = m_strForwardEmail;
  153. }
  154. else
  155. {
  156. lpForward = NULL;
  157. }
  158. dwErr = SmtpCreateUser(
  159. m_iadsImpl.QueryComputer(),
  160. szUserName,
  161. lpForward,
  162. m_fLocal,
  163. m_lInboxSizeInMemory,
  164. m_lInboxSizeInMsgNumber,
  165. m_strMailRoot,
  166. m_iadsImpl.QueryInstance() );
  167. if ( dwErr != NOERROR ) {
  168. ErrorTrace ( (LPARAM) this, "Failed to add user: %x", dwErr );
  169. hr = SmtpCreateExceptionFromWin32Error ( dwErr );
  170. goto Exit;
  171. }
  172. Exit:
  173. TraceFunctLeave ();
  174. return hr;
  175. }
  176. STDMETHODIMP CSmtpAdminUser::Delete ( )
  177. {
  178. TraceFunctEnter ( "CSmtpAdminUser::Delete" );
  179. HRESULT hr = NOERROR;
  180. DWORD dwErr = NOERROR;
  181. if( !m_strEmailId || !m_strDomain )
  182. {
  183. hr = SmtpCreateException ( IDS_SMTPEXCEPTION_INVALID_ADDRESS );
  184. goto Exit;
  185. }
  186. WCHAR szUserName[512];
  187. wsprintfW( szUserName, L"%s@%s", (LPWSTR) m_strEmailId, (LPWSTR) m_strDomain );
  188. dwErr = SmtpDeleteUser(
  189. m_iadsImpl.QueryComputer(),
  190. szUserName,
  191. m_iadsImpl.QueryInstance() );
  192. if ( dwErr != NOERROR ) {
  193. ErrorTrace ( (LPARAM) this, "Failed to delete user: %x", dwErr );
  194. hr = SmtpCreateExceptionFromWin32Error ( dwErr );
  195. goto Exit;
  196. }
  197. Exit:
  198. TraceFunctLeave ();
  199. return hr;
  200. }
  201. STDMETHODIMP CSmtpAdminUser::Get ( )
  202. {
  203. TraceFunctEnter ( "CSmtpAdminUser::Get" );
  204. HRESULT hr = NOERROR;
  205. DWORD dwErr = NOERROR;
  206. LPSMTP_USER_PROPS pUserProps = NULL;
  207. if( !m_strEmailId || !m_strDomain )
  208. {
  209. hr = SmtpCreateException ( IDS_SMTPEXCEPTION_INVALID_ADDRESS );
  210. goto Exit;
  211. }
  212. WCHAR szUserName[512];
  213. wsprintfW( szUserName, L"%s@%s", (LPWSTR) m_strEmailId, (LPWSTR) m_strDomain );
  214. dwErr = SmtpGetUserProps(
  215. m_iadsImpl.QueryComputer(),
  216. szUserName,
  217. &pUserProps,
  218. m_iadsImpl.QueryInstance() );
  219. if ( dwErr != NOERROR ) {
  220. ErrorTrace ( (LPARAM) this, "Failed to get user prop: %x", dwErr );
  221. hr = SmtpCreateExceptionFromWin32Error ( dwErr );
  222. goto Exit;
  223. }
  224. // free old entry
  225. m_strMailRoot.Empty();
  226. m_strForwardEmail.Empty();
  227. m_strMailRoot = pUserProps->wszVRoot;
  228. m_strForwardEmail = pUserProps->wszForward;
  229. m_fAutoForward = (!m_strForwardEmail || !m_strForwardEmail.Length()) ? FALSE : TRUE;
  230. m_lInboxSizeInMemory = pUserProps->dwMailboxMax;
  231. m_lInboxSizeInMsgNumber = pUserProps->dwMailboxMessageMax;
  232. m_fLocal = !!pUserProps->dwLocal;
  233. // free pUserProps
  234. if ( pUserProps )
  235. {
  236. ::NetApiBufferFree ( pUserProps );
  237. }
  238. Exit:
  239. TraceFunctLeave ();
  240. return hr;
  241. }
  242. STDMETHODIMP CSmtpAdminUser::Set ( )
  243. {
  244. TraceFunctEnter ( "CSmtpAdminUser::Set" );
  245. HRESULT hr = NOERROR;
  246. DWORD dwErr = NOERROR;
  247. SMTP_USER_PROPS UserProps;
  248. if( !m_strEmailId || !m_strDomain )
  249. {
  250. hr = SmtpCreateException ( IDS_SMTPEXCEPTION_INVALID_ADDRESS );
  251. goto Exit;
  252. }
  253. WCHAR szUserName[512];
  254. wsprintfW( szUserName, L"%s@%s", (LPWSTR) m_strEmailId, (LPWSTR) m_strDomain );
  255. UserProps.fc = FC_SMTP_USER_PROPS_ALL;
  256. if( !m_strForwardEmail || !m_strForwardEmail.Length() )
  257. {
  258. UserProps.wszForward = NULL;
  259. UserProps.fc -= FC_SMTP_USER_PROPS_FORWARD;
  260. }
  261. else
  262. {
  263. UserProps.wszForward = (LPWSTR)m_strForwardEmail;
  264. }
  265. UserProps.wszVRoot = (LPWSTR)m_strMailRoot;
  266. UserProps.dwMailboxMax = m_lInboxSizeInMemory;
  267. UserProps.dwMailboxMessageMax = m_lInboxSizeInMsgNumber;
  268. UserProps.dwLocal = m_fLocal;
  269. dwErr = SmtpSetUserProps(
  270. m_iadsImpl.QueryComputer(),
  271. szUserName,
  272. &UserProps,
  273. m_iadsImpl.QueryInstance() );
  274. if ( dwErr != NOERROR ) {
  275. ErrorTrace ( (LPARAM) this, "Failed to set user prop: %x", dwErr );
  276. hr = SmtpCreateExceptionFromWin32Error ( dwErr );
  277. goto Exit;
  278. }
  279. Exit:
  280. TraceFunctLeave ();
  281. return hr;
  282. }