summaryrefslogtreecommitdiffstats
path: root/comm/chat/protocols/xmpp/xmpp-base.sys.mjs
blob: f7e0ccd98ee0d4338df2b99f805690c4c3eee783 (plain)
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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { clearTimeout, setTimeout } from "resource://gre/modules/Timer.sys.mjs";

import { IMServices } from "resource:///modules/IMServices.sys.mjs";
import { Status } from "resource:///modules/imStatusUtils.sys.mjs";
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
import {
  executeSoon,
  nsSimpleEnumerator,
  EmptyEnumerator,
  ClassInfo,
  l10nHelper,
} from "resource:///modules/imXPCOMUtils.sys.mjs";
import {
  GenericAccountPrototype,
  GenericAccountBuddyPrototype,
  GenericConvIMPrototype,
  GenericConvChatPrototype,
  GenericConversationPrototype,
  TooltipInfo,
} from "resource:///modules/jsProtoHelper.sys.mjs";
import { NormalizedMap } from "resource:///modules/NormalizedMap.sys.mjs";
import {
  Stanza,
  SupportedFeatures,
} from "resource:///modules/xmpp-xml.sys.mjs";
import { XMPPSession } from "resource:///modules/xmpp-session.sys.mjs";

const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
  DownloadUtils: "resource://gre/modules/DownloadUtils.sys.mjs",
  FileUtils: "resource://gre/modules/FileUtils.sys.mjs",
});
ChromeUtils.defineModuleGetter(
  lazy,
  "NetUtil",
  "resource://gre/modules/NetUtil.jsm"
);
XPCOMUtils.defineLazyServiceGetter(
  lazy,
  "imgTools",
  "@mozilla.org/image/tools;1",
  "imgITools"
);

XPCOMUtils.defineLazyGetter(lazy, "_", () =>
  l10nHelper("chrome://chat/locale/xmpp.properties")
);

XPCOMUtils.defineLazyGetter(lazy, "TXTToHTML", function () {
  let cs = Cc["@mozilla.org/txttohtmlconv;1"].getService(Ci.mozITXTToHTMLConv);
  return aTxt => cs.scanTXT(aTxt, cs.kEntities);
});

// Parses the status from a presence stanza into an object of statusType,
// statusText and idleSince.
function parseStatus(aStanza) {
  let statusType = Ci.imIStatusInfo.STATUS_AVAILABLE;
  let show = aStanza.getElement(["show"]);
  if (show) {
    show = show.innerText;
    if (show == "away") {
      statusType = Ci.imIStatusInfo.STATUS_AWAY;
    } else if (show == "chat") {
      statusType = Ci.imIStatusInfo.STATUS_AVAILABLE; // FIXME
    } else if (show == "dnd") {
      statusType = Ci.imIStatusInfo.STATUS_UNAVAILABLE;
    } else if (show == "xa") {
      statusType = Ci.imIStatusInfo.STATUS_IDLE;
    }
  }

  let idleSince = 0;
  let date = _getDelay(aStanza);
  if (date) {
    idleSince = date.getTime();
  }

  let query = aStanza.getElement(["query"]);
  if (query && query.uri == Stanza.NS.last) {
    let now = Math.floor(Date.now() / 1000);
    idleSince = now - parseInt(query.attributes.seconds, 10);
    statusType = Ci.imIStatusInfo.STATUS_IDLE;
  }

  let status = aStanza.getElement(["status"]);
  status = status ? status.innerText : "";

  return { statusType, statusText: status, idleSince };
}

// Returns a Date object for the delay value (stamp) in aStanza if it exists,
// otherwise returns undefined.
function _getDelay(aStanza) {
  // XEP-0203: Delayed Delivery.
  let date;
  let delay = aStanza.getElement(["delay"]);
  if (delay && delay.uri == Stanza.NS.delay) {
    if (delay.attributes.stamp) {
      date = new Date(delay.attributes.stamp);
    }
  }
  if (date && isNaN(date.getTime())) {
    return undefined;
  }

  return date;
}

// Writes aMsg in aConv as an outgoing message with optional date as the
// message may be sent from another client.
function _displaySentMsg(aConv, aMsg, aDate) {
  let who;
  if (aConv._account._connection) {
    who = aConv._account._connection._jid.jid;
  }
  if (!who) {
    who = aConv._account.name;
  }

  let flags = { outgoing: true };
  flags._alias = aConv.account.alias || aConv.account.statusInfo.displayName;

  if (aDate) {
    flags.time = aDate / 1000;
    flags.delayed = true;
  }
  aConv.writeMessage(who, aMsg, flags);
}

// The timespan after which we consider roomInfo to be stale.
var kListRefreshInterval = 12 * 60 * 60 * 1000; // 12 hours.

/* This is an ordered list, used to determine chat buddy flags:
 *  index = member    -> voiced
 *          moderator -> moderator
 *          admin     -> admin
 *          owner     -> founder
 */
var kRoles = [
  "outcast",
  "visitor",
  "participant",
  "member",
  "moderator",
  "admin",
  "owner",
];

function MUCParticipant(aNick, aJid, aPresenceStanza) {
  this._jid = aJid;
  this.name = aNick;
  this.onPresenceStanza(aPresenceStanza);
}
MUCParticipant.prototype = {
  __proto__: ClassInfo("prplIConvChatBuddy", "XMPP ConvChatBuddy object"),

  buddy: false,

  // The occupant jid of the participant which is of the form room@domain/nick.
  _jid: null,

  // The real jid of the participant which is of the form local@domain/resource.
  accountJid: null,

  statusType: null,
  statusText: null,
  get alias() {
    return this.name;
  },

  role: 2, // "participant" by default

  // Called when a presence stanza is received for this participant.
  onPresenceStanza(aStanza) {
    let statusInfo = parseStatus(aStanza);
    this.statusType = statusInfo.statusType;
    this.statusText = statusInfo.statusText;

    let x = aStanza.children.filter(
      child => child.localName == "x" && child.uri == Stanza.NS.muc_user
    );
    if (x.length == 0) {
      return;
    }

    // XEP-0045 (7.2.3): We only expect a single <x/> element of this namespace,
    // so we ignore any others.
    x = x[0];

    let item = x.getElement(["item"]);
    if (!item) {
      return;
    }

    this.role = Math.max(
      kRoles.indexOf(item.attributes.role),
      kRoles.indexOf(item.attributes.affiliation)
    );

    let accountJid = item.attributes.jid;
    if (accountJid) {
      this.accountJid = accountJid;
    }
  },

  get voiced() {
    /* FIXME: The "voiced" role corresponds to users that can send messages to
     * the room. If the chat is unmoderated, this should include everyone, not
     * just members. */
    return this.role == kRoles.indexOf("member");
  },
  get moderator() {
    return this.role == kRoles.indexOf("moderator");
  },
  get admin() {
    return this.role == kRoles.indexOf("admin");
  },
  get founder() {
    return this.role == kRoles.indexOf("owner");
  },
  typing: false,
};

