-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathui.go
204 lines (175 loc) · 4.68 KB
/
ui.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
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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/agl/xmpp-client/xmpp"
"golang.org/x/crypto/otr"
"github.com/agl/xmpp-client/xlib"
)
var configFile *string = flag.String("config-file", "", "Location of the config file")
var createAccount *bool = flag.Bool("create", false, "If true, attempt to create account")
func main() {
flag.Parse()
xlib.XIOTerm_Init()
defer xlib.XIOTerm_Exit()
xio := xlib.NewXIOTerm()
defer xio.Destroy()
xio.Resize()
resizeChan := make(chan os.Signal)
go func() {
for _ = range resizeChan {
xio.Resize()
}
}()
signal.Notify(resizeChan, syscall.SIGWINCH)
if len(*configFile) == 0 {
homeDir := os.Getenv("HOME")
if len(homeDir) == 0 {
xio.Alert("$HOME not set. Please either export $HOME or use the -config-file option.\n")
return
}
persistentDir := filepath.Join(homeDir, "Persistent")
if stat, err := os.Lstat(persistentDir); err == nil && stat.IsDir() {
// Looks like Tails.
homeDir = persistentDir
}
*configFile = filepath.Join(homeDir, ".xmpp-client")
}
config, err := xlib.ParseConfig(*configFile)
if err != nil {
xio.Alert("Failed to parse config file: " + err.Error())
config = xlib.NewConfig(*configFile)
if !xlib.Enroll(config, xio) {
return
}
config.Save()
}
password := config.Password
if len(password) == 0 {
if password, err = xio.ReadPassword(fmt.Sprintf("Password for %s (will not be saved to disk): ", config.Account)); err != nil {
xio.Alert("Failed to read password: " + err.Error())
return
}
config.Password = password
}
xio.SetPrompt("> ")
var createCallback xmpp.FormCallback
if *createAccount {
user, _, _ := xlib.UserDom(config.Account)
createCallback = func(title, instructions string, fields []interface{}) error {
return promptForForm(xio, user, password, title, instructions, fields)
}
}
lgr := xlib.NewLineLogger(xio)
s, err := xlib.Connect(xio, config, lgr, createCallback)
if err != nil {
xio.Alert("Failed to connect: " + err.Error())
return
}
s.SignalPresence("")
s.FetchRoster()
input := NewInput(xio)
commandChan := make(chan interface{})
go input.ProcessCommands(s, commandChan)
xio.Info(fmt.Sprintf("Your fingerprint is %x", s.GetFingerprint()))
go s.Handle()
MainLoop:
for {
select {
case cmd, ok := <-commandChan:
if !ok {
xio.Warn("Exiting because command channel closed")
break MainLoop
}
s.LastAction()
switch cmd := cmd.(type) {
case quitCommand:
s.Quit()
break MainLoop
case versionCommand:
s.GetVersion(cmd.User)
case rosterCommand:
s.Xio.Info("Current roster:")
maxLen := 0
roster := s.GetRoster()
for _, item := range roster {
if maxLen < len(item.Jid) {
maxLen = len(item.Jid)
}
}
for _, item := range roster {
state, ok := s.GetState(item.Jid)
line := ""
if ok {
line += "[*] "
} else if cmd.OnlineOnly {
continue
} else {
line += "[ ] "
}
line += item.Jid
numSpaces := 1 + (maxLen - len(item.Jid))
for i := 0; i < numSpaces; i++ {
line += " "
}
line += item.Subscription + "\t" + item.Name
if ok {
line += "\t" + state
}
s.Xio.Info(line)
}
case rosterEditCommand:
s.DoEditRoster()
case rosterEditDoneCommand:
s.DoEditDoneRoster()
case toggleStatusUpdatesCommand:
s.ToggleStatusUpdates()
case confirmCommand:
s.HandleConfirmOrDeny(cmd.User, true /* confirm */)
case denyCommand:
s.HandleConfirmOrDeny(cmd.User, false /* deny */)
case addCommand:
s.SendPresence(cmd.User, "subscribe", "" /* generate id */)
case joinCommand:
s.Xio.Info(fmt.Sprintf("Warning: OTR is ***NOT SUPPORTED*** for Multi-User-Chats"))
s.JoinMUC(cmd.User, "", "")
case leaveCommand:
s.LeaveMUC(cmd.User)
case msgCommand:
s.Msg(cmd.to, cmd.msg, cmd.setPromptIsEncrypted)
case otrCommand:
s.Send(string(cmd.User), otr.QueryMessage)
case otrInfoCommand:
xio.Info(fmt.Sprintf("Your OTR fingerprint is %x", s.GetFingerprint()))
s.PrintConversations()
case endOTRCommand:
s.EndConversation(cmd.User)
case authQACommand:
s.AuthQACommand(cmd.User, cmd.Question, cmd.Secret)
case authOobCommand:
s.AuthOOBCommand(cmd.User, cmd.Fingerprint)
case awayCommand:
s.SignalPresence("away")
case chatCommand:
s.SignalPresence("chat")
case dndCommand:
s.SignalPresence("dnd")
case xaCommand:
s.SignalPresence("xa")
case onlineCommand:
s.SignalPresence("")
case ignoreCommand:
s.IgnoreUser(cmd.User)
case unignoreCommand:
s.UnignoreUser(cmd.User)
case ignoreListCommand:
s.IgnoreList()
}
}
}
os.Stdout.Write([]byte("\n"))
}