func (c *IRCClient) WriteChatLog(timestamp time.Time, from, channel, message string) *common.IRCLog {
	ircLog := &common.IRCLog{
		UserId:    c.UserId,
		LogId:     c.logIdSeq.Incr(),
		Timestamp: common.UnixMilli(timestamp),
		ServerId:  c.ServerId,
		Channel:   channel,
		From:      from,
		Message:   message,
	}

	common.RedisSave(ircLog)

	return ircLog
}
func MakeDefaultPacketHandler() *PacketMux {
	h := NewPacketMux()
	h.HandleFunc("register", func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
		reqBody := packet.body.(*ReqRegister)
		resBody := resp.body.(*ResRegister)

		g := NewGoogleOauth(reqBody.AccessToken)
		userinfo, err := g.GetUserInfo()
		if err != nil {
			log.Println("GetUserInfo Error:", err)
			resp.Status = -500
			resp.Msg = err.Error()
			return
		}
		log.Printf("%+v\n", userinfo)
		id, ok := userinfo["id"].(string)
		if !ok {
			log.Println("oauth Error!")
			resp.Status = -500
			resp.Msg = "Invalid Access Token"
			return
		}
		resBody.AuthKey = manager.user.RegisterUser(id)
	})

	h.HandleFunc("login", func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
		reqBody := packet.body.(*ReqLogin)

		user, err := manager.user.GetUserByKey(reqBody.AuthKey)
		if err != nil {
			log.Println("[Login] GetUserInfo Error:", err)
			resp.Status = -401
			resp.Msg = err.Error()
			return
		}
		c.user = user

		// add connection to user
		manager.user.register <- c

		log.Printf("%+v\n", user)
	})

	h.HandleFunc("getServers", AuthUser(func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
		resBody := resp.body.(*ResGetServers)

		resBody.Servers = c.user.GetServers()
		resBody.Channels = c.user.GetChannels()

		log.Printf("%+v %+v\n", resp, resBody)
	}))

	h.HandleFunc("getInitLogs", AuthUser(func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
		reqBody := packet.body.(*ReqGetInitLogs)
		resBody := resp.body.(*ResGetInitLogs)

		lastLogId := int64(-1)
		if reqBody.LastLogId != 0 {
			lastLogId = reqBody.LastLogId
		}

		var err error
		resBody.Logs, err = c.user.GetInitLogs(lastLogId, reqBody.LogCount)
		if err != nil {
			log.Printf("getInitLog Error :", err)
			resp.Status = -500
			resp.Msg = err.Error()
			return
		}
		log.Printf("%+v %+v\n", resp, resBody)
	}))

	h.HandleFunc("getPastLogs", AuthUser(func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
		reqBody := packet.body.(*ReqGetPastLogs)
		resBody := resp.body.(*ResGetPastLogs)

		var err error
		resBody.Logs, err = c.user.GetPastLogs(reqBody.LastLogId, reqBody.LogCount, reqBody.ServerId, reqBody.Channel)
		if err != nil {
			log.Printf("getPastLogs Error :", err)
			resp.Status = -500
			resp.Msg = err.Error()
			return
		}
		log.Printf("%+v %+v\n", resp, resBody)
	}))

	h.HandleFunc("pushLog", AuthUser(func(c *Connection, packet *Packet) {
		log.Printf("%+v\n", packet)
	}))

	h.HandleFunc("sendLog", AuthUser(func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
		reqBody := packet.body.(*ReqSendLog)
		resBody := resp.body.(*ResSendLog)

		server, err := c.user.GetServer(reqBody.ServerId)
		if err != nil || !server.Active {
			log.Println("SendLog failed. server is not connected")
			resp.Status = -500
			resp.Msg = "Server is not connected"
			return
		}

		irclog := &common.IRCLog{
			UserId:    c.user.Id,
			LogId:     c.user.logIdSeq.Incr(),
			ServerId:  reqBody.ServerId,
			Timestamp: common.UnixMilli(time.Now()),
			Channel:   reqBody.Channel,
			From:      server.User.Nickname,
			Message:   reqBody.Message,
		}
		c.user.SendChatMsg(reqBody.ServerId, irclog.Channel, irclog.Message)
		resBody.Log = irclog
		c.user.Send(MakePacket(&SendPushLog{Log: irclog}), c)
	}))

	h.HandleFunc("addServer", AuthUser(func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
		reqBody := packet.body.(*ReqAddServer)
		resBody := resp.body.(*ResAddServer)

		var err error
		resBody.Server, err = c.user.AddServer(reqBody.Server)
		if err != nil {
			log.Println("AddServer Error :", err)
			resp.Status = -500
			resp.Msg = err.Error()
			return
		}
		c.user.Send(resp, c)
	}))

	h.HandleFunc("addChannel", AuthUser(func(c *Connection, packet *Packet) {
		reqBody := packet.body.(*ReqAddChannel)
		c.user.AddChannelMsg(reqBody.ServerId, reqBody.Channel)
	}))

	return h
}
Beispiel #3
0
func MakeDefaultPacketHandler() *PacketMux {
	h := NewPacketMux()
	h.HandleFunc("register", func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
		reqBody := packet.body.(*ReqRegister)
		resBody := resp.body.(*ResRegister)

		g := NewGoogleOauth(reqBody.AccessToken)
		userinfo, err := g.GetUserInfo()
		if err != nil {
			log.Println("GetUserInfo Error:", err)
			resp.Status = -500
			resp.Msg = err.Error()
			return
		}
		log.Printf("%+v\n", userinfo)
		id, ok := userinfo["id"].(string)
		if !ok {
			log.Println("oauth Error!")
			resp.Status = -500
			resp.Msg = "Invalid Access Token"
			return
		}
		email := userinfo["email"].(string)
		verified := userinfo["verified_email"].(bool)
		if !verified {
			log.Println("Email is not verified")
			resp.Status = -403
			resp.Msg = "Your email is not verified"
			return
		}
		if config.AllowedUserOnly {
			if !manager.user.CheckAllowedUser(email) {
				log.Println("Not Allowed User:"******"You're not allowed user. Please, contact to admin"
				return
			}
		}
		resBody.AuthKey = manager.user.RegisterUser(id)
	})

	h.HandleFunc("login", func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
		reqBody := packet.body.(*ReqLogin)
		resBody := resp.body.(*ResLogin)

		user, err := manager.user.GetUserByKey(reqBody.AuthKey)
		if err != nil {
			log.Println("[Login] GetUserInfo Error:", err)
			resp.Status = -401
			resp.Msg = err.Error()
			return
		}
		c.user = user

		if reqBody.PushType != "" {
			resBody.Alert, err = c.user.GetNotification(reqBody.PushType, reqBody.PushToken)
			if err != nil {
				log.Println("[Login] GetPushAlertStatus Error:", err)
				resp.Status = -500
				resp.Msg = err.Error()
				return
			}
			c.pushToken = fmt.Sprintf("%s:%s", reqBody.PushType, reqBody.PushToken)
		}
		resBody.KeepAliveInterval = config.KeepAliveInterval

		// add connection to user
		manager.user.register <- c

		log.Printf("%+v\n", user)
	})

	h.HandleFunc("getServers", AuthUser(func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
		resBody := resp.body.(*ResGetServers)

		resBody.Servers = c.user.GetServers()
		resBody.Channels = c.user.GetChannels()

		log.Printf("%+v %+v\n", resp, resBody)
	}))

	h.HandleFunc("getInitLogs", AuthUser(func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
		reqBody := packet.body.(*ReqGetInitLogs)
		resBody := resp.body.(*ResGetInitLogs)

		lastLogId := int64(-1)
		if reqBody.LastLogId != 0 {
			lastLogId = reqBody.LastLogId
		}

		var err error
		resBody.Logs, err = c.user.GetInitLogs(lastLogId, reqBody.LogCount)
		if err != nil {
			log.Printf("getInitLog Error :", err)
			resp.Status = -500
			resp.Msg = err.Error()
			return
		}
		log.Printf("%+v %+v\n", resp, resBody)
	}))

	h.HandleFunc("getPastLogs", AuthUser(func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
		reqBody := packet.body.(*ReqGetPastLogs)
		resBody := resp.body.(*ResGetPastLogs)

		var err error
		resBody.Logs, err = c.user.GetPastLogs(reqBody.LastLogId, reqBody.LogCount, reqBody.ServerId, reqBody.Channel)
		if err != nil {
			log.Printf("getPastLogs Error :", err)
			resp.Status = -500
			resp.Msg = err.Error()
			return
		}
		log.Printf("%+v %+v\n", resp, resBody)
	}))

	h.HandleFunc("pushLog", AuthUser(func(c *Connection, packet *Packet) {
		c.user.AckPushMessage(packet.MsgId)
	}))

	h.HandleFunc("sendLog", AuthUser(func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
		reqBody := packet.body.(*ReqSendLog)
		resBody := resp.body.(*ResSendLog)

		server, err := c.user.GetServer(reqBody.ServerId)
		if err != nil || !server.Active {
			log.Println("SendLog failed. server is not connected")
			resp.Status = -500
			resp.Msg = "Server is not connected"
			return
		}

		irclog := &common.IRCLog{
			UserId:    c.user.Id,
			LogId:     c.user.logIdSeq.Incr(),
			ServerId:  reqBody.ServerId,
			Timestamp: common.UnixMilli(time.Now()),
			Channel:   reqBody.Channel,
			From:      server.User.Nickname,
			Message:   reqBody.Message,
		}
		c.user.SendChatMsg(reqBody.ServerId, irclog.Channel, irclog.Message)
		resBody.Log = irclog
		c.user.Send(MakePacket(&SendPushLog{Log: irclog}), c)
	}))

	h.HandleFunc("addServer", AuthUser(func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
		reqBody := packet.body.(*ReqAddServer)
		resBody := resp.body.(*ResAddServer)

		var err error
		resBody.Server, err = c.user.AddServer(reqBody.Server)
		if err != nil {
			log.Println("AddServer Error :", err)
			resp.Status = -500
			resp.Msg = err.Error()
			return
		}
		c.user.Send(resp, c)
	}))

	h.HandleFunc("addChannel", AuthUser(func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
		reqBody := packet.body.(*ReqAddChannel)
		resBody := resp.body.(*ResAddChannel)

		resBody.Channel = &common.IRCChannel{Name: reqBody.Channel, ServerId: reqBody.ServerId}
		c.user.AddChannelMsg(reqBody.ServerId, reqBody.Channel)
	}))

	h.HandleFunc("setNotification", AuthUser(func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
		reqBody := packet.body.(*ReqSetNotification)

		if err := c.user.SetNotification(reqBody.PushType, reqBody.PushToken, reqBody.Alert); err != nil {
			log.Println("SetNotification Error : ", err)
			resp.Status = -500
			resp.Msg = err.Error()
			return
		}
	}))

	h.HandleFunc("joinPartChannel", AuthUser(func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
		reqBody := packet.body.(*ReqJoinPartChannel)

		c.user.JoinPartChannel(reqBody.ServerId, reqBody.Channel, reqBody.Join)
	}))

	h.HandleFunc("delChannel", AuthUser(func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
		reqBody := packet.body.(*ReqDelChannel)

		c.user.DelChannel(reqBody.ServerId, reqBody.Channel)
	}))

	h.HandleFunc("ping", func(c *Connection, packet *Packet) {
		resp := packet.MakeResponse()
		defer c.Send(resp)
	})

	return h
}