예제 #1
0
파일: client_irc.go 프로젝트: snichme/gobot
func getGroupForUser(robot types.Robot, username string) string {
	users := robot.Brain().Get("users").([]map[string]string)
	for _, user := range users {
		if user["username"] == username {
			return user["group"]
		}
	}
	return "guest"
}
예제 #2
0
파일: client_irc.go 프로젝트: snichme/gobot
func send(robot types.Robot, s ircx.Sender, message, target, user string) {
	q := types.Query{
		Statement: message,
		Context: types.QueryContext{
			Username: user,
			Group:    getGroupForUser(robot, user),
		},
	}

	if found, c := robot.Query(q); found {
		for answer := range c {
			fmt.Println(answer)
			s.Send(&irc.Message{
				Command: irc.PRIVMSG,
				Params:  []string{target, ":" + string(answer)},
			})
		}
	}
}
예제 #3
0
func restHandler(robot types.Robot) http.HandlerFunc {
	isAuthorized := func(username, password string) bool {
		users := robot.Brain().Get("users").([]map[string]string)
		for _, user := range users {
			if user["username"] == username && user["password"] == password {
				return true
			}
		}
		return false
	}

	getGroupForUser := func(username string) string {
		users := robot.Brain().Get("users").([]map[string]string)
		for _, user := range users {
			if user["username"] == username {
				return user["group"]
			}
		}
		return "guest"
	}

	return func(rw http.ResponseWriter, req *http.Request) {

		if req.Method != "POST" {
			http.Error(rw, "Not found", http.StatusNotFound)
			return
		}

		username, password, ok := req.BasicAuth()
		if ok == false || !isAuthorized(username, password) {
			http.Error(rw, "Forbidden", http.StatusForbidden)
			return
		}

		req.ParseForm()
		query := req.Form.Get("query")
		if len(query) == 0 {
			http.Error(rw, "No query", http.StatusBadRequest)
			return
		}

		q := types.Query{
			Statement: query,
			Context: types.QueryContext{
				Username: username,
				Group:    getGroupForUser(username), // no auth impl yet
			},
		}
		if found, c := robot.Query(q); found {
			var answers []types.Answer
			for answer := range c {
				answers = append(answers, answer)
			}
			resp := RestResponse{
				RobotName: robot.Name(),
				Query:     query,
				Answers:   answers,
			}
			js, _ := json.Marshal(resp)
			rw.Header().Set("Content-Type", "application/json")
			rw.Write(js)
		} else {
			http.Error(rw, "Not found", http.StatusNotFound)
		}
	}
}