summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mattermost/rsc/google/googleserver/chat.go
diff options
context:
space:
mode:
authorChristopher Speller <crspeller@gmail.com>2016-05-12 23:56:07 -0400
committerChristopher Speller <crspeller@gmail.com>2016-05-12 23:56:07 -0400
commit38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8 (patch)
treea4fde09672192b97d453ad605b030bd5a10c5a45 /vendor/github.com/mattermost/rsc/google/googleserver/chat.go
parent84d2482ddbff9564c9ad75b2d30af66e3ddfd44d (diff)
downloadchat-38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8.tar.gz
chat-38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8.tar.bz2
chat-38ee83e45b4de7edf89bf9f0ef629eb4c6ad0fa8.zip
Moving to glide
Diffstat (limited to 'vendor/github.com/mattermost/rsc/google/googleserver/chat.go')
-rw-r--r--vendor/github.com/mattermost/rsc/google/googleserver/chat.go80
1 files changed, 80 insertions, 0 deletions
diff --git a/vendor/github.com/mattermost/rsc/google/googleserver/chat.go b/vendor/github.com/mattermost/rsc/google/googleserver/chat.go
new file mode 100644
index 000000000..8b064dc92
--- /dev/null
+++ b/vendor/github.com/mattermost/rsc/google/googleserver/chat.go
@@ -0,0 +1,80 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// TODO: Add ChatHangup.
+// TODO: Auto-hangup chats that are gone.
+
+package main
+
+import (
+ "fmt"
+
+ "github.com/mattermost/rsc/google"
+ "github.com/mattermost/rsc/xmpp"
+)
+
+type chatClient struct {
+ email string
+ id string
+ xmpp *xmpp.Client
+}
+
+var chatClients = map[string]*chatClient{}
+
+func (*Server) chatClient(cid *google.ChatID) (*chatClient, error) {
+ id := cid.ID
+ cc := chatClients[cid.ID]
+ if cc == nil {
+ a := google.Cfg.AccountByEmail(cid.Email)
+ if a == nil {
+ return nil, fmt.Errorf("unknown account %s", cid.Email)
+ }
+ // New client.
+ cli, err := xmpp.NewClient("talk.google.com:443", a.Email, a.Password)
+ if err != nil {
+ return nil, err
+ }
+ cc = &chatClient{email: a.Email, id: id, xmpp: cli}
+ cc.xmpp.Status(cid.Status, cid.StatusMsg)
+ chatClients[id] = cc
+ }
+ return cc, nil
+}
+
+func (srv *Server) ChatRecv(cid *google.ChatID, msg *xmpp.Chat) error {
+ cc, err := srv.chatClient(cid)
+ if err != nil {
+ return err
+ }
+ chat, err := cc.xmpp.Recv()
+ if err != nil {
+ return err
+ }
+ *msg = chat
+ return nil
+}
+
+func (srv *Server) ChatStatus(cid *google.ChatID, _ *Empty) error {
+ cc, err := srv.chatClient(cid)
+ if err != nil {
+ return err
+ }
+ return cc.xmpp.Status(cid.Status, cid.StatusMsg)
+}
+
+func (srv *Server) ChatSend(arg *google.ChatSend, _ *Empty) error {
+ cc, err := srv.chatClient(arg.ID)
+ if err != nil {
+ return err
+ }
+ return cc.xmpp.Send(arg.Msg)
+}
+
+func (srv *Server) ChatRoster(cid *google.ChatID, _ *Empty) error {
+ cc, err := srv.chatClient(cid)
+ if err != nil {
+ return err
+ }
+ return cc.xmpp.Roster()
+}