summaryrefslogtreecommitdiff
path: root/client/application.go
diff options
context:
space:
mode:
Diffstat (limited to 'client/application.go')
-rw-r--r--client/application.go100
1 files changed, 86 insertions, 14 deletions
diff --git a/client/application.go b/client/application.go
index d396f24..640abd6 100644
--- a/client/application.go
+++ b/client/application.go
@@ -145,37 +145,109 @@ func (a *application) GetInfo(id string, callback func(*proto.Object)) {
})
}
-func (a *application) auth(name string, authCallback func(success bool)) {
- callback := func(response proto.Command) {
+func (a *application) authAnon(as string, callback func(ok bool, uid string)) {
+ cb := func(response proto.Command) {
+ switch response.Kind {
+ case "you-are":
+ if len(response.Args) == 0 {
+ callback(false, "")
+ break
+ }
+ me := response.Args[0]
+ a.onAuth(me.Id)
+ if callback != nil {
+ callback(true, me.Id)
+ }
+ case "fail":
+ var uid string
+ if len(response.Args) != 0 {
+ if response.Args[0].Kind == "name-taken" &&
+ response.Args[0].Fields["anonymous"] == "no" {
+ uid = response.Args[0].Fields["id"]
+ } else {
+ a.cmdWindow.err(proto.Strfail(response.Args[0]))
+ }
+ }
+ if callback != nil {
+ callback(false, uid)
+ }
+ }
+ }
+ a.Request(proto.NewCmd("auth", "", proto.Object {
+ "anonymous", "", map[string]string {"": as},
+ }), cb)
+}
+
+func (a *application) authPassword(
+ uid string, password string, callback func(ok bool)) {
+ cb := func(response proto.Command) {
switch response.Kind {
case "you-are":
if len(response.Args) == 0 {
- authCallback(false)
+ callback(false)
break
}
me := response.Args[0]
- a.authenticated = true
- a.uid = me.Id
- a.cache.Watch(a.uid)
- a.onAuth()
- if authCallback != nil {
- authCallback(true)
+ a.onAuth(me.Id)
+ if callback != nil {
+ callback(true)
}
case "fail":
if len(response.Args) != 0 {
a.cmdWindow.err(proto.Strfail(response.Args[0]))
}
- if authCallback != nil {
- authCallback(false)
+ if callback != nil {
+ callback(false)
}
}
}
a.Request(proto.NewCmd("auth", "", proto.Object {
- "anonymous", "", map[string]string {"": name},
- }), callback)
+ "password", "", map[string]string {"": password, "id": uid},
+ }), cb)
+}
+
+func (a *application) setPassword(password string, callback func(ok bool)) {
+ cb := func(response proto.Command) {
+ switch response.Kind {
+ case "ok":
+ if callback != nil {
+ callback(true)
+ }
+ case "fail":
+ if len(response.Args) != 0 &&
+ response.Args[0].Kind == "password-required" {
+ lp := newLoginPrompt("")
+ lp.uid = a.uid
+ lp.customPrompt = "current password"
+ lp.callback = func(ok bool) {
+ if ok {
+ a.setPassword(password, callback)
+ } else {
+ if callback != nil {
+ callback(false)
+ }
+ }
+ }
+ a.pushPrompt(lp)
+ } else {
+ if len(response.Args) != 0 {
+ a.cmdWindow.err(proto.Strfail(response.Args[0]))
+ }
+ if callback != nil {
+ callback(false)
+ }
+ }
+ }
+ }
+ a.Request(proto.NewCmd("auth-update", a.uid, proto.Object {
+ "password", "", map[string]string {"": password},
+ }), cb)
}
-func (a *application) onAuth() {
+func (a *application) onAuth(uid string) {
+ a.authenticated = true
+ a.uid = uid
+ a.cache.Watch(uid)
a.Request(proto.NewCmd("channels", ""), func(response proto.Command) {
if response.Kind == "channels" {
previousChannels := make(channelList, len(a.channelList))