File: script.cpp

package info (click to toggle)
kde4libs 4:4.14.2-5+deb8u2
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 82,428 kB
  • ctags: 99,415
  • sloc: cpp: 761,864; xml: 12,344; ansic: 6,295; java: 4,060; perl: 2,938; yacc: 2,507; python: 1,207; sh: 1,179; ruby: 337; lex: 278; makefile: 29
file content (777 lines) | stat: -rw-r--r-- 28,220 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/*
   Copyright (c) 2003 Malte Starostik <malte@kde.org>
   Copyright (c) 2011 Dawit Alemayehu <adawit@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "script.h"

#include <QtCore/QString>
#include <QtCore/QRegExp>
#include <QtCore/QDateTime>
#include <QtCore/QTimer>
#include <QtCore/QEventLoop>

#include <QtNetwork/QHostInfo>
#include <QtNetwork/QHostAddress>
#include <QtNetwork/QNetworkInterface>

#include <QtScript/QScriptValue>
#include <QtScript/QScriptEngine>
#include <QtScript/QScriptProgram>
#include <QtScript/QScriptContextInfo>

#include <kurl.h>
#include <klocalizedstring.h>
#include <kio/hostinfo_p.h>

#define QL1S(x)    QLatin1String(x)

namespace
{
    static int findString (const QString& s, const char* const* values)
    {
        int index = 0;
        const QString lower = s.toLower();
        for (const char* const* p = values; *p; ++p, ++index) {
            if (s.compare(QLatin1String(*p), Qt::CaseInsensitive) == 0) {
              return index;
            }
        }
        return -1;
    }

    static const QDateTime getTime (QScriptContext* context)
    {
        const QString tz = context->argument(context->argumentCount() - 1).toString();
        if (tz.compare(QLatin1String("gmt"), Qt::CaseInsensitive) == 0) {
            return QDateTime::currentDateTimeUtc();
        }
        return QDateTime::currentDateTime();
    }

    template <typename T>
    static bool checkRange (T value, T min, T max)
    {
        return ((min <= max && value >= min && value <= max) ||
                (min > max && (value <= min || value >= max)));
    }

    static bool isLocalHostAddress (const QHostAddress& address)
    {
        if (address == QHostAddress::LocalHost)
            return true;

        if (address == QHostAddress::LocalHostIPv6)
            return true;

        return false;
    }

    static bool isIPv6Address (const QHostAddress& address)
    {
        return address.protocol() == QAbstractSocket::IPv6Protocol;
    }

    static bool isIPv4Address (const QHostAddress& address)
    {
        return (address.protocol() == QAbstractSocket::IPv4Protocol);
    }

    static bool isSpecialAddress(const QHostAddress& address)
    {
        // Catch all the special addresses and return false.
        if (address == QHostAddress::Null)
            return true;

        if (address == QHostAddress::Any)
            return true;

        if (address == QHostAddress::AnyIPv6)
            return true;

        if (address == QHostAddress::Broadcast)
            return true;

        return false;
    }

    static bool addressLessThanComparison(const QHostAddress& addr1,  const QHostAddress& addr2)
    {
        if (addr1.protocol() == QAbstractSocket::IPv4Protocol &&
            addr2.protocol() == QAbstractSocket::IPv4Protocol) {
            return addr1.toIPv4Address() < addr2.toIPv4Address();
        }

        if (addr1.protocol() == QAbstractSocket::IPv6Protocol &&
            addr2.protocol() == QAbstractSocket::IPv6Protocol) {
            const Q_IPV6ADDR ipv6addr1 = addr1.toIPv6Address();
            const Q_IPV6ADDR ipv6addr2 = addr2.toIPv6Address();
            for (int i=0; i < 16; ++i) {
                if (ipv6addr1[i] != ipv6addr2[i]) {
                    return ((ipv6addr1[i] & 0xff) - (ipv6addr2[i] & 0xff));
                }
            }
        }

        return false;
    }

    static QString addressListToString(const QList<QHostAddress>& addressList,
                                       const QHash<QString, QString>& actualEntryMap)
    {
        QString result;
        Q_FOREACH(const QHostAddress& address, addressList) {
            if (!result.isEmpty()) {
                result += QLatin1Char(';');
            }
            result += actualEntryMap.value(address.toString());
        }
        return result;
    }

    class Address
    {
    public:
        struct Error {};
        static Address resolve( const QString& host )
        {
            return Address( host );
        }

        QList<QHostAddress> addresses() const
        {
            return m_addressList;
        }

        QHostAddress address() const
        {
           if (m_addressList.isEmpty())
              return QHostAddress();

           return m_addressList.first();
        }

    private:
        Address( const QString& host )
        {
            // Always try to see if it's already an IP first, to avoid Qt doing a
            // needless reverse lookup
            QHostAddress address ( host );
            if ( address.isNull() ) {
                QHostInfo hostInfo = KIO::HostInfo::lookupCachedHostInfoFor(host);
                if (hostInfo.hostName().isEmpty() || hostInfo.error() != QHostInfo::NoError) {
                    hostInfo = QHostInfo::fromName(host);
                    KIO::HostInfo::cacheLookup(hostInfo);
                }
                m_addressList = hostInfo.addresses();
            } else {
                m_addressList.clear();
                m_addressList.append(address);
            }
        }

        QList<QHostAddress> m_addressList;
    };


    // isPlainHostName(host)
    // @returns true if @p host doesn't contains a domain part
    QScriptValue IsPlainHostName(QScriptContext* context, QScriptEngine* engine)
    {
        if (context->argumentCount() != 1) {
            return engine->undefinedValue();
        }
        return engine->toScriptValue(context->argument(0).toString().indexOf(QLatin1Char('.')) == -1);
    }

    // dnsDomainIs(host, domain)
    // @returns true if the domain part of @p host matches @p domain
    QScriptValue DNSDomainIs (QScriptContext* context, QScriptEngine* engine)
    {
        if (context->argumentCount() != 2) {
            return engine->undefinedValue();
        }

        const QString host = context->argument(0).toString();
        const QString domain = context->argument(1).toString();
        return engine->toScriptValue(host.endsWith(domain, Qt::CaseInsensitive));
    }

    // localHostOrDomainIs(host, fqdn)
    // @returns true if @p host is unqualified or equals @p fqdn
    QScriptValue LocalHostOrDomainIs (QScriptContext* context, QScriptEngine* engine)
    {
        if (context->argumentCount() != 2) {
            return engine->undefinedValue();
        }

        const QString host = context->argument(0).toString();
        if (!host.contains(QLatin1Char('.'))) {
            return engine->toScriptValue(true);
        }
        const QString fqdn = context->argument(1).toString();
        return engine->toScriptValue((host.compare(fqdn, Qt::CaseInsensitive) == 0));
    }

    // isResolvable(host)
    // @returns true if host is resolvable to a IPv4 address.
    QScriptValue IsResolvable (QScriptContext* context, QScriptEngine* engine)
    {
        if (context->argumentCount() != 1) {
            return engine->undefinedValue();
        }

        try {
            const Address info = Address::resolve(context->argument(0).toString());
            bool hasResolvableIPv4Address = false;

            Q_FOREACH(const QHostAddress& address, info.addresses()) {
                if (!isSpecialAddress(address) && isIPv4Address(address)) {
                    hasResolvableIPv4Address = true;
                    break;
                }
            }

            return engine->toScriptValue(hasResolvableIPv4Address);
        }
        catch (const Address::Error&) {
            return engine->toScriptValue(false);
        }
    }

    // isInNet(host, subnet, mask)
    // @returns true if the IPv4 address of host is within the specified subnet
    // and mask, false otherwise.
    QScriptValue IsInNet (QScriptContext* context, QScriptEngine* engine)
    {
        if (context->argumentCount() != 3) {
            return engine->undefinedValue();
        }

        try {
            const Address info = Address::resolve(context->argument(0).toString());
            bool isInSubNet = false;
            QString subnetStr = context->argument(1).toString();
            subnetStr += QLatin1Char('/');
            subnetStr += context->argument(2).toString();
            const QPair<QHostAddress, int> subnet = QHostAddress::parseSubnet(subnetStr);
            Q_FOREACH(const QHostAddress& address, info.addresses()) {
                if (!isSpecialAddress(address) && isIPv4Address(address) && address.isInSubnet(subnet)) {
                    isInSubNet = true;
                    break;
                }
            }
            return engine->toScriptValue(isInSubNet);
        }
        catch (const Address::Error&) {
            return engine->toScriptValue(false);
        }
    }

    // dnsResolve(host)
    // @returns the IPv4 address for host or an empty string if host is not resolvable.
    QScriptValue DNSResolve (QScriptContext* context, QScriptEngine* engine)
    {
        if (context->argumentCount() != 1) {
            return engine->undefinedValue();
        }

        try {
            const Address info = Address::resolve(context->argument(0).toString());
            QString resolvedAddress (QLatin1String(""));
            Q_FOREACH(const QHostAddress& address, info.addresses()) {
                if (!isSpecialAddress(address) && isIPv4Address(address)) {
                    resolvedAddress = address.toString();
                    break;
                }
            }
            return engine->toScriptValue(resolvedAddress);
        }
        catch (const Address::Error&) {
            return engine->toScriptValue(QString(QLatin1String("")));
        }
    }

    // myIpAddress()
    // @returns the local machine's IPv4 address. Note that this will return
    // the address for the first interfaces that match its criteria even if the
    // machine has multiple interfaces.
    QScriptValue MyIpAddress (QScriptContext* context, QScriptEngine* engine)
    {
        if (context->argumentCount()) {
            return engine->undefinedValue();
        }

        QString ipAddress;
        const QList<QHostAddress> addresses = QNetworkInterface::allAddresses();
        Q_FOREACH(const QHostAddress address, addresses) {
            if (isIPv4Address(address) && !isSpecialAddress(address) && !isLocalHostAddress(address)) {
                ipAddress = address.toString();
                break;
            }
        }

        return engine->toScriptValue(ipAddress);
    }

    // dnsDomainLevels(host)
    // @returns the number of dots ('.') in @p host
    QScriptValue DNSDomainLevels (QScriptContext* context, QScriptEngine* engine)
    {
        if (context->argumentCount() != 1) {
            return engine->undefinedValue();
        }

        const QString host = context->argument(0).toString();
        if (host.isNull()) {
            return engine->toScriptValue(0);
        }

        return engine->toScriptValue(host.count(QLatin1Char('.')));
    }

    // shExpMatch(str, pattern)
    // @returns true if @p str matches the shell @p pattern
    QScriptValue ShExpMatch (QScriptContext* context, QScriptEngine* engine)
    {
        if (context->argumentCount() != 2) {
            return engine->undefinedValue();
        }

        QRegExp pattern(context->argument(1).toString(), Qt::CaseSensitive, QRegExp::Wildcard);
        return engine->toScriptValue(pattern.exactMatch(context->argument(0).toString()));
    }

    // weekdayRange(day [, "GMT" ])
    // weekdayRange(day1, day2 [, "GMT" ])
    // @returns true if the current day equals day or between day1 and day2 resp.
    // If the last argument is "GMT", GMT timezone is used, otherwise local time
    QScriptValue WeekdayRange (QScriptContext* context, QScriptEngine* engine)
    {
        if (context->argumentCount() < 1 || context->argumentCount() > 3) {
            return engine->undefinedValue();
        }

        static const char* const days[] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat", 0 };

        const int d1 = findString(context->argument(0).toString(), days);
        if (d1 == -1) {
            return engine->undefinedValue();
        }

        int d2 = findString(context->argument(1).toString(), days);
        if (d2 == -1) {
            d2 = d1;
        }

        // Adjust the days of week coming from QDateTime since it starts
        // counting with Monday as 1 and ends with Sunday as day 7.
        int dayOfWeek = getTime(context).date().dayOfWeek();
        if (dayOfWeek == 7) {
            dayOfWeek = 0;
        }
        return engine->toScriptValue(checkRange(dayOfWeek, d1, d2));
    }

    // dateRange(day [, "GMT" ])
    // dateRange(day1, day2 [, "GMT" ])
    // dateRange(month [, "GMT" ])
    // dateRange(month1, month2 [, "GMT" ])
    // dateRange(year [, "GMT" ])
    // dateRange(year1, year2 [, "GMT" ])
    // dateRange(day1, month1, day2, month2 [, "GMT" ])
    // dateRange(month1, year1, month2, year2 [, "GMT" ])
    // dateRange(day1, month1, year1, day2, month2, year2 [, "GMT" ])
    // @returns true if the current date (GMT or local time according to
    // presence of "GMT" as last argument) is within the given range
    QScriptValue DateRange (QScriptContext* context, QScriptEngine* engine)
    {
        if (context->argumentCount() < 1 || context->argumentCount() > 7) {
            return engine->undefinedValue();
        }

        static const char* const months[] = { "jan", "feb", "mar", "apr", "may", "jun",
                                              "jul", "aug", "sep", "oct", "nov", "dec", 0 };

        QVector<int> values;
        for (int i = 0; i < context->argumentCount(); ++i)
        {
            int value = -1;
            if (context->argument(i).isNumber()) {
                value = context->argument(i).toInt32();
            } else {
                // QDate starts counting months from 1, so we add 1 here.
                value = findString(context->argument(i).toString(), months) + 1;
            }

            if (value > 0) {
                values.append(value);
            } else {
                break;
            }
        }

        const QDate now = getTime(context).date();

        // day1, month1, year1, day2, month2, year2
        if (values.size() == 6) {
            const QDate d1 (values[2], values[1], values[0]);
            const QDate d2 (values[5], values[4], values[3]);
            return engine->toScriptValue(checkRange(now, d1, d2));
        }
        // day1, month1, day2, month2
        else if (values.size() == 4 && values[ 1 ] < 13 && values[ 3 ] < 13) {
            const QDate d1 (now.year(), values[1], values[0]);
            const QDate d2 (now.year(), values[3], values[2]);
            return engine->toScriptValue(checkRange(now, d1, d2));
        }
        // month1, year1, month2, year2
        else if (values.size() == 4) {
            const QDate d1 (values[1], values[0], now.day());
            const QDate d2 (values[3], values[2], now.day());
            return engine->toScriptValue(checkRange(now, d1, d2));
        }
        // year1, year2
        else if (values.size() == 2 && values[0] >= 1000 && values[1] >= 1000) {
            return engine->toScriptValue(checkRange(now.year(), values[0], values[1]));
        }
        // day1, day2
        else if (values.size() == 2 && context->argument(0).isNumber() && context->argument(1).isNumber()) {
            return engine->toScriptValue(checkRange(now.day(), values[0], values[1]));
        }
        // month1, month2
        else if (values.size() == 2) {
            return engine->toScriptValue(checkRange(now.month(), values[0], values[1]));
        }
        // year
        else if (values.size() == 1 && values[ 0 ] >= 1000) {
            return engine->toScriptValue(checkRange(now.year(), values[0], values[0]));
        }
        // day
        else if (values.size() == 1 && context->argument(0).isNumber()) {
            return engine->toScriptValue(checkRange(now.day(), values[0], values[0]));
        }
        // month
        else if (values.size() == 1) {
            return engine->toScriptValue(checkRange(now.month(), values[0], values[0]));
        }

        return engine->undefinedValue();
    }

    // timeRange(hour [, "GMT" ])
    // timeRange(hour1, hour2 [, "GMT" ])
    // timeRange(hour1, min1, hour2, min2 [, "GMT" ])
    // timeRange(hour1, min1, sec1, hour2, min2, sec2 [, "GMT" ])
    // @returns true if the current time (GMT or local based on presence
    // of "GMT" argument) is within the given range
    QScriptValue TimeRange (QScriptContext* context, QScriptEngine* engine)
    {
        if (context->argumentCount() < 1 || context->argumentCount() > 7) {
            return engine->undefinedValue();
        }

        QVector<int> values;
        for (int i = 0; i < context->argumentCount(); ++i) {
            if (!context->argument(i).isNumber()) {
                break;
            }
            values.append(context->argument(i).toNumber());
        }

        const QTime now = getTime(context).time();

        // hour1, min1, sec1, hour2, min2, sec2
        if (values.size() == 6) {
            const QTime t1 (values[0], values[1], values[2]);
            const QTime t2 (values[3], values[4], values[5]);
            return engine->toScriptValue(checkRange(now, t1, t2));
        }
        // hour1, min1, hour2, min2
        else if (values.size() == 4) {
            const QTime t1 (values[0], values[1]);
            const QTime t2 (values[2], values[3]);
            return engine->toScriptValue(checkRange(now, t1, t2));
        }
        // hour1, hour2
        else if (values.size() == 2) {
            return engine->toScriptValue(checkRange(now.hour(), values[0], values[1]));
        }
        // hour
        else if (values.size() == 1) {
            return engine->toScriptValue(checkRange(now.hour(), values[0], values[0]));
        }

        return engine->undefinedValue();
    }


    /*
     * Implementation of Microsoft's IPv6 Extension for PAC
     *
     * Documentation:
     * http://msdn.microsoft.com/en-us/library/gg308477(v=vs.85).aspx
     * http://msdn.microsoft.com/en-us/library/gg308478(v=vs.85).aspx
     * http://msdn.microsoft.com/en-us/library/gg308474(v=vs.85).aspx
     * http://blogs.msdn.com/b/wndp/archive/2006/07/13/ipv6-pac-extensions-v0-9.aspx
     */

    // isResolvableEx(host)
    // @returns true if host is resolvable to an IPv4 or IPv6 address.
    QScriptValue IsResolvableEx (QScriptContext* context, QScriptEngine* engine)
    {
        if (context->argumentCount() != 1) {
            return engine->undefinedValue();
        }

        try {
            const Address info = Address::resolve(context->argument(0).toString());
            bool hasResolvableIPAddress = false;
            Q_FOREACH(const QHostAddress& address, info.addresses()) {
                if (isIPv4Address(address) || isIPv6Address(address)) {
                    hasResolvableIPAddress = true;
                    break;
                }
            }
            return engine->toScriptValue(hasResolvableIPAddress);
        }
        catch (const Address::Error&) {
            return engine->toScriptValue(false);
        }
    }

    // isInNetEx(ipAddress, ipPrefix )
    // @returns true if ipAddress is within the specified ipPrefix.
    QScriptValue IsInNetEx (QScriptContext* context, QScriptEngine* engine)
    {
        if (context->argumentCount() != 2) {
            return engine->undefinedValue();
        }

        try {
            const Address info = Address::resolve(context->argument(0).toString());
            bool isInSubNet = false;
            const QString subnetStr = context->argument(1).toString();
            const QPair<QHostAddress, int> subnet = QHostAddress::parseSubnet(subnetStr);

            Q_FOREACH(const QHostAddress& address, info.addresses()) {
                if (isSpecialAddress(address)) {
                    continue;
                }

                if (address.isInSubnet(subnet)) {
                    isInSubNet = true;
                    break;
                }
            }
            return engine->toScriptValue(isInSubNet);
        }
        catch (const Address::Error&) {
            return engine->toScriptValue(false);
        }
    }

    // dnsResolveEx(host)
    // @returns a semi-colon delimited string containing IPv6 and IPv4 addresses
    // for host or an empty string if host is not resolvable.
    QScriptValue DNSResolveEx (QScriptContext* context, QScriptEngine* engine)
    {
        if (context->argumentCount() != 1) {
            return engine->undefinedValue();
        }

        try {
            const Address info = Address::resolve (context->argument(0).toString());

            QStringList addressList;
            QString resolvedAddress (QLatin1String(""));

            Q_FOREACH(const QHostAddress& address, info.addresses()) {
                if (!isSpecialAddress(address)) {
                    addressList << address.toString();
                }
            }
            if (!addressList.isEmpty()) {
                resolvedAddress = addressList.join(QLatin1String(";"));
            }

            return engine->toScriptValue(resolvedAddress);
        }
        catch (const Address::Error&) {
            return engine->toScriptValue(QString(QLatin1String("")));
        }
    }

    // myIpAddressEx()
    // @returns a semi-colon delimited string containing all IP addresses for localhost (IPv6 and/or IPv4),
    // or an empty string if unable to resolve localhost to an IP address.
    QScriptValue MyIpAddressEx (QScriptContext* context, QScriptEngine* engine)
    {
        if (context->argumentCount()) {
            return engine->undefinedValue();
        }

        QStringList ipAddressList;
        const QList<QHostAddress> addresses = QNetworkInterface::allAddresses();
        Q_FOREACH(const QHostAddress address, addresses) {
            if (!isSpecialAddress(address) && !isLocalHostAddress(address)) {
                ipAddressList << address.toString();
            }
        }

        return engine->toScriptValue(ipAddressList.join(QLatin1String(";")));
    }

    // sortIpAddressList(ipAddressList)
    // @returns a sorted ipAddressList. If both IPv4 and IPv6 addresses are present in
    // the list. The sorted IPv6 addresses will precede the sorted IPv4 addresses.
    QScriptValue SortIpAddressList(QScriptContext* context, QScriptEngine* engine)
    {
        if (context->argumentCount() != 1) {
           return engine->undefinedValue();
        }

        QHash<QString, QString> actualEntryMap;
        QList<QHostAddress> ipV4List, ipV6List;
        const QStringList ipAddressList = context->argument(0).toString().split(QLatin1Char(';'));

        Q_FOREACH(const QString& ipAddress, ipAddressList) {
            QHostAddress address(ipAddress);
            switch (address.protocol()) {
            case QAbstractSocket::IPv4Protocol:
                ipV4List << address;
                actualEntryMap.insert(address.toString(), ipAddress);
                break;
            case QAbstractSocket::IPv6Protocol:
                ipV6List << address;
                actualEntryMap.insert(address.toString(), ipAddress);
                break;
            default:
                break;
            }
        }

        QString sortedAddress (QLatin1String(""));

        if (!ipV6List.isEmpty()) {
            qSort(ipV6List.begin(), ipV6List.end(), addressLessThanComparison);
            sortedAddress += addressListToString(ipV6List, actualEntryMap);
        }

        if (!ipV4List.isEmpty()) {
            qSort(ipV4List.begin(), ipV4List.end(), addressLessThanComparison);
            if (!sortedAddress.isEmpty()) {
                sortedAddress += QLatin1Char(';');
            }
            sortedAddress += addressListToString(ipV4List, actualEntryMap);
        }

        return engine->toScriptValue(sortedAddress);

    }

    // getClientVersion
    // @return the version number of this engine for future extension. We too start
    // this at version 1.0.
    QScriptValue GetClientVersion (QScriptContext* context, QScriptEngine* engine)
    {
        if (context->argumentCount()) {
            return engine->undefinedValue();
        }

        const QString version (QLatin1String("1.0"));
        return engine->toScriptValue(version);
    }

    void registerFunctions(QScriptEngine* engine)
    {
        QScriptValue value = engine->globalObject();
        value.setProperty(QL1S("isPlainHostName"), engine->newFunction(IsPlainHostName));
        value.setProperty(QL1S("dnsDomainIs"), engine->newFunction(DNSDomainIs));
        value.setProperty(QL1S("localHostOrDomainIs"), engine->newFunction(LocalHostOrDomainIs));
        value.setProperty(QL1S("isResolvable"), engine->newFunction(IsResolvable));
        value.setProperty(QL1S("isInNet"), engine->newFunction(IsInNet));
        value.setProperty(QL1S("dnsResolve"), engine->newFunction(DNSResolve));
        value.setProperty(QL1S("myIpAddress"), engine->newFunction(MyIpAddress));
        value.setProperty(QL1S("dnsDomainLevels"), engine->newFunction(DNSDomainLevels));
        value.setProperty(QL1S("shExpMatch"), engine->newFunction(ShExpMatch));
        value.setProperty(QL1S("weekdayRange"), engine->newFunction(WeekdayRange));
        value.setProperty(QL1S("dateRange"), engine->newFunction(DateRange));
        value.setProperty(QL1S("timeRange"), engine->newFunction(TimeRange));

        // Microsoft's IPv6 PAC Extensions...
        value.setProperty(QL1S("isResolvableEx"), engine->newFunction(IsResolvableEx));
        value.setProperty(QL1S("isInNetEx"), engine->newFunction(IsInNetEx));
        value.setProperty(QL1S("dnsResolveEx"), engine->newFunction(DNSResolveEx));
        value.setProperty(QL1S("myIpAddressEx"), engine->newFunction(MyIpAddressEx));
        value.setProperty(QL1S("sortIpAddressList"), engine->newFunction(SortIpAddressList));
        value.setProperty(QL1S("getClientVersion"), engine->newFunction(GetClientVersion));
    }
}

