summaryrefslogtreecommitdiffstats
path: root/comm/chat/components/src/imAccounts.sys.mjs
blob: f06b503fa6a72b16ddaf90645d061d31b6f228ac (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
/* 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 { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
import {
  ClassInfo,
  executeSoon,
  l10nHelper,
} from "resource:///modules/imXPCOMUtils.sys.mjs";

const { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);
import { IMServices } from "resource:///modules/IMServices.sys.mjs";
import {
  GenericAccountPrototype,
  GenericAccountBuddyPrototype,
} from "resource:///modules/jsProtoHelper.sys.mjs";

const lazy = {};
XPCOMUtils.defineLazyGetter(lazy, "_", () =>
  l10nHelper("chrome://chat/locale/accounts.properties")
);
XPCOMUtils.defineLazyGetter(lazy, "_maxDebugMessages", () =>
  Services.prefs.getIntPref("messenger.accounts.maxDebugMessages")
);
XPCOMUtils.defineLazyServiceGetter(
  lazy,
  "HttpProtocolHandler",
  "@mozilla.org/network/protocol;1?name=http",
  "nsIHttpProtocolHandler"
);

var kPrefAutologinPending = "messenger.accounts.autoLoginPending";
let kPrefAccountOrder = "mail.accountmanager.accounts";
var kPrefAccountPrefix = "messenger.account.";
var kAccountKeyPrefix = "account";
var kAccountOptionPrefPrefix = "options.";
var kPrefAccountName = "name";
var kPrefAccountPrpl = "prpl";
var kPrefAccountAutoLogin = "autoLogin";
var kPrefAccountAutoJoin = "autoJoin";
var kPrefAccountAlias = "alias";
var kPrefAccountFirstConnectionState = "firstConnectionState";

var gUserCanceledPrimaryPasswordPrompt = false;

var SavePrefTimer = {
  saveNow() {
    if (this._timer) {
      clearTimeout(this._timer);
      this._timer = null;
    }
    Services.prefs.savePrefFile(null);
  },
  _timer: null,
  unInitTimer() {
    if (this._timer) {
      this.saveNow();
    }
  },
  initTimer() {
    if (!this._timer) {
      this._timer = setTimeout(this.saveNow.bind(this), 5000);
    }
  },
};

var AutoLoginCounter = {
  _count: 0,
  startAutoLogin() {
    ++this._count;
    if (this._count != 1) {
      return;
    }
    Services.prefs.setIntPref(kPrefAutologinPending, Date.now() / 1000);
    SavePrefTimer.saveNow();
  },
  finishedAutoLogin() {
    --this._count;
    if (this._count != 0) {
      return;
    }
    Services.prefs.clearUserPref(kPrefAutologinPending);
    SavePrefTimer.initTimer();
  },
};

function UnknownProtocol(aPrplId) {
  this.id = aPrplId;
}
UnknownProtocol.prototype = {
  __proto__: ClassInfo("prplIProtocol", "Unknown protocol"),
  get name() {
    return "";
  },
  get normalizedName() {
    // Use the ID, but remove the 'prpl-' prefix.
    return this.id.replace(/^prpl-/, "");
  },
  get iconBaseURI() {
    return "chrome://chat/skin/prpl-unknown/";
  },
  getOptions() {
    return [];
  },
  get usernamePrefix() {
    return "";
  },
  getUsernameSplit() {
    return [];
  },
  get usernameEmptyText() {
    return "";
  },

  getAccount(aKey, aName) {
    throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
  },
  accountExists() {
    throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
  },

  // false seems an acceptable default for all options
  // (they should never be called anyway).
  get chatHasTopic() {
    return false;
  },
  get noPassword() {
    return false;
  },
  get passwordOptional() {
    return true;
  },
  get slashCommandsNative() {
    return false;
  },
  get canEncrypt() {
    return false;
  },
};

// An unknown prplIAccount.
function UnknownAccount(aAccount) {
  this._init(aAccount.protocol, aAccount);
}
UnknownAccount.prototype = GenericAccountPrototype;

function UnknownAccountBuddy(aAccount, aBuddy, aTag) {
  this._init(new UnknownAccount(aAccount), aBuddy, aTag);
}
UnknownAccountBuddy.prototype = GenericAccountBuddyPrototype;

/**
 * @param {string} aKey - Account key for preferences.
 * @param {string} [aName] - Name of the account if it is new. Will be stored
 *  in account preferences. If not provided, the value from the account
 *  preferences is used instead.
 * @param {string} [aPrplId] - Protocol ID for this account if it is new. Will
 *  be stored in account preferences. If not provided, the value from the
 *  account preferences is used instead.
 */
function imAccount(aKey, aName, aPrplId) {
  if (!aKey.startsWith(kAccountKeyPrefix)) {
    throw Components.Exception(`Invalid key: ${aKey}`, Cr.NS_ERROR_INVALID_ARG);
  }

  this.id = aKey;
  this.numericId = parseInt(aKey.substr(kAccountKeyPrefix.length));
  gAccountsService._keepAccount(this);
  this.prefBranch = Services.prefs.getBranch(kPrefAccountPrefix + aKey + ".");

  if (aName) {
    this.name = aName;
    this.prefBranch.setStringPref(kPrefAccountName, aName);

    this.firstConnectionState = Ci.imIAccount.FIRST_CONNECTION_UNKNOWN;
  } else {
    this.name = this.prefBranch.getStringPref(kPrefAccountName);
  }

  let prplId = aPrplId;
  if (prplId) {
    this.prefBranch.setCharPref(kPrefAccountPrpl, prplId);
  } else {
    prplId = this.prefBranch.getCharPref(kPrefAccountPrpl);
  }

  // Get the protocol plugin, or fallback to an UnknownProtocol instance.
  this.protocol = IMServices.core.getProtocolById(prplId);
  if (!this.protocol) {
    this.protocol = new UnknownProtocol(prplId);
    this._connectionErrorReason = Ci.imIAccount.ERROR_UNKNOWN_PRPL;
    return;
  }

  // Ensure the account is correctly stored in blist.sqlite.
  IMServices.contacts.storeAccount(this.numericId, this.name, prplId);

  // Get the prplIAccount from the protocol plugin.
  this.prplAccount = this.protocol.getAccount(this);

  // Send status change notifications to the account.
  this.observedStatusInfo = null; // (To execute the setter).

  // If we have never finished the first connection attempt for this account,
  // mark the account as having caused a crash.
  if (this.firstConnectionState == Ci.imIAccount.FIRST_CONNECTION_PENDING) {
    this.firstConnectionState = Ci.imIAccount.FIRST_CONNECTION_CRASHED;
  }

  Services.logins.initializationPromise.then(() => {
    // If protocol is falsy remove() was called on this instance while waiting
    // for the promise to resolve. Since the instance was disposed there is
    // nothing to do.
    if (!this.protocol) {
      return;
    }

    // Check for errors that should prevent connection attempts.
    if (this._passwordRequired && !this.password) {
      this._connectionErrorReason = Ci.imIAccount.ERROR_MISSING_PASSWORD;
    } else if (
      this.firstConnectionState == Ci.imIAccount.FIRST_CONNECTION_CRASHED
    ) {
      this._connectionErrorReason = Ci.imIAccount.ERROR_CRASHED;
    }
  });
}

imAccount.prototype = {
  __proto__: ClassInfo(["imIAccount", "prplIAccount"], "im account object"),

  name: "",
  id: "",
  numericId: 0,
  protocol: null,
  prplAccount: null,
  connectionState: Ci.imIAccount.STATE_DISCONNECTED,
  connectionStateMsg: "",
  connectionErrorMessage: "",
  _connectionErrorReason: Ci.prplIAccount.NO_ERROR,
  get connectionErrorReason() {
    if (
      this._connectionErrorReason != Ci.prplIAccount.NO_ERROR &&
      (this._connectionErrorReason != Ci.imIAccount.ERROR_MISSING_PASSWORD ||
        !this._password)
    ) {
      return this._connectionErrorReason;
    }
    return this.prplAccount.connectionErrorReason;
  },

  observe(aSubject, aTopic, aData) {
    if (aTopic == "account-connect-progress") {
      this.connectionStateMsg = aData;
    } else if (aTopic == "account-connecting") {
      if (this.prplAccount.connectionErrorReason != Ci.prplIAccount.NO_ERROR) {
        delete this.connectionErrorMessage;
        if (this.timeOfNextReconnect - Date.now() > 1000) {
          // This is a manual reconnection, reset the auto-reconnect stuff
          this.timeOfLastConnect = 0;
          this._cancelReconnection();
        }
      }
      if (this.firstConnectionState != Ci.imIAccount.FIRST_CONNECTION_OK) {
        this.firstConnectionState = Ci.imIAccount.FIRST_CONNECTION_PENDING;
      }
      this.connectionState = Ci.imIAccount.STATE_CONNECTING;
    } else if (aTopic == "account-connected") {
      this.connectionState = Ci.imIAccount.STATE_CONNECTED;
      this._finishedAutoLogin();
      this.timeOfLastConnect = Date.now();
      if (this.firstConnectionState != Ci.imIAccount.FIRST_CONNECTION_OK) {
        this.firstConnectionState = Ci.imIAccount.FIRST_CONNECTION_OK;
      }
      delete this.connectionStateMsg;

      if (
        this.canJoinChat &&
        this.prefBranch.prefHasUserValue(kPrefAccountAutoJoin)
      ) {
        let autojoin = this.prefBranch.getStringPref(kPrefAccountAutoJoin);
        if (autojoin) {
          for (let room of autojoin.trim().split(/,\s*/)) {
            if (room) {
              this.joinChat(this.getChatRoomDefaultFieldValues(room));
            }
          }
        }
      }
    } else if (aTopic == "account-disconnecting") {
      this.connectionState = Ci.imIAccount.STATE_DISCONNECTING;
      this.connectionErrorMessage = aData;
      delete this.connectionStateMsg;
      this._finishedAutoLogin();

      let firstConnectionState = this.firstConnectionState;
      if (
        firstConnectionState != Ci.imIAccount.FIRST_CONNECTION_OK &&
        firstConnectionState != Ci.imIAccount.FIRST_CONNECTION_CRASHED
      ) {
        this.firstConnectionState = Ci.imIAccount.FIRST_CONNECTION_UNKNOWN;
      }

      let connectionErrorReason = this.prplAccount.connectionErrorReason;
      if (connectionErrorReason != Ci.prplIAccount.NO_ERROR) {
        if (
          connectionErrorReason == Ci.prplIAccount.ERROR_NETWORK_ERROR ||
          connectionErrorReason == Ci.prplIAccount.ERROR_ENCRYPTION_ERROR
        ) {
          this._startReconnectTimer();
        }
        this._sendNotification("account-connect-error");
      }
    } else if (aTopic == "account-disconnected") {
      this.connectionState = Ci.imIAccount.STATE_DISCONNECTED;
      let connectionErrorReason = this.prplAccount.connectionErrorReason;
      if (connectionErrorReason != Ci.prplIAccount.NO_ERROR) {
        // If the account was disconnected with an error, save the debug messages.
        this._omittedDebugMessagesBeforeError += this._omittedDebugMessages;
        if (this._debugMessagesBeforeError) {
          this._omittedDebugMessagesBeforeError +=
            this._debugMessagesBeforeError.length;
        }
        this._debugMessagesBeforeError = this._debugMessages;
      } else {
        // After a clean disconnection, drop the debug messages that
        // could have been left by a previous error.
        delete this._omittedDebugMessagesBeforeError;
        delete this._debugMessagesBeforeError;
      }
      delete this._omittedDebugMessages;
      delete this._debugMessages;
      if (
        this._statusObserver &&
        connectionErrorReason == Ci.prplIAccount.NO_ERROR &&
        this.statusInfo.statusType > Ci.imIStatusInfo.STATUS_OFFLINE
      ) {
        // If the status changed back to online while an account was still
        // disconnecting, it was not reconnected automatically at that point,
        // so we must do it now. (This happens for protocols like IRC where
        // disconnection is not immediate.)
        this._sendNotification(aTopic, aData);
        this.connect();
        return;
      }
    } else {
      throw Components.Exception("", Cr.NS_ERROR_UNEXPECTED);
    }
    this._sendNotification(aTopic, aData);
  },

  _debugMessages: null,
  _omittedDebugMessages: 0,
  _debugMessagesBeforeError: null,
  _omittedDebugMessagesBeforeError: 0,
  logDebugMessage(aMessage, aLevel) {
    if (!this._debugMessages) {
      this._debugMessages = [];
    }
    if (
      lazy._maxDebugMessages &&
      this._debugMessages.length >= lazy._maxDebugMessages
    ) {
      this._debugMessages.shift();
      ++this._omittedDebugMessages;
    }
    this._debugMessages.push({ logLevel: aLevel, message: aMessage });
  },
  _createDebugMessage(aMessage) {
    let scriptError = Cc["@mozilla.org/scripterror;1"].createInstance(
      Ci.nsIScriptError
    );
    scriptError.init(
      aMessage,
      "",
      "",
      0,
      null,
      Ci.nsIScriptError.warningFlag,
      "component javascript"
    );
    return { logLevel: 0, message: scriptError };
  },
  getDebugMessages() {
    let messages = [];
    if (this._omittedDebugMessagesBeforeError) {
      let text = this._omittedDebugMessagesBeforeError + " messages omitted";
      messages.push(this._createDebugMessage(text));
    }
    if (this._debugMessagesBeforeError) {
      messages = messages.concat(this._debugMessagesBeforeError);
    }
    if (this._omittedDebugMessages) {
      let text = this._omittedDebugMessages + " messages omitted";
      messages.push(this._createDebugMessage(text));
    }
    if (this._debugMessages) {
      messages = messages.concat(this._debugMessages);
    }
    if (messages.length) {
      let appInfo = Services.appinfo;
      let header =
        `${appInfo.name} ${appInfo.version} (${appInfo.appBuildID}), ` +
        `Gecko ${appInfo.platformVersion} (${appInfo.platformBuildID}) ` +
        `on ${lazy.HttpProtocolHandler.oscpu}`;
      messages.unshift(this._createDebugMessage(header));
    }

    return messages;
  },

  _observedStatusInfo: null,
  get observedStatusInfo() {
    return this._observedStatusInfo;
  },
  _statusObserver: null,
  set observedStatusInfo(aUserStatusInfo) {
    if (!this.prplAccount) {
      return;
    }
    if (this._statusObserver) {
      this.statusInfo.removeObserver(this._statusObserver);
    }
    this._observedStatusInfo = aUserStatusInfo;
    if (this._statusObserver) {
      this.statusInfo.addObserver(this._statusObserver);
    }
  },
  _removeStatusObserver() {
    if (this._statusObserver) {
      this.statusInfo.removeObserver(this._statusObserver);
      delete this._statusObserver;
    }
  },
  get statusInfo() {
    return this._observedStatusInfo || IMServices.core.globalUserStatus;
  },

  reconnectAttempt: 0,
  timeOfLastConnect: 0,
  timeOfNextReconnect: 0,
  _reconnectTimer: null,
  _startReconnectTimer() {
    if (Services.io.offline) {
      console.error("_startReconnectTimer called while offline");
      return;
    }

    /* If the last successful connection is older than 10 seconds, reset the
       number of reconnection attempts. */
    const kTimeBeforeSuccessfulConnection = 10;
    if (
      this.timeOfLastConnect &&
      this.timeOfLastConnect + kTimeBeforeSuccessfulConnection * 1000 <
        Date.now()
    ) {
      delete this.reconnectAttempt;
      delete this.timeOfLastConnect;
    }

    let timers = Services.prefs
      .getCharPref("messenger.accounts.reconnectTimer")
      .split(",");
    let delay = timers[Math.min(this.reconnectAttempt, timers.length - 1)];
    let msDelay = parseInt(delay) * 1000;
    ++this.reconnectAttempt;
    this.timeOfNextReconnect = Date.now() + msDelay;
    this._reconnectTimer = setTimeout(this.connect.bind(this), msDelay);
  },

  _sendNotification(aTopic, aData) {
    Services.obs.notifyObservers(this, aTopic, aData);
  },

  get firstConnectionState() {
    try {
      return this.prefBranch.getIntPref(kPrefAccountFirstConnectionState);
    } catch (e) {
      return Ci.imIAccount.FIRST_CONNECTION_OK;
    }
  },
  set firstConnectionState(aState) {
    if (aState == Ci.imIAccount.FIRST_CONNECTION_OK) {
      this.prefBranch.clearUserPref(kPrefAccountFirstConnectionState);
    } else {
      this.prefBranch.setIntPref(kPrefAccountFirstConnectionState, aState);
      // We want to save this pref immediately when trying to connect.
      if (aState == Ci.imIAccount.FIRST_CONNECTION_PENDING) {
        SavePrefTimer.saveNow();
      } else {
        SavePrefTimer.initTimer();
      }
    }
  },

  _pendingReconnectForConnectionInfoChange: false,
  _connectionInfoChanged() {
    // The next connection will be the first connection with these parameters.
    this.firstConnectionState = Ci.imIAccount.FIRST_CONNECTION_UNKNOWN;

    // We want to attempt to reconnect with the new settings only if a
    // previous attempt failed or a connection attempt is currently
    // pending (so we can return early if the account is currently
    // connected or disconnected without error).
    // The code doing the reconnection attempt is wrapped within an
    // executeSoon call so that when multiple settings are changed at
    // once we don't attempt to reconnect until they are all saved.
    // If a reconnect attempt is already scheduled, we can also return early.
    if (
      this._pendingReconnectForConnectionInfoChange ||
      this.connected ||
      (this.disconnected &&
        this.connectionErrorReason == Ci.prplIAccount.NO_ERROR)
    ) {
      return;
    }

    this._pendingReconnectForConnectionInfoChange = true;
    executeSoon(
      function () {
        delete this._pendingReconnectForConnectionInfoChange;
        // If the connection parameters have changed while we were
        // trying to connect, cancel the ongoing connection attempt and
        // try again with the new parameters.
        if (this.connecting) {
          this.disconnect();
          this.connect();
          return;
        }
        // If the account was disconnected because of a non-fatal
        // connection error, retry now that we have new parameters.
        let errorReason = this.connectionErrorReason;
        if (
          this.disconnected &&
          errorReason != Ci.prplIAccount.NO_ERROR &&
          errorReason != Ci.imIAccount.ERROR_MISSING_PASSWORD &&
          errorReason != Ci.imIAccount.ERROR_CRASHED &&
          errorReason != Ci.imIAccount.ERROR_UNKNOWN_PRPL
        ) {
          this.connect();
        }
      }.bind(this)
    );
  },

  // If the protocol plugin is missing, we can't access the normalizedName,
  // but in lots of cases this.name is equivalent.
  get normalizedName() {
    return this.prplAccount ? this.prplAccount.normalizedName : this.name;
  },
  normalize(aName) {
    return this.prplAccount ? this.prplAccount.normalize(aName) : aName;
  },

  _sendUpdateNotification() {
    this._sendNotification("account-updated");
  },

  set alias(val) {
    if (val) {
      this.prefBranch.setStringPref(kPrefAccountAlias, val);
    } else {
      this.prefBranch.clearUserPref(kPrefAccountAlias);
    }
    this._sendUpdateNotification();
  },
  get alias() {
    try {
      return this.prefBranch.getStringPref(kPrefAccountAlias);
    } catch (e) {
      return "";
    }
  },

  _password: "",
  get password() {
    if (this._password) {
      return this._password;
    }

    // Avoid prompting the user for the primary password more than once at startup.
    if (gUserCanceledPrimaryPasswordPrompt) {
      return "";
    }

    let passwordURI = "im://" + this.protocol.id;
    let logins;
    try {
      logins = Services.logins.findLogins(passwordURI, null, passwordURI);
    } catch (e) {
      this._handlePrimaryPasswordException(e);
      return "";
    }
    let normalizedName = this.normalizedName;
    for (let login of logins) {
      if (login.username == normalizedName) {
        this._password = login.password;
        if (
          this._connectionErrorReason == Ci.imIAccount.ERROR_MISSING_PASSWORD
        ) {
          // We have found a password for an account marked as missing password,
          // re-check all others accounts missing a password. But first,
          // remove the error on our own account to avoid re-checking it.
          delete this._connectionErrorReason;
          gAccountsService._checkIfPasswordStillMissing();
        }
        return this._password;
      }
    }
    return "";
  },
  _checkIfPasswordStillMissing() {
    if (
      this._connectionErrorReason != Ci.imIAccount.ERROR_MISSING_PASSWORD ||
      !this.password
    ) {
      return;
    }

    delete this._connectionErrorReason;
    this._sendUpdateNotification();
  },
  get _passwordRequired() {
    return !this.protocol.noPassword && !this.protocol.passwordOptional;
  },
  set password(aPassword) {
    this._password = aPassword;
    if (gUserCanceledPrimaryPasswordPrompt) {
      return;
    }
    let newLogin = Cc["@mozilla.org/login-manager/loginInfo;1"].createInstance(
      Ci.nsILoginInfo
    );
    let passwordURI = "im://" + this.protocol.id;
    newLogin.init(
      passwordURI,
      null,
      passwordURI,
      this.normalizedName,
      aPassword,
      "",
      ""
    );
    try {
      let logins = Services.logins.findLogins(passwordURI, null, passwordURI);
      let saved = false;
      for (let login of logins) {
        if (newLogin.matches(login, true)) {
          if (aPassword) {
            Services.logins.modifyLogin(login, newLogin);
          } else {
            Services.logins.removeLogin(login);
          }
          saved = true;
          break;
        }
      }
      if (!saved && aPassword) {
        Services.logins.addLogin(newLogin);
      }
    } catch (e) {
      this._handlePrimaryPasswordException(e);
    }

    this._connectionInfoChanged();
    if (
      aPassword &&
      this._connectionErrorReason == Ci.imIAccount.ERROR_MISSING_PASSWORD
    ) {
      this._connectionErrorReason = Ci.imIAccount.NO_ERROR;
    } else if (!aPassword && this._passwordRequired) {
      this._connectionErrorReason = Ci.imIAccount.ERROR_MISSING_PASSWORD;
    }
    this._sendUpdateNotification();
  },
  _handlePrimaryPasswordException(aException) {
    if (aException.result != Cr.NS_ERROR_ABORT) {
      throw aException;
    }

    gUserCanceledPrimaryPasswordPrompt = true;
    executeSoon(function () {
      gUserCanceledPrimaryPasswordPrompt = false;
    });
  },

  get autoLogin() {
    return this.prefBranch.getBoolPref(kPrefAccountAutoLogin, true);
  },
  set autoLogin(val) {
    this.prefBranch.setBoolPref(kPrefAccountAutoLogin, val);
    SavePrefTimer.initTimer();
    this._sendUpdateNotification();
  },
  _autoLoginPending: false,
  checkAutoLogin() {
    // No auto-login if: the account has an error at the imIAccount level
    // (unknown protocol, missing password, first connection crashed),
    // the account is already connected or connecting, or autoLogin is off.
    if (
      this._connectionErrorReason != Ci.prplIAccount.NO_ERROR ||
      this.connecting ||
      this.connected ||
      !this.autoLogin
    ) {
      return;
    }

    this._autoLoginPending = true;
    AutoLoginCounter.startAutoLogin();
    try {
      this.connect();
    } catch (e) {
      console.error(e);
      this._finishedAutoLogin();
    }
  },
  _finishedAutoLogin() {
    if (!this.hasOwnProperty("_autoLoginPending")) {
      return;
    }
    delete this._autoLoginPending;
    AutoLoginCounter.finishedAutoLogin();
  },

  // Delete the account (from the preferences, mozStorage, and call unInit).
  remove() {
    let login = Cc["@mozilla.org/login-manager/loginInfo;1"].createInstance(
      Ci.nsILoginInfo
    );
    let passwordURI = "im://" + this.protocol.id;
    // Note: the normalizedName may not be exactly right if the
    // protocol plugin is missing.
    login.init(passwordURI, null, passwordURI, this.normalizedName, "", "", "");
    let logins = Services.logins.findLogins(passwordURI, null, passwordURI);
    for (let l of logins) {
      if (login.matches(l, true)) {
        Services.logins.removeLogin(l);
        break;
      }
    }
    if (this.connected || this.connecting) {
      this.disconnect();
    }
    if (this.prplAccount) {
      this.prplAccount.remove();
    }
    this.unInit();
    IMServices.contacts.forgetAccount(this.numericId);
    for (let prefName of this.prefBranch.getChildList("")) {
      this.prefBranch.clearUserPref(prefName);
    }
  },
  unInit() {
    // remove any pending reconnection timer.
    this._cancelReconnection();

    // Keeping a status observer could cause an immediate reconnection.
    this._removeStatusObserver();

    // remove any pending autologin preference used for crash detection.
    this._finishedAutoLogin();

    // If the first connection was pending on quit, we set it back to unknown.
    if (this.firstConnectionState == Ci.imIAccount.FIRST_CONNECTION_PENDING) {
      this.firstConnectionState = Ci.imIAccount.FIRST_CONNECTION_UNKNOWN;
    }

    // and make sure we cleanup the save pref timer.
    SavePrefTimer.unInitTimer();

    if (this.prplAccount) {
      this.prplAccount.unInit();
    }

    delete this.protocol;
    delete this.prplAccount;
  },

  get _ensurePrplAccount() {
    if (this.prplAccount) {
      return this.prplAccount;
    }
    throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
  },
  connect() {
    if (!this.prplAccount) {
      throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
    }

    if (this._passwordRequired) {
      // If the previous connection attempt failed because we have a wrong password,
      // clear the passwor cache so that if there's no password in the password
      // manager the user gets prompted again.
      if (
        this.connectionErrorReason ==
        Ci.prplIAccount.ERROR_AUTHENTICATION_FAILED
      ) {
        delete this._password;
      }

      let password = this.password;
      if (!password) {
        let prompts = Services.prompt;
        let shouldSave = { value: false };
        password = { value: "" };
        if (
          !prompts.promptPassword(
            null,
            lazy._("passwordPromptTitle", this.name),
            lazy._("passwordPromptText", this.name),
            password,
            lazy._("passwordPromptSaveCheckbox"),
            shouldSave
          )
        ) {
          return;
        }

        if (shouldSave.value) {
          this.password = password.value;
        } else {
          this._password = password.value;
        }
      }
    }

    if (!this._statusObserver) {
      this._statusObserver = {
        observe: function (aSubject, aTopic, aData) {
          // Disconnect or reconnect the account automatically, otherwise notify
          // the prplAccount instance.
          let statusType = aSubject.statusType;
          let connectionErrorReason = this.connectionErrorReason;
          if (statusType == Ci.imIStatusInfo.STATUS_OFFLINE) {
            if (this.connected || this.connecting) {
              this.prplAccount.disconnect();
            }
            this._cancelReconnection();
          } else if (
            statusType > Ci.imIStatusInfo.STATUS_OFFLINE &&
            this.disconnected &&
            (connectionErrorReason == Ci.prplIAccount.NO_ERROR ||
              connectionErrorReason == Ci.prplIAccount.ERROR_NETWORK_ERROR ||
              connectionErrorReason == Ci.prplIAccount.ERROR_ENCRYPTION_ERROR)
          ) {
            this.prplAccount.connect();
          } else if (this.connected) {
            this.prplAccount.observe(aSubject, aTopic, aData);
          }
        }.bind(this),
      };

      this.statusInfo.addObserver(this._statusObserver);
    }

    if (
      !Services.io.offline &&
      this.statusInfo.statusType > Ci.imIStatusInfo.STATUS_OFFLINE &&
      this.disconnected
    ) {
      this.prplAccount.connect();
    }
  },
  disconnect() {
    this._removeStatusObserver();
    if (!this.disconnected) {
      this._ensurePrplAccount.disconnect();
    }
  },

  get disconnected() {
    return this.connectionState == Ci.imIAccount.STATE_DISCONNECTED;
  },
  get connected() {
    return this.connectionState == Ci.imIAccount.STATE_CONNECTED;
  },
  get connecting() {
    return this.connectionState == Ci.imIAccount.STATE_CONNECTING;
  },
  get disconnecting() {
    return this.connectionState == Ci.imIAccount.STATE_DISCONNECTING;
  },

  _cancelReconnection() {
    if (this._reconnectTimer) {
      clearTimeout(this._reconnectTimer);
      delete this._reconnectTimer;
    }
    delete this.reconnectAttempt;
    delete this.timeOfNextReconnect;
  },
  cancelReconnection() {
    if (!this.disconnected) {
      throw Components.Exception("", Cr.NS_ERROR_UNEXPECTED);
    }

    // Ensure we don't keep a status observer that could re-enable the
    // auto-reconnect timers.
    this.disconnect();

    this._cancelReconnection();
  },
  createConversation(aName) {
    return this._ensurePrplAccount.createConversation(aName);
  },
  addBuddy(aTag, aName) {
    this._ensurePrplAccount.addBuddy(aTag, aName);
  },
  loadBuddy(aBuddy, aTag) {
    if (this.prplAccount) {
      return this.prplAccount.loadBuddy(aBuddy, aTag);
    }
    // Generate dummy account buddies for unknown protocols.
    return new UnknownAccountBuddy(this, aBuddy, aTag);
  },
  requestBuddyInfo(aBuddyName) {
    this._ensurePrplAccount.requestBuddyInfo(aBuddyName);
  },
  getChatRoomFields() {
    return this._ensurePrplAccount.getChatRoomFields();
  },
  getChatRoomDefaultFieldValues(aDefaultChatName) {
    return this._ensurePrplAccount.getChatRoomDefaultFieldValues(
      aDefaultChatName
    );
  },
  get canJoinChat() {
    return this.prplAccount ? this.prplAccount.canJoinChat : false;
  },
  joinChat(aComponents) {
    this._ensurePrplAccount.joinChat(aComponents);
  },
  setBool(aName, aVal) {
    this.prefBranch.setBoolPref(kAccountOptionPrefPrefix + aName, aVal);
    this._connectionInfoChanged();
    if (this.prplAccount) {
      this.prplAccount.setBool(aName, aVal);
    }
    SavePrefTimer.initTimer();
  },
  setInt(aName, aVal) {
    this.prefBranch.setIntPref(kAccountOptionPrefPrefix + aName, aVal);
    this._connectionInfoChanged();
    if (this.prplAccount) {
      this.prplAccount.setInt(aName, aVal);
    }
    SavePrefTimer.initTimer();
  },
  setString(aName, aVal) {
    this.prefBranch.setStringPref(kAccountOptionPrefPrefix + aName, aVal);
    this._connectionInfoChanged();
    if (this.prplAccount) {
      this.prplAccount.setString(aName, aVal);
    }
    SavePrefTimer.initTimer();
  },
  save() {
    SavePrefTimer.saveNow();
  },

  getSessions() {
    return this._ensurePrplAccount.getSessions();
  },
  get encryptionStatus() {
    return this._ensurePrplAccount.encryptionStatus;
  },
};

var gAccountsService = null;

export function AccountsService() {}
AccountsService.prototype = {
  initAccounts() {
    this._initAutoLoginStatus();
    this._accounts = [];
    this._accountsById = {};
    gAccountsService = this;
    let accountIdArray = MailServices.accounts.accounts
      .map(account => account.incomingServer.getCharValue("imAccount"))
      .filter(accountKey => accountKey?.startsWith(kAccountKeyPrefix));
    for (let account of accountIdArray) {
      new imAccount(account);
    }

    this._prefObserver = this.observe.bind(this);
    Services.prefs.addObserver(kPrefAccountOrder, this._prefObserver);
  },

  _prefObserver: null,
  observe(aSubject, aTopic, aData) {
    if (aTopic != "nsPref:changed" || aData != kPrefAccountOrder) {
      return;
    }

    const imAccounts = MailServices.accounts.accounts
      .map(account => account.incomingServer.getCharValue("imAccount"))
      .filter(k => k?.startsWith(kAccountKeyPrefix))
      .map(k =>
        this.getAccountByNumericId(parseInt(k.substr(kAccountKeyPrefix.length)))
      )
      .filter(a => a);

    // Only update _accounts if it's a reorder operation
    if (imAccounts.length == this._accounts.length) {
      this._accounts = imAccounts;
      Services.obs.notifyObservers(this, "account-list-updated");
    }
  },

  unInitAccounts() {
    for (let account of this._accounts) {
      account.unInit();
    }
    gAccountsService = null;
    delete this._accounts;
    delete this._accountsById;
    Services.prefs.removeObserver(kPrefAccountOrder, this._prefObserver);
    delete this._prefObserver;
  },

  autoLoginStatus: Ci.imIAccountsService.AUTOLOGIN_ENABLED,
  _initAutoLoginStatus() {
    /* If auto-login is already disabled, do nothing */
    if (this.autoLoginStatus != Ci.imIAccountsService.AUTOLOGIN_ENABLED) {
      return;
    }

    let prefs = Services.prefs;
    if (!prefs.getIntPref("messenger.startup.action")) {
      // the value 0 means that we start without connecting the accounts
      this.autoLoginStatus = Ci.imIAccountsService.AUTOLOGIN_USER_DISABLED;
      return;
    }

    /* Disable auto-login if we are running in safe mode */
    if (Services.appinfo.inSafeMode) {
      this.autoLoginStatus = Ci.imIAccountsService.AUTOLOGIN_SAFE_MODE;
      return;
    }

    /* Check if we crashed at the last startup during autologin */
    let autoLoginPending;
    if (
      prefs.getPrefType(kPrefAutologinPending) == prefs.PREF_INVALID ||
      !(autoLoginPending = prefs.getIntPref(kPrefAutologinPending))
    ) {
      // if the pref isn't set, then we haven't crashed: keep autologin enabled
      return;
    }

    // Last autologin hasn't finished properly.
    // For now, assume it's because of a crash.
    this.autoLoginStatus = Ci.imIAccountsService.AUTOLOGIN_CRASH;
    prefs.deleteBranch(kPrefAutologinPending);

    // If the crash reporter isn't built, we can't know anything more.
    if (!("nsICrashReporter" in Ci)) {
      return;
    }

    try {
      // Try to get more info with breakpad
      let lastCrashTime = 0;

      /* Locate the LastCrash file */
      let lastCrash = Services.dirsvc.get("UAppData", Ci.nsIFile);
      lastCrash.append("Crash Reports");
      lastCrash.append("LastCrash");
      if (lastCrash.exists()) {
        /* Ok, the file exists, now let's try to read it */
        let is = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(
          Ci.nsIFileInputStream
        );
        let sis = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(
          Ci.nsIScriptableInputStream
        );
        is.init(lastCrash, -1, 0, 0);
        sis.init(sis);

        lastCrashTime = parseInt(sis.read(lastCrash.fileSize));

        sis.close();
      }
      // The file not existing is totally acceptable, it just means that
      // either we never crashed or breakpad is not enabled.
      // In this case, lastCrashTime will keep its 0 initialization value.

      /* dump("autoLoginPending = " + autoLoginPending +
              ", lastCrash = " + lastCrashTime +
              ", difference = " + lastCrashTime - autoLoginPending + "\n");*/

      if (lastCrashTime < autoLoginPending) {
        // the last crash caught by breakpad is older than our last autologin
        // attempt.
        // If breakpad is currently enabled, we can be confident that
        // autologin was interrupted for an exterior reason
        // (application killed by the user, power outage, ...)
        try {
          Services.appinfo
            .QueryInterface(Ci.nsICrashReporter)
            .annotateCrashReport("=", "");
        } catch (e) {
          // This should fail with NS_ERROR_INVALID_ARG if breakpad is enabled,
          // and NS_ERROR_NOT_INITIALIZED if it is not.
          if (e.result != Cr.NS_ERROR_NOT_INITIALIZED) {
            this.autoLoginStatus = Ci.imIAccountsService.AUTOLOGIN_ENABLED;
          }
        }
      }
    } catch (e) {
      // if we failed to get the last crash time, then keep the
      // AUTOLOGIN_CRASH value in mAutoLoginStatus and return.
    }
  },

  processAutoLogin() {
    if (!this._accounts) {
      // if we're already shutting down
      return;
    }

    for (let account of this._accounts) {
      account.checkAutoLogin();
    }

    // Make sure autologin is now enabled, so that we don't display a
    // message stating that it is disabled and asking the user if it
    // should be processed now.
    this.autoLoginStatus = Ci.imIAccountsService.AUTOLOGIN_ENABLED;

    // Notify observers so that any message stating that autologin is
    // disabled can be removed
    Services.obs.notifyObservers(this, "autologin-processed");
  },

  _checkingIfPasswordStillMissing: false,
  _checkIfPasswordStillMissing() {
    // Avoid recursion.
    if (this._checkingIfPasswordStillMissing) {
      return;
    }

    this._checkingIfPasswordStillMissing = true;
    for (let account of this._accounts) {
      account._checkIfPasswordStillMissing();
    }
    delete this._checkingIfPasswordStillMissing;
  },

  getAccountById(aAccountId) {
    if (!aAccountId.startsWith(kAccountKeyPrefix)) {
      throw Components.Exception(
        `Invalid id: ${aAccountId}`,
        Cr.NS_ERROR_INVALID_ARG
      );
    }

    let id = parseInt(aAccountId.substr(kAccountKeyPrefix.length));
    return this.getAccountByNumericId(id);
  },

  _keepAccount(aAccount) {
    this._accounts.push(aAccount);
    this._accountsById[aAccount.numericId] = aAccount;
  },
  getAccountByNumericId(aAccountId) {
    return this._accountsById[aAccountId];
  },
  getAccounts() {
    return this._accounts;
  },

  createAccount(aName, aPrpl) {
    // Ensure an account with the same name and protocol doesn't already exist.
    let prpl = IMServices.core.getProtocolById(aPrpl);
    if (!prpl) {
      throw Components.Exception("", Cr.NS_ERROR_UNEXPECTED);
    }
    if (prpl.accountExists(aName)) {
      console.error("Attempted to create a duplicate account!");
      throw Components.Exception("", Cr.NS_ERROR_ALREADY_INITIALIZED);
    }

    /* First get a unique id for the new account. */
    let id;
    for (id = 1; ; ++id) {
      if (this._accountsById.hasOwnProperty(id)) {
        continue;
      }

      /* id isn't used by a known account, double check it isn't
       already used in the sqlite database. This should never
       happen, except if we have a corrupted profile. */
      if (!IMServices.contacts.accountIdExists(id)) {
        break;
      }
      Services.console.logStringMessage(
        "No account " +
          id +
          " but there is some data in the buddy list for an account with this number. Your profile may be corrupted."
      );
    }

    /* Actually create the new account. */
    let key = kAccountKeyPrefix + id;
    let account = new imAccount(key, aName, aPrpl);

    Services.obs.notifyObservers(account, "account-added");
    return account;
  },

  deleteAccount(aAccountId) {
    let account = this.getAccountById(aAccountId);
    if (!account) {
      throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
    }

    let index = this._accounts.indexOf(account);
    if (index == -1) {
      throw Components.Exception("", Cr.NS_ERROR_UNEXPECTED);
    }

    let id = account.numericId;
    account.remove();
    this._accounts.splice(index, 1);
    delete this._accountsById[id];
    Services.obs.notifyObservers(account, "account-removed");
  },

  QueryInterface: ChromeUtils.generateQI(["imIAccountsService"]),
  classDescription: "Accounts",
};