1*2a97ec55SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2a97ec55SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2a97ec55SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2a97ec55SAndrew Rist  * distributed with this work for additional information
6*2a97ec55SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2a97ec55SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2a97ec55SAndrew Rist  * "License"); you may not use this file except in compliance
9*2a97ec55SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2a97ec55SAndrew Rist  *
11*2a97ec55SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2a97ec55SAndrew Rist  *
13*2a97ec55SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2a97ec55SAndrew Rist  * software distributed under the License is distributed on an
15*2a97ec55SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2a97ec55SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2a97ec55SAndrew Rist  * specific language governing permissions and limitations
18*2a97ec55SAndrew Rist  * under the License.
19*2a97ec55SAndrew Rist  *
20*2a97ec55SAndrew Rist  *************************************************************/
21*2a97ec55SAndrew Rist 
22*2a97ec55SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_extensions.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "errormail.hxx"
28cdf0e10cSrcweir #include "config.hxx"
29cdf0e10cSrcweir #include <unotools/bootstrap.hxx>
30cdf0e10cSrcweir #include <rtl/ustring.hxx>
31cdf0e10cSrcweir #include <rtl/string.hxx>
32cdf0e10cSrcweir #include <rtl/strbuf.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #if defined(UNIX) || defined(OS2)
35cdf0e10cSrcweir     #include <sys/utsname.h>
36cdf0e10cSrcweir #endif
37cdf0e10cSrcweir #ifdef WIN32
38cdf0e10cSrcweir     #include <windows.h>
39cdf0e10cSrcweir #endif
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 
42cdf0e10cSrcweir using namespace com::sun::star::lang;
43cdf0e10cSrcweir using namespace com::sun::star::uno;
44cdf0e10cSrcweir using namespace oooimprovement;
45cdf0e10cSrcweir using ::rtl::OUString;
46cdf0e10cSrcweir using ::rtl::OString;
47cdf0e10cSrcweir using ::rtl::OStringBuffer;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 
50cdf0e10cSrcweir namespace {
replaceAll(const OString & str,sal_Char old,const OString & replacement)51cdf0e10cSrcweir     static OString replaceAll(const OString& str, sal_Char old, const OString& replacement)
52cdf0e10cSrcweir     {
53cdf0e10cSrcweir         OStringBuffer result;
54cdf0e10cSrcweir         sal_Int32 idx = 0;
55cdf0e10cSrcweir         do {
56cdf0e10cSrcweir             result.append(str.getToken(0, old, idx));
57cdf0e10cSrcweir             if(idx>=0) result.append(replacement);
58cdf0e10cSrcweir         } while(idx >= 0);
59cdf0e10cSrcweir         return result.makeStringAndClear();
60cdf0e10cSrcweir     };
61cdf0e10cSrcweir 
xmlAttrEncode(const OString & input)62cdf0e10cSrcweir     static OString xmlAttrEncode(const OString& input)
63cdf0e10cSrcweir     {
64cdf0e10cSrcweir         OString result = replaceAll(input, '&', OString("&amp;"));
65cdf0e10cSrcweir         result = replaceAll(result, '<', OString("&lt;"));
66cdf0e10cSrcweir         result = replaceAll(result, '"', OString("&quot;"));
67cdf0e10cSrcweir         return replaceAll(result, '>', OString("&gt;"));
68cdf0e10cSrcweir     }
69cdf0e10cSrcweir 
getPlatform()70cdf0e10cSrcweir     static OString getPlatform()
71cdf0e10cSrcweir     {
72cdf0e10cSrcweir         #ifdef SOLARIS
73cdf0e10cSrcweir             return "Solaris";
74cdf0e10cSrcweir         #elif defined LINUX
75cdf0e10cSrcweir             return "Linux";
76cdf0e10cSrcweir         #elif defined WIN32
77cdf0e10cSrcweir             return "Win32";
78cdf0e10cSrcweir         #elif defined UNIX
79cdf0e10cSrcweir             return "Unix";
80cdf0e10cSrcweir         #elif defined OS2
81cdf0e10cSrcweir             return "OS/2";
82cdf0e10cSrcweir         #else
83cdf0e10cSrcweir             return "Unknown";
84cdf0e10cSrcweir         #endif
85cdf0e10cSrcweir     };
86cdf0e10cSrcweir 
87cdf0e10cSrcweir #if defined(UNIX) || defined(OS2)
getLocale()88cdf0e10cSrcweir     static const OString getLocale()
89cdf0e10cSrcweir     {
90cdf0e10cSrcweir         const char * locale = getenv( "LC_ALL" );
91cdf0e10cSrcweir         if( NULL == locale )
92cdf0e10cSrcweir             locale = getenv( "LC_CTYPE" );
93cdf0e10cSrcweir 
94cdf0e10cSrcweir         if( NULL == locale )
95cdf0e10cSrcweir             locale = getenv( "LANG" );
96cdf0e10cSrcweir 
97cdf0e10cSrcweir         if( NULL == locale )
98cdf0e10cSrcweir             locale = "C";
99cdf0e10cSrcweir         return locale;
100cdf0e10cSrcweir     };
101cdf0e10cSrcweir 
getSystemInfoXml(const Reference<XMultiServiceFactory> &)102cdf0e10cSrcweir     static OString getSystemInfoXml(const Reference<XMultiServiceFactory>&)
103cdf0e10cSrcweir     {
104cdf0e10cSrcweir         struct utsname info;
105cdf0e10cSrcweir         //memset(&info, 0, sizeof(info));
106cdf0e10cSrcweir         uname(&info);
107cdf0e10cSrcweir         OStringBuffer result =
108cdf0e10cSrcweir             "<systeminfo:systeminfo xmlns:systeminfo=\"http://openoffice.org/2002/systeminfo\">\n"
109cdf0e10cSrcweir             "<systeminfo:System name=\""
110cdf0e10cSrcweir             + xmlAttrEncode(OString(info.sysname)) + "\" version=\""
111cdf0e10cSrcweir             + xmlAttrEncode(OString(info.version)) + "\" build=\""
112cdf0e10cSrcweir             + xmlAttrEncode(OString(info.release)) + "\" locale=\""
113cdf0e10cSrcweir             + xmlAttrEncode(OString(getLocale())) + "\"/>\n"
114cdf0e10cSrcweir             "<systeminfo:CPU type=\""
115cdf0e10cSrcweir             + xmlAttrEncode(OString(info.machine)) + "\"/>\n"
116cdf0e10cSrcweir             "</systeminfo:systeminfo>\n";
117cdf0e10cSrcweir         return result.makeStringAndClear();
118cdf0e10cSrcweir     };
119cdf0e10cSrcweir #else
getSystemInfoXml(const Reference<XMultiServiceFactory> &)120cdf0e10cSrcweir     static OString getSystemInfoXml(const Reference<XMultiServiceFactory>&)
121cdf0e10cSrcweir     {
122cdf0e10cSrcweir         OSVERSIONINFO info;
123cdf0e10cSrcweir         ZeroMemory(&info, sizeof(OSVERSIONINFO));
124cdf0e10cSrcweir         info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
125cdf0e10cSrcweir         GetVersionEx(&info);
126cdf0e10cSrcweir         OStringBuffer result = OString(
127cdf0e10cSrcweir             "<systeminfo:systeminfo xmlns:systeminfo=\"http://openoffice.org/2002/systeminfo\">\n"
128cdf0e10cSrcweir             "<systeminfo:System name=\"");
129cdf0e10cSrcweir         if(VER_PLATFORM_WIN32_NT == info.dwPlatformId)
130cdf0e10cSrcweir             result.append(OString("Windows NT"));
131cdf0e10cSrcweir         else
132cdf0e10cSrcweir             result.append(OString("Windows"));
133cdf0e10cSrcweir         result.append("\" version=\"").append(static_cast<long>(info.dwMajorVersion));
134cdf0e10cSrcweir         result.append(".").append(static_cast<long>(info.dwMinorVersion));
135cdf0e10cSrcweir         result.append("\" build=\"").append(static_cast<long>(info.dwBuildNumber));
136cdf0e10cSrcweir         result.append("\" locale=\"").append(static_cast<long>(GetUserDefaultLangID()));
137cdf0e10cSrcweir         result.append("\"/>\n");
138cdf0e10cSrcweir         result.append("<systeminfo:CPU type=\""
139cdf0e10cSrcweir             /* x86 or AMD64 */  "\"/>\n"
140cdf0e10cSrcweir             "</systeminfo:systeminfo>\n");
141cdf0e10cSrcweir         return result.makeStringAndClear();
142cdf0e10cSrcweir     };
143cdf0e10cSrcweir #endif
144cdf0e10cSrcweir 
getOfficeInfoXml(const Reference<XMultiServiceFactory> & sf)145cdf0e10cSrcweir     static OString getOfficeInfoXml(const Reference<XMultiServiceFactory>& sf)
146cdf0e10cSrcweir     {
147cdf0e10cSrcweir         Config config(sf);
148cdf0e10cSrcweir         const OString product = OUStringToOString(config.getCompleteProductname(), RTL_TEXTENCODING_ASCII_US);
149cdf0e10cSrcweir         const OString platform = getPlatform();
150cdf0e10cSrcweir         const OString language = OUStringToOString(config.getSetupLocale(), RTL_TEXTENCODING_ASCII_US);
151cdf0e10cSrcweir         // If the oooimprovement lib is packaged in an extension, this needs to
152cdf0e10cSrcweir         // be done in another way: The build version string needs to be made
153cdf0e10cSrcweir         // available in an UNO service (if no better place is found for this,
154cdf0e10cSrcweir         // com.sun.star.comp.extensions.oooimprovecore.Core in oooimprovecore
155cdf0e10cSrcweir         // is likely the best fit)
156cdf0e10cSrcweir         const OString build = OUStringToOString(::utl::Bootstrap::getBuildIdData(OUString()), RTL_TEXTENCODING_ASCII_US);
157cdf0e10cSrcweir         const OString exceptiontype = "";
158cdf0e10cSrcweir         OStringBuffer result =
159cdf0e10cSrcweir             "<officeinfo:officeinfo xmlns:officeinfo=\"http://openoffice.org/2002/officeinfo\" platform=\""
160cdf0e10cSrcweir             + xmlAttrEncode(platform) + "\" language=\""
161cdf0e10cSrcweir             + xmlAttrEncode(language) + "\" build=\""
162cdf0e10cSrcweir             + xmlAttrEncode(build) + "\" exceptiontype=\""
163cdf0e10cSrcweir             + xmlAttrEncode(exceptiontype) + "\" product=\""
164cdf0e10cSrcweir             + xmlAttrEncode(product) + " \" />\n";
165cdf0e10cSrcweir         return result.makeStringAndClear();
166cdf0e10cSrcweir     };
167cdf0e10cSrcweir }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir namespace oooimprovement
170cdf0e10cSrcweir {
Errormail(const Reference<XMultiServiceFactory> & sf)171cdf0e10cSrcweir     Errormail::Errormail(const Reference<XMultiServiceFactory>& sf)
172cdf0e10cSrcweir         : m_ServiceFactory(sf)
173cdf0e10cSrcweir     {}
174cdf0e10cSrcweir 
getXml()175cdf0e10cSrcweir     OString Errormail::getXml()
176cdf0e10cSrcweir     {
177cdf0e10cSrcweir         Config config(m_ServiceFactory);
178cdf0e10cSrcweir         const OString usertype;
179cdf0e10cSrcweir         const OString email = OUStringToOString(config.getReporterEmail(), RTL_TEXTENCODING_ASCII_US);
180cdf0e10cSrcweir         OString feedback;
181cdf0e10cSrcweir         {
182cdf0e10cSrcweir             OStringBuffer temp;
183cdf0e10cSrcweir             temp.append(config.getReportCount());
184cdf0e10cSrcweir             feedback = temp.makeStringAndClear();
185cdf0e10cSrcweir         }
186cdf0e10cSrcweir         const OString title;
187cdf0e10cSrcweir         OStringBuffer result =
188cdf0e10cSrcweir             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
189cdf0e10cSrcweir             "<!DOCTYPE errormail:errormail PUBLIC \"-//OpenOffice.org//DTD ErrorMail 1.0//EN\" \"errormail.dtd\">\n"
190cdf0e10cSrcweir             "<errormail:errormail xmlns:errormail=\"http://openoffice.org/2002/errormail\" usertype=\""
191cdf0e10cSrcweir             + xmlAttrEncode(usertype) + "\">\n"
192cdf0e10cSrcweir             "<reportmail:mail xmlns:reportmail=\"http://openoffice.org/2002/reportmail\" version=\"1.1\" feedback=\""
193cdf0e10cSrcweir             + xmlAttrEncode(feedback) + "\" email=\""
194cdf0e10cSrcweir             + xmlAttrEncode(email) + "\">\n"
195cdf0e10cSrcweir             "<reportmail:title>"
196cdf0e10cSrcweir             + xmlAttrEncode(title) + "</reportmail:title>\n"
197cdf0e10cSrcweir            "<reportmail:attachment name=\"data.zip\" media-type=\"application/zip\" class=\"OOoImprovementLog\"/>\n"
198cdf0e10cSrcweir             "</reportmail:mail>\n"
199cdf0e10cSrcweir             + getOfficeInfoXml(m_ServiceFactory)
200cdf0e10cSrcweir             + getSystemInfoXml(m_ServiceFactory) +
201cdf0e10cSrcweir             "</errormail:errormail>\n";
202cdf0e10cSrcweir         return result.makeStringAndClear();
203cdf0e10cSrcweir     }
204cdf0e10cSrcweir }
205