// MUC (Multi-User Chat)
export var XMPPMUCConversationPrototype = {
  __proto__: GenericConvChatPrototype,
  // By default users are not in a MUC.
  _left: true,

  // Tracks all received messages to avoid possible duplication if the server
  // sends us the last few messages again when we rejoin a room.
  _messageIds: new Set(),

  _init(aAccount, aJID, aNick) {
    this._messageIds = new Set();
    GenericConvChatPrototype._init.call(this, aAccount, aJID, aNick);
  },

  _targetResource: "",

  // True while we are rejoining a room previously parted by the user.
  _rejoined: false,

  get topic() {
    return this._topic;
  },
  set topic(aTopic) {
    // XEP-0045 (8.1): Modifying the room subject.
    let subject = Stanza.node("subject", null, null, aTopic.trim());
    let s = Stanza.message(
      this.name,
      null,
      null,
      { type: "groupchat" },
      subject
    );
    let notAuthorized = lazy._(
      "conversation.error.changeTopicFailedNotAuthorized"
    );
    this._account.sendStanza(
      s,
      this._account.handleErrors(
        {
          forbidden: notAuthorized,
          notAcceptable: notAuthorized,
          itemNotFound: notAuthorized,
        },
        this
      )
    );
  },
  get topicSettable() {
    return true;
  },

  /* Called when the user enters a chat message */
  dispatchMessage(aMsg, aAction = false) {
    if (aAction) {
      // XEP-0245: The /me Command.
      // We need to prepend "/me " as the first four characters of the message
      // body.
      aMsg = "/me " + aMsg;
    }
    // XEP-0045 (7.4): Sending a message to all occupants in a room.
    let s = Stanza.message(this.name, aMsg, null, { type: "groupchat" });
    let notInRoom = lazy._(
      "conversation.error.sendFailedAsNotInRoom",
      this.name,
      aMsg
    );
    this._account.sendStanza(
      s,
      this._account.handleErrors(
        {
          itemNotFound: notInRoom,
          notAcceptable: notInRoom,
        },
        this
      )
    );
  },

  /* Called by the account when a presence stanza is received for this muc */
  onPresenceStanza(aStanza) {
    let from = aStanza.attributes.from;
    let nick = this._account._parseJID(from).resource;
    let jid = this._account.normalize(from);
    let x = aStanza
      .getElements(["x"])
      .find(
        e => e.uri == Stanza.NS.muc_user || e.uri == Stanza.NS.vcard_update
      );

    // Check if the join failed.
    if (this.left && aStanza.attributes.type == "error") {
      let error = this._account.parseError(aStanza);
      let message;
      switch (error.condition) {
        case "not-authorized":
        case "registration-required":
          // XEP-0045 (7.2.7): Members-Only Rooms.
          message = lazy._("conversation.error.joinFailedNotAuthorized");
          break;
        case "not-allowed":
          message = lazy._("conversation.error.creationFailedNotAllowed");
          break;
        case "remote-server-not-found":
          message = lazy._(
            "conversation.error.joinFailedRemoteServerNotFound",
            this.name
          );
          break;
        case "forbidden":
          // XEP-0045 (7.2.8): Banned users.
          message = lazy._("conversation.error.joinForbidden", this.name);
          break;
        default:
          message = lazy._("conversation.error.joinFailed", this.name);
          this.ERROR("Failed to join MUC: " + aStanza.convertToString());
          break;
      }
      this.writeMessage(this.name, message, { system: true, error: true });
      this.joining = false;
      return;
    }

    if (!x) {
      this.WARN(
        "Received a MUC presence stanza without an x element or " +
          "with a namespace we don't handle."
      );
      return;
    }
    // Handle a MUC resource avatar
    if (
      x.uri == Stanza.NS.vcard_update &&
      aStanza.attributes.from == this.normalizedName
    ) {
      let photo = aStanza.getElement(["x", "photo"]);
      if (photo && photo.uri == Stanza.NS.vcard_update) {
        let hash = photo.innerText;
        if (hash && hash != this._photoHash) {
          this._account._addVCardRequest(this.normalizedName);
        } else if (!hash && this._photoHash) {
          delete this._photoHash;
          this.convIconFilename = "";
        }
      }
      return;
    }
    let codes = x.getElements(["status"]).map(elt => elt.attributes.code);
    let item = x.getElement(["item"]);

    // Changes the nickname of a participant for this muc.
    let changeNick = () => {
      if (!item || !item.attributes.nick) {
        this.WARN(
          "Received a MUC presence code 303 or 210 stanza without an " +
            "item element or a nick attribute."
        );
        return;
      }
      let newNick = item.attributes.nick;
      this.updateNick(nick, newNick, nick == this.nick);
    };

    if (aStanza.attributes.type == "unavailable") {
      if (!this._participants.has(nick)) {
        this.WARN(
          "received unavailable presence for an unknown MUC participant: " +
            from
        );
        return;
      }
      if (codes.includes("303")) {
        // XEP-0045 (7.6): Changing Nickname.
        // Service Updates Nick for user.
        changeNick();
        return;
      }
      if (item && item.attributes.role == "none") {
        // XEP-0045: an occupant has left the room.
        this.removeParticipant(nick);

        // Who caused the participant to leave the room.
        let actor = item.getElement(["actor"]);
        let actorNick = actor ? actor.attributes.nick : "";
        let isActor = actorNick ? ".actor" : "";

        // Why the participant left.
        let reasonNode = item.getElement(["reason"]);
        let reason = reasonNode ? reasonNode.innerText : "";
        let isReason = reason ? ".reason" : "";

        let isYou = nick == this.nick ? ".you" : "";
        let affectedNick = isYou ? "" : nick;
        if (isYou) {
          this.left = true;
        }

        let message;
        if (codes.includes("301")) {
          // XEP-0045 (9.1): Banning a User.
          message = "conversation.message.banned";
        } else if (codes.includes("307")) {
          // XEP-0045 (8.2): Kicking an Occupant.
          message = "conversation.message.kicked";
        } else if (codes.includes("322") || codes.includes("321")) {
          // XEP-0045: Inform user that he or she is being removed from the
          // room because the room has been changed to members-only and the
          // user is not a member.
          message = "conversation.message.removedNonMember";
        } else if (codes.includes("332")) {
          // XEP-0045: Inform user that he or she is being removed from the
          // room because the MUC service is being shut down.
          message = "conversation.message.mucShutdown";

          // The reason here just duplicates what's in the system message.
          reason = isReason = "";
        } else {
          // XEP-0045 (7.14): Received when the user parts a room.
          message = "conversation.message.parted";

          // The reason is in a status element in this case.
          reasonNode = aStanza.getElement(["status"]);
          reason = reasonNode ? reasonNode.innerText : "";
          isReason = reason ? ".reason" : "";
        }

        if (message) {
          let messageID = message + isYou + isActor + isReason;
          let params = [actorNick, affectedNick, reason].filter(s => s);
          this.writeMessage(this.name, lazy._(messageID, ...params), {
            system: true,
          });
        }
      } else {
        this.WARN("Unhandled type==unavailable MUC presence stanza.");
      }
      return;
    }

    if (codes.includes("201")) {
      // XEP-0045 (10.1): Creating room.
      // Service Acknowledges Room Creation
      // and Room is awaiting configuration.
      // XEP-0045 (10.1.2): Instant room.
      let query = Stanza.node(
        "query",
        Stanza.NS.muc_owner,
        null,
        Stanza.node("x", Stanza.NS.xdata, { type: "submit" })
      );
      let s = Stanza.iq("set", null, jid, query);
      this._account.sendStanza(s, aStanzaReceived => {
        if (aStanzaReceived.attributes.type != "result") {
          return false;
        }

        // XEP-0045: Service Informs New Room Owner of Success
        // for instant and reserved rooms.
        this.left = false;
        this.joining = false;
        return true;
      });
    } else if (codes.includes("210")) {
      // XEP-0045 (7.6): Changing Nickname.
      // Service modifies this user's nickname in accordance with local service
      // policies.
      changeNick();
      return;
    } else if (codes.includes("110")) {
      // XEP-0045: Room exists and joined successfully.
      this.left = false;
      this.joining = false;
      // TODO (Bug 1172350): Implement Service Discovery Extensions (XEP-0128) to obtain
      // configuration of this room.
    } else if (codes.includes("104") && nick == this.name) {
      // https://xmpp.org/extensions/inbox/muc-avatars.html (XEP-XXXX)
      this._account._addVCardRequest(this.normalizedName);
    }

    if (!this._participants.get(nick)) {
      let participant = new MUCParticipant(nick, from, aStanza);
      this._participants.set(nick, participant);
      this.notifyObservers(
        new nsSimpleEnumerator([participant]),
        "chat-buddy-add"
      );
      if (this.nick != nick && !this.joining) {
        this.writeMessage(
          this.name,
          lazy._("conversation.message.join", nick),
          {
            system: true,
          }
        );
      } else if (this.nick == nick && this._rejoined) {
        this.writeMessage(this.name, lazy._("conversation.message.rejoined"), {
          system: true,
        });
        this._rejoined = false;
      }
    } else {
      this._participants.get(nick).onPresenceStanza(aStanza);
      this.notifyObservers(this._participants.get(nick), "chat-buddy-update");
    }
  },

  /* Called by the account when a message is received for this muc */
  incomingMessage(aMsg, aStanza, aDate) {
    let from = this._account._parseJID(aStanza.attributes.from).resource;
    let id = aStanza.attributes.id;
    let flags = {};
    if (!from) {
      flags.system = true;
      from = this.name;
    } else if (aStanza.attributes.type == "error") {
      aMsg = lazy._("conversation.error.notDelivered", aMsg);
      flags.system = true;
      flags.error = true;
    } else if (from == this._nick) {
      flags.outgoing = true;
    } else {
      flags.incoming = true;
    }
    if (aDate) {
      flags.time = aDate / 1000;
      flags.delayed = true;
    }
    if (id) {
      // Checks if a message exists in conversation to avoid duplication.
      if (this._messageIds.has(id)) {
        return;
      }
      this._messageIds.add(id);
    }
    this.writeMessage(from, aMsg, flags);
  },

  getNormalizedChatBuddyName(aNick) {
    return this._account.normalizeFullJid(this.name + "/" + aNick);
  },

  // Leaves MUC conversation.
  part(aMsg = null) {
    let s = Stanza.presence(
      { to: this.name + "/" + this._nick, type: "unavailable" },
      aMsg ? Stanza.node("status", null, null, aMsg.trim()) : null
    );
    this._account.sendStanza(s);
    delete this.chatRoomFields;
  },

  // Invites a user to MUC conversation.
  invite(aJID, aMsg = null) {
    // XEP-0045 (7.8): Inviting Another User to a Room.
    // XEP-0045 (7.8.2): Mediated Invitation.
    let invite = Stanza.node(
      "invite",
      null,
      { to: aJID },
      aMsg ? Stanza.node("reason", null, null, aMsg) : null
    );
    let x = Stanza.node("x", Stanza.NS.muc_user, null, invite);
    let s = Stanza.node("message", null, { to: this.name }, x);
    this._account.sendStanza(
      s,
      this._account.handleErrors(
        {
          forbidden: lazy._("conversation.error.inviteFailedForbidden"),
          // ejabberd uses error not-allowed to indicate that this account does not
          // have the required privileges to invite users instead of forbidden error,
          // and this is not mentioned in the spec (XEP-0045).
          notAllowed: lazy._("conversation.error.inviteFailedForbidden"),
          itemNotFound: lazy._("conversation.error.failedJIDNotFound", aJID),
        },
        this
      )
    );
  },

  // Bans a participant from MUC conversation.
  ban(aNickName, aMsg = null) {
    // XEP-0045 (9.1): Banning a User.
    let participant = this._participants.get(aNickName);
    if (!participant) {
      this.writeMessage(
        this.name,
        lazy._("conversation.error.nickNotInRoom", aNickName),
        { system: true }
      );
      return;
    }
    if (!participant.accountJid) {
      this.writeMessage(
        this.name,
        lazy._("conversation.error.banCommandAnonymousRoom"),
        { system: true }
      );
      return;
    }

    let attributes = { affiliation: "outcast", jid: participant.accountJid };
    let item = Stanza.node(
      "item",
      null,
      attributes,
      aMsg ? Stanza.node("reason", null, null, aMsg) : null
    );
    let s = Stanza.iq(
      "set",
      null,
      this.name,
      Stanza.node("query", Stanza.NS.muc_admin, null, item)
    );
    this._account.sendStanza(s, this._banKickHandler, this);
  },

  // Kicks a participant from MUC conversation.
  kick(aNickName, aMsg = null) {
    // XEP-0045 (8.2): Kicking an Occupant.
    let attributes = { role: "none", nick: aNickName };
    let item = Stanza.node(
      "item",
      null,
      attributes,
      aMsg ? Stanza.node("reason", null, null, aMsg) : null
    );
    let s = Stanza.iq(
      "set",
      null,
      this.name,
      Stanza.node("query", Stanza.NS.muc_admin, null, item)
    );
    this._account.sendStanza(s, this._banKickHandler, this);
  },

  // Callback for ban and kick commands.
  _banKickHandler(aStanza) {
    return this._account._handleResult(
      {
        notAllowed: lazy._("conversation.error.banKickCommandNotAllowed"),
        conflict: lazy._("conversation.error.banKickCommandConflict"),
      },
      this
    )(aStanza);
  },

  // Changes nick in MUC conversation to a new one.
  setNick(aNewNick) {
    // XEP-0045 (7.6): Changing Nickname.
    let s = Stanza.presence({ to: this.name + "/" + aNewNick }, null);
    this._account.sendStanza(
      s,
      this._account.handleErrors(
        {
          // XEP-0045 (7.6): Changing Nickname (example 53).
          // TODO: We should discover if the user has a reserved nickname (maybe
          // before joining a room), cf. XEP-0045 (7.12).
          notAcceptable: lazy._(
            "conversation.error.changeNickFailedNotAcceptable",
            aNewNick
          ),
          // XEP-0045 (7.2.9): Nickname Conflict.
          conflict: lazy._(
            "conversation.error.changeNickFailedConflict",
            aNewNick
          ),
        },
        this
      )
    );
  },

  // Called by the account when a message stanza is received for this muc and
  // needs to be handled.
  onMessageStanza(aStanza) {
    let x = aStanza.getElement(["x"]);
    let decline = x.getElement(["decline"]);
    if (decline) {
      // XEP-0045 (7.8): Inviting Another User to a Room.
      // XEP-0045 (7.8.2): Mediated Invitation.
      let invitee = decline.attributes.jid;
      let reasonNode = decline.getElement(["reason"]);
      let reason = reasonNode ? reasonNode.innerText : "";
      let msg;
      if (reason) {
        msg = lazy._(
          "conversation.message.invitationDeclined.reason",
          invitee,
          reason
        );
      } else {
        msg = lazy._("conversation.message.invitationDeclined", invitee);
      }

      this.writeMessage(this.name, msg, { system: true });
    } else {
      this.WARN("Unhandled message stanza.");
    }
  },

  /* Called when the user closed the conversation */
  close() {
    if (!this.left) {
      this.part();
    }
    GenericConvChatPrototype.close.call(this);
  },
  unInit() {
    this._account.removeConversation(this.name);
    GenericConvChatPrototype.unInit.call(this);
  },

  _photoHash: null,
  _saveIcon(aPhotoNode) {
    this._account._saveResourceIcon(aPhotoNode, this).then(
      url => {
        this.convIconFilename = url;
      },
      error => {
        this._account.WARN(
          "Error while loading conversation icon for " +
            this.normalizedName +
            ": " +
            error.message
        );
      }
    );
  },
};

function XMPPMUCConversation(aAccount, aJID, aNick) {
  this._init(aAccount, aJID, aNick);
}
XMPPMUCConversation.prototype = XMPPMUCConversationPrototype;

