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.

387 lines
8.5 KiB

  1. // dl.cpp : Implementation of CsmtpadmApp and DLL registration.
  2. #include "stdafx.h"
  3. #include "smtpadm.h"
  4. #include "dl.h"
  5. #include "oleutil.h"
  6. #include "smtpapi.h"
  7. #include <lmapibuf.h>
  8. #include "smtpcmn.h"
  9. // Must define THIS_FILE_* macros to use SmtpCreateException()
  10. #define THIS_FILE_HELP_CONTEXT 0
  11. #define THIS_FILE_PROG_ID _T("Smtpadm.DL.1")
  12. #define THIS_FILE_IID IID_ISmtpAdminDL
  13. #define DEFAULT_NEWSGROUP_NAME _T("")
  14. #define DEFAULT_NEWSGROUP_DESCRIPTION _T("")
  15. #define DEFAULT_NEWSGROUP_MODERATOR _T("")
  16. #define DEFAULT_NEWSGROUP_READONLY FALSE
  17. /////////////////////////////////////////////////////////////////////////////
  18. //
  19. //
  20. // Use a macro to define all the default methods
  21. //
  22. DECLARE_METHOD_IMPLEMENTATION_FOR_STANDARD_EXTENSION_INTERFACES(SmtpAdminDL, CSmtpAdminDL, IID_ISmtpAdminDL)
  23. STDMETHODIMP CSmtpAdminDL::InterfaceSupportsErrorInfo(REFIID riid)
  24. {
  25. static const IID* arr[] =
  26. {
  27. &IID_ISmtpAdminDL,
  28. };
  29. for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
  30. {
  31. if (InlineIsEqualGUID(*arr[i],riid))
  32. return S_OK;
  33. }
  34. return S_FALSE;
  35. }
  36. CSmtpAdminDL::CSmtpAdminDL ()
  37. // CComBSTR's are initialized to NULL by default.
  38. {
  39. m_pSmtpNameList = NULL;
  40. m_lType = NAME_TYPE_LIST_NORMAL;
  41. m_lCount = 0;
  42. m_lMemberType = NAME_TYPE_USER;
  43. m_iadsImpl.SetService ( MD_SERVICE_NAME );
  44. m_iadsImpl.SetName ( _T("DL") );
  45. m_iadsImpl.SetClass ( _T("IIsSmtpDL") );
  46. }
  47. CSmtpAdminDL::~CSmtpAdminDL ()
  48. {
  49. if ( m_pSmtpNameList ) {
  50. ::NetApiBufferFree ( m_pSmtpNameList );
  51. m_pSmtpNameList = NULL;
  52. }
  53. // All CComBSTR's are freed automatically.
  54. }
  55. //////////////////////////////////////////////////////////////////////
  56. // Properties:
  57. //////////////////////////////////////////////////////////////////////
  58. //
  59. // IADs methods:
  60. //
  61. DECLARE_SIMPLE_IADS_IMPLEMENTATION(CSmtpAdminDL,m_iadsImpl)
  62. // DL property
  63. STDMETHODIMP CSmtpAdminDL::get_DLName ( BSTR * pstrDLName )
  64. {
  65. return StdPropertyGet ( m_strDLName, pstrDLName );
  66. }
  67. STDMETHODIMP CSmtpAdminDL::put_DLName ( BSTR strDLName )
  68. {
  69. return StdPropertyPut ( &m_strDLName, strDLName );
  70. }
  71. STDMETHODIMP CSmtpAdminDL::get_Domain ( BSTR * pstrDomain )
  72. {
  73. return StdPropertyGet ( m_strDomain, pstrDomain );
  74. }
  75. STDMETHODIMP CSmtpAdminDL::put_Domain ( BSTR strDomain )
  76. {
  77. return StdPropertyPut ( &m_strDomain, strDomain );
  78. }
  79. STDMETHODIMP CSmtpAdminDL::get_Type ( long * plType )
  80. {
  81. return StdPropertyGet ( m_lType, plType );
  82. }
  83. STDMETHODIMP CSmtpAdminDL::put_Type ( long lType )
  84. {
  85. return StdPropertyPut ( &m_lType, lType );
  86. }
  87. STDMETHODIMP CSmtpAdminDL::get_MemberName ( BSTR * pstrMemberName )
  88. {
  89. return StdPropertyGet ( m_strMemberName, pstrMemberName );
  90. }
  91. STDMETHODIMP CSmtpAdminDL::put_MemberName ( BSTR strMemberName )
  92. {
  93. return StdPropertyPut ( &m_strMemberName, strMemberName );
  94. }
  95. STDMETHODIMP CSmtpAdminDL::get_MemberDomain ( BSTR * pstrMemberDomain )
  96. {
  97. return StdPropertyGet ( m_strMemberDomain, pstrMemberDomain );
  98. }
  99. STDMETHODIMP CSmtpAdminDL::put_MemberDomain ( BSTR strMemberDomain )
  100. {
  101. return StdPropertyPut ( &m_strMemberDomain, strMemberDomain );
  102. }
  103. STDMETHODIMP CSmtpAdminDL::get_MemberType( long * plMemberType )
  104. {
  105. return StdPropertyGet ( m_lMemberType, plMemberType );
  106. }
  107. // enumeration
  108. STDMETHODIMP CSmtpAdminDL::get_Count ( long * plCount )
  109. {
  110. return StdPropertyGet ( m_lCount, plCount );
  111. }
  112. //////////////////////////////////////////////////////////////////////
  113. // Methods:
  114. //////////////////////////////////////////////////////////////////////
  115. STDMETHODIMP CSmtpAdminDL::Create ( )
  116. {
  117. TraceFunctEnter ( "CSmtpAdminDL::Create" );
  118. HRESULT hr = NOERROR;
  119. DWORD dwErr = NOERROR;
  120. if( !m_strDLName || !m_strDomain )
  121. {
  122. hr = SmtpCreateException ( IDS_SMTPEXCEPTION_INVALID_ADDRESS );
  123. goto Exit;
  124. }
  125. WCHAR szFullName[512];
  126. wsprintfW( szFullName, L"%s@%s", (LPWSTR) m_strDLName, (LPWSTR) m_strDomain );
  127. dwErr = SmtpCreateDistList(
  128. m_iadsImpl.QueryComputer(),
  129. szFullName,
  130. m_lType,
  131. m_iadsImpl.QueryInstance() );
  132. if ( dwErr != NOERROR ) {
  133. ErrorTrace ( (LPARAM) this, "Failed to create DL: %x", dwErr );
  134. hr = SmtpCreateExceptionFromWin32Error ( dwErr );
  135. goto Exit;
  136. }
  137. Exit:
  138. TraceFunctLeave ();
  139. return hr;
  140. }
  141. STDMETHODIMP CSmtpAdminDL::Delete ( )
  142. {
  143. TraceFunctEnter ( "CSmtpAdminDL::Delete" );
  144. HRESULT hr = NOERROR;
  145. DWORD dwErr = NOERROR;
  146. if( !m_strDLName || !m_strDomain )
  147. {
  148. hr = SmtpCreateException ( IDS_SMTPEXCEPTION_INVALID_ADDRESS );
  149. goto Exit;
  150. }
  151. WCHAR szFullName[512];
  152. wsprintfW( szFullName, L"%s@%s", (LPWSTR) m_strDLName, (LPWSTR) m_strDomain );
  153. dwErr = SmtpDeleteDistList(
  154. m_iadsImpl.QueryComputer(),
  155. szFullName,
  156. m_iadsImpl.QueryInstance() );
  157. if ( dwErr != NOERROR ) {
  158. ErrorTrace ( (LPARAM) this, "Failed to delete DL: %x", dwErr );
  159. hr = SmtpCreateExceptionFromWin32Error ( dwErr );
  160. goto Exit;
  161. }
  162. Exit:
  163. TraceFunctLeave ();
  164. return hr;
  165. }
  166. STDMETHODIMP CSmtpAdminDL::AddMember ( )
  167. {
  168. TraceFunctEnter ( "CSmtpAdminDL::AddMember" );
  169. HRESULT hr = NOERROR;
  170. DWORD dwErr = NOERROR;
  171. if( !m_strDLName || !m_strDomain || !m_strMemberName || !m_strMemberDomain )
  172. {
  173. hr = SmtpCreateException ( IDS_SMTPEXCEPTION_INVALID_ADDRESS );
  174. goto Exit;
  175. }
  176. WCHAR szFullDLName[512];
  177. WCHAR szFullMemName[512];
  178. wsprintfW( szFullDLName, L"%s@%s", (LPWSTR) m_strDLName, (LPWSTR) m_strDomain );
  179. wsprintfW( szFullMemName, L"%s@%s", (LPWSTR) m_strMemberName, (LPWSTR) m_strMemberDomain );
  180. dwErr = SmtpCreateDistListMember(
  181. m_iadsImpl.QueryComputer(),
  182. szFullDLName,
  183. szFullMemName,
  184. m_iadsImpl.QueryInstance() );
  185. if ( dwErr != NOERROR ) {
  186. ErrorTrace ( (LPARAM) this, "Failed to add DL member: %x", dwErr );
  187. hr = SmtpCreateExceptionFromWin32Error ( dwErr );
  188. goto Exit;
  189. }
  190. Exit:
  191. TraceFunctLeave ();
  192. return hr;
  193. }
  194. STDMETHODIMP CSmtpAdminDL::RemoveMember ( )
  195. {
  196. TraceFunctEnter ( "CSmtpAdminDL::RemoveMember" );
  197. HRESULT hr = NOERROR;
  198. DWORD dwErr = NOERROR;
  199. if( !m_strDLName || !m_strDomain || !m_strMemberName || !m_strMemberDomain )
  200. {
  201. hr = SmtpCreateException ( IDS_SMTPEXCEPTION_INVALID_ADDRESS );
  202. goto Exit;
  203. }
  204. WCHAR szFullDLName[512];
  205. WCHAR szFullMemName[512];
  206. wsprintfW( szFullDLName, L"%s@%s", (LPWSTR) m_strDLName, (LPWSTR) m_strDomain );
  207. wsprintfW( szFullMemName, L"%s@%s", (LPWSTR) m_strMemberName, (LPWSTR) m_strMemberDomain );
  208. dwErr = SmtpDeleteDistListMember(
  209. m_iadsImpl.QueryComputer(),
  210. szFullDLName,
  211. szFullMemName,
  212. m_iadsImpl.QueryInstance() );
  213. if ( dwErr != NOERROR ) {
  214. ErrorTrace ( (LPARAM) this, "Failed to remove DL member: %x", dwErr );
  215. hr = SmtpCreateExceptionFromWin32Error ( dwErr );
  216. goto Exit;
  217. }
  218. Exit:
  219. TraceFunctLeave ();
  220. return hr;
  221. }
  222. STDMETHODIMP CSmtpAdminDL::FindMembers( BSTR strWildmat,
  223. long cMaxResults
  224. )
  225. {
  226. TraceFunctEnter ( "CSmtpAdminDL::FindMembers" );
  227. HRESULT hr = NOERROR;
  228. DWORD dwErr = NOERROR;
  229. if( !m_strDLName || !m_strDomain )
  230. {
  231. hr = SmtpCreateException ( IDS_SMTPEXCEPTION_INVALID_ADDRESS );
  232. goto Exit;
  233. }
  234. // Free the old name list:
  235. if ( m_pSmtpNameList ) {
  236. ::NetApiBufferFree ( m_pSmtpNameList );
  237. m_pSmtpNameList = NULL;
  238. }
  239. m_lCount = 0;
  240. WCHAR szFullDLName[512];
  241. wsprintfW( szFullDLName, L"%s@%s", (LPWSTR) m_strDLName, (LPWSTR) m_strDomain );
  242. dwErr = SmtpGetNameListFromList (
  243. m_iadsImpl.QueryComputer(),
  244. szFullDLName,
  245. strWildmat,
  246. NAME_TYPE_ALL,
  247. cMaxResults,
  248. TRUE,
  249. &m_pSmtpNameList,
  250. m_iadsImpl.QueryInstance());
  251. if ( dwErr != 0 ) {
  252. ErrorTraceX ( (LPARAM) this, "Failed to find alias: %x", dwErr );
  253. SetLastError( dwErr );
  254. hr = SmtpCreateExceptionFromWin32Error ( dwErr );
  255. goto Exit;
  256. }
  257. m_lCount = m_pSmtpNameList->cEntries;
  258. Exit:
  259. TraceFunctLeave ();
  260. return hr;
  261. }
  262. STDMETHODIMP CSmtpAdminDL::GetNthMember ( long lIndex )
  263. {
  264. TraceFunctEnter ( "CSmtpAdminDL::GetNthMember" );
  265. WCHAR* pchStartOfDomain = NULL;
  266. WCHAR* p = NULL;
  267. LPSMTP_NAME_ENTRY pNameEntry;
  268. if( !m_pSmtpNameList )
  269. {
  270. return SmtpCreateException (IDS_SMTPEXCEPTION_DIDNT_ENUMERATE);
  271. }
  272. if( lIndex < 0 || lIndex >= m_lCount )
  273. {
  274. return SmtpCreateException (IDS_SMTPEXCEPTION_INVALID_INDEX);
  275. }
  276. //_ASSERT( CAddr::ValidateEmailName(m_pSmtpNameList[lIndex].lpszName) );
  277. pNameEntry = &m_pSmtpNameList->aNameEntry[lIndex];
  278. p = pNameEntry->lpszName;
  279. while( *p && *p != '@' ) p++;
  280. _ASSERT( *p );
  281. if( !*p )
  282. {
  283. return SmtpCreateException (IDS_SMTPEXCEPTION_INVALID_ADDRESS);
  284. }
  285. pchStartOfDomain = p+1;
  286. m_lMemberType = pNameEntry->dwType;
  287. m_strMemberDomain = (LPCWSTR) pchStartOfDomain;
  288. *(pchStartOfDomain-1) = '\0';
  289. m_strMemberName = pNameEntry->lpszName; // converted to UNICODE
  290. *(pchStartOfDomain-1) = '@'; // turn it back
  291. return NOERROR;
  292. }