forked from coyim/coyim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
108 lines (93 loc) · 3.4 KB
/
connection.go
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
package gui
import (
"fmt"
"log"
"math/rand"
"time"
"github.com/twstrike/coyim/config"
"github.com/twstrike/coyim/i18n"
"github.com/twstrike/coyim/xmpp/errors"
)
func (u *gtkUI) connectAccount(account *account) {
switch p := account.session.GetConfig().Password; p {
case "":
u.askForPasswordAndConnect(account)
default:
go u.connectWithPassword(account, p)
}
}
func (u *gtkUI) connectionFailureMoreInfoConnectionLost() {
u.notify(i18n.Local("Connection lost"), i18n.Local("We lost connection to the server for unknown reasons.\n\nWe will try to reconnect."))
}
func (u *gtkUI) connectionFailureMoreInfoTCPBindingFailed() {
u.notify(i18n.Local("Connection failure"), i18n.Local("We couldn't connect to the server because we couldn't determine a server address for the given domain.\n\nWe will try to reconnect."))
}
func (u *gtkUI) connectionFailureMoreInfoConnectionFailedGeneric() {
u.notify(i18n.Local("Connection failure"), i18n.Local("We couldn't connect to the server for some reason - verify that the server address is correct and that you are actually connected to the internet.\n\nWe will try to reconnect."))
}
func (u *gtkUI) connectionFailureMoreInfoConnectionFailed(ee error) func() {
return func() {
u.notify(i18n.Local("Connection failure"),
fmt.Sprintf(i18n.Local("We couldn't connect to the server - verify that the server address is correct and that you are actually connected to the internet.\n\nThis is the error we got: %s\n\nWe will try to reconnect."), ee.Error()))
}
}
func (u *gtkUI) connectWithPassword(account *account, password string) error {
if !account.session.IsDisconnected() {
return nil
}
removeNotification := u.showConnectAccountNotification(account)
defer removeNotification()
err := account.session.Connect(password, u.verifierFor(account))
switch err {
case config.ErrTorNotRunning:
u.notifyTorIsNotRunning(account)
case errors.ErrTCPBindingFailed:
u.notifyConnectionFailure(account, u.connectionFailureMoreInfoTCPBindingFailed)
case errors.ErrAuthenticationFailed:
u.askForPasswordAndConnect(account)
case errors.ErrConnectionFailed:
u.notifyConnectionFailure(account, u.connectionFailureMoreInfoConnectionFailedGeneric)
default:
ff, ok := err.(*errors.ErrFailedToConnect)
if ok {
u.notifyConnectionFailure(account, u.connectionFailureMoreInfoConnectionFailed(ff))
}
}
return err
}
func (u *gtkUI) askForPasswordAndConnect(account *account) {
if !account.IsAskingForPassword() {
accountName := account.session.GetConfig().Account
doInUIThread(func() {
account.AskForPassword()
u.askForPassword(accountName,
func() {
account.session.SetWantToBeOnline(false)
account.AskedForPassword()
},
func(password string) error {
account.AskedForPassword()
return u.connectWithPassword(account, password)
})
})
}
}
func (u *gtkUI) connectWithRandomDelay(a *account) {
sleepDelay := time.Duration(rand.Int31n(7643)) * time.Millisecond
log.Printf("connectWithRandomDelay(%v, %v)\n", a.session.GetConfig().Account, sleepDelay)
time.Sleep(sleepDelay)
a.session.SetWantToBeOnline(true)
a.Connect()
}
func (u *gtkUI) connectAllAutomatics(all bool) {
log.Printf("connectAllAutomatics(%v)\n", all)
var acc []*account
for _, a := range u.accounts {
if (all || a.session.GetConfig().ConnectAutomatically) && a.session.IsDisconnected() {
acc = append(acc, a)
}
}
for _, a := range acc {
go u.connectWithRandomDelay(a)
}
}