/* Helper class for buddy conversations */
export var XMPPConversationPrototype = {
  __proto__: GenericConvIMPrototype,

  _typingTimer: null,
  supportChatStateNotifications: true,
  _typingState: "active",

  // Indicates that current conversation is with a MUC participant and the
  // recipient jid (stored in the userName) is of the form room@domain/nick.
  _isMucParticipant: false,

  get buddy() {
    return this._account._buddies.get(this.name);
  },
  get title() {
    return this.contactDisplayName;
  },
  get contactDisplayName() {
    return this.buddy ? this.buddy.contactDisplayName : this.name;
  },
  get userName() {
    return this.buddy ? this.buddy.userName : this.name;
  },

  // Returns jid (room@domain/nick) if it is with a MUC participant, and the
  // name of conversation otherwise.
  get normalizedName() {
    if (this._isMucParticipant) {
      return this._account.normalizeFullJid(this.name);
    }
    return this._account.normalize(this.name);
  },

  // Used to avoid showing full jids in typing notifications.
  get shortName() {
    if (this.buddy) {
      return this.buddy.contactDisplayName;
    }

    let jid = this._account._parseJID(this.name);
    if (!jid) {
      return this.name;
    }

    // Returns nick of the recipient if conversation is with a participant of
    // a MUC we are in as jid of the recipient is of the form room@domain/nick.
    if (this._isMucParticipant) {
      return jid.resource;
    }

    return jid.node;
  },

  get shouldSendTypingNotifications() {
    return (
      this.supportChatStateNotifications &&
      Services.prefs.getBoolPref("purple.conversations.im.send_typing")
    );
  },

  /* Called when the user is typing a message
   * aString - the currently typed message
   * Returns the number of characters that can still be typed */
  sendTyping(aString) {
    if (!this.shouldSendTypingNotifications) {
      return Ci.prplIConversation.NO_TYPING_LIMIT;
    }

    this._cancelTypingTimer();
    if (aString.length) {
      this._typingTimer = setTimeout(this.finishedComposing.bind(this), 10000);
    }

    this._setTypingState(aString.length ? "composing" : "active");

    return Ci.prplIConversation.NO_TYPING_LIMIT;
  },

  finishedComposing() {
    if (!this.shouldSendTypingNotifications) {
      return;
    }

    this._setTypingState("paused");
  },

  _setTypingState(aNewState) {
    if (this._typingState == aNewState) {
      return;
    }

    let s = Stanza.message(this.to, null, aNewState);

    // We don't care about errors in response to typing notifications
    // (e.g. because the user has left the room when talking to a MUC
    // participant).
    this._account.sendStanza(s, () => true);

    this._typingState = aNewState;
  },
  _cancelTypingTimer() {
    if (this._typingTimer) {
      clearTimeout(this._typingTimer);
      delete this._typingTimer;
    }
  },

  // Holds the resource of user that you are currently talking to, but if the
  // user is a participant of a MUC we are in, holds the nick of user you are
  // talking to.
  _targetResource: "",

  get to() {
    if (!this._targetResource || this._isMucParticipant) {
      return this.userName;
    }
    return this.userName + "/" + this._targetResource;
  },

  /* Called when the user enters a chat message */
  dispatchMessage(aMsg, aAction = false) {
    if (aAction) {
      // XEP-0245: The /me Command.
      // We need to prepend "/me " as the first four characters of the message
      // body.
      aMsg = "/me" + aMsg;
    }
    this._cancelTypingTimer();
    let cs = this.shouldSendTypingNotifications ? "active" : null;
    let s = Stanza.message(this.to, aMsg, cs);
    this._account.sendStanza(s);
    _displaySentMsg(this, aMsg);
    delete this._typingState;
  },

  // Invites the contact to a MUC room.
  invite(aRoomJid, aPassword = null) {
    // XEP-0045 (7.8): Inviting Another User to a Room.
    // XEP-0045 (7.8.1) and XEP-0249: Direct Invitation.
    let x = Stanza.node("x", Stanza.NS.conference, {
      jid: aRoomJid,
      password: aPassword,
    });
    this._account.sendStanza(Stanza.node("message", null, { to: this.to }, x));
  },

  // Query the user for its Software Version.
  // XEP-0092: Software Version.
  getVersion() {
    // TODO: Use Service Discovery to determine if the user's client supports
    // jabber:iq:version protocol.

    let s = Stanza.iq(
      "get",
      null,
      this.to,
      Stanza.node("query", Stanza.NS.version)
    );
    this._account.sendStanza(s, aStanza => {
      // TODO: handle other errors that can result from querying
      // user for its software version.
      if (
        this._account.handleErrors(
          {
            default: lazy._("conversation.error.version.unknown"),
          },
          this
        )(aStanza)
      ) {
        return;
      }

      let query = aStanza.getElement(["query"]);
      if (!query || query.uri != Stanza.NS.version) {
        this.WARN(
          "Received a response to version query which does not " +
            "contain query element or 'jabber:iq:version' namespace."
        );
        return;
      }

      let name = query.getElement(["name"]);
      let version = query.getElement(["version"]);
      if (!name || !version) {
        // XEP-0092: name and version elements are REQUIRED.
        this.WARN(
          "Received a response to version query which does not " +
            "contain name or version."
        );
        return;
      }

      let messageID = "conversation.message.version";
      let params = [this.shortName, name.innerText, version.innerText];

      // XEP-0092: os is OPTIONAL.
      let os = query.getElement(["os"]);
      if (os) {
        params.push(os.innerText);
        messageID += "WithOS";
      }

      this.writeMessage(this.name, lazy._(messageID, ...params), {
        system: true,
      });
    });
  },

  /* Perform entity escaping before displaying the message. We assume incoming
     messages have already been escaped, and will otherwise be filtered. */
  prepareForDisplaying(aMsg) {
    if (aMsg.outgoing && !aMsg.system) {
      aMsg.displayMessage = lazy.TXTToHTML(aMsg.displayMessage);
    }
    GenericConversationPrototype.prepareForDisplaying.apply(this, arguments);
  },

  /* Called by the account when a message is received from the buddy */
  incomingMessage(aMsg, aStanza, aDate) {
    let from = aStanza.attributes.from;
    this._targetResource = this._account._parseJID(from).resource;
    let flags = {};
    let error = this._account.parseError(aStanza);
    if (error) {
      let norm = this._account.normalize(from);
      let muc = this._account._mucs.get(norm);

      if (!aMsg) {
        // Failed outgoing message.
        switch (error.condition) {
          case "remote-server-not-found":
            aMsg = lazy._("conversation.error.remoteServerNotFound");
            break;
          case "service-unavailable":
            aMsg = lazy._(
              "conversation.error.sendServiceUnavailable",
              this.shortName
            );
            break;
          default:
            aMsg = lazy._("conversation.error.unknownSendError");
            break;
        }
      } else if (
        this._isMucParticipant &&
        muc &&
        !muc.left &&
        error.condition == "item-not-found"
      ) {
        // XEP-0045 (7.5): MUC private messages.
        // If we try to send to participant not in a room we are in.
        aMsg = lazy._(
          "conversation.error.sendFailedAsRecipientNotInRoom",
          this._targetResource,
          aMsg
        );
      } else if (
        this._isMucParticipant &&
        (error.condition == "item-not-found" ||
          error.condition == "not-acceptable")
      ) {
        // If we left a room and try to send to a participant in it or the
        // room is removed.
        aMsg = lazy._(
          "conversation.error.sendFailedAsNotInRoom",
          this._account.normalize(from),
          aMsg
        );
      } else {
        aMsg = lazy._("conversation.error.notDelivered", aMsg);
      }
      flags.system = true;
      flags.error = true;
    } else {
      flags = { incoming: true, _alias: this.contactDisplayName };
      // XEP-0245: The /me Command.
      if (aMsg.startsWith("/me ")) {
        flags.action = true;
        aMsg = aMsg.slice(4);
      }
    }
    if (aDate) {
      flags.time = aDate / 1000;
      flags.delayed = true;
    }
    this.writeMessage(from, aMsg, flags);
  },

  /* Called when the user closed the conversation */
  close() {
    // TODO send the stanza indicating we have left the conversation?
    GenericConvIMPrototype.close.call(this);
  },
  unInit() {
    this._account.removeConversation(this.normalizedName);
    GenericConvIMPrototype.unInit.call(this);
  },
};

// Creates XMPP conversation.
function XMPPConversation(aAccount, aNormalizedName, aMucParticipant) {
  this._init(aAccount, aNormalizedName);
  if (aMucParticipant) {
    this._isMucParticipant = true;
  }
}
XMPPConversation.prototype = XMPPConversationPrototype;

/* Helper class for buddies */
export var XMPPAccountBuddyPrototype = {
  __proto__: GenericAccountBuddyPrototype,

  subscription: "none",
  // Returns a list of TooltipInfo objects to be displayed when the user
  // hovers over the buddy.
  getTooltipInfo() {
    if (!this._account.connected) {
      return null;
    }

    let tooltipInfo = [];
    if (this._resources) {
      for (let r in this._resources) {
        let status = this._resources[r];
        let statusString = Status.toLabel(status.statusType);
        if (
          status.statusType == Ci.imIStatusInfo.STATUS_IDLE &&
          status.idleSince
        ) {
          let now = Math.floor(Date.now() / 1000);
          let valuesAndUnits = lazy.DownloadUtils.convertTimeUnits(
            now - status.idleSince
          );
          if (!valuesAndUnits[2]) {
            valuesAndUnits.splice(2, 2);
          }
          statusString += " (" + valuesAndUnits.join(" ") + ")";
        }
        if (status.statusText) {
          statusString += " - " + status.statusText;
        }
        let label = r
          ? lazy._("tooltip.status", r)
          : lazy._("tooltip.statusNoResource");
        tooltipInfo.push(new TooltipInfo(label, statusString));
      }
    }

    // The subscription value is interesting to display only in unusual cases.
    if (this.subscription != "both") {
      tooltipInfo.push(
        new TooltipInfo(lazy._("tooltip.subscription"), this.subscription)
      );
    }

    return tooltipInfo;
  },

  // _rosterAlias is the value stored in the roster on the XMPP
  // server. For most servers we will be read/write.
  _rosterAlias: "",
  set rosterAlias(aNewAlias) {
    let old = this.displayName;
    this._rosterAlias = aNewAlias;
    if (old != this.displayName) {
      this._notifyObservers("display-name-changed", old);
    }
  },
  _vCardReceived: false,
  // _vCardFormattedName is the display name the contact has set for
  // himself in his vCard. It's read-only from our point of view.
  _vCardFormattedName: "",
  set vCardFormattedName(aNewFormattedName) {
    let old = this.displayName;
    this._vCardFormattedName = aNewFormattedName;
    if (old != this.displayName) {
      this._notifyObservers("display-name-changed", old);
    }
  },

  // _serverAlias is set by jsProtoHelper to the value we cached in sqlite.
  // Use it only if we have neither of the other two values; usually because
  // we haven't connected to the server yet.
  get serverAlias() {
    return this._rosterAlias || this._vCardFormattedName || this._serverAlias;
  },
  set serverAlias(aNewAlias) {
    if (!this._rosterItem) {
      this.ERROR(
        "attempting to update the server alias of an account buddy " +
          "for which we haven't received a roster item."
      );
      return;
    }

    let item = this._rosterItem;
    if (aNewAlias) {
      item.attributes.name = aNewAlias;
    } else if ("name" in item.attributes) {
      delete item.attributes.name;
    }

    let s = Stanza.iq(
      "set",
      null,
      null,
      Stanza.node("query", Stanza.NS.roster, null, item)
    );
    this._account.sendStanza(s);

    // If we are going to change the alias on the server, discard the cached
    // value that we got from our local sqlite storage at startup.
    delete this._serverAlias;
  },

  /* Display name of the buddy */
  get contactDisplayName() {
    return this.buddy.contact.displayName || this.displayName;
  },

  get tag() {
    return this._tag;
  },
  set tag(aNewTag) {
    let oldTag = this._tag;
    if (oldTag.name == aNewTag.name) {
      this.ERROR("attempting to set the tag to the same value");
      return;
    }

    this._tag = aNewTag;
    IMServices.contacts.accountBuddyMoved(this, oldTag, aNewTag);

    if (!this._rosterItem) {
      this.ERROR(
        "attempting to change the tag of an account buddy without roster item"
      );
      return;
    }

    let item = this._rosterItem;
    let oldXML = item.getXML();
    // Remove the old tag if it was listed in the roster item.
    item.children = item.children.filter(
      c => c.qName != "group" || c.innerText != oldTag.name
    );
    // Ensure the new tag is listed.
    let newTagName = aNewTag.name;
    if (!item.getChildren("group").some(g => g.innerText == newTagName)) {
      item.addChild(Stanza.node("group", null, null, newTagName));
    }
    // Avoid sending anything to the server if the roster item hasn't changed.
    // It's possible that the roster item hasn't changed if the roster
    // item had several groups and the user moved locally the contact
    // to another group where it already was on the server.
    if (item.getXML() == oldXML) {
      return;
    }

    let s = Stanza.iq(
      "set",
      null,
      null,
      Stanza.node("query", Stanza.NS.roster, null, item)
    );
    this._account.sendStanza(s);
  },

  remove() {
    if (!this._account.connected) {
      return;
    }

    let s = Stanza.iq(
      "set",
      null,
      null,
      Stanza.node(
        "query",
        Stanza.NS.roster,
        null,
        Stanza.node("item", null, {
          jid: this.normalizedName,
          subscription: "remove",
        })
      )
    );
    this._account.sendStanza(s);
  },

  _photoHash: null,
  _saveIcon(aPhotoNode) {
    this._account._saveResourceIcon(aPhotoNode, this).then(
      url => {
        this.buddyIconFilename = url;
      },
      error => {
        this._account.WARN(
          "Error loading buddy icon for " +
            this.normalizedName +
            ": " +
            error.message
        );
      }
    );
  },

  _preferredResource: undefined,
  _resources: null,
  onAccountDisconnected() {
    delete this._preferredResource;
    delete this._resources;
  },
  // Called by the account when a presence stanza is received for this buddy.
  onPresenceStanza(aStanza) {
    let preferred = this._preferredResource;

    // Facebook chat's XMPP server doesn't send resources, let's
    // replace undefined resources with empty resources.
    let resource =
      this._account._parseJID(aStanza.attributes.from).resource || "";

    let type = aStanza.attributes.type;

    // Reset typing status if the buddy is in a conversation and becomes unavailable.
    let conv = this._account._conv.get(this.normalizedName);
    if (type == "unavailable" && conv) {
      conv.updateTyping(Ci.prplIConvIM.NOT_TYPING, this.contactDisplayName);
    }

    if (type == "unavailable" || type == "error") {
      if (!this._resources || !(resource in this._resources)) {
        // Ignore for already offline resources.
        return;
      }
      delete this._resources[resource];
      if (preferred == resource) {
        preferred = undefined;
      }
    } else {
      let statusInfo = parseStatus(aStanza);
      let priority = aStanza.getElement(["priority"]);
      priority = priority ? parseInt(priority.innerText, 10) : 0;

      if (!this._resources) {
        this._resources = {};
      }
      this._resources[resource] = {
        statusType: statusInfo.statusType,
        statusText: statusInfo.statusText,
        idleSince: statusInfo.idleSince,
        priority,
        stanza: aStanza,
      };
    }

    let photo = aStanza.getElement(["x", "photo"]);
    if (photo && photo.uri == Stanza.NS.vcard_update) {
      let hash = photo.innerText;
      if (hash && hash != this._photoHash) {
        this._account._addVCardRequest(this.normalizedName);
      } else if (!hash && this._photoHash) {
        delete this._photoHash;
        this.buddyIconFilename = "";
      }
    }

    for (let r in this._resources) {
      if (
        preferred === undefined ||
        this._resources[r].statusType > this._resources[preferred].statusType
      ) {
        // FIXME also compare priorities...
        preferred = r;
      }
    }
    if (
      preferred != undefined &&
      preferred == this._preferredResource &&
      resource != preferred
    ) {
      // The presence information change is only for an unused resource,
      // only potential buddy tooltips need to be refreshed.
      this._notifyObservers("status-detail-changed");
      return;
    }

    // Presence info has changed enough that if we are having a
    // conversation with one resource of this buddy, we should send
    // the next message to all resources.
    // FIXME: the test here isn't exactly right...
    if (
      this._preferredResource != preferred &&
      this._account._conv.has(this.normalizedName)
    ) {
      delete this._account._conv.get(this.normalizedName)._targetResource;
    }

    this._preferredResource = preferred;
    if (preferred === undefined) {
      let statusType = Ci.imIStatusInfo.STATUS_UNKNOWN;
      if (type == "unavailable") {
        statusType = Ci.imIStatusInfo.STATUS_OFFLINE;
      }
      this.setStatus(statusType, "");
    } else {
      preferred = this._resources[preferred];
      this.setStatus(preferred.statusType, preferred.statusText);
    }
  },

  /* Can send messages to buddies who appear offline */
  get canSendMessage() {
    return this.account.connected;
  },

  /* Called when the user wants to chat with the buddy */
  createConversation() {
    return this._account.createConversation(this.normalizedName);
  },
};

