func handleCheckToken(j *simplejson.Json, s kiwi.MessageSender, m *kiwi.Message) error { uid, err := j.Get("uid").Int64() if err != nil { return ErrDeformedRequest } token, err := j.Get("token").String() if err != nil { return ErrDeformedRequest } network, err := j.Get("network").Int() if err != nil { return ErrDeformedRequest } db, err := sql.Open("mysql", dbConnInfo) if err != nil { return err } defer db.Close() // valid user and his token tokenIsValid, err := checkToken(uid, token) if err != nil { return err } if !tokenIsValid { sendNeedsLogin("check-token-return", s) return nil } // associate new conn id with user uidConnIdMap.add(uid, s.GetConn().ID) // update network stmtUpe, err := db.Prepare("UPDATE user SET network=? WHERE id=?") if err != nil { return err } defer stmtUpe.Close() _, err = stmtUpe.Exec(network, uid) if err != nil { return errors.New(fmt.Sprintf("cannot update conn id of user %d err: %s", uid, err.Error())) } retMsg := simplejson.New() retMsg.Set("action", "check-token-return") retMsg.Set("errCode", ErrNone) retMsg.Set("errMsg", ErrCodeMsg[ErrNone]) byts, _ := retMsg.MarshalJSON() s.SendWholeBytes(byts, false) return nil }
func handleLogin(j *simplejson.Json, s kiwi.MessageSender, m *kiwi.Message) error { email, err := j.Get("email").String() if err != nil { return ErrDeformedRequest } password, err := j.Get("password").String() if err != nil { return ErrDeformedRequest } network, err := j.Get("network").Int() if err != nil { return ErrDeformedRequest } db, err := sql.Open("mysql", dbConnInfo) if err != nil { return err } defer db.Close() // validate login info stmtOut, err := db.Prepare("SELECT id,nickname,avatar FROM user WHERE email=? AND password=?") if err != nil { return err } defer stmtOut.Close() var uid int64 var nickname sql.NullString var avatar []byte err = stmtOut.QueryRow(email, makePassword(password)).Scan(&uid, &nickname, &avatar) if err != nil { if err == sql.ErrNoRows { retMsg := simplejson.New() retMsg.Set("action", "login-return") retMsg.Set("uid", -1) retMsg.Set("token", "") retMsg.Set("errCode", ErrInvalidLoginInfo) retMsg.Set("errMsg", ErrCodeMsg[ErrInvalidLoginInfo]) byts, _ := retMsg.MarshalJSON() s.SendWholeBytes(byts, false) return nil } return err } // associate new conn id with user uidConnIdMap.add(uid, s.GetConn().ID) // update token/network token := makeToken() stmtUpe, err := db.Prepare("UPDATE user SET token=?,network=? WHERE id=?") if err != nil { return err } defer stmtUpe.Close() _, err = stmtUpe.Exec(token, network, uid) if err != nil { return errors.New(fmt.Sprintf("cannot update token of user %d err: %s", uid, err.Error())) } retMsg := simplejson.New() retMsg.Set("action", "login-return") retMsg.Set("uid", uid) retMsg.Set("token", token) retMsg.Set("nickname", nickname.String) retMsg.Set("avatar", encodeAvatar(avatar)) retMsg.Set("errCode", ErrNone) retMsg.Set("errMsg", ErrCodeMsg[ErrNone]) byts, _ := retMsg.MarshalJSON() s.SendWholeBytes(byts, false) return nil }
func handRegister(j *simplejson.Json, s kiwi.MessageSender, m *kiwi.Message) error { email, err := j.Get("email").String() if err != nil { return ErrDeformedRequest } nickname, err := j.Get("nickname").String() if err != nil { return ErrDeformedRequest } password, err := j.Get("password").String() if err != nil { return ErrDeformedRequest } db, err := sql.Open("mysql", dbConnInfo) if err != nil { return err } defer db.Close() // validate login info stmtOut, err := db.Prepare("SELECT COUNT(*) FROM user WHERE email=?") if err != nil { return err } defer stmtOut.Close() var count int err = stmtOut.QueryRow(email).Scan(&count) if err != nil { return err } if count != 0 { retMsg := simplejson.New() retMsg.Set("action", "register-return") retMsg.Set("uid", -1) retMsg.Set("token", "") retMsg.Set("errCode", ErrEmailAlreadyExists) retMsg.Set("errMsg", ErrCodeMsg[ErrEmailAlreadyExists]) byts, _ := retMsg.MarshalJSON() s.SendWholeBytes(byts, false) return nil } // make a default avatar with random color avatar := makeDefaultAvatar() stmtIns, err := db.Prepare("INSERT INTO user (email, password, nickname, token, avatar) VALUES(?, ?, ?, ?, ?)") if err != nil { return err } defer stmtIns.Close() token := makeToken() res, err := stmtIns.Exec(email, makePassword(password), nickname, token, avatar) if err != nil { return err } uid, _ := res.LastInsertId() // associate new conn id with user uidConnIdMap.add(uid, s.GetConn().ID) retMsg := simplejson.New() retMsg.Set("action", "register-return") retMsg.Set("uid", uid) retMsg.Set("nickname", nickname) retMsg.Set("avatar", encodeAvatar(avatar)) retMsg.Set("token", token) retMsg.Set("errCode", ErrNone) retMsg.Set("errMsg", ErrCodeMsg[ErrNone]) byts, _ := retMsg.MarshalJSON() s.SendWholeBytes(byts, false) return nil }
func handleSendMessage(j *simplejson.Json, s kiwi.MessageSender, m *kiwi.Message) error { token, err := j.Get("token").String() if err != nil { return ErrDeformedRequest } from, err := j.Get("from").Int64() if err != nil { return ErrDeformedRequest } to, err := j.Get("to").Int64() if err != nil { return ErrDeformedRequest } msgID, err := j.Get("msgID").Int() if err != nil { return ErrDeformedRequest } msgType, err := j.Get("msgType").Int() if err != nil { return ErrDeformedRequest } msgContent, err := j.Get("msgContent").String() if err != nil { return ErrDeformedRequest } // valid user and his token tokenIsValid, err := checkToken(from, token) if err != nil { return err } if !tokenIsValid { sendNeedsLogin("send-message-return", s) return nil } // retrieve buddy's conn id buddyConnId, ok := uidConnIdMap.getConnId(to) if !ok { sendUnreachable(msgID, s) return nil } conn, ok := s.GetConn().Server.ConnPool.Get(buddyConnId) if !ok { sendUnreachable(msgID, s) return nil } msg := simplejson.New() msg.Set("action", "new-message") msg.Set("from", from) msg.Set("msgType", msgType) msg.Set("msgContent", msgContent) msg.Set("errCode", ErrNone) msg.Set("errMsg", ErrCodeMsg[ErrNone]) sender := &kiwi.DefaultMessageSender{} sender.SetConn(conn) byts, _ := msg.MarshalJSON() _, err = sender.SendWholeBytes(byts, false) if err != nil { sendUnreachable(msgID, s) } return nil }