1/*
2    SPDX-FileCopyrightText: Sandro Knauß <knauss@kolabsys.com>
3
4    SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7// add this function to trim user input of whitespace when needed
8String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };
9
10var page = Dialog.addPage("kolabwizard.ui", qsTr("Personal Settings"));
11var page2 = Dialog.addPage("kolabwizard2.ui", qsTr("Autoconfiguration"));
12var userChangedServerAddress = false;
13
14page.widget().nameEdit.text = SetupManager.name()
15page.widget().emailEdit.text = SetupManager.email()
16page.widget().passwordEdit.password = SetupManager.password()
17
18function guessServerName()
19{
20    var email = page.widget().emailEdit.text;
21    var pos = email.indexOf("@");
22    if (pos >= 0 && (pos + 1) < email.length) {
23      var server = email.slice(pos + 1, email.length);
24      return server;
25    }
26}
27
28function validateInput()
29{
30  if (page.widget().emailEdit.text.trim() == "" || page.widget().passwordEdit.password.trim() == "") {
31    page.setValid(false);
32  } else {
33    page.setValid(true);
34  }
35}
36
37//Server test
38servertest_running = false;
39//0 = not running, 1=submission, 2 = smtp, 3 = imap
40servertest_mode = 0;
41
42function testResultFail()
43{
44  testOk( -1 );
45}
46
47var imapRes;
48function testOk( arg )
49{
50    print("testOk arg =", arg);
51
52    if (servertest_mode < 3) {   // submission & smtp
53        if (arg == "tls" ) { // tls is really STARTTLS
54          smtp.setEncryption("TLS");
55          if (servertest_mode == 1) {   //submission port 587
56              smtp.setPort(587);
57          } else {
58              smtp.setPort(25);
59          }
60        } else if ( arg == "ssl" ) {    //only possible as smtps
61            smtp.setPort(465);
62            smtp.setEncryption("SSL");
63        } else if (servertest_mode == 2) { //test submission and smtp failed or only possible unencrypted -> set to standard value and open editor
64            smtp.setPort(587);
65            smtp.setEncryption("TLS");
66            smtp.setEditMode(true);
67        } else if (servertest_mode == 1) { // submission test failed -> start smtp request
68            servertest_mode = 2;
69            ServerTest.test(page2.widget().lineEditSmtp.text, "smtp");
70            return;
71        }
72
73        // start imap test
74        servertest_mode = 3;
75        if (page2.widget().lineEditImap.text) {
76            SetupManager.setupInfo(qsTr("Probing IMAP server..."));
77            ServerTest.test(page2.widget().lineEditImap.text, "imap");
78        } else {
79            SetupManager.execute();
80        }
81    } else if (servertest_mode == 3) {   //imap
82        if ( arg == "ssl" ) {
83          // The ENUM used for authentication (in the kolab resource only)
84          kolabRes.setOption( "Safety", "SSL" ); // SSL/TLS
85          kolabRes.setOption( "ImapPort", 993 );
86        } else if ( arg == "tls" ) { // tls is really STARTTLS
87          kolabRes.setOption( "Safety", "STARTTLS" );  // STARTTLS
88          kolabRes.setOption( "ImapPort", 143 );
89        } else {
90          // safe default fallback in case server test failed
91          kolabRes.setOption( "Safety", "STARTTLS" );
92          kolabRes.setOption( "ImapPort", 143 );
93          kolabRes.setEditMode(true);
94        }
95        SetupManager.execute();
96    } else {
97        print ("Unknown servertest_mode = ", servertest_mode);
98    }
99}
100
101var identity; // global so it can be accessed in setup and testOk
102
103var kolabRes;
104var smtp;
105var imapRes;
106
107var ac_mail;
108var ac_freebusy;
109var ac_ldap;
110
111var ac_mail_stat;
112var ac_freebusy_stat;
113var ac_ldap_stat;
114
115function checkAutoconfig()
116{
117    ac_mail_stat = false;
118    ac_freebusy_stat = false;
119    ac_ldap_stat = false;
120
121    page2.widget().lineEditImap.text = guessServerName();
122    page2.widget().lineEditSmtp.text = guessServerName();
123    page2.widget().lineEditImap.visible = false;
124    page2.widget().lineEditSmtp.visible = false;
125    page2.widget().lineEditImapLabel.visible = false;
126    page2.widget().lineEditSmtpLabel.visible = false;
127
128    page2.widget().checkBoxFreebusyEdit.text = qsTr("Create");
129    page2.widget().checkBoxLdapEdit.text = qsTr("Create");
130
131    ac_mail = SetupManager.ispDB('autoconfigkolabmail');
132    ac_mail.ispdbFinished.connect(mail_finished);
133    ac_mail.info.connect(mail_text);
134    ac_mail.setEmail(page.widget().emailEdit.text);
135    ac_mail.setPassword(page.widget().passwordEdit.password);
136    ac_mail.start();
137
138    ac_freebusy = SetupManager.ispDB('autoconfigkolabfreebusy');
139    ac_freebusy.ispdbFinished.connect(freebusy_finished);
140    ac_freebusy.info.connect(freebusy_text);
141    ac_freebusy.setEmail(page.widget().emailEdit.text);
142    ac_freebusy.setPassword(page.widget().passwordEdit.password);
143    ac_freebusy.start();
144
145    ac_ldap = SetupManager.ispDB('autoconfigkolabldap');
146    ac_ldap.ispdbFinished.connect(ldap_finished);
147    ac_ldap.info.connect(ldap_text);
148    ac_ldap.setEmail(page.widget().emailEdit.text);
149    ac_ldap.setPassword(page.widget().passwordEdit.password);
150    ac_ldap.start();
151}
152
153function mail_finished(stat) {
154    ac_mail_stat = stat;
155    if (stat) {
156        page2.widget().lineEditImap.visible = false;
157        page2.widget().lineEditSmtp.visible = false;
158        page2.widget().lineEditImapLabel.visible = false;
159        page2.widget().lineEditSmtpLabel.visible = false;
160    } else {
161        page2.widget().lineEditImap.visible = true;
162        page2.widget().lineEditSmtp.visible = true;
163        page2.widget().lineEditImapLabel.visible = true;
164        page2.widget().lineEditSmtpLabel.visible = true;
165    }
166}
167
168function mail_text(text) {
169    page2.widget().labelImapSearch.text = text;
170    page2.widget().labelSmtpSearch.text = text;
171}
172
173function freebusy_finished(stat) {
174    ac_freebusy_stat = stat;
175    if (stat) {
176        page2.widget().checkBoxFreebusyEdit.text = qsTr("Manual Edit");
177    }
178}
179
180function freebusy_text(text) {
181    page2.widget().labelFreebusySearch.text = text;
182}
183
184function ldap_finished(stat) {
185    ac_ldap_stat = stat;
186    if (stat) {
187        page2.widget().checkBoxLdapEdit.text = qsTr("Manual Edit");
188    }
189}
190
191function ldap_text(text) {
192    page2.widget().labelLdapSearch.text = text;
193}
194
195function setup()
196{
197    SetupManager.openWallet();
198    smtp = SetupManager.createTransport("smtp");
199    smtp.setEditMode(page2.widget().checkBoxSmtpEdit.checked);
200    smtp.setPassword(page.widget().passwordEdit.password);
201
202    if (ac_mail_stat) {
203        ac_mail.fillSmtpServer(0, smtp);
204    } else if (page2.widget().lineEditSmtp.text) {
205        var serverAddress = page2.widget().lineEditSmtp.text;
206        servertest_running = true;
207        servertest_mode = 1;
208        smtp.setName(serverAddress);
209        smtp.setHost(serverAddress);
210        smtp.setUsername(page.widget().emailEdit.text);
211        smtp.setAuthenticationType("plain");
212
213        SetupManager.setupInfo(qsTr("Probing SMTP server..."));
214        ServerTest.test( serverAddress, "submission" );   //probe port and encryption
215    }
216
217    for (i = 0; i < ac_mail.countIdentities(); i++) {
218        var j = SetupManager.createIdentity();
219        j.setTransport(smtp);
220        //templates
221        //drafts
222        //fcc
223        ac_mail.fillIdentitiy(i,j);
224        if (i == ac_mail.defaultIdentity()) {
225            identity = j;
226        }
227    }
228
229    if (ac_mail.countIdentities() == 0) {
230        identity = SetupManager.createIdentity();
231        identity.setEmail(page.widget().emailEdit.text);
232        identity.setRealName(page.widget().nameEdit.text);
233        identity.setTransport(smtp);
234    }
235
236    kolabRes = SetupManager.createResource("akonadi_kolab_resource");
237    kolabRes.setEditMode(page2.widget().checkBoxImapEdit.checked);
238    kolabRes.setOption("Password", page.widget().passwordEdit.password);
239    kolabRes.setOption("UseDefaultIdentity", false);
240    kolabRes.setOption("AccountIdentity", identity.uoid());
241    kolabRes.setOption("DisconnectedModeEnabled", true);
242    kolabRes.setOption("IntervalCheckTime", 60);
243    kolabRes.setOption("SubscriptionEnabled", true);
244    kolabRes.setOption("SieveSupport", true);
245
246    if (ac_mail_stat) {
247        ac_mail.fillImapServer(0, kolabRes);
248    } else if (page2.widget().lineEditImap.text) {
249        var serverAddress = page2.widget().lineEditImap.text;
250        kolabRes.setOption("ImapServer", serverAddress);
251        kolabRes.setOption("UserName", page.widget().emailEdit.text.trim());
252
253        if (!servertest_running) {
254            servertest_mode = 2;
255            servertest_running = true;
256            SetupManager.setupInfo(qsTr("Probing IMAP server..."));
257            ServerTest.test(serverAddress, "imap");
258        }       kolabRes.setOption("Authentication", 7);
259    }
260
261    if (ac_ldap_stat) {
262        for (i = 0; i < ac_ldap.countLdapServers(); i++) {
263            var ldap = SetupManager.createLdap();
264            ldap.setEditMode(page2.widget().checkBoxLdapEdit.checked);
265            ac_ldap.fillLdapServer(i,ldap);
266        }
267    } else if (page2.widget().checkBoxLdapEdit.checked) {
268        var ldap = SetupManager.createLdap();
269        ldap.setEditMode(page2.widget().checkBoxLdapEdit.checked);
270        ldap.setPassword(page.widget().passwordEdit.password);
271        ldap.setUser(page.widget().emailEdit.text);
272        ldap.setServer(guessServerName());
273    }
274
275    if (ac_freebusy_stat) {
276        var korganizer = SetupManager.createConfigFile("akonadi-calendarrc");
277        korganizer.setEditMode(page2.widget().checkBoxFreebusyEdit.checked);
278        korganizer.setEditName("freebusy");
279        korganizer.setName("korganizer");
280        ac_freebusy.fillFreebusyServer(0,korganizer);
281    } else if (page2.widget().checkBoxFreebusyEdit.checked) {
282        var korganizer = SetupManager.createConfigFile("akonadi-calendarrc");
283        korganizer.setEditMode(page2.widget().checkBoxFreebusyEdit.checked);
284        korganizer.setEditName("freebusy");
285        korganizer.setName( "korganizer" );
286        korganizer.setConfig( "FreeBusy Retrieve", "FreeBusyFullDomainRetrieval","true");
287        korganizer.setConfig( "FreeBusy Retrieve", "FreeBusyRetrieveAuto", "true" );
288        korganizer.setConfig( "FreeBusy Retrieve", "FreeBusyRetrieveUrl", "https://" + guessServerName()  + "/freebusy/" );
289    }
290
291    if (!servertest_running) {
292        SetupManager.execute();
293    }
294}
295
296try {
297  ServerTest.testFail.connect(testResultFail);
298  ServerTest.testResult.connect(testOk);
299
300  page.widget().emailEdit.textChanged.connect(validateInput);
301  page.widget().passwordEdit.passwordChanged.connect(validateInput);
302
303  page.pageLeftNext.connect(checkAutoconfig);
304  page2.pageLeftNext.connect(setup);
305} catch (e) {
306  print(e);
307}
308
309validateInput();
310