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.

275 lines
6.4 KiB

  1. // --------------------------------------------------------------------------------
  2. // Mimeutil.cpp
  3. // Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  4. // --------------------------------------------------------------------------------
  5. #include "pch.hxx"
  6. #include "resource.h"
  7. #include "dllmain.h"
  8. #include "mimeutil.h"
  9. #include "demand.h"
  10. HRESULT HrHasBodyParts(LPMIMEMESSAGE pMsg)
  11. {
  12. DWORD dwFlags=0;
  13. if (pMsg)
  14. pMsg->GetFlags(&dwFlags);
  15. return (dwFlags&IMF_HTML || dwFlags & IMF_PLAIN)? S_OK : S_FALSE;
  16. }
  17. HRESULT HrHasEncodedBodyParts(LPMIMEMESSAGE pMsg, ULONG cBody, LPHBODY rghBody)
  18. {
  19. ULONG uBody;
  20. if (cBody==0 || rghBody==NULL)
  21. return S_FALSE;
  22. for (uBody=0; uBody<cBody; uBody++)
  23. {
  24. if (HrIsBodyEncoded(pMsg, rghBody[uBody])==S_OK)
  25. return S_OK;
  26. }
  27. return S_FALSE;
  28. }
  29. /*
  30. * looks for non-7bit or non-8bit encoding
  31. */
  32. HRESULT HrIsBodyEncoded(LPMIMEMESSAGE pMsg, HBODY hBody)
  33. {
  34. LPSTR lpsz;
  35. HRESULT hr=S_FALSE;
  36. if (!FAILED(MimeOleGetBodyPropA(pMsg, hBody, PIDTOSTR(PID_HDR_CNTXFER), NOFLAGS, &lpsz)))
  37. {
  38. if (lstrcmpi(lpsz, STR_ENC_7BIT)!=0 && lstrcmpi(lpsz, STR_ENC_8BIT)!=0)
  39. hr=S_OK;
  40. SafeMimeOleFree(lpsz);
  41. }
  42. return hr;
  43. }
  44. // sizeof(lspzBuffer) needs to be == or > CCHMAX_CSET_NAME
  45. HRESULT HrGetMetaTagName(HCHARSET hCharset, LPSTR lpszBuffer, DWORD cchSize)
  46. {
  47. INETCSETINFO rCsetInfo;
  48. CODEPAGEINFO rCodePage;
  49. HRESULT hr;
  50. LPSTR psz;
  51. if (hCharset == NULL)
  52. return E_INVALIDARG;
  53. hr = MimeOleGetCharsetInfo(hCharset, &rCsetInfo);
  54. if (FAILED(hr))
  55. goto error;
  56. hr = MimeOleGetCodePageInfo(rCsetInfo.cpiInternet, &rCodePage);
  57. if (FAILED(hr))
  58. goto error;
  59. psz = rCodePage.szWebCset;
  60. if (FIsEmpty(psz)) // if no webset, try the body cset
  61. psz = rCodePage.szBodyCset;
  62. if (FIsEmpty(psz))
  63. {
  64. hr = E_FAIL;
  65. goto error;
  66. }
  67. StrCpyN(lpszBuffer, psz, cchSize);
  68. error:
  69. return hr;
  70. }
  71. HRESULT HrIsInRelatedSection(LPMIMEMESSAGE pMsg, HBODY hBody)
  72. {
  73. HBODY hBodyParent;
  74. if (!FAILED(pMsg->GetBody(IBL_PARENT, hBody, &hBodyParent)) &&
  75. (pMsg->IsContentType(hBodyParent, STR_CNT_MULTIPART, STR_SUB_RELATED)==S_OK))
  76. return S_OK;
  77. else
  78. return S_FALSE;
  79. }
  80. HRESULT HrMarkGhosted(LPMIMEMESSAGE pMsg, HBODY hBody)
  81. {
  82. PROPVARIANT pv;
  83. Assert (pMsg && hBody);
  84. pv.vt = VT_I4;
  85. pv.lVal = TRUE;
  86. return pMsg->SetBodyProp(hBody, PIDTOSTR(PID_ATT_GHOSTED), NOFLAGS, &pv);
  87. }
  88. HRESULT HrIsReferencedUrl(LPMIMEMESSAGE pMsg, HBODY hBody)
  89. {
  90. PROPVARIANT rVariant;
  91. rVariant.vt = VT_I4;
  92. if (!FAILED(pMsg->GetBodyProp(hBody, PIDTOSTR(PID_ATT_RENDERED), NOFLAGS, &rVariant)) && rVariant.ulVal)
  93. return S_OK;
  94. return S_FALSE;
  95. }
  96. HRESULT HrIsGhosted(LPMIMEMESSAGE pMsg, HBODY hBody)
  97. {
  98. PROPVARIANT pv;
  99. pv.vt = VT_I4;
  100. if (pMsg->GetBodyProp(hBody, PIDTOSTR(PID_ATT_GHOSTED), NOFLAGS, &pv)==S_OK &&
  101. pv.vt == VT_I4 && pv.lVal == TRUE)
  102. return S_OK;
  103. else
  104. return S_FALSE;
  105. }
  106. HRESULT HrGhostKids(LPMIMEMESSAGE pMsg, HBODY hBody)
  107. {
  108. HRESULT hr=S_OK;
  109. if (pMsg && hBody)
  110. {
  111. if (!FAILED(pMsg->GetBody(IBL_FIRST, hBody, &hBody)))
  112. {
  113. do
  114. {
  115. if (HrIsReferencedUrl(pMsg, hBody)==S_OK)
  116. {
  117. hr = HrMarkGhosted(pMsg, hBody);
  118. if (FAILED(hr))
  119. goto error;
  120. }
  121. }
  122. while (!FAILED(pMsg->GetBody(IBL_NEXT, hBody, &hBody)));
  123. }
  124. }
  125. error:
  126. return hr;
  127. }
  128. HRESULT HrDeleteGhostedKids(LPMIMEMESSAGE pMsg, HBODY hBody)
  129. {
  130. HRESULT hr=S_OK;
  131. ULONG cKids=0,
  132. uKid;
  133. LPHBODY rghKids=0;
  134. pMsg->CountBodies(hBody, FALSE, &cKids);
  135. if (cKids)
  136. {
  137. if (!MemAlloc((LPVOID *)&rghKids, sizeof(HBODY) * cKids))
  138. {
  139. hr = E_OUTOFMEMORY;
  140. goto error;
  141. }
  142. cKids = 0;
  143. if (!FAILED(pMsg->GetBody(IBL_FIRST, hBody, &hBody)))
  144. {
  145. do
  146. {
  147. if (HrIsGhosted(pMsg, hBody)==S_OK)
  148. {
  149. rghKids[cKids++] = hBody;
  150. }
  151. }
  152. while (!FAILED(pMsg->GetBody(IBL_NEXT, hBody, &hBody)));
  153. }
  154. for (uKid = 0; uKid < cKids; uKid++)
  155. {
  156. hr=pMsg->DeleteBody(rghKids[uKid], 0);
  157. if (FAILED(hr))
  158. goto error;
  159. }
  160. }
  161. error:
  162. SafeMemFree(rghKids);
  163. return hr;
  164. }
  165. UINT uCodePageFromCharset(HCHARSET hCharset)
  166. {
  167. INETCSETINFO CsetInfo;
  168. UINT uiCodePage = GetACP();
  169. if (hCharset &&
  170. (MimeOleGetCharsetInfo(hCharset, &CsetInfo)==S_OK))
  171. uiCodePage = CsetInfo.cpiInternet ;
  172. return uiCodePage;
  173. }
  174. UINT uCodePageFromMsg(LPMIMEMESSAGE pMsg)
  175. {
  176. HCHARSET hCharset=0;
  177. if (pMsg)
  178. pMsg->GetCharset(&hCharset);
  179. return uCodePageFromCharset(hCharset);
  180. }
  181. HRESULT HrIStreamWToInetCset(LPSTREAM pstmW, HCHARSET hCharset, LPSTREAM *ppstmOut)
  182. {
  183. IMimeBody *pBody;
  184. HRESULT hr;
  185. hr = MimeOleCreateBody(&pBody);
  186. if (!FAILED(hr))
  187. {
  188. hr = pBody->InitNew();
  189. if (!FAILED(hr))
  190. {
  191. hr = pBody->SetData(IET_UNICODE, STR_CNT_TEXT, STR_SUB_HTML, IID_IStream, pstmW);
  192. if (!FAILED(hr))
  193. {
  194. hr = pBody->SetCharset(hCharset, CSET_APPLY_ALL);
  195. if (!FAILED(hr))
  196. hr = pBody->GetData(IET_INETCSET, ppstmOut);
  197. }
  198. }
  199. pBody->Release();
  200. }
  201. return hr;
  202. }
  203. HRESULT HrCopyHeader(LPMIMEMESSAGE pMsgDest, HBODY hBodyDest, LPMIMEMESSAGE pMsgSrc, HBODY hBodySrc, LPCSTR pszName)
  204. {
  205. LPSTR lpszProp;
  206. HRESULT hr;
  207. hr = MimeOleGetBodyPropA(pMsgSrc, hBodySrc, pszName, NOFLAGS, &lpszProp);
  208. if (!FAILED(hr))
  209. {
  210. hr = MimeOleSetBodyPropA(pMsgDest, hBodyDest, pszName, NOFLAGS, lpszProp);
  211. SafeMimeOleFree(lpszProp);
  212. }
  213. return hr;
  214. }