Contact cannot be a null ptr, and it is accessed before anyway. Disscussed with kedge.
[kdenetwork.git] / kppp / ppplog.cpp
blob46960e463d7d9e4299f48ff42a4d8537ea57ba08
1 /*
3 * kPPP: A pppd front end for the KDE project
5 * $Id$
7 * (c) 1998 Mario Weilguni <mweilguni@kde.org>
9 * Copyright (C) 1997 Bernd Johannes Wuebben
10 * wuebben@math.cornell.edu
12 * based on EzPPP:
13 * Copyright (C) 1997 Jay Painter
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Library General Public
17 * License as published by the Free Software Foundation; either
18 * version 2 of the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Library General Public License for more details.
25 * You should have received a copy of the GNU Library General Public
26 * License along with this program; if not, write to the Free
27 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include <qlabel.h>
31 #include <qdir.h>
32 #include <qpushbutton.h>
33 //Added by qt3to4:
34 #include <QVBoxLayout>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <unistd.h>
40 #include <sys/stat.h>
42 #include <kdialog.h>
43 #include <qregexp.h>
44 #include <QTextEdit>
45 #include <qlayout.h>
47 #include <kmessagebox.h>
49 #include "pppdata.h"
50 #include "requester.h"
51 #include <klocale.h>
54 int PPPL_MakeLog(QStringList &list) {
55 int pid = -1, newpid;
56 char buffer[1024], *p;
57 const char *pidp;
58 int fd;
60 fd = Requester::rq->openSysLog();
61 if(fd < 0) {
62 list.append(i18n("Cannot open any of the following logfiles:"));
63 const char * const * logFile = &kppp_syslog[0];
64 while(*logFile) {
65 list.append(*logFile);
66 logFile++;
68 return 1;
71 FILE *f = fdopen(fd, "r");
72 while(fgets(buffer, sizeof(buffer), f) != 0) {
73 // pppd line ?
74 p = (char *)strstr(buffer, "pppd[");
75 if(p == 0)
76 continue;
77 pidp = p += strlen("pppd[");
78 while(*p && isdigit(*p))
79 p++;
80 if(*p != ']')
81 continue;
83 /* find out pid of pppd */
84 sscanf(pidp, "%d", &newpid);
85 if(newpid != pid) {
86 pid = newpid;
87 list.clear();
89 if(buffer[strlen(buffer)-1] == '\n')
90 buffer[strlen(buffer)-1] = '\0';
91 list.append(buffer);
93 fclose(f);
95 if(list.isEmpty())
96 return 2;
98 /* clear security related info */
100 const char *keyword[] = {"name = \"",
101 "user=\"",
102 "password=\"",
105 for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it )
107 QByteArray tmp = (*it).toLocal8Bit();
108 for(int j = 0; keyword[j] != 0; j++) {
109 char *p;
111 if( (p = (char *)strstr(tmp.data(), keyword[j])) != 0) {
112 p += strlen(keyword[j]);
113 while(*p && *p != '"')
114 *p++ = 'X';
120 return 0;
124 void PPPL_AnalyseLog(QStringList &list, QStringList &result) {
125 QString msg;
126 const char *rmsg = "Remote message: ";
128 result.clear();
130 // setup the analysis database
131 struct {
132 const char *regexp;
133 const char *answer;
134 } hints[] = {
135 {"Receive serial link is not 8-bit clean",
136 I18N_NOOP("You have launched pppd before the remote server " \
137 "was ready to establish a PPP connection.\n"
138 "Please use the terminal-based login to verify") },
140 {"Serial line is looped back",
141 I18N_NOOP("You have not started the PPP software on the peer system.") },
143 {"AP authentication failed",
144 I18N_NOOP("Check that you supplied the correct username and password.")} ,
146 {"is locked by pid",
147 I18N_NOOP("You should not pass 'lock' as an argument to pppd. "
148 "Check /etc/ppp/options and ~/.ppprc") },
150 {"CP: timeout sending",
151 I18N_NOOP("The remote system does not seem to answer to\n"
152 "configuration request. Contact your provider.") },
154 {"unrecognized option",
155 I18N_NOOP("You have passed an invalid option to pppd. See 'man pppd' "
156 "for a complete list of valid arguments.") },
158 // terminator
159 {0,0}
163 // scan the log for keywords and try to offer any help
164 for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it )
166 // look for remote message
167 int pos = (*it).indexOf(rmsg);
168 if (pos != -1)
170 QString str = (*it);
171 str.remove(0, pos + strlen(rmsg));
172 if(!str.isEmpty()) {
173 msg = i18n("Notice that the remote system has sent the following"
174 " message:\n\"%1\"\nThis may give you a hint why the"
175 " the connection has failed.", str);
176 result.append(msg);
180 // check in the hint database
181 for(uint k = 0; hints[k].regexp != 0; k++) {
182 QRegExp rx(hints[k].regexp);
183 QString l(*it);
184 if(l.contains(rx)) {
185 result.append(i18n(hints[k].answer));
186 break;
191 if (result.isEmpty())
192 result.append(i18n("Unable to provide help."));
196 void PPPL_ShowLog() {
197 QStringList sl, result;
199 PPPL_MakeLog(sl);
201 bool foundConnect = false;
202 bool foundLCP = gpppdata.getPPPDebug();
203 QString lcp = QLatin1String("[LCP");
204 QString conn = QLatin1String("Connect:");
205 QStringList::ConstIterator it = sl.constBegin();
206 for( ; it != sl.constEnd(); it++) {
207 if((*it).indexOf(lcp) >= 0) {
208 foundLCP = true;
209 break;
211 if((*it).contains(conn))
212 foundConnect = true;
214 if(foundConnect && !foundLCP) {
215 int result = KMessageBox::warningYesNo(0,
216 i18n("KPPP could not prepare a PPP log. It is very likely "
217 "that pppd was started without the \"debug\" option.\n"
218 "Without this option it's difficult to find out PPP "
219 "problems, so you should turn on the debug option.\n"
220 "Shall I turn it on now?"), QString(), KGuiItem(i18n("Restart pppd")), KGuiItem(i18n("Do Not Restart")));
222 if(result == KMessageBox::Yes) {
223 gpppdata.setPPPDebug(true);
224 KMessageBox::information(0,
225 i18n("The \"debug\" option has been added. You "
226 "should now try to reconnect. If that fails "
227 "again, you will get a PPP log that may help "
228 "you to track down the connection problem."));
229 // return;
232 // return;
235 PPPL_AnalyseLog(sl, result);
237 KDialog *dlg = new KDialog();
238 dlg->setButtons(KDialog::Close | KDialog::Ok);
239 dlg->setWindowTitle(i18n("PPP Log"));
240 dlg->setButtonText(KDialog::Ok,i18n("Write to File"));
241 QVBoxLayout *tl = new QVBoxLayout(dlg);
242 tl->setSpacing(10);
243 tl->setMargin(10);
244 QTextEdit *edit = new QTextEdit(dlg);
245 edit->setReadOnly(true);
246 QLabel *label = new QLabel(i18n("kppp's diagnosis (just guessing):"), dlg);
247 QTextEdit *diagnosis = new QTextEdit(dlg);
248 diagnosis->setReadOnly(true);
249 edit->setMinimumSize(600, 250);
250 label->setMinimumSize(600, 15);
251 diagnosis->setMinimumSize(600, 60);
253 tl->addWidget(edit, 1);
254 tl->addWidget(label);
255 tl->addWidget(diagnosis, 1);
256 dlg->setFixedSize(dlg->sizeHint());
258 for(int i = 0; i < sl.count(); i++)
259 edit->append(sl.at(i));
260 for(int i = 0; i < result.count(); i++)
261 diagnosis->append(result.at(i));
263 if(dlg->exec()) {
264 QDir d = QDir::home();
265 QString s = d.absolutePath() + "/PPP-logfile";
266 int old_umask = umask(0077);
268 FILE *f = fopen(QFile::encodeName(s), "w");
269 for(int i = 0; i < sl.count(); i++)
270 fprintf(f, "%s\n", sl.at(i).toLocal8Bit().data());
271 fclose(f);
272 umask(old_umask);
274 QString msg = i18n("The PPP log has been saved\nas \"%1\".\n\nIf you want to send a bug report, or have\nproblems connecting to the Internet, please\nattach this file. It will help the maintainers\nto find the bug and to improve KPPP", s);
275 KMessageBox::information(0, msg);
277 delete dlg;