function XMPPAccountBuddy(aAccount, aBuddy, aTag, aUserName) {
  this._init(aAccount, aBuddy, aTag, aUserName);
}
XMPPAccountBuddy.prototype = XMPPAccountBuddyPrototype;

var XMPPRoomInfoPrototype = {
  __proto__: ClassInfo("prplIRoomInfo", "XMPP RoomInfo Object"),
  get topic() {
    return "";
  },
  get participantCount() {
    return Ci.prplIRoomInfo.NO_PARTICIPANT_COUNT;
  },
  get chatRoomFieldValues() {
    let roomJid = this._account._roomList.get(this.name);
    return this._account.getChatRoomDefaultFieldValues(roomJid);
  },
};
function XMPPRoomInfo(aName, aAccount) {
  this.name = aName;
  this._account = aAccount;
}
XMPPRoomInfo.prototype = XMPPRoomInfoPrototype;

/* Helper class for account */
export var XMPPAccountPrototype = {
  __proto__: GenericAccountPrototype,

  _jid: null, // parsed Jabber ID: node, domain, resource
  _connection: null, // XMPPSession socket
  authMechanisms: null, // hook to let prpls tweak the list of auth mechanisms

  // Contains the domain of MUC service which is obtained using service
  // discovery.
  _mucService: null,

  // Maps room names to room jid.
  _roomList: new Map(),

  // Callbacks used when roomInfo is available.
  _roomInfoCallbacks: new Set(),

  // Determines if roomInfo that we have is expired or not.
  _lastListTime: 0,
  get isRoomInfoStale() {
    return Date.now() - this._lastListTime > kListRefreshInterval;
  },

  // If true, we are waiting for replies.
  _pendingList: false,

  // An array of jids for which we still need to request vCards.
  _pendingVCardRequests: [],

  // XEP-0280: Message Carbons.
  // If true, message carbons are currently enabled.
  _isCarbonsEnabled: false,

  /* Generate unique id for a stanza. Using id and unique sid is defined in
   * RFC 6120 (Section 8.2.3, 4.7.3).
   */
  generateId: () => Services.uuid.generateUUID().toString().slice(1, -1),

  _init(aProtoInstance, aImAccount) {
    GenericAccountPrototype._init.call(this, aProtoInstance, aImAccount);

    // Ongoing conversations.
    // The keys of this._conv are assumed to be normalized like account@domain
    // for normal conversations and like room@domain/nick for MUC participant
    // convs.
    this._conv = new NormalizedMap(this.normalizeFullJid.bind(this));

    this._buddies = new NormalizedMap(this.normalize.bind(this));
    this._mucs = new NormalizedMap(this.normalize.bind(this));

    this._pendingVCardRequests = [];
  },

  get canJoinChat() {
    return true;
  },
  chatRoomFields: {
    room: {
      get label() {
        return lazy._("chatRoomField.room");
      },
      required: true,
    },
    server: {
      get label() {
        return lazy._("chatRoomField.server");
      },
      required: true,
    },
    nick: {
      get label() {
        return lazy._("chatRoomField.nick");
      },
      required: true,
    },
    password: {
      get label() {
        return lazy._("chatRoomField.password");
      },
      isPassword: true,
    },
  },
  parseDefaultChatName(aDefaultChatName) {
    if (!aDefaultChatName) {
      return { nick: this._jid.node };
    }

    let params = aDefaultChatName.trim().split(/\s+/);
    let jid = this._parseJID(params[0]);

    // We swap node and domain as domain is required for parseJID, but node and
    // resource are optional. In MUC join command, Node is required as it
    // represents a room, but domain and resource are optional as we get muc
    // domain from service discovery.
    if (!jid.node && jid.domain) {
      [jid.node, jid.domain] = [jid.domain, jid.node];
    }

    let chatFields = {
      room: jid.node,
      server: jid.domain || this._mucService,
      nick: jid.resource || this._jid.node,
    };
    if (params.length > 1) {
      chatFields.password = params[1];
    }
    return chatFields;
  },
  getChatRoomDefaultFieldValues(aDefaultChatName) {
    let rv = GenericAccountPrototype.getChatRoomDefaultFieldValues.call(
      this,
      aDefaultChatName
    );
    if (!rv.values.nick) {
      rv.values.nick = this._jid.node;
    }
    if (!rv.values.server && this._mucService) {
      rv.values.server = this._mucService;
    }

    return rv;
  },

  // XEP-0045: Requests joining room if it exists or
  // creating room if it does not exist.
  joinChat(aComponents) {
    let jid =
      aComponents.getValue("room") + "@" + aComponents.getValue("server");
    let nick = aComponents.getValue("nick");

    let muc = this._mucs.get(jid);
    if (muc) {
      if (!muc.left) {
        // We are already in this conversation.
        return muc;
      } else if (!muc.chatRoomFields) {
        // We are rejoining a room that was parted by the user.
        muc._rejoined = true;
      }
    } else {
      muc = new this._MUCConversationConstructor(this, jid, nick);
      this._mucs.set(jid, muc);
    }

    // Store the prplIChatRoomFieldValues to enable later reconnections.
    muc.chatRoomFields = aComponents;
    muc.joining = true;
    muc.removeAllParticipants();

    let password = aComponents.getValue("password");
    let x = Stanza.node(
      "x",
      Stanza.NS.muc,
      null,
      password ? Stanza.node("password", null, null, password) : null
    );
    let logString;
    if (password) {
      logString =
        "<presence .../> (Stanza containing password to join MUC " +
        jid +
        "/" +
        nick +
        " not logged)";
    }
    this.sendStanza(
      Stanza.presence({ to: jid + "/" + nick }, x),
      undefined,
      undefined,
      logString
    );
    return muc;
  },

  _idleSince: 0,
  observe(aSubject, aTopic, aData) {
    if (aTopic == "idle-time-changed") {
      let idleTime = parseInt(aData, 10);
      if (idleTime) {
        this._idleSince = Math.floor(Date.now() / 1000) - idleTime;
      } else {
        delete this._idleSince;
      }
      this._shouldSendPresenceForIdlenessChange = true;
      executeSoon(
        function () {
          if ("_shouldSendPresenceForIdlenessChange" in this) {
            this._sendPresence();
          }
        }.bind(this)
      );
    } else if (aTopic == "status-changed") {
      this._sendPresence();
    } else if (aTopic == "user-icon-changed") {
      delete this._cachedUserIcon;
      this._forceUserIconUpdate = true;
      this._sendVCard();
    } else if (aTopic == "user-display-name-changed") {
      this._forceUserDisplayNameUpdate = true;
    }
    this._sendVCard();
  },

  /* GenericAccountPrototype events */
  /* Connect to the server */
  connect() {
    this._jid = this._parseJID(this.name);

    // For the resource, if the user has edited the option, always use that.
    if (this.prefs.prefHasUserValue("resource")) {
      let resource = this.getString("resource");

      // this._jid needs to be updated. This value is however never used
      // because while connected it's the jid of the session that's
      // interesting.
      this._jid = this._setJID(this._jid.domain, this._jid.node, resource);
    } else if (this._jid.resource) {
      // If there is a resource in the account name (inherited from libpurple),
      // migrate it to the pref so it appears correctly in the advanced account
      // options next time.
      this.prefs.setStringPref("resource", this._jid.resource);
    }

    this._connection = new XMPPSession(
      this.getString("server") || this._jid.domain,
      this.getInt("port") || 5222,
      this.getString("connection_security"),
      this._jid,
      this.imAccount.password,
      this
    );
  },

  remove() {
    this._conv.forEach(conv => conv.close());
    this._mucs.forEach(muc => muc.close());
    this._buddies.forEach((buddy, jid) => this._forgetRosterItem(jid));
  },

  unInit() {
    if (this._connection) {
      this._disconnect(undefined, undefined, true);
    }
    delete this._jid;
    delete this._conv;
    delete this._buddies;
    delete this._mucs;
  },

  /* Disconnect from the server */
  disconnect() {
    this._disconnect();
  },

  addBuddy(aTag, aName) {
    if (!this._connection) {
      throw new Error("The account isn't connected");
    }

    let jid = this.normalize(aName);
    if (!jid || !jid.includes("@")) {
      throw new Error("Invalid username");
    }

    if (this._buddies.has(jid)) {
      let subscription = this._buddies.get(jid).subscription;
      if (subscription && (subscription == "both" || subscription == "to")) {
        this.DEBUG("not re-adding an existing buddy");
        return;
      }
    } else {
      let s = Stanza.iq(
        "set",
        null,
        null,
        Stanza.node(
          "query",
          Stanza.NS.roster,
          null,
          Stanza.node(
            "item",
            null,
            { jid },
            Stanza.node("group", null, null, aTag.name)
          )
        )
      );
      this.sendStanza(
        s,
        this._handleResult({
          default: aError => {
            this.WARN(
              "Unable to add a roster item due to " + aError + " error."
            );
          },
        })
      );
    }
    this.sendStanza(Stanza.presence({ to: jid, type: "subscribe" }));
  },

  /* Loads a buddy from the local storage.
   * Called for each buddy locally stored before connecting
   * to the server. */
  loadBuddy(aBuddy, aTag) {
    let buddy = new this._accountBuddyConstructor(this, aBuddy, aTag);
    this._buddies.set(buddy.normalizedName, buddy);
    return buddy;
  },

  /* Replies to a buddy request in order to accept it or deny it. */
  replyToBuddyRequest(aReply, aRequest) {
    if (!this._connection) {
      return;
    }
    let s = Stanza.presence({ to: aRequest.userName, type: aReply });
    this.sendStanza(s);
    this.removeBuddyRequest(aRequest);
  },

  requestBuddyInfo(aJid) {
    if (!this.connected) {
      Services.obs.notifyObservers(EmptyEnumerator, "user-info-received", aJid);
      return;
    }

    let userName;
    let tooltipInfo = [];
    let jid = this._parseJID(aJid);
    let muc = this._mucs.get(jid.node + "@" + jid.domain);
    let participant;
    if (muc) {
      participant = muc._participants.get(jid.resource);
      if (participant) {
        if (participant.accountJid) {
          userName = participant.accountJid;
        }
        if (!muc.left) {
          let statusType = participant.statusType;
          let statusText = participant.statusText;
          tooltipInfo.push(
            new TooltipInfo(statusType, statusText, Ci.prplITooltipInfo.status)
          );

          if (participant.buddyIconFilename) {
            tooltipInfo.push(
              new TooltipInfo(
                null,
                participant.buddyIconFilename,
                Ci.prplITooltipInfo.icon
              )
            );
          }
        }
      }
    }
    Services.obs.notifyObservers(
      new nsSimpleEnumerator(tooltipInfo),
      "user-info-received",
      aJid
    );

    let iq = Stanza.iq(
      "get",
      null,
      aJid,
      Stanza.node("vCard", Stanza.NS.vcard)
    );
    this.sendStanza(iq, aStanza => {
      let vCardInfo = {};
      let vCardNode = aStanza.getElement(["vCard"]);

      // In the case of an error response, we just notify the observers with
      // what info we already have.
      if (aStanza.attributes.type == "result" && vCardNode) {
        vCardInfo = this.parseVCard(vCardNode);
      }

      // The real jid of participant which is of the form local@domain/resource.
      // We consider the jid is provided by server is more correct than jid is
      // set by the user.
      if (userName) {
        vCardInfo.userName = userName;
      }

      // vCard fields we want to display in the tooltip.
      const kTooltipFields = [
        "userName",
        "fullName",
        "nickname",
        "title",
        "organization",
        "email",
        "birthday",
        "locality",
        "country",
        "telephone",
      ];

      let tooltipInfo = [];
      for (let field of kTooltipFields) {
        if (vCardInfo.hasOwnProperty(field)) {
          tooltipInfo.push(
            new TooltipInfo(lazy._("tooltip." + field), vCardInfo[field])
          );
        }
      }
      if (vCardInfo.photo) {
        let dataURI = this._getPhotoURI(vCardInfo.photo);

        // Store the photo URI for this participant.
        if (participant) {
          participant.buddyIconFilename = dataURI;
        }

        tooltipInfo.push(
          new TooltipInfo(null, dataURI, Ci.prplITooltipInfo.icon)
        );
      }
      Services.obs.notifyObservers(
        new nsSimpleEnumerator(tooltipInfo),
        "user-info-received",
        aJid
      );
    });
  },

  // Parses the photo node of a received vCard if exists and returns string of
  // data URI, otherwise returns null.
  _getPhotoURI(aPhotoNode) {
    if (!aPhotoNode) {
      return null;
    }

    let type = aPhotoNode.getElement(["TYPE"]);
    let value = aPhotoNode.getElement(["BINVAL"]);
    if (!type || !value) {
      return null;
    }

    return "data:" + type.innerText + ";base64," + value.innerText;
  },

  // Parses the vCard into the properties of the returned object.
  parseVCard(aVCardNode) {
    // XEP-0054: vcard-temp.
    let aResult = {};
    for (let node of aVCardNode.children.filter(
      child => child.type == "node"
    )) {
      let localName = node.localName;
      let innerText = node.innerText;
      if (innerText) {
        if (localName == "FN") {
          aResult.fullName = innerText;
        } else if (localName == "NICKNAME") {
          aResult.nickname = innerText;
        } else if (localName == "TITLE") {
          aResult.title = innerText;
        } else if (localName == "BDAY") {
          aResult.birthday = innerText;
        } else if (localName == "JABBERID") {
          aResult.userName = innerText;
        }
      }
      if (localName == "ORG") {
        let organization = node.getElement(["ORGNAME"]);
        if (organization && organization.innerText) {
          aResult.organization = organization.innerText;
        }
      } else if (localName == "EMAIL") {
        let userID = node.getElement(["USERID"]);
        if (userID && userID.innerText) {
          aResult.email = userID.innerText;
        }
      } else if (localName == "ADR") {
        let locality = node.getElement(["LOCALITY"]);
        if (locality && locality.innerText) {
          aResult.locality = locality.innerText;
        }

        let country = node.getElement(["CTRY"]);
        if (country && country.innerText) {
          aResult.country = country.innerText;
        }
      } else if (localName == "PHOTO") {
        aResult.photo = node;
      } else if (localName == "TEL") {
        let number = node.getElement(["NUMBER"]);
        if (number && number.innerText) {
          aResult.telephone = number.innerText;
        }
      }
      // TODO: Parse the other fields of vCard and display it in system messages
      // in response to /whois.
    }
    return aResult;
  },

  // Returns undefined if not an error stanza, and an object
  // describing the error otherwise:
  parseError(aStanza) {
    if (aStanza.attributes.type != "error") {
      return undefined;
    }

    let retval = { stanza: aStanza };
    let error = aStanza.getElement(["error"]);

    // RFC 6120 Section 8.3.2: Type must be one of
    // auth -- retry after providing credentials
    // cancel -- do not retry (the error cannot be remedied)
    // continue -- proceed (the condition was only a warning)
    // modify -- retry after changing the data sent
    // wait -- retry after waiting (the error is temporary).
    retval.type = error.attributes.type;

    // RFC 6120 Section 8.3.3.
    const kDefinedConditions = [
      "bad-request",
      "conflict",
      "feature-not-implemented",
      "forbidden",
      "gone",
      "internal-server-error",
      "item-not-found",
      "jid-malformed",
      "not-acceptable",
      "not-allowed",
      "not-authorized",
      "policy-violation",
      "recipient-unavailable",
      "redirect",
      "registration-required",
      "remote-server-not-found",
      "remote-server-timeout",
      "resource-constraint",
      "service-unavailable",
      "subscription-required",
      "undefined-condition",
      "unexpected-request",
    ];
    let condition = kDefinedConditions.find(c => error.getElement([c]));
    if (!condition) {
      // RFC 6120 Section 8.3.2.
      this.WARN(
        "Nonstandard or missing defined-condition element in error stanza."
      );
      condition = "undefined-condition";
    }
    retval.condition = condition;

    let errortext = error.getElement(["text"]);
    if (errortext) {
      retval.text = errortext.innerText;
    }

    return retval;
  },

  // Returns an error-handling callback for use with sendStanza generated
  // from aHandlers, an object defining the error handlers.
  // If the stanza passed to the callback is an error stanza, it checks if
  // aHandlers contains a property with the name of the defined condition
  // of the error.
  // * If the property is a function, it is called with the parsed error
  //   as its argument, bound to aThis (if provided).
  //   It should return true if the error was handled.
  // * If the property is a string, it is displayed as a system message
  //   in the conversation given by aThis.
  handleErrors(aHandlers, aThis) {
    return aStanza => {
      if (!aHandlers) {
        return false;
      }

      let error = this.parseError(aStanza);
      if (!error) {
        return false;
      }

      let toCamelCase = aStr => {
        // Turn defined condition string into a valid camelcase
        // JS property name.
        let capitalize = s => s[0].toUpperCase() + s.slice(1);
        let uncapitalize = s => s[0].toLowerCase() + s.slice(1);
        return uncapitalize(aStr.split("-").map(capitalize).join(""));
      };
      let condition = toCamelCase(error.condition);
      // Check if we have a handler property for this kind of error or a
      // default handler.
      if (!(condition in aHandlers) && !("default" in aHandlers)) {
        return false;
      }

      // Try to get the handler for condition, if we cannot get it, try to get
      // the default handler.
      let handler = aHandlers[condition];
      if (!handler) {
        handler = aHandlers.default;
      }

      if (typeof handler == "string") {
        // The string is an error message to be displayed in the conversation.
        if (!aThis || !aThis.writeMessage) {
          this.ERROR(
            "HandleErrors was passed an error message string, but " +
              "no conversation to display it in:\n" +
              handler
          );
          return true;
        }
        aThis.writeMessage(aThis.name, handler, { system: true, error: true });
        return true;
      } else if (typeof handler == "function") {
        // If we're given a function, call this error handler.
        return handler.call(aThis, error);
      }

      // If this happens, there's a bug somewhere.
      this.ERROR(
        "HandleErrors was passed a handler for '" +
          condition +
          "'' which is neither a function nor a string."
      );
      return false;
    };
  },

  // Returns a callback suitable for use in sendStanza, to handle type==result
  // responses. aHandlers and aThis are passed on to handleErrors for error
  // handling.
  _handleResult(aHandlers, aThis) {
    return aStanza => {
      if (aStanza.attributes.type == "result") {
        return true;
      }
      return this.handleErrors(aHandlers, aThis)(aStanza);
    };
  },

  /* XMPPSession events */

  /* Called when the XMPP session is started */
  onConnection() {
    // Request the roster. The account will be marked as connected when this is
    // complete.
    this.reportConnecting(lazy._("connection.downloadingRoster"));
    let s = Stanza.iq(
      "get",
      null,
      null,
      Stanza.node("query", Stanza.NS.roster)
    );
    this.sendStanza(s, this.onRoster, this);

    // XEP-0030 and XEP-0045 (6): Service Discovery.
    // Queries Server for Associated Services.
    let iq = Stanza.iq(
      "get",
      null,
      this._jid.domain,
      Stanza.node("query", Stanza.NS.disco_items)
    );
    this.sendStanza(iq, this.onServiceDiscovery, this);

    // XEP-0030: Service Discovery Information Features.
    iq = Stanza.iq(
      "get",
      null,
      this._jid.domain,
      Stanza.node("query", Stanza.NS.disco_info)
    );
    this.sendStanza(iq, this.onServiceDiscoveryInfo, this);
  },

  /* Called whenever a stanza is received */
  onXmppStanza(aStanza) {},

  /* Called when a iq stanza is received */
  onIQStanza(aStanza) {
    let type = aStanza.attributes.type;
    if (type == "set") {
      for (let query of aStanza.getChildren("query")) {
        if (query.uri != Stanza.NS.roster) {
          continue;
        }

        // RFC 6121 2.1.6 (Roster push):
        // A receiving client MUST ignore the stanza unless it has no 'from'
        // attribute (i.e., implicitly from the bare JID of the user's
        // account) or it has a 'from' attribute whose value matches the
        // user's bare JID <user@domainpart>.
        let from = aStanza.attributes.from;
        if (from && from != this._jid.node + "@" + this._jid.domain) {
          this.WARN("Ignoring potentially spoofed roster push.");
          return;
        }

        for (let item of query.getChildren("item")) {
          this._onRosterItem(item, true);
        }
        return;
      }
    } else if (type == "get") {
      let id = aStanza.attributes.id;
      let from = aStanza.attributes.from;

      // XEP-0199: XMPP server-to-client ping (XEP-0199)
      let ping = aStanza.getElement(["ping"]);
      if (ping && ping.uri == Stanza.NS.ping) {
        this.sendStanza(Stanza.iq("result", id, from));
        return;
      }

      let query = aStanza.getElement(["query"]);
      if (query && query.uri == Stanza.NS.version) {
        // XEP-0092: Software Version.
        let children = [];
        children.push(Stanza.node("name", null, null, Services.appinfo.name));
        children.push(
          Stanza.node("version", null, null, Services.appinfo.version)
        );
        let versionQuery = Stanza.node(
          "query",
          Stanza.NS.version,
          null,
          children
        );
        this.sendStanza(Stanza.iq("result", id, from, versionQuery));
        return;
      }
      if (query && query.uri == Stanza.NS.disco_info) {
        // XEP-0030: Service Discovery.
        let children = [];
        if (aStanza.attributes.node == Stanza.NS.muc_rooms) {
          // XEP-0045 (6.7): Room query.
          // TODO: Currently, we return an empty <query/> element, but we
          // should return non-private rooms.
        } else {
          children = SupportedFeatures.map(feature =>
            Stanza.node("feature", null, { var: feature })
          );
          children.unshift(
            Stanza.node("identity", null, {
              category: "client",
              type: "pc",
              name: Services.appinfo.name,
            })
          );
        }
        let discoveryQuery = Stanza.node(
          "query",
          Stanza.NS.disco_info,
          null,
          children
        );
        this.sendStanza(Stanza.iq("result", id, from, discoveryQuery));
        return;
      }
    }
    this.WARN(`Unhandled IQ ${type} stanza.`);
    if (type == "get" || type == "set") {
      // RFC 6120 (section 8.2.3): An entity that receives an IQ request of
      // type "get" or "set" MUST reply with an IQ response of type "result"
      // or "error".
      let id = aStanza.attributes.id;
      let from = aStanza.attributes.from;
      let condition = Stanza.node("service-unavailable", Stanza.NS.stanzas, {
        type: "cancel",
      });
      let error = Stanza.node("error", null, { type: "cancel" }, condition);
      this.sendStanza(Stanza.iq("error", id, from, error));
    }
  },

  /* Called when a presence stanza is received */
  onPresenceStanza(aStanza) {
    let from = aStanza.attributes.from;
    this.DEBUG("Received presence stanza for " + from);

    let jid = this.normalize(from);
    let type = aStanza.attributes.type;
    if (type == "subscribe") {
      this.addBuddyRequest(
        jid,
        this.replyToBuddyRequest.bind(this, "subscribed"),
        this.replyToBuddyRequest.bind(this, "unsubscribed")
      );
    } else if (
      type == "unsubscribe" ||
      type == "unsubscribed" ||
      type == "subscribed"
    ) {
      // Nothing useful to do for these presence stanzas, as we will also
      // receive a roster push containing more or less the same information
    } else if (this._buddies.has(jid)) {
      this._buddies.get(jid).onPresenceStanza(aStanza);
    } else if (this._mucs.has(jid)) {
      this._mucs.get(jid).onPresenceStanza(aStanza);
    } else if (jid != this.normalize(this._connection._jid.jid)) {
      this.WARN("received presence stanza for unknown buddy " + from);
    } else if (
      jid == this._jid.node + "@" + this._jid.domain &&
      this._connection._resource != this._parseJID(from).resource
    ) {
      // Ignore presence stanzas for another resource.
    } else {
      this.WARN("Unhandled presence stanza.");
    }
  },

  // XEP-0030: Discovering services and their features that are supported by
  // the server.
  onServiceDiscovery(aStanza) {
    let query = aStanza.getElement(["query"]);
    if (
      aStanza.attributes.type != "result" ||
      !query ||
      query.uri != Stanza.NS.disco_items
    ) {
      this.LOG("Could not get services for this server: " + this._jid.domain);
      return true;
    }

    // Discovering the Features that are Supported by each service.
    query.getElements(["item"]).forEach(item => {
      let jid = item.attributes.jid;
      if (!jid) {
        return;
      }
      let iq = Stanza.iq(
        "get",
        null,
        jid,
        Stanza.node("query", Stanza.NS.disco_info)
      );
      this.sendStanza(iq, receivedStanza => {
        let query = receivedStanza.getElement(["query"]);
        let from = receivedStanza.attributes.from;
        if (
          aStanza.attributes.type != "result" ||
          !query ||
          query.uri != Stanza.NS.disco_info
        ) {
          this.LOG("Could not get features for this service: " + from);
          return true;
        }
        let features = query
          .getElements(["feature"])
          .map(elt => elt.attributes.var);
        let identity = query.getElement(["identity"]);
        if (
          identity &&
          identity.attributes.category == "conference" &&
          identity.attributes.type == "text" &&
          features.includes(Stanza.NS.muc)
        ) {
          // XEP-0045 (6.2): this feature is for a MUC Service.
          // XEP-0045 (15.2): Service Discovery Category/Type.
          this._mucService = from;
        }
        // TODO: Handle other services that are supported by XMPP through
        // their features.

        return true;
      });
    });
    return true;
  },

  // XEP-0030: Discovering Service Information and its features that are
  // supported by the server.
  onServiceDiscoveryInfo(aStanza) {
    let query = aStanza.getElement(["query"]);
    if (
      aStanza.attributes.type != "result" ||
      !query ||
      query.uri != Stanza.NS.disco_info
    ) {
      this.LOG("Could not get features for this server: " + this._jid.domain);
      return true;
    }

    let features = query
      .getElements(["feature"])
      .map(elt => elt.attributes.var);
    if (features.includes(Stanza.NS.carbons)) {
      // XEP-0280: Message Carbons.
      // Enabling Carbons on server, as it's disabled by default on server.
      if (Services.prefs.getBoolPref("chat.xmpp.messageCarbons")) {
        let iqStanza = Stanza.iq(
          "set",
          null,
          null,
          Stanza.node("enable", Stanza.NS.carbons)
        );
        this.sendStanza(iqStanza, aStanza => {
          let error = this.parseError(aStanza);
          if (error) {
            this.WARN(
              "Unable to enable message carbons due to " +
                error.condition +
                " error."
            );
            return true;
          }

          let type = aStanza.attributes.type;
          if (type != "result") {
            this.WARN(
              "Received unexpected stanza with " +
                type +
                " type " +
                "while enabling message carbons."
            );
            return true;
          }

          this.LOG("Message carbons enabled.");
          this._isCarbonsEnabled = true;
          return true;
        });
      }
    }
    // TODO: Handle other features that are supported by the server.
    return true;
  },

  requestRoomInfo(aCallback) {
    if (this._roomInfoCallbacks.has(aCallback)) {
      return;
    }

    if (this.isRoomInfoStale && !this._pendingList) {
      this._roomList = new Map();
      this._lastListTime = Date.now();
      this._roomInfoCallback = aCallback;
      this._pendingList = true;

      // XEP-0045 (6.3): Discovering Rooms.
      let iq = Stanza.iq(
        "get",
        null,
        this._mucService,
        Stanza.node("query", Stanza.NS.disco_items)
      );
      this.sendStanza(iq, this.onRoomDiscovery, this);
    } else {
      let rooms = [...this._roomList.keys()];
      aCallback.onRoomInfoAvailable(rooms, !this._pendingList);
    }

    if (this._pendingList) {
      this._roomInfoCallbacks.add(aCallback);
    }
  },

  onRoomDiscovery(aStanza) {
    let query = aStanza.getElement(["query"]);
    if (!query || query.uri != Stanza.NS.disco_items) {
      this.LOG("Could not get rooms for this server: " + this._jid.domain);
      return;
    }

    // XEP-0059: Result Set Management.
    let set = query.getElement(["set"]);
    let last = set ? set.getElement(["last"]) : null;
    if (last) {
      let iq = Stanza.iq(
        "get",
        null,
        this._mucService,
        Stanza.node("query", Stanza.NS.disco_items)
      );
      this.sendStanza(iq, this.onRoomDiscovery, this);
    } else {
      this._pendingList = false;
    }

    let rooms = [];
    query.getElements(["item"]).forEach(item => {
      let jid = this._parseJID(item.attributes.jid);
      if (!jid) {
        return;
      }

      let name = item.attributes.name;
      if (!name) {
        name = jid.node ? jid.node : jid.jid;
      }

      this._roomList.set(name, jid.jid);
      rooms.push(name);
    });

    this._roomInfoCallback.onRoomInfoAvailable(rooms, !this._pendingList);
  },

  getRoomInfo(aName) {
    return new XMPPRoomInfo(aName, this);
  },

  // Returns null if not an invitation stanza, and an object
  // describing the invitation otherwise.
  parseInvitation(aStanza) {
    let x = aStanza.getElement(["x"]);
    if (!x) {
      return null;
    }
    let retVal = {
      shouldDecline: false,
    };

    // XEP-0045. Direct Invitation (7.8.1)
    // Described in XEP-0249.
    // jid (chatroom) is required.
    // Password, reason, continue and thread are optional.
    if (x.uri == Stanza.NS.conference) {
      if (!x.attributes.jid) {
        this.WARN("Received an invitation with missing MUC jid.");
        return null;
      }
      retVal.mucJid = this.normalize(x.attributes.jid);
      retVal.from = this.normalize(aStanza.attributes.from);
      retVal.password = x.attributes.password;
      retVal.reason = x.attributes.reason;
      retVal.continue = x.attributes.continue;
      retVal.thread = x.attributes.thread;
      return retVal;
    }

    // XEP-0045. Mediated Invitation (7.8.2)
    // Sent by the chatroom on behalf of someone in the chatroom.
    // jid (chatroom) and from (inviter) are required.
    // password and reason are optional.
    if (x.uri == Stanza.NS.muc_user) {
      let invite = x.getElement(["invite"]);
      if (!invite || !invite.attributes.from) {
        this.WARN("Received an invitation with missing MUC invite or from.");
        return null;
      }
      retVal.mucJid = this.normalize(aStanza.attributes.from);
      retVal.from = this.normalize(invite.attributes.from);
      retVal.shouldDecline = true;
      let continueElement = invite.getElement(["continue"]);
      retVal.continue = !!continueElement;
      if (continueElement) {
        retVal.thread = continueElement.attributes.thread;
      }
      if (x.getElement(["password"])) {
        retVal.password = x.getElement(["password"]).innerText;
      }
      if (invite.getElement(["reason"])) {
        retVal.reason = invite.getElement(["reason"]).innerText;
      }
      return retVal;
    }

    return null;
  },

  /* Called when a message stanza is received */
  onMessageStanza(aStanza) {
    // XEP-0280: Message Carbons.
    // Sending and Receiving Messages.
    // Indicates that the forwarded message was sent or received.
    let isSent = false;
    let carbonStanza =
      aStanza.getElement(["sent"]) || aStanza.getElement(["received"]);
    if (carbonStanza) {
      if (carbonStanza.uri != Stanza.NS.carbons) {
        this.WARN(
          "Received a forwarded message which does not '" +
            Stanza.NS.carbons +
            "' namespace."
        );
        return;
      }

      isSent = carbonStanza.localName == "sent";
      carbonStanza = carbonStanza.getElement(["forwarded", "message"]);
      if (this._isCarbonsEnabled) {
        aStanza = carbonStanza;
      } else {
        this.WARN(
          "Received an unexpected forwarded message while message " +
            "carbons are not enabled."
        );
        return;
      }
    }

    // For forwarded sent messages, we need to use "to" attribute to
    // get the right conversation as from in this case is this account.
    let convJid = isSent ? aStanza.attributes.to : aStanza.attributes.from;

    let normConvJid = this.normalize(convJid);
    let isMuc = this._mucs.has(normConvJid);

    let type = aStanza.attributes.type;
    let x = aStanza.getElement(["x"]);
    let body;
    let b = aStanza.getElement(["body"]);
    if (b) {
      // If there's a <body> child we have more than just typing notifications.
      // Prefer HTML (in <html><body>) and use plain text (<body>) as fallback.
      let htmlBody = aStanza.getElement(["html", "body"]);
      if (htmlBody) {
        body = htmlBody.innerXML;
      } else {
        // Even if the message is in plain text, the prplIMessage
        // should contain a string that's correctly escaped for
        // insertion in an HTML document.
        body = lazy.TXTToHTML(b.innerText);
      }
    }

    let subject = aStanza.getElement(["subject"]);
    // Ignore subject when !isMuc. We're being permissive about subject changes
    // in the comment below, so we need to be careful about where that makes
    // sense. Psi+'s OTR plugin includes a subject and body in its message
    // stanzas.
    if (subject && isMuc) {
      // XEP-0045 (7.2.16): Check for a subject element in the stanza and update
      // the topic if it exists.
      // We are breaking the spec because only a message that contains a
      // <subject/> but no <body/> element shall be considered a subject change
      // for MUC, but we ignore that to be compatible with ejabberd versions
      // before 15.06.
      let muc = this._mucs.get(normConvJid);
      let nick = this._parseJID(convJid).resource;
      // TODO There can be multiple subject elements with different xml:lang
      // attributes.
      muc.setTopic(subject.innerText, nick);
      return;
    }

    let invitation = this.parseInvitation(aStanza);
    if (invitation) {
      let messageID;
      if (invitation.reason) {
        messageID = "conversation.muc.invitationWithReason2";
      } else {
        messageID = "conversation.muc.invitationWithoutReason";
      }
      if (invitation.password) {
        messageID += ".password";
      }
      let params = [
        invitation.from,
        invitation.mucJid,
        invitation.password,
        invitation.reason,
      ].filter(s => s);
      let message = lazy._(messageID, ...params);

      this.addChatRequest(
        invitation.mucJid,
        () => {
          let chatRoomFields = this.getChatRoomDefaultFieldValues(
            invitation.mucJid
          );
          if (invitation.password) {
            chatRoomFields.setValue("password", invitation.password);
          }
          let muc = this.joinChat(chatRoomFields);
          muc.writeMessage(muc.name, message, { system: true });
        },
        (request, tryToDeny) => {
          // Only mediated invitations (XEP-0045) can explicitly decline.
          if (invitation.shouldDecline && tryToDeny) {
            let decline = Stanza.node(
              "decline",
              null,
              { from: invitation.from },
              null
            );
            let x = Stanza.node("x", Stanza.NS.muc_user, null, decline);
            let s = Stanza.node("message", null, { to: invitation.mucJid }, x);
            this.sendStanza(s);
          }
          // Always show invite reason or password, even if the invite wasn't
          // automatically declined based on the setting.
          if (!request || invitation.reason || invitation.password) {
            let conv = this.createConversation(invitation.from);
            if (conv) {
              conv.writeMessage(invitation.from, message, { system: true });
            }
          }
        }
      );
    }

    if (body) {
      let date = _getDelay(aStanza);
      if (
        type == "groupchat" ||
        (type == "error" && isMuc && !this._conv.has(convJid))
      ) {
        if (!isMuc) {
          this.WARN(
            "Received a groupchat message for unknown MUC " + normConvJid
          );
          return;
        }
        let muc = this._mucs.get(normConvJid);
        muc.incomingMessage(body, aStanza, date);
        return;
      }

      let conv = this.createConversation(convJid);
      if (!conv) {
        return;
      }

      if (isSent) {
        _displaySentMsg(conv, body, date);
        return;
      }
      conv.incomingMessage(body, aStanza, date);
    } else if (type == "error") {
      let conv = this.createConversation(convJid);
      if (conv) {
        conv.incomingMessage(null, aStanza);
      }
    } else if (x && x.uri == Stanza.NS.muc_user) {
      let muc = this._mucs.get(normConvJid);
      if (!muc) {
        this.WARN(
          "Received a groupchat message for unknown MUC " + normConvJid
        );
        return;
      }
      muc.onMessageStanza(aStanza);
      return;
    }

    // If this is a sent message carbon, the user is typing on another client.
    if (isSent) {
      return;
    }

    // Don't create a conversation to only display the typing notifications.
    if (!this._conv.has(normConvJid) && !this._conv.has(convJid)) {
      return;
    }

    // Ignore errors while delivering typing notifications.
    if (type == "error") {
      return;
    }

    let typingState = Ci.prplIConvIM.NOT_TYPING;
    let state;
    let s = aStanza.getChildrenByNS(Stanza.NS.chatstates);
    if (s.length > 0) {
      state = s[0].localName;
    }
    if (state) {
      this.DEBUG(state);
      if (state == "composing") {
        typingState = Ci.prplIConvIM.TYPING;
      } else if (state == "paused") {
        typingState = Ci.prplIConvIM.TYPED;
      }
    }
    let convName = normConvJid;

    // If the bare JID is a MUC that we have joined, use the full JID as this
    // is a private message to a MUC participant.
    if (isMuc) {
      convName = convJid;
    }

    let conv = this._conv.get(convName);
    if (!conv) {
      return;
    }
    conv.updateTyping(typingState, conv.shortName);
    conv.supportChatStateNotifications = !!state;
  },

  /** Called when there is an error in the XMPP session */
  onError(aError, aException) {
    if (aError === null || aError === undefined) {
      aError = Ci.prplIAccount.ERROR_OTHER_ERROR;
    }
    this._disconnect(aError, aException.toString());
  },

  onVCard(aStanza) {
    let jid = this._pendingVCardRequests.shift();
    this._requestNextVCard();
    if (!this._buddies.has(jid) && !this._mucs.has(jid)) {
      this.WARN("Received a vCard for unknown buddy " + jid);
      return;
    }

    let vCard = aStanza.getElement(["vCard"]);
    let error = this.parseError(aStanza);
    if (
      (error &&
        (error.condition == "item-not-found" ||
          error.condition == "service-unavailable")) ||
      !vCard ||
      !vCard.children.length
    ) {
      this.LOG("No vCard exists (or the user does not exist) for " + jid);
      return;
    } else if (error) {
      this.WARN("Received unexpected vCard error " + error.condition);
      return;
    }

    let stanzaJid = this.normalize(aStanza.attributes.from);
    if (jid && jid != stanzaJid) {
      this.ERROR(
        "Received vCard for a different jid (" +
          stanzaJid +
          ") " +
          "than the requested " +
          jid
      );
    }

    let foundFormattedName = false;
    let vCardInfo = this.parseVCard(vCard);
    if (this._mucs.has(jid)) {
      const conv = this._mucs.get(jid);
      if (vCardInfo.photo) {
        conv._saveIcon(vCardInfo.photo);
      }
      return;
    }
    let buddy = this._buddies.get(jid);
    if (vCardInfo.fullName) {
      buddy.vCardFormattedName = vCardInfo.fullName;
      foundFormattedName = true;
    }
    if (vCardInfo.photo) {
      buddy._saveIcon(vCardInfo.photo);
    }
    if (!foundFormattedName && buddy._vCardFormattedName) {
      buddy.vCardFormattedName = "";
    }
    buddy._vCardReceived = true;
  },

  /**
   * Save the icon for a resource to the local file system.
   *
   * @param photo - The vcard photo node representing the icon.
   * @param {prplIChatBuddy|prplIConversation} resource - Resource the icon is for.
   * @returns {Promise<string>} Resolves with the file:// URI to the local icon file.
   */
  _saveResourceIcon(photo, resource) {
    // Some servers seem to send a photo node without a type declared.
    let type = photo.getElement(["TYPE"]);
    if (!type) {
      return Promise.reject(new Error("Missing image type"));
    }
    type = type.innerText;
    const kExt = {
      "image/gif": "gif",
      "image/jpeg": "jpg",
      "image/png": "png",
    };
    if (!kExt.hasOwnProperty(type)) {
      return Promise.reject(new Error("Unknown image type"));
    }

    let content = "",
      data = "";
    // Strip all characters not allowed in base64 before parsing.
    let parseBase64 = aBase => atob(aBase.replace(/[^A-Za-z0-9\+\/\=]/g, ""));
    for (let line of photo.getElement(["BINVAL"]).innerText.split("\n")) {
      data += line;
      // Mozilla's atob() doesn't handle padding with "=" or "=="
      // unless it's at the end of the string, so we have to work around that.
      if (line.endsWith("=")) {
        content += parseBase64(data);
        data = "";
      }
    }
    content += parseBase64(data);

    // Store a sha1 hash of the photo we have just received.
    let ch = Cc["@mozilla.org/security/hash;1"].createInstance(
      Ci.nsICryptoHash
    );
    ch.init(ch.SHA1);
    let dataArray = Object.keys(content).map(i => content.charCodeAt(i));
    ch.update(dataArray, dataArray.length);
    let hash = ch.finish(false);
    function toHexString(charCode) {
      return charCode.toString(16).padStart(2, "0");
    }
    resource._photoHash = Object.keys(hash)
      .map(i => toHexString(hash.charCodeAt(i)))
      .join("");

    let istream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
      Ci.nsIStringInputStream
    );
    istream.setData(content, content.length);

    let fileName = resource._photoHash + "." + kExt[type];
    let file = lazy.FileUtils.getFile("ProfD", [
      "icons",
      this.protocol.normalizedName,
      this.normalizedName,
      fileName,
    ]);
    let ostream = lazy.FileUtils.openSafeFileOutputStream(file);
    return new Promise(resolve => {
      lazy.NetUtil.asyncCopy(istream, ostream, rc => {
        if (Components.isSuccessCode(rc)) {
          resolve(Services.io.newFileURI(file).spec);
        }
      });
    });
  },

  _requestNextVCard() {
    if (!this._pendingVCardRequests.length) {
      return;
    }
    let s = Stanza.iq(
      "get",
      null,
      this._pendingVCardRequests[0],
      Stanza.node("vCard", Stanza.NS.vcard)
    );
    this.sendStanza(s, this.onVCard, this);
  },

  _addVCardRequest(aJID) {
    let requestPending = !!this._pendingVCardRequests.length;
    this._pendingVCardRequests.push(aJID);
    if (!requestPending) {
      this._requestNextVCard();
    }
  },

  // XEP-0029 (Section 2) and RFC 6122 (Section 2): The node and domain are
  // lowercase, while resources are case sensitive and can contain spaces.
  normalizeFullJid(aJID) {
    return this._parseJID(aJID.trim()).jid;
  },

  // Standard normalization for XMPP removes the resource part of jids.
  normalize(aJID) {
    return aJID
      .trim()
      .split("/", 1)[0] // up to first slash
      .toLowerCase();
  },

  // RFC 6122 (Section 2): [ localpart "@" ] domainpart [ "/" resourcepart ] is
  // the form of jid.
  // Localpart is parsed as node and optional.
  // Domainpart is parsed as domain and required.
  // resourcepart is parsed as resource and optional.
  _parseJID(aJid) {
    let match = /^(?:([^"&'/:<>@]+)@)?([^@/<>'\"]+)(?:\/(.*))?$/.exec(
      aJid.trim()
    );
    if (!match) {
      return null;
    }

    let result = {
      node: match[1],
      domain: match[2].toLowerCase(),
      resource: match[3],
    };
    return this._setJID(result.domain, result.node, result.resource);
  },

  // Constructs jid as an object from domain, node and resource parts.
  // The object has properties (node, domain, resource and jid).
  // aDomain is required, but aNode and aResource are optional.
  _setJID(aDomain, aNode = null, aResource = null) {
    if (!aDomain) {
      throw new Error("aDomain must have a value");
    }

    let result = {
      node: aNode,
      domain: aDomain.toLowerCase(),
      resource: aResource,
    };
    let jid = result.domain;
    if (result.node) {
      result.node = result.node.toLowerCase();
      jid = result.node + "@" + jid;
    }
    if (result.resource) {
      jid += "/" + result.resource;
    }
    result.jid = jid;
    return result;
  },

  _onRosterItem(aItem, aNotifyOfUpdates) {
    let jid = aItem.attributes.jid;
    if (!jid) {
      this.WARN("Received a roster item without jid: " + aItem.getXML());
      return "";
    }
    jid = this.normalize(jid);

    let subscription = "";
    if ("subscription" in aItem.attributes) {
      subscription = aItem.attributes.subscription;
    }
    if (subscription == "remove") {
      this._forgetRosterItem(jid);
      return "";
    }

    let buddy;
    if (this._buddies.has(jid)) {
      buddy = this._buddies.get(jid);
      let groups = aItem.getChildren("group");
      if (groups.length) {
        // If the server specified at least one group, ensure the group we use
        // as the account buddy's tag is still a group on the server...
        let tagName = buddy.tag.name;
        if (!groups.some(g => g.innerText == tagName)) {
          // ... otherwise we need to move our account buddy to a new group.
          tagName = groups[0].innerText;
          if (tagName) {
            // Should always be true, but check just in case...
            let oldTag = buddy.tag;
            buddy._tag = IMServices.tags.createTag(tagName);
            IMServices.contacts.accountBuddyMoved(buddy, oldTag, buddy._tag);
          }
        }
      }
    } else {
      let tag;
      for (let group of aItem.getChildren("group")) {
        let name = group.innerText;
        if (name) {
          tag = IMServices.tags.createTag(name);
          break; // TODO we should create an accountBuddy per group,
          // but this._buddies would probably not like that...
        }
      }
      buddy = new this._accountBuddyConstructor(
        this,
        null,
        tag || IMServices.tags.defaultTag,
        jid
      );
    }

    // We request the vCard only if we haven't received it yet and are
    // subscribed to presence for that contact.
    if (
      (subscription == "both" || subscription == "to") &&
      !buddy._vCardReceived
    ) {
      this._addVCardRequest(jid);
    }

    let alias = "name" in aItem.attributes ? aItem.attributes.name : "";
    if (alias) {
      if (aNotifyOfUpdates && this._buddies.has(jid)) {
        buddy.rosterAlias = alias;
      } else {
        buddy._rosterAlias = alias;
      }
    } else if (buddy._rosterAlias) {
      buddy.rosterAlias = "";
    }

    if (subscription) {
      buddy.subscription = subscription;
    }
    if (!this._buddies.has(jid)) {
      this._buddies.set(jid, buddy);
      IMServices.contacts.accountBuddyAdded(buddy);
    } else if (aNotifyOfUpdates) {
      buddy._notifyObservers("status-detail-changed");
    }

    // Keep the xml nodes of the item so that we don't have to
    // recreate them when changing something (eg. the alias) in it.
    buddy._rosterItem = aItem;

    return jid;
  },
  _forgetRosterItem(aJID) {
    IMServices.contacts.accountBuddyRemoved(this._buddies.get(aJID));
    this._buddies.delete(aJID);
  },

  /* When the roster is received */
  onRoster(aStanza) {
    // For the first element that is a roster stanza.
    for (let qe of aStanza.getChildren("query")) {
      if (qe.uri != Stanza.NS.roster) {
        continue;
      }

      // Find all the roster items in the new message.
      let newRoster = new Set();
      for (let item of qe.getChildren("item")) {
        let jid = this._onRosterItem(item);
        if (jid) {
          newRoster.add(jid);
        }
      }
      // If an item was in the old roster, but not in the new, forget it.
      for (let jid of this._buddies.keys()) {
        if (!newRoster.has(jid)) {
          this._forgetRosterItem(jid);
        }
      }
      break;
    }

    this._sendPresence();
    this._buddies.forEach(b => {
      if (b.subscription == "both" || b.subscription == "to") {
        b.setStatus(Ci.imIStatusInfo.STATUS_OFFLINE, "");
      }
    });
    this.reportConnected();
    this._sendVCard();
  },

  /* Public methods */

  sendStanza(aStanza, aCallback, aThis, aLogString) {
    return this._connection.sendStanza(aStanza, aCallback, aThis, aLogString);
  },

  // Variations of the XMPP protocol can change these default constructors:
  _conversationConstructor: XMPPConversation,
  _MUCConversationConstructor: XMPPMUCConversation,
  _accountBuddyConstructor: XMPPAccountBuddy,

  /* Create a new conversation */
  createConversation(aName) {
    let convName = this.normalize(aName);

    // Checks if conversation is with a participant of a MUC we are in. We do
    // not want to strip the resource as it is of the form room@domain/nick.
    let isMucParticipant = this._mucs.has(convName);
    if (isMucParticipant) {
      convName = this.normalizeFullJid(aName);
    }

    // Checking that the aName can be parsed and is not broken.
    let jid = this._parseJID(convName);
    if (
      !jid ||
      !jid.domain ||
      (isMucParticipant && (!jid.node || !jid.resource))
    ) {
      this.ERROR("Could not create conversation as jid is broken: " + convName);
      throw new Error("Invalid JID");
    }

    if (!this._conv.has(convName)) {
      this._conv.set(
        convName,
        new this._conversationConstructor(this, convName, isMucParticipant)
      );
    }

    return this._conv.get(convName);
  },

  /* Remove an existing conversation */
  removeConversation(aNormalizedName) {
    if (this._conv.has(aNormalizedName)) {
      this._conv.delete(aNormalizedName);
    } else if (this._mucs.has(aNormalizedName)) {
      this._mucs.delete(aNormalizedName);
    }
  },

  /* Private methods */

  /**
   * Disconnect from the server
   *
   * @param {number} aError - The error reason, passed to reportDisconnecting.
   * @param {string} aErrorMessage - The error message, passed to reportDisconnecting.
   * @param {boolean} aQuiet - True to avoid sending status change notifications
   *   during the uninitialization of the account.
   */
  _disconnect(
    aError = Ci.prplIAccount.NO_ERROR,
    aErrorMessage = "",
    aQuiet = false
  ) {
    if (!this._connection) {
      return;
    }

    this.reportDisconnecting(aError, aErrorMessage);

    this._buddies.forEach(b => {
      if (!aQuiet) {
        b.setStatus(Ci.imIStatusInfo.STATUS_UNKNOWN, "");
      }
      b.onAccountDisconnected();
    });

    this._mucs.forEach(muc => {
      muc.joining = false; // In case we never finished joining.
      muc.left = true;
    });

    this._connection.disconnect();
    delete this._connection;

    // We won't receive "user-icon-changed" notifications while the
    // account isn't connected, so clear the cache to avoid keeping an
    // obsolete icon.
    delete this._cachedUserIcon;
    // Also clear the cached user vCard, as we will want to redownload it
    // after reconnecting.
    delete this._userVCard;

    // Clear vCard requests.
    this._pendingVCardRequests = [];

    this.reportDisconnected();
  },

  /* Set the user status on the server */
  _sendPresence() {
    delete this._shouldSendPresenceForIdlenessChange;

    if (!this._connection) {
      return;
    }

    let si = this.imAccount.statusInfo;
    let statusType = si.statusType;
    let show = "";
    if (statusType == Ci.imIStatusInfo.STATUS_UNAVAILABLE) {
      show = "dnd";
    } else if (
      statusType == Ci.imIStatusInfo.STATUS_AWAY ||
      statusType == Ci.imIStatusInfo.STATUS_IDLE
    ) {
      show = "away";
    }
    let children = [];
    if (show) {
      children.push(Stanza.node("show", null, null, show));
    }
    let statusText = si.statusText;
    if (statusText) {
      children.push(Stanza.node("status", null, null, statusText));
    }
    if (this._idleSince) {
      let time = Math.floor(Date.now() / 1000) - this._idleSince;
      children.push(Stanza.node("query", Stanza.NS.last, { seconds: time }));
    }
    if (this.prefs.prefHasUserValue("priority")) {
      let priority = Math.max(-128, Math.min(127, this.getInt("priority")));
      if (priority) {
        children.push(Stanza.node("priority", null, null, priority.toString()));
      }
    }
    this.sendStanza(
      Stanza.presence({ "xml:lang": "en" }, children),
      aStanza => {
        // As we are implicitly subscribed to our own presence (rfc6121#4), we
        // will receive the presence stanza mirrored back to us. We don't need
        // to do anything with this response.
        return true;
      }
    );
  },

  _downloadingUserVCard: false,
  _downloadUserVCard() {
    // If a download is already in progress, don't start another one.
    if (this._downloadingUserVCard) {
      return;
    }
    this._downloadingUserVCard = true;
    let s = Stanza.iq("get", null, null, Stanza.node("vCard", Stanza.NS.vcard));
    this.sendStanza(s, this.onUserVCard, this);
  },

  onUserVCard(aStanza) {
    delete this._downloadingUserVCard;
    let userVCard = aStanza.getElement(["vCard"]) || null;
    if (userVCard) {
      // Strip any server-specific namespace off the incoming vcard
      // before storing it.
      this._userVCard = Stanza.node(
        "vCard",
        Stanza.NS.vcard,
        null,
        userVCard.children
      );
    }

    // If a user icon exists in the vCard we received from the server,
    // we need to ensure the line breaks in its binval are exactly the
    // same as those we would include if we sent the icon, and that
    // there isn't any other whitespace.
    if (this._userVCard) {
      let binval = this._userVCard.getElement(["PHOTO", "BINVAL"]);
      if (binval && binval.children.length) {
        binval = binval.children[0];
        binval.text = binval.text
          .replace(/[^A-Za-z0-9\+\/\=]/g, "")
          .replace(/.{74}/g, "$&\n");
      }
    } else {
      // Downloading the vCard failed.
      if (
        this.handleErrors({
          itemNotFound: () => false, // OK, no vCard exists yet.
          default: () => true,
        })(aStanza)
      ) {
        this.WARN(
          "Unexpected error retrieving the user's vcard, " +
            "so we won't attempt to set it either."
        );
        return;
      }
      // Set this so that we don't get into an infinite loop trying to download
      // the vcard again. The check in sendVCard is for hasOwnProperty.
      this._userVCard = null;
    }
    this._sendVCard();
  },

  _cachingUserIcon: false,
  _cacheUserIcon() {
    if (this._cachingUserIcon) {
      return;
    }

    let userIcon = this.imAccount.statusInfo.getUserIcon();
    if (!userIcon) {
      this._cachedUserIcon = null;
      this._sendVCard();
      return;
    }

    this._cachingUserIcon = true;
    let channel = lazy.NetUtil.newChannel({
      uri: userIcon,
      loadingPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
      securityFlags:
        Ci.nsILoadInfo.SEC_REQUIRE_SAME_ORIGIN_INHERITS_SEC_CONTEXT,
      contentPolicyType: Ci.nsIContentPolicy.TYPE_IMAGE,
    });
    lazy.NetUtil.asyncFetch(channel, (inputStream, resultCode) => {
      if (!Components.isSuccessCode(resultCode)) {
        return;
      }
      try {
        let type = channel.contentType;
        let buffer = lazy.NetUtil.readInputStreamToString(
          inputStream,
          inputStream.available()
        );
        let readImage = lazy.imgTools.decodeImageFromBuffer(
          buffer,
          buffer.length,
          type
        );
        let scaledImage;
        if (readImage.width <= 96 && readImage.height <= 96) {
          scaledImage = lazy.imgTools.encodeImage(readImage, type);
        } else {
          if (type != "image/jpeg") {
            type = "image/png";
          }
          scaledImage = lazy.imgTools.encodeScaledImage(
            readImage,
            type,
            64,
            64
          );
        }

        let bstream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(
          Ci.nsIBinaryInputStream
        );
        bstream.setInputStream(scaledImage);

        let data = bstream.readBytes(bstream.available());
        this._cachedUserIcon = {
          type,
          binval: btoa(data).replace(/.{74}/g, "$&\n"),
        };
      } catch (e) {
        console.error(e);
        this._cachedUserIcon = null;
      }
      delete this._cachingUserIcon;
      this._sendVCard();
    });
  },
  _sendVCard() {
    if (!this._connection) {
      return;
    }

    // We have to download the user's existing vCard before updating it.
    // This lets us preserve the fields that we don't change or don't know.
    // Some servers may reject a new vCard if we don't do this first.
    if (!this.hasOwnProperty("_userVCard")) {
      // The download of the vCard is asynchronous and will call _sendVCard back
      // when the user's vCard has been received.
      this._downloadUserVCard();
      return;
    }

    // Read the local user icon asynchronously from the disk.
    // _cacheUserIcon will call _sendVCard back once the icon is ready.
    if (!this.hasOwnProperty("_cachedUserIcon")) {
      this._cacheUserIcon();
      return;
    }

    // If the user currently doesn't have any vCard on the server or
    // the download failed, an empty new one.
    if (!this._userVCard) {
      this._userVCard = Stanza.node("vCard", Stanza.NS.vcard);
    }

    // Keep a serialized copy of the existing user vCard so that we
    // can avoid resending identical data to the server.
    let existingVCard = this._userVCard.getXML();

    let fn = this._userVCard.getElement(["FN"]);
    let displayName = this.imAccount.statusInfo.displayName;
    if (displayName) {
      // If a display name is set locally, update or add an FN field to the vCard.
      if (!fn) {
        this._userVCard.addChild(
          Stanza.node("FN", Stanza.NS.vcard, null, displayName)
        );
      } else if (fn.children.length) {
        fn.children[0].text = displayName;
      } else {
        fn.addText(displayName);
      }
    } else if ("_forceUserDisplayNameUpdate" in this) {
      // We remove a display name stored on the server without replacing
      // it with a new value only if this _sendVCard call is the result of
      // a user action. This is to avoid removing data from the server each
      // time the user connects from a new profile.
      this._userVCard.children = this._userVCard.children.filter(
        n => n.qName != "FN"
      );
    }
    delete this._forceUserDisplayNameUpdate;

    if (this._cachedUserIcon) {
      // If we have a local user icon, update or add it in the PHOTO field.
      let photoChildren = [
        Stanza.node("TYPE", Stanza.NS.vcard, null, this._cachedUserIcon.type),
        Stanza.node(
          "BINVAL",
          Stanza.NS.vcard,
          null,
          this._cachedUserIcon.binval
        ),
      ];
      let photo = this._userVCard.getElement(["PHOTO"]);
      if (photo) {
        photo.children = photoChildren;
      } else {
        this._userVCard.addChild(
          Stanza.node("PHOTO", Stanza.NS.vcard, null, photoChildren)
        );
      }
    } else if ("_forceUserIconUpdate" in this) {
      // Like for the display name, we remove a photo without
      // replacing it only if the call is caused by a user action.
      this._userVCard.children = this._userVCard.children.filter(
        n => n.qName != "PHOTO"
      );
    }
    delete this._forceUserIconUpdate;

    // Send the vCard only if it has really changed.
    // We handle the result response from the server (it does not require
    // any further action).
    if (this._userVCard.getXML() != existingVCard) {
      this.sendStanza(
        Stanza.iq("set", null, null, this._userVCard),
        this._handleResult()
      );
    } else {
      this.LOG(
        "Not sending the vCard because the server stored vCard is identical."
      );
    }
  },
};