namespace KPAC
{
    Script::Script(const QString& code)
    {
        m_engine = new QScriptEngine;
        registerFunctions(m_engine);

        QScriptProgram program (code);
        const QScriptValue result = m_engine->evaluate(program);
        if (m_engine->hasUncaughtException() || result.isError())
            throw Error(m_engine->uncaughtException().toString());
    }

    Script::~Script()
    {
        delete m_engine;
    }

    QString Script::evaluate(const KUrl& url)
    {
        QScriptValue func = m_engine->globalObject().property(QL1S("FindProxyForURL"));

        if (!func.isValid()) {
            func = m_engine->globalObject().property(QL1S("FindProxyForURLEx"));
            if (!func.isValid()) {
                throw Error(i18n("Could not find 'FindProxyForURL' or 'FindProxyForURLEx'"));
                return QString();
            }
        }

        KUrl cleanUrl = url;
        cleanUrl.setUserInfo(QString());
        if (cleanUrl.scheme().toLower() == QLatin1String("https")) {
            cleanUrl.setPath(QString());
            cleanUrl.setQuery(QString());
        }

        QScriptValueList args;
        args << cleanUrl.url();
        args << cleanUrl.host();

        QScriptValue result = func.call(QScriptValue(), args);
        if (result.isError()) {
            throw Error(i18n("Got an invalid reply when calling %1", func.toString()));
        }

        return result.toString();
    }
}

// vim: ts=4 sw=4 et