Example #1
0
func (con Component) Handler(c context.Context) http.Handler {
	mw, i18nFound := con.dispatcher.Middleware("I18N")
	logger := webfw.GetLogger(c)

	rnd := renderer.NewRenderer(con.dispatcher.Config.Renderer.Dir, "raw.tmpl")
	rnd.Delims("{%", "%}")

	if i18nFound {
		if i18n, ok := mw.(middleware.I18N); ok {
			rnd.Funcs(i18n.TemplateFuncMap())
		}
	} else {
		logger.Infoln("I18N middleware not found")
	}

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		params := webfw.GetParams(c, r)

		err := rnd.Render(w, renderer.RenderData{"apiPattern": con.apiPattern},
			c.GetAll(r), "components/"+params["name"]+".tmpl")

		if err != nil {
			webfw.GetLogger(c).Print(err)
		}
	})
}
Example #2
0
func (con App) Handler(c context.Context) http.Handler {
	cfg := readeef.GetConfig(c)
	rnd := webfw.GetRenderer(c)

	if cfg.Logger.Level == "debug" {
		rnd.SkipCache(true)
	}

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		action := webfw.GetMultiPatternIdentifier(c, r)

		data := renderer.RenderData{}
		if action == "history" {
			params := webfw.GetParams(c, r)
			data["history"] = "/web/" + params["history"]
		}

		if r.Method != "HEAD" {
			err := rnd.Render(w, data, c.GetAll(r), "app.tmpl")
			if err != nil {
				webfw.GetLogger(c).Print(err)
			}
		}

		w.Header().Set("X-Readeef", "1")
	})
}
Example #3
0
func (con Auth) Handler(c context.Context) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		action := webfw.GetMultiPatternIdentifier(c, r)
		sess := webfw.GetSession(c, r)

		var resp responseError

		switch action {
		case "auth-data":
			user := readeef.GetUser(c, r)
			resp = getAuthData(user, sess, con.capabilities)
		case "logout":
			resp = logout(sess)
		}

		var b []byte
		if resp.err == nil {
			b, resp.err = json.Marshal(resp.val)
		}
		if resp.err == nil {
			w.Write(b)
		} else {
			webfw.GetLogger(c).Print(resp.err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}
	})
}
Example #4
0
func (con UserSettings) Handler(c context.Context) http.Handler {
	cfg := readeef.GetConfig(c)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		user := readeef.GetUser(c, r)

		params := webfw.GetParams(c, r)
		attr := params["attribute"]

		var resp responseError
		if r.Method == "GET" {
			resp = getUserAttribute(user, attr)
		} else if r.Method == "POST" {
			buf := util.BufferPool.GetBuffer()
			defer util.BufferPool.Put(buf)

			buf.ReadFrom(r.Body)

			resp = setUserAttribute(user, []byte(cfg.Auth.Secret), attr, buf.Bytes())
		}

		var b []byte
		if resp.err == nil {
			b, resp.err = json.Marshal(resp.val)
		}

		if resp.err != nil {
			webfw.GetLogger(c).Print(resp.err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.Write(b)
	})
}
func (con FeedUpdateNotificator) Handler(c context.Context) http.HandlerFunc {
	var mutex sync.RWMutex

	receivers := make(map[chan readeef.Feed]bool)

	go func() {
		for {
			select {
			case feed := <-con.updateFeed:
				mutex.RLock()

				readeef.Debug.Printf("Feed %s updated. Notifying %d receivers.", feed.Link, len(receivers))
				for receiver, _ := range receivers {
					receiver <- feed
				}

				mutex.RUnlock()
			}
		}
	}()

	return func(w http.ResponseWriter, r *http.Request) {
		var err error

		receiver := make(chan readeef.Feed)

		mutex.Lock()
		receivers[receiver] = true
		mutex.Unlock()
		defer func() {
			mutex.Lock()
			delete(receivers, receiver)
			mutex.Unlock()
		}()

		f := <-receiver
		readeef.Debug.Println("Feed " + f.Link + " updated")

		resp := map[string]interface{}{"Feed": feed{
			Id: f.Id, Title: f.Title, Description: f.Description,
			Link: f.Link, Image: f.Image,
		}}

		var b []byte
		if err == nil {
			b, err = json.Marshal(resp)
		}
		if err != nil {
			webfw.GetLogger(c).Print(err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.Write(b)
	}
}
Example #6
0
func (con App) Handler(c context.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		rnd := webfw.GetRenderer(c)

		err := rnd.Render(w, nil, c.GetAll(r), "app.tmpl")
		if err != nil {
			webfw.GetLogger(c).Print(err)
		}
	}
}
Example #7
0
func (con Article) Handler(c context.Context) http.Handler {
	logger := webfw.GetLogger(c)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		user := readeef.GetUser(c, r)

		params := webfw.GetParams(c, r)
		action := webfw.GetMultiPatternIdentifier(c, r)

		logger.Infof("Invoking Article controller with action '%s', article id '%s'\n", action, params["article-id"])

		var articleId int64
		var resp responseError

		articleId, resp.err = strconv.ParseInt(params["article-id"], 10, 64)

		if resp.err == nil {
			id := data.ArticleId(articleId)
			switch action {
			case "fetch":
				resp = fetchArticle(user, id)
			case "read":
				resp = articleReadState(user, id, params["value"] == "true")
			case "favorite":
				resp = articleFavoriteState(user, id, params["value"] == "true")
			case "format":
				resp = formatArticle(user, id, con.extractor, webfw.GetConfig(c), con.config)
			}
		}

		var b []byte
		if resp.err == nil {
			b, resp.err = json.Marshal(resp.val)
		}

		if resp.err == nil {
			w.Write(b)
		} else {
			webfw.GetLogger(c).Print(resp.err)

			w.WriteHeader(http.StatusInternalServerError)
		}
	})
}
Example #8
0
func (con Component) Handler(c context.Context) http.Handler {
	i18nmw, i18nFound := con.dispatcher.Middleware("I18N")
	urlmw, urlFound := con.dispatcher.Middleware("Url")
	logger := webfw.GetLogger(c)
	cfg := readeef.GetConfig(c)

	rnd := renderer.NewRenderer(con.dispatcher.Config.Renderer.Dir, "raw.tmpl")
	rnd.Delims("{%", "%}")

	if cfg.Logger.Level == "debug" {
		rnd.SkipCache(true)
	}

	if i18nFound {
		if i18n, ok := i18nmw.(middleware.I18N); ok {
			rnd.Funcs(i18n.TemplateFuncMap())
		}
	} else {
		logger.Infoln("I18N middleware not found")
	}

	if urlFound {
		if url, ok := urlmw.(middleware.Url); ok {
			rnd.Funcs(url.TemplateFuncMap(c))
		}
	} else {
		logger.Infoln("Url middleware not found")
	}

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		params := webfw.GetParams(c, r)

		if r.Method != "HEAD" {
			err := rnd.Render(w, renderer.RenderData{"apiPattern": con.apiPattern, "config": cfg},
				c.GetAll(r), "components/"+params["name"]+".tmpl")

			if err != nil {
				webfw.GetLogger(c).Print(err)
			}
		}
	})
}
Example #9
0
func (con User) Handler(c context.Context) http.Handler {
	cfg := readeef.GetConfig(c)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		user := readeef.GetUser(c, r)

		action := webfw.GetMultiPatternIdentifier(c, r)
		params := webfw.GetParams(c, r)

		var resp responseError
		switch action {
		case "list":
			resp = listUsers(user)
		case "add":
			buf := util.BufferPool.GetBuffer()
			defer util.BufferPool.Put(buf)

			buf.ReadFrom(r.Body)

			resp = addUser(user, data.Login(params["login"]), buf.String(), []byte(cfg.Auth.Secret))
		case "remove":
			resp = removeUser(user, data.Login(params["login"]))
		case "setAttr":
			resp = setAttributeForUser(user, []byte(cfg.Auth.Secret), data.Login(params["login"]), params["attr"], []byte(params["value"]))
		}

		switch resp.err {
		case errForbidden:
			w.WriteHeader(http.StatusForbidden)
			return
		case errUserExists:
			resp.val["Error"] = true
			resp.val["ErrorType"] = resp.errType
			resp.err = nil
		case errCurrentUser:
			resp.val["Error"] = true
			resp.val["ErrorType"] = resp.errType
			resp.err = nil
		}

		var b []byte
		if resp.err == nil {
			b, resp.err = json.Marshal(resp.val)
		}
		if resp.err != nil {
			webfw.GetLogger(c).Print(resp.err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.Write(b)
	})
}
Example #10
0
func (con Login) Handler(c context.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		l := webfw.GetLogger(c)
		sess := webfw.GetSession(c, r)
		renderData := renderer.RenderData{}

		if r.Method == "GET" {
			if v, ok := sess.Flash("form-error"); ok {
				renderData["form-error"] = v
			}
		} else {
			if err := r.ParseForm(); err != nil {
				l.Fatal(err)
			}
			username := data.Login(r.Form.Get("username"))
			password := r.Form.Get("password")

			repo := GetRepo(c)
			conf := GetConfig(c)
			u := repo.UserByLogin(username)

			formError := false
			if u.Err() != nil {
				sess.SetFlash("form-error", "login-incorrect")
				formError = true
			} else if !u.Authenticate(password, []byte(conf.Auth.Secret)) {
				sess.SetFlash("form-error", "login-incorrect")
				formError = true
			} else {
				sess.Set(AuthUserKey, u)
				sess.Set(AuthNameKey, username)
			}

			if formError {
				http.Redirect(w, r, r.URL.String(), http.StatusTemporaryRedirect)
			} else {
				var returnPath string
				if v, ok := sess.Flash("return-to"); ok {
					returnPath = v.(string)
				} else {
					returnPath = webfw.GetDispatcher(c).Pattern
				}
				http.Redirect(w, r, returnPath, http.StatusTemporaryRedirect)
			}
			return
		}
		err := webfw.GetRenderCtx(c, r)(w, renderData, "login.tmpl")
		if err != nil {
			l.Print(err)
		}
	}
}
Example #11
0
func (con Login) Handler(c context.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		l := webfw.GetLogger(c)
		sess := webfw.GetSession(c, r)
		data := renderer.RenderData{}

		if r.Method == "GET" {
			if v, ok := sess.Flash("form-error"); ok {
				data["form-error"] = v
			}
		} else {
			if err := r.ParseForm(); err != nil {
				l.Fatal(err)
			}
			username := r.Form.Get("username")
			password := r.Form.Get("password")

			db := GetDB(c)

			formError := false
			if u, err := db.GetUser(username); err != nil {
				sess.SetFlash("form-error", "login-incorrect")
				formError = true
			} else if !u.Authenticate(password) {
				sess.SetFlash("form-error", "login-incorrect")
				formError = true
			} else {
				sess.Set(authkey, u)
				sess.Set(namekey, u.Login)
			}

			if formError {
				http.Redirect(w, r, r.URL.String(), http.StatusTemporaryRedirect)
			} else {
				var returnPath string
				if v, ok := sess.Flash("return-to"); ok {
					returnPath = v.(string)
				} else {
					returnPath = webfw.GetDispatcher(c).Pattern
				}
				http.Redirect(w, r, returnPath, http.StatusTemporaryRedirect)
			}
			return
		}
		err := webfw.GetRenderCtx(c, r)(w, data, "login.tmpl")
		if err != nil {
			l.Print(err)
		}
	}
}
Example #12
0
func (con Component) Handler(c context.Context) http.HandlerFunc {
	rnd := renderer.NewRenderer(con.dir, "raw.tmpl")
	rnd.Delims("{%", "%}")

	return func(w http.ResponseWriter, r *http.Request) {
		params := webfw.GetParams(c, r)

		err := rnd.Render(w, renderer.RenderData{"apiPattern": con.apiPattern},
			c.GetAll(r), "components/"+params["name"]+".tmpl")

		if err != nil {
			webfw.GetLogger(c).Print(err)
		}
	}
}
Example #13
0
func (con Auth) Handler(c context.Context) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		user := readeef.GetUser(c, r)

		resp := getAuthData(user)

		b, err := json.Marshal(resp.val)
		if err != nil {
			webfw.GetLogger(c).Print(err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.Write(b)
	})
}
Example #14
0
func (con App) Handler(c context.Context) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		rnd := webfw.GetRenderer(c)
		action := webfw.GetMultiPatternIdentifier(c, r)

		data := renderer.RenderData{}
		if action == "history" {
			params := webfw.GetParams(c, r)
			data["history"] = "/web/" + params["history"]
		}

		err := rnd.Render(w, data, c.GetAll(r), "app.tmpl")
		if err != nil {
			webfw.GetLogger(c).Print(err)
		}
	})
}
Example #15
0
func (con Nonce) Handler(c context.Context) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		nonce := con.nonce.Generate()
		type response struct {
			Nonce string
		}
		resp := response{nonce}

		b, err := json.Marshal(resp)
		if err != nil {
			webfw.GetLogger(c).Print(err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.Write(b)

		con.nonce.Set(nonce)
	})
}
Example #16
0
func (con Auth) Handler(c context.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		user := readeef.GetUser(c, r)

		type User struct {
			Login     string
			FirstName string
			LastName  string
			Email     string
			Admin     bool
		}
		type response struct {
			Auth        bool
			User        User
			ProfileData map[string]interface{}
		}

		resp := response{
			Auth: true,
			User: User{
				Login:     user.Login,
				FirstName: user.FirstName,
				LastName:  user.LastName,
				Email:     user.Email,
				Admin:     user.Admin,
			},
			ProfileData: user.ProfileData,
		}

		b, err := json.Marshal(resp)
		if err != nil {
			webfw.GetLogger(c).Print(err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.Write(b)
	}
}
Example #17
0
func (con Search) Handler(c context.Context) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		user := readeef.GetUser(c, r)
		params := webfw.GetParams(c, r)
		query := params["query"]

		var resp responseError

		if resp.err = r.ParseForm(); resp.err == nil {
			highlight := ""
			feedId := ""

			if vs := r.Form["highlight"]; len(vs) > 0 {
				highlight = vs[0]
			}

			if vs := r.Form["id"]; len(vs) > 0 {
				if vs[0] != "all" && vs[0] != "favorite" {
					feedId = vs[0]
				}
			}

			resp = search(user, con.searchIndex, query, highlight, feedId)
		}

		var b []byte
		if resp.err == nil {
			b, resp.err = json.Marshal(resp.val)
		}
		if resp.err != nil {
			webfw.GetLogger(c).Print(resp.err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.Write(b)
	})
}
Example #18
0
func (con HubbubController) Handler(c context.Context) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		params := r.URL.Query()
		pathParams := webfw.GetParams(c, r)
		logger := webfw.GetLogger(c)
		feedId, err := strconv.ParseInt(pathParams["feed-id"], 10, 64)

		if err != nil {
			webfw.GetLogger(c).Print(err)
			return
		}

		repo := con.hubbub.repo
		f := repo.FeedById(data.FeedId(feedId))
		s := f.Subscription()

		err = s.Err()

		if err != nil {
			webfw.GetLogger(c).Print(err)
			return
		}

		logger.Infoln("Receiving hubbub event " + params.Get("hub.mode") + " for " + f.String())

		data := s.Data()
		switch params.Get("hub.mode") {
		case "subscribe":
			if lease, err := strconv.Atoi(params.Get("hub.lease_seconds")); err == nil {
				data.LeaseDuration = int64(lease) * int64(time.Second)
			}
			data.VerificationTime = time.Now()

			w.Write([]byte(params.Get("hub.challenge")))
		case "unsubscribe":
			w.Write([]byte(params.Get("hub.challenge")))
		case "denied":
			w.Write([]byte{})
			webfw.GetLogger(c).Printf("Unable to subscribe to '%s': %s\n", params.Get("hub.topic"), params.Get("hub.reason"))
		default:
			w.Write([]byte{})

			buf := util.BufferPool.GetBuffer()
			defer util.BufferPool.Put(buf)

			if _, err := buf.ReadFrom(r.Body); err != nil {
				webfw.GetLogger(c).Print(err)
				return
			}

			newArticles := false

			if pf, err := parser.ParseFeed(buf.Bytes(), parser.ParseRss2, parser.ParseAtom, parser.ParseRss1); err == nil {
				f.Refresh(pf)
				f.Update()

				if f.HasErr() {
					webfw.GetLogger(c).Print(f.Err())
					return
				}

				newArticles = len(f.NewArticles()) > 0
			} else {
				webfw.GetLogger(c).Print(err)
				return
			}

			if newArticles {
				con.hubbub.NotifyReceivers(f)
			}

			return
		}

		switch params.Get("hub.mode") {
		case "subscribe":
			data.SubscriptionFailure = false
		case "unsubscribe", "denied":
			data.SubscriptionFailure = true
		}

		s.Data(data)
		s.Update()
		if s.HasErr() {
			webfw.GetLogger(c).Print(s.Err())
			return
		}

		if data.SubscriptionFailure {
			con.hubbub.removeFeed <- f
		} else {
			con.hubbub.addFeed <- f
		}
	})
}
Example #19
0
func (con User) Handler(c context.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var err error

		db := readeef.GetDB(c)
		user := readeef.GetUser(c, r)

		if !user.Admin {
			readeef.Debug.Println("User " + user.Login + " is not an admin")

			w.WriteHeader(http.StatusForbidden)
			return
		}

		action := webfw.GetMultiPatternIdentifier(c, r)
		params := webfw.GetParams(c, r)
		resp := make(map[string]interface{})

		switch action {
		case "list":
			users, err := db.GetUsers()
			if err != nil {
				break
			}

			type user struct {
				Login     string
				FirstName string
				LastName  string
				Email     string
				Active    bool
				Admin     bool
			}

			userList := []user{}
			for _, u := range users {
				userList = append(userList, user{
					Login:     u.Login,
					FirstName: u.FirstName,
					LastName:  u.LastName,
					Email:     u.Email,
					Active:    u.Active,
					Admin:     u.Admin,
				})
			}

			resp["Users"] = userList
		case "add":
			login := params["login"]

			_, err = db.GetUser(login)
			/* TODO: non-fatal error */
			if err == nil {
				err = errors.New("User with login " + login + " already exists")
				break
			} else if err != sql.ErrNoRows {
				break
			}

			buf := util.BufferPool.GetBuffer()
			defer util.BufferPool.Put(buf)

			buf.ReadFrom(r.Body)

			u := readeef.User{Login: login}

			err = u.SetPassword(buf.String())
			if err != nil {
				break
			}

			err = db.UpdateUser(u)
			if err != nil {
				break
			}

			resp["Success"] = true
			resp["Login"] = login
		case "remove":
			login := params["login"]

			if user.Login == login {
				err = errors.New("The current user cannot be removed")
				break
			}

			var u readeef.User

			u, err = db.GetUser(login)
			if err != nil {
				break
			}

			err = db.DeleteUser(u)
			if err != nil {
				break
			}

			resp["Success"] = true
			resp["Login"] = login
		case "active":
			login := params["login"]

			if user.Login == login {
				err = errors.New("The current user cannot be removed")
				break
			}

			active := params["state"] == "true"

			var u readeef.User

			u, err = db.GetUser(login)
			if err != nil {
				break
			}

			u.Active = active
			err = db.UpdateUser(u)
			if err != nil {
				break
			}

			resp["Success"] = true
			resp["Login"] = login
		}

		var b []byte
		if err == nil {
			b, err = json.Marshal(resp)
		}
		if err != nil {
			webfw.GetLogger(c).Print(err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.Write(b)
	}
}
Example #20
0
func (con Feed) Handler(c context.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var err error

		db := readeef.GetDB(c)
		user := readeef.GetUser(c, r)

		action := webfw.GetMultiPatternIdentifier(c, r)
		params := webfw.GetParams(c, r)
		resp := make(map[string]interface{})

	SWITCH:
		switch action {
		case "list":
			var feeds []readeef.Feed
			feeds, err = db.GetUserTagsFeeds(user)

			if err != nil {
				break
			}

			respFeeds := []feed{}

			for _, f := range feeds {
				respFeeds = append(respFeeds, feed{
					Id: f.Id, Title: f.Title, Description: f.Description,
					Link: f.Link, Image: f.Image, Tags: f.Tags,
					UpdateError: f.UpdateError, SubscribeError: f.SubscribeError,
				})
			}

			resp["Feeds"] = respFeeds
		case "discover":
			r.ParseForm()

			link := r.FormValue("url")
			var u *url.URL

			/* TODO: non-fatal error */
			if u, err = url.Parse(link); err != nil {
				break
				/* TODO: non-fatal error */
			} else if !u.IsAbs() {
				u.Scheme = "http"
				if u.Host == "" {
					parts := strings.SplitN(u.Path, "/", 2)
					u.Host = parts[0]
					if len(parts) > 1 {
						u.Path = "/" + parts[1]
					} else {
						u.Path = ""
					}
				}
				link = u.String()
			}

			var feeds []readeef.Feed
			feeds, err = con.fm.DiscoverFeeds(link)
			if err != nil {
				break
			}

			var userFeeds []readeef.Feed
			userFeeds, err = db.GetUserFeeds(user)
			if err != nil {
				break
			}

			userFeedIdMap := make(map[int64]bool)
			userFeedLinkMap := make(map[string]bool)
			for _, f := range userFeeds {
				userFeedIdMap[f.Id] = true
				userFeedLinkMap[f.Link] = true

				u, err := url.Parse(f.Link)
				if err == nil && strings.HasPrefix(u.Host, "www.") {
					u.Host = u.Host[4:]
					userFeedLinkMap[u.String()] = true
				}
			}

			respFeeds := []feed{}
			for _, f := range feeds {
				if !userFeedIdMap[f.Id] && !userFeedLinkMap[f.Link] {
					respFeeds = append(respFeeds, feed{
						Id: f.Id, Title: f.Title, Description: f.Description,
						Link: f.Link, Image: f.Image,
					})
				}
			}

			resp["Feeds"] = respFeeds
		case "opml":
			buf := util.BufferPool.GetBuffer()
			defer util.BufferPool.Put(buf)

			buf.ReadFrom(r.Body)

			var opml parser.Opml
			opml, err = parser.ParseOpml(buf.Bytes())
			if err != nil {
				break
			}

			var userFeeds []readeef.Feed
			userFeeds, err = db.GetUserFeeds(user)
			if err != nil {
				break
			}

			userFeedMap := make(map[int64]bool)
			for _, f := range userFeeds {
				userFeedMap[f.Id] = true
			}

			var feeds []readeef.Feed
			for _, opmlFeed := range opml.Feeds {
				var discovered []readeef.Feed

				discovered, err = con.fm.DiscoverFeeds(opmlFeed.Url)
				if err != nil {
					continue
				}

				for _, f := range discovered {
					if !userFeedMap[f.Id] {
						if len(opmlFeed.Tags) > 0 {
							f.Link += "#" + strings.Join(opmlFeed.Tags, ",")
						}

						feeds = append(feeds, f)
					}
				}
			}

			respFeeds := []feed{}
			for _, f := range feeds {
				respFeeds = append(respFeeds, feed{
					Id: f.Id, Title: f.Title, Description: f.Description,
					Link: f.Link, Image: f.Image,
				})
			}
			resp["Feeds"] = respFeeds
		case "add":
			r.ParseForm()
			links := r.Form["url"]
			success := false

			for _, link := range links {
				/* TODO: non-fatal error */
				var u *url.URL
				if u, err = url.Parse(link); err != nil {
					break SWITCH
					/* TODO: non-fatal error */
				} else if !u.IsAbs() {
					err = errors.New("Feed has no link")
					break SWITCH
				}

				var f readeef.Feed
				f, err = con.fm.AddFeedByLink(link)
				if err != nil {
					break SWITCH
				}

				f, err = db.CreateUserFeed(readeef.GetUser(c, r), f)
				if err != nil {
					break SWITCH
				}

				tags := strings.SplitN(u.Fragment, ",", -1)
				if u.Fragment != "" && len(tags) > 0 {
					err = db.CreateUserFeedTag(f, tags...)
				}

				success = true
			}

			resp["Success"] = success
		case "remove":
			var id int64
			id, err = strconv.ParseInt(params["feed-id"], 10, 64)

			/* TODO: non-fatal error */
			if err != nil {
				break
			}

			var feed readeef.Feed
			feed, err = db.GetUserFeed(id, user)
			/* TODO: non-fatal error */
			if err != nil {
				break
			}

			err = db.DeleteUserFeed(feed)
			/* TODO: non-fatal error */
			if err != nil {
				break
			}

			con.fm.RemoveFeed(feed)

			resp["Success"] = true
		case "tags":
			var id int64
			id, err = strconv.ParseInt(params["feed-id"], 10, 64)

			/* TODO: non-fatal error */
			if err != nil {
				break
			}

			var feed readeef.Feed
			feed, err = db.GetUserFeed(id, user)
			/* TODO: non-fatal error */
			if err != nil {
				break
			}

			if r.Method == "GET" {
				resp["Tags"] = feed.Tags
			} else if r.Method == "POST" {
				var tags []string

				tags, err = db.GetUserFeedTags(user, feed)
				if err != nil {
					break
				}

				err = db.DeleteUserFeedTag(feed, tags...)
				if err != nil {
					break
				}

				decoder := json.NewDecoder(r.Body)

				tags = []string{}
				err = decoder.Decode(&tags)
				if err != nil {
					break
				}

				err = db.CreateUserFeedTag(feed, tags...)
				if err != nil {
					break
				}

				resp["Success"] = true
				resp["Id"] = feed.Id
			}
		case "read":
			feedId := params["feed-id"]
			timestamp := params["timestamp"]

			var seconds int64
			seconds, err = strconv.ParseInt(timestamp, 10, 64)
			/* TODO: non-fatal error */
			if err != nil {
				break
			}

			t := time.Unix(seconds/1000, 0)

			switch {
			case feedId == "tag:__all__":
				err = db.MarkUserArticlesByDateAsRead(user, t, true)
			case feedId == "__favorite__":
				// Favorites are assumbed to have been read already
			case strings.HasPrefix(feedId, "tag:"):
				tag := feedId[4:]
				err = db.MarkUserTagArticlesByDateAsRead(user, tag, t, true)
			default:
				var id int64

				id, err = strconv.ParseInt(feedId, 10, 64)
				/* TODO: non-fatal error */
				if err != nil {
					break SWITCH
				}

				var feed readeef.Feed
				feed, err = db.GetUserFeed(id, user)
				/* TODO: non-fatal error */
				if err != nil {
					break SWITCH
				}

				err = db.MarkFeedArticlesByDateAsRead(feed, t, true)
			}

			if err == nil {
				resp["Success"] = true
			}
		case "articles":
			var articles []readeef.Article

			var limit, offset int

			feedId := params["feed-id"]

			limit, err = strconv.Atoi(params["limit"])
			/* TODO: non-fatal error */
			if err != nil {
				break
			}

			offset, err = strconv.Atoi(params["offset"])
			/* TODO: non-fatal error */
			if err != nil {
				break
			}

			newerFirst := params["newer-first"] == "true"
			unreadOnly := params["unread-only"] == "true"

			if limit > 50 {
				limit = 50
			}

			if feedId == "__favorite__" {
				if newerFirst {
					articles, err = db.GetUserFavoriteArticlesDesc(user, limit, offset)
				} else {
					articles, err = db.GetUserFavoriteArticles(user, limit, offset)
				}
				if err != nil {
					break
				}
			} else if feedId == "tag:__all__" {
				if newerFirst {
					if unreadOnly {
						articles, err = db.GetUnreadUserArticlesDesc(user, limit, offset)
					} else {
						articles, err = db.GetUserArticlesDesc(user, limit, offset)
					}
				} else {
					if unreadOnly {
						articles, err = db.GetUnreadUserArticles(user, limit, offset)
					} else {
						articles, err = db.GetUserArticles(user, limit, offset)
					}
				}
				if err != nil {
					break
				}
			} else if strings.HasPrefix(feedId, "tag:") {
				tag := feedId[4:]
				if newerFirst {
					if unreadOnly {
						articles, err = db.GetUnreadUserTagArticlesDesc(user, tag, limit, offset)
					} else {
						articles, err = db.GetUserTagArticlesDesc(user, tag, limit, offset)
					}
				} else {
					if unreadOnly {
						articles, err = db.GetUnreadUserTagArticles(user, tag, limit, offset)
					} else {
						articles, err = db.GetUserTagArticles(user, tag, limit, offset)
					}
				}
				if err != nil {
					break
				}
			} else {
				var f readeef.Feed

				var id int64
				id, err = strconv.ParseInt(feedId, 10, 64)

				if err != nil {
					err = errors.New("Unknown feed id " + feedId)
					break
				}

				f, err = db.GetFeed(id)
				/* TODO: non-fatal error */
				if err != nil {
					break
				}

				f.User = user

				if newerFirst {
					if unreadOnly {
						f, err = db.GetUnreadFeedArticlesDesc(f, limit, offset)
					} else {
						f, err = db.GetFeedArticlesDesc(f, limit, offset)
					}
				} else {
					if unreadOnly {
						f, err = db.GetUnreadFeedArticles(f, limit, offset)
					} else {
						f, err = db.GetFeedArticles(f, limit, offset)
					}
				}
				if err != nil {
					break
				}

				articles = f.Articles
			}

			resp["Articles"] = articles
		}

		var b []byte
		if err == nil {
			b, err = json.Marshal(resp)
		}

		if err == nil {
			w.Write(b)
		} else {
			webfw.GetLogger(c).Print(err)

			w.WriteHeader(http.StatusInternalServerError)
		}

	}
}
Example #21
0
func (con Article) Handler(c context.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var err error

		db := readeef.GetDB(c)
		user := readeef.GetUser(c, r)

		params := webfw.GetParams(c, r)
		action := params["action"]

		readeef.Debug.Printf("Invoking Article controller with action '%s', feed id '%s' and article id '%s'\n", action, params["feed-id"], params["article-id"])

		var feedId int64
		feedId, err = strconv.ParseInt(params["feed-id"], 10, 64)

		var article readeef.Article
		if err == nil {
			article, err = db.GetFeedArticle(feedId, params["article-id"], user)
		}

		if err == nil {
			switch action {
			case "read":
				read := params["value"] == "true"
				previouslyRead := article.Read

				if previouslyRead != read {
					err = db.MarkUserArticlesAsRead(user, []readeef.Article{article}, read)

					if err != nil {
						break
					}
				}

				type response struct {
					Success bool
					Read    bool
				}

				resp := response{Success: previouslyRead != read, Read: read}

				var b []byte
				b, err = json.Marshal(resp)
				if err != nil {
					break
				}

				w.Write(b)
			case "favorite":
				favorite := params["value"] == "true"
				previouslyFavorite := article.Favorite

				if previouslyFavorite != favorite {
					err = db.MarkUserArticlesAsFavorite(user, []readeef.Article{article}, favorite)

					if err != nil {
						break
					}
				}

				type response struct {
					Success  bool
					Favorite bool
				}

				resp := response{Success: previouslyFavorite != favorite, Favorite: favorite}

				var b []byte
				b, err = json.Marshal(resp)
				if err != nil {
					break
				}

				w.Write(b)
			}
		}

		if err != nil {
			webfw.GetLogger(c).Print(err)

			w.WriteHeader(http.StatusInternalServerError)
		}
	}
}
Example #22
0
func (smw Session) Handler(ph http.Handler, c context.Context) http.Handler {
	var abspath string
	var maxAge, cleanupInterval, cleanupMaxAge time.Duration

	if filepath.IsAbs(smw.Path) {
		abspath = smw.Path
	} else {
		var err error
		abspath, err = filepath.Abs(path.Join(filepath.Dir(os.Args[0]), smw.Path))

		if err != nil {
			panic(err)
		}
	}

	if smw.MaxAge != "" {
		var err error
		maxAge, err = time.ParseDuration(smw.MaxAge)

		if err != nil {
			panic(err)
		}
	}

	logger := webfw.GetLogger(c)

	if smw.CleanupInterval != "" {
		var err error
		cleanupInterval, err = time.ParseDuration(smw.CleanupInterval)

		if err != nil {
			panic(err)
		}

		cleanupMaxAge, err = time.ParseDuration(smw.CleanupMaxAge)

		if err != nil {
			panic(err)
		}

		go func() {
			for _ = range time.Tick(cleanupInterval) {
				logger.Print("Cleaning up old sessions")

				if err := context.CleanupSessions(abspath, cleanupMaxAge); err != nil {
					logger.Printf("Failed to clean up sessions: %v", err)
				}
			}
		}()
	}

	handler := func(w http.ResponseWriter, r *http.Request) {
		uriParts := strings.SplitN(r.RequestURI, "?", 2)
		if uriParts[0] == "" {
			uriParts[0] = r.URL.Path
		}

		ignore := false
		for _, prefix := range smw.IgnoreURLPrefix {
			if prefix[0] == '/' {
				prefix = prefix[1:]
			}

			if strings.HasPrefix(uriParts[0], smw.Pattern+prefix+"/") {
				ignore = true
				break
			}

			if uriParts[0] == smw.Pattern+prefix {
				ignore = true
				break
			}
		}

		if ignore {
			ph.ServeHTTP(w, r)
			return
		}

		firstTimer := false
		var sess context.Session

		if smw.SessionGenerator == nil {
			sess = context.NewSession(smw.Secret, smw.Cipher, abspath)
		} else {
			sess = smw.SessionGenerator(smw.Secret, smw.Cipher, abspath)
		}
		sess.SetMaxAge(maxAge)

		err := sess.Read(r, c)

		if err != nil && err != context.ErrExpired && err != context.ErrNotExist {
			sess.SetName(util.UUID())
			firstTimer = true

			if err != context.ErrCookieNotExist {
				logger.Printf("Error reading session: %v", err)
			}
		}

		c.Set(r, context.BaseCtxKey("session"), sess)
		c.Set(r, context.BaseCtxKey("firstTimer"), firstTimer)

		rec := util.NewRecorderHijacker(w)

		ph.ServeHTTP(rec, r)

		for k, v := range rec.Header() {
			w.Header()[k] = v
		}

		if sess != nil {
			if err := sess.Write(w); err != nil {
				logger.Printf("Unable to write session: %v", err)
			}
		}

		w.WriteHeader(rec.GetCode())
		w.Write(rec.GetBody().Bytes())
	}

	return http.HandlerFunc(handler)
}
Example #23
0
func (mw Auth) Handler(ph http.Handler, c context.Context) http.Handler {
	logger := webfw.GetLogger(c)
	handler := func(w http.ResponseWriter, r *http.Request) {
		for _, prefix := range mw.IgnoreURLPrefix {
			if prefix[0] == '/' {
				prefix = prefix[1:]
			}

			if strings.HasPrefix(r.URL.Path, mw.Pattern+prefix+"/") {
				ph.ServeHTTP(w, r)
				return
			}
		}

		route, _, ok := webfw.GetDispatcher(c).RequestRoute(r)
		if !ok {
			ph.ServeHTTP(w, r)
			return
		}

		repo := GetRepo(c)

		switch ac := route.Controller.(type) {
		case AuthController:
			if !ac.LoginRequired(c, r) {
				ph.ServeHTTP(w, r)
				return
			}

			sess := webfw.GetSession(c, r)

			var u content.User
			validUser := false
			if uv, ok := sess.Get(AuthUserKey); ok {
				if u, ok = uv.(content.User); ok {
					validUser = true
				}
			}

			if !validUser {
				if uv, ok := sess.Get(AuthNameKey); ok {
					if n, ok := uv.(data.Login); ok {
						u = repo.UserByLogin(n)

						if u.HasErr() {
							logger.Print(u.Err())
						} else {
							validUser = true
							sess.Set(AuthUserKey, u)
						}
					}
				}
			}

			if validUser && !u.Data().Active {
				logger.Infoln("User " + u.Data().Login + " is inactive")
				validUser = false
			}

			if !validUser {
				d := webfw.GetDispatcher(c)
				sess.SetFlash(CtxKey("return-to"), r.URL.Path)
				path := d.NameToPath("auth-login", webfw.MethodGet)

				if path == "" {
					path = "/"
				}

				http.Redirect(w, r, path, http.StatusMovedPermanently)
				return
			}

		case ApiAuthController:
			if !ac.AuthRequired(c, r) {
				ph.ServeHTTP(w, r)
				return
			}

			url, login, signature, nonce, date, t := authData(r)

			validUser := false

			var u content.User

			if login != "" && signature != "" && !t.IsZero() {
				switch {
				default:
					u = repo.UserByLogin(data.Login(login))
					if u.HasErr() {
						logger.Printf("Error getting db user '%s': %v\n", login, u.Err())
						break
					}

					decoded, err := base64.StdEncoding.DecodeString(signature)
					if err != nil {
						logger.Printf("Error decoding auth header: %v\n", err)
						break
					}

					if t.Add(30 * time.Second).Before(time.Now()) {
						break
					}

					if !mw.Nonce.Check(nonce) {
						break
					}
					mw.Nonce.Remove(nonce)

					buf := util.BufferPool.GetBuffer()
					defer util.BufferPool.Put(buf)

					buf.ReadFrom(r.Body)
					r.Body = ioutil.NopCloser(buf)

					bodyHash := md5.New()
					if _, err := bodyHash.Write(buf.Bytes()); err != nil {
						logger.Printf("Error generating the hash for the request body: %v\n", err)
						break
					}

					contentMD5 := base64.StdEncoding.EncodeToString(bodyHash.Sum(nil))

					message := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s\n",
						url, r.Method, contentMD5, r.Header.Get("Content-Type"),
						date, nonce)

					b := make([]byte, base64.StdEncoding.EncodedLen(len(u.Data().MD5API)))
					base64.StdEncoding.Encode(b, u.Data().MD5API)

					hm := hmac.New(sha256.New, b)
					if _, err := hm.Write([]byte(message)); err != nil {
						logger.Printf("Error generating the hashed message: %v\n", err)
						break
					}

					if !hmac.Equal(hm.Sum(nil), decoded) {
						logger.Printf("Error matching the supplied auth message to the generated one.\n")
						break
					}

					if !u.Data().Active {
						logger.Println("User " + u.Data().Login + " is inactive")
						break
					}

					validUser = true
				}
			}

			if validUser {
				c.Set(r, context.BaseCtxKey("user"), u)
			} else {
				if rej, ok := ac.(AuthRejectHandler); ok {
					rej.AuthReject(c, r)
				} else {
					w.WriteHeader(http.StatusUnauthorized)
					return
				}
			}
		}

		ph.ServeHTTP(w, r)
	}

	return http.HandlerFunc(handler)
}
Example #24
0
func (con WebSocket) Handler(c context.Context) http.Handler {
	var mutex sync.RWMutex

	receivers := make(map[chan content.Feed]bool)
	logger := webfw.GetLogger(c)

	go func() {
		for {
			select {
			case feed := <-con.updateFeed:
				logger.Infoln("New articles notification for " + feed.String())
				mutex.RLock()

				for receiver, _ := range receivers {
					receiver <- feed
				}

				mutex.RUnlock()
			}
		}
	}()

	cfg := readeef.GetConfig(c)
	return websocket.Handler(func(ws *websocket.Conn) {
		user := readeef.GetUser(c, ws.Request())
		sess := webfw.GetSession(c, ws.Request())

		msg := make(chan apiRequest)
		resp := make(chan apiResponse)

		receiver := make(chan content.Feed)

		mutex.Lock()
		receivers[receiver] = true
		mutex.Unlock()
		defer func() {
			mutex.Lock()
			close(receiver)
			delete(receivers, receiver)
			mutex.Unlock()
		}()

		go func() {
			for {
				var r responseError

				select {
				case data := <-msg:
					var err error
					var processor Processor

					if processor, err = data.processor(c, sess, user, con.fm, con.sp, con.extractor,
						con.capabilities, []byte(cfg.Auth.Secret)); err == nil {
						if len(data.Arguments) > 0 {
							err = json.Unmarshal([]byte(data.Arguments), processor)
						}

						if err == nil {
							r = processor.Process()
						}
					}

					if err != nil {
						r.err = err
						switch err.(type) {
						case *json.UnmarshalTypeError:
							r.errType = errTypeInvalidArgValue
						default:
							if err == errInvalidMethodValue {
								r.errType = errTypeInvalidMethodValue
							} else if err == content.ErrNoContent {
								r.err = errResourceNotFound
								r.errType = errTypeResourceNotFound
							}
						}
					}

					go func() {
						var err string
						if r.err != nil {
							err = r.err.Error()
						}
						resp <- apiResponse{
							Success: r.err == nil, Error: err, ErrorType: r.errType,
							Method: data.Method, Tag: data.Tag, Arguments: r.val,
						}
					}()
				case f := <-receiver:
					if f == nil || user == nil {
						// Socket was closed
						return
					}
					logger.Infoln("Received notification for feed update of " + f.String())

					r := newResponse()

					uf := user.FeedById(f.Data().Id)

					if !uf.HasErr() {
						r.val["Feed"] = uf

						go func() {
							var err string
							if r.err != nil {
								err = r.err.Error()
							}
							resp <- apiResponse{
								Success: r.err == nil, Error: err, ErrorType: r.errType,
								Method: "feed-update-notifier", Tag: "", Arguments: r.val,
							}
						}()
					}
				case r := <-resp:
					websocket.JSON.Send(ws, r)
				}
			}
		}()

		for {
			var data apiRequest
			if err := websocket.JSON.Receive(ws, &data); err != nil {
				if err == io.EOF {
					// Websocket was closed
					break
				} else {
					websocket.JSON.Send(ws, apiResponse{
						Success: false, ErrorType: errTypeMessageParse,
						Error: err.Error(), Method: data.Method,
					})
				}
			}

			if forbidden(c, ws.Request()) {
				websocket.JSON.Send(ws, apiResponse{
					Success: false, ErrorType: errTypeUnauthorized,
					Error: errUnauthorized.Error(), Method: data.Method,
				})
				break
			}

			msg <- data
		}

		logger.Infoln("Closing web socket")
	})
}
Example #25
0
func (controller TtRss) Handler(c context.Context) http.Handler {
	repo := readeef.GetRepo(c)
	logger := webfw.GetLogger(c)
	config := readeef.GetConfig(c)

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		action := webfw.GetMultiPatternIdentifier(c, r)

		if action == "redirecter" {
			http.Redirect(w, r, "/", http.StatusMovedPermanently)
		}

		req := ttRssRequest{}

		resp := ttRssResponse{}

		var err error
		var errType string
		var user content.User
		var con interface{}

		switch {
		default:
			var b []byte
			in := map[string]interface{}{}

			if b, err = ioutil.ReadAll(r.Body); err != nil {
				err = fmt.Errorf("reading request body: %s", err)
				break
			}

			if err = json.Unmarshal(b, &in); err != nil {
				err = fmt.Errorf("decoding JSON request: %s", err)
				break
			}

			req = ttRssConvertRequest(in)

			logger.Debugf("Request: %#v\n", req)

			resp.Seq = req.Seq

			if req.Op != "login" && req.Op != "isLoggedIn" {
				if sess, ok := ttRssSessions[req.Sid]; ok {
					user = repo.UserByLogin(data.Login(sess.login))
					if repo.Err() != nil {
						errType = "NOT_LOGGED_IN"
					} else {
						sess.lastVisit = time.Now()
						ttRssSessions[req.Sid] = sess
					}
				} else {
					errType = "NOT_LOGGED_IN"
				}
			}

			if errType != "" {
				logger.Debugf("TT-RSS Sessions: %#v\n", ttRssSessions)
				break
			}

			logger.Debugf("TT-RSS OP: %s\n", req.Op)
			switch req.Op {
			case "getApiLevel":
				con = ttRssGenericContent{Level: TTRSS_API_LEVEL}
			case "getVersion":
				con = ttRssGenericContent{Version: TTRSS_VERSION}
			case "login":
				user = repo.UserByLogin(data.Login(req.User))
				if repo.Err() != nil {
					errType = "LOGIN_ERROR"
					err = fmt.Errorf("getting TT-RSS user: %s", repo.Err())
					break
				}

				if !user.Authenticate(req.Password, []byte(config.Auth.Secret)) {
					errType = "LOGIN_ERROR"
					err = fmt.Errorf("authentication for TT-RSS user '%s'", user.Data().Login)
					break
				}

				var sessId string

				login := user.Data().Login

				for id, sess := range ttRssSessions {
					if sess.login == login {
						sessId = id
					}
				}

				if sessId == "" {
					sessId = strings.Replace(util.UUID(), "-", "", -1)
					ttRssSessions[sessId] = ttRssSession{login: login, lastVisit: time.Now()}
				}

				con = ttRssGenericContent{
					ApiLevel:  TTRSS_API_LEVEL,
					SessionId: sessId,
				}
			case "logout":
				delete(ttRssSessions, req.Sid)
				con = ttRssGenericContent{Status: "OK"}
			case "isLoggedIn":
				if _, ok := ttRssSessions[req.Sid]; ok {
					con = ttRssGenericContent{Status: true}
				} else {
					con = ttRssGenericContent{Status: false}
				}
			case "getUnread":
				var ar content.ArticleRepo
				o := data.ArticleCountOptions{UnreadOnly: true}

				if req.IsCat {
					tagId := data.TagId(req.FeedId)
					if tagId > 0 {
						ar = user.TagById(tagId)
					} else if tagId == TTRSS_CAT_UNCATEGORIZED {
						ar = user
						o.UntaggedOnly = true
					} else if tagId == TTRSS_CAT_SPECIAL {
						ar = user
						o.FavoriteOnly = true
					}
				} else {
					switch req.FeedId {
					case TTRSS_FAVORITE_ID:
						ar = user
						o.FavoriteOnly = true
					case TTRSS_FRESH_ID:
						ar = user
						o.AfterDate = time.Now().Add(TTRSS_FRESH_DURATION)
					case TTRSS_ALL_ID, 0:
						ar = user
					default:
						if req.FeedId > 0 {
							feed := user.FeedById(req.FeedId)
							if feed.HasErr() {
								err = feed.Err()
								break
							}

							ar = feed
						}

					}

				}

				if ar == nil {
					con = ttRssGenericContent{Unread: "0"}
				} else if con == nil {
					con = ttRssGenericContent{Unread: strconv.FormatInt(ar.Count(o), 10)}
				}
			case "getCounters":
				if req.OutputMode == "" {
					req.OutputMode = "flc"
				}
				cContent := ttRssCountersContent{}

				o := data.ArticleCountOptions{UnreadOnly: true}
				unreadCount := user.Count(o)
				cContent = append(cContent,
					ttRssCounter{Id: "global-unread", Counter: unreadCount})

				feeds := user.AllFeeds()
				cContent = append(cContent,
					ttRssCounter{Id: "subscribed-feeds", Counter: int64(len(feeds))})

				cContent = append(cContent, ttRssCounter{Id: TTRSS_ARCHIVED_ID})

				cContent = append(cContent,
					ttRssCounter{Id: TTRSS_FAVORITE_ID,
						Counter:    user.Count(data.ArticleCountOptions{UnreadOnly: true, FavoriteOnly: true}),
						AuxCounter: user.Count(data.ArticleCountOptions{FavoriteOnly: true})})

				cContent = append(cContent, ttRssCounter{Id: TTRSS_PUBLISHED_ID})

				freshTime := time.Now().Add(TTRSS_FRESH_DURATION)
				cContent = append(cContent,
					ttRssCounter{Id: TTRSS_FRESH_ID,
						Counter:    user.Count(data.ArticleCountOptions{UnreadOnly: true, AfterDate: freshTime}),
						AuxCounter: 0})

				cContent = append(cContent,
					ttRssCounter{Id: TTRSS_ALL_ID,
						Counter:    user.Count(),
						AuxCounter: 0})

				for _, f := range feeds {
					cContent = append(cContent,
						ttRssCounter{Id: int64(f.Data().Id), Counter: f.Count(o)},
					)

				}

				cContent = append(cContent, ttRssCounter{Id: TTRSS_CAT_LABELS, Counter: 0, Kind: "cat"})

				for _, t := range user.Tags() {
					cContent = append(cContent,
						ttRssCounter{
							Id:      int64(t.Data().Id),
							Counter: t.Count(o),
							Kind:    "cat",
						},
					)
				}

				cContent = append(cContent,
					ttRssCounter{
						Id:      TTRSS_CAT_UNCATEGORIZED,
						Counter: user.Count(data.ArticleCountOptions{UnreadOnly: true, UntaggedOnly: true}),
						Kind:    "cat",
					},
				)

				if user.HasErr() {
					err = fmt.Errorf("Error getting user counters: %v\n", user.Err())
				}

				con = cContent
			case "getFeeds":
				fContent := ttRssFeedsContent{}

				if req.CatId == TTRSS_CAT_ALL || req.CatId == TTRSS_CAT_SPECIAL {
					unreadFav := user.Count(data.ArticleCountOptions{UnreadOnly: true, FavoriteOnly: true})

					if unreadFav > 0 || !req.UnreadOnly {
						fContent = append(fContent, ttRssFeed{
							Id:     TTRSS_FAVORITE_ID,
							Title:  ttRssSpecialTitle(TTRSS_FAVORITE_ID),
							Unread: unreadFav,
							CatId:  TTRSS_FAVORITE_ID,
						})
					}

					freshTime := time.Now().Add(TTRSS_FRESH_DURATION)
					unreadFresh := user.Count(data.ArticleCountOptions{UnreadOnly: true, AfterDate: freshTime})

					if unreadFresh > 0 || !req.UnreadOnly {
						fContent = append(fContent, ttRssFeed{
							Id:     TTRSS_FRESH_ID,
							Title:  ttRssSpecialTitle(TTRSS_FRESH_ID),
							Unread: unreadFresh,
							CatId:  TTRSS_FAVORITE_ID,
						})
					}

					unreadAll := user.Count(data.ArticleCountOptions{UnreadOnly: true})

					if unreadAll > 0 || !req.UnreadOnly {
						fContent = append(fContent, ttRssFeed{
							Id:     TTRSS_ALL_ID,
							Title:  ttRssSpecialTitle(TTRSS_ALL_ID),
							Unread: unreadAll,
							CatId:  TTRSS_FAVORITE_ID,
						})
					}
				}

				var feeds []content.UserFeed
				var catId int
				if req.CatId == TTRSS_CAT_ALL || req.CatId == TTRSS_CAT_ALL_EXCEPT_VIRTUAL {
					feeds = user.AllFeeds()
				} else {
					if req.CatId == TTRSS_CAT_UNCATEGORIZED {
						tagged := user.AllTaggedFeeds()
						for _, t := range tagged {
							if len(t.Tags()) == 0 {
								feeds = append(feeds, t)
							}
						}
					} else if req.CatId > 0 {
						catId = int(req.CatId)
						t := user.TagById(req.CatId)
						tagged := t.AllFeeds()
						if t.HasErr() {
							err = t.Err()
							break
						}
						for _, t := range tagged {
							feeds = append(feeds, t)
						}
					}
				}

				if len(feeds) > 0 {
					o := data.ArticleCountOptions{UnreadOnly: true}
					for i := range feeds {
						if req.Limit > 0 {
							if i < req.Offset || i >= req.Limit+req.Offset {
								continue
							}
						}

						d := feeds[i].Data()
						unread := feeds[i].Count(o)

						if unread > 0 || !req.UnreadOnly {
							fContent = append(fContent, ttRssFeed{
								Id:          d.Id,
								Title:       d.Title,
								FeedUrl:     d.Link,
								CatId:       catId,
								Unread:      unread,
								LastUpdated: time.Now().Unix(),
								OrderId:     0,
							})
						}
					}
				}

				if user.HasErr() {
					err = fmt.Errorf("Error getting user feeds: %v\n", user.Err())
				}

				con = fContent
			case "getCategories":
				cContent := ttRssCategoriesContent{}
				o := data.ArticleCountOptions{UnreadOnly: true}

				for _, t := range user.Tags() {
					td := t.Data()
					count := t.Count(o)

					if count > 0 || !req.UnreadOnly {
						cContent = append(cContent,
							ttRssCat{Id: strconv.FormatInt(int64(td.Id), 10), Title: string(td.Value), Unread: count},
						)
					}
				}

				count := user.Count(data.ArticleCountOptions{UnreadOnly: true, UntaggedOnly: true})
				if count > 0 || !req.UnreadOnly {
					cContent = append(cContent,
						ttRssCat{Id: strconv.FormatInt(TTRSS_CAT_UNCATEGORIZED, 10), Title: "Uncategorized", Unread: count},
					)
				}

				o.FavoriteOnly = true
				count = user.Count(o)

				if count > 0 || !req.UnreadOnly {
					cContent = append(cContent,
						ttRssCat{Id: strconv.FormatInt(TTRSS_CAT_SPECIAL, 10), Title: "Special", Unread: count},
					)
				}

				con = cContent
			case "getHeadlines":
				if req.FeedId == 0 {
					errType = "INCORRECT_USAGE"
					break
				}

				limit := req.Limit
				if limit == 0 {
					limit = 200
				}

				var articles []content.UserArticle
				var articleRepo content.ArticleRepo
				var feedTitle string
				firstId := data.ArticleId(0)
				o := data.ArticleQueryOptions{Limit: limit, Offset: req.Skip, UnreadFirst: true, SkipSessionProcessors: true}

				if req.IsCat {
					if req.FeedId == TTRSS_CAT_UNCATEGORIZED {
						ttRssSetupSorting(req, user)
						articleRepo = user
						o.UntaggedOnly = true
						feedTitle = "Uncategorized"
					} else if req.FeedId > 0 {
						t := user.TagById(data.TagId(req.FeedId))
						ttRssSetupSorting(req, t)
						articleRepo = t
						feedTitle = string(t.Data().Value)
					}
				} else {
					if req.FeedId == TTRSS_FAVORITE_ID {
						ttRssSetupSorting(req, user)
						o.FavoriteOnly = true
						articleRepo = user
						feedTitle = "Starred articles"
					} else if req.FeedId == TTRSS_FRESH_ID {
						ttRssSetupSorting(req, user)
						o.AfterDate = time.Now().Add(TTRSS_FRESH_DURATION)
						articleRepo = user
						feedTitle = "Fresh articles"
					} else if req.FeedId == TTRSS_ALL_ID {
						ttRssSetupSorting(req, user)
						articleRepo = user
						feedTitle = "All articles"
					} else if req.FeedId > 0 {
						feed := user.FeedById(req.FeedId)

						ttRssSetupSorting(req, feed)
						articleRepo = feed
						feedTitle = feed.Data().Title
					}
				}

				if req.SinceId > 0 {
					o.AfterId = req.SinceId
				}

				if articleRepo != nil {
					if req.Search != "" {
						if controller.sp != nil {
							if as, ok := articleRepo.(content.ArticleSearch); ok {
								articles = as.Query(req.Search, controller.sp, limit, req.Skip)
							}
						}
					} else {
						var skip bool

						switch req.ViewMode {
						case "all_articles":
						case "adaptive":
						case "unread":
							o.UnreadOnly = true
						case "marked":
							o.FavoriteOnly = true
						default:
							skip = true
						}

						if !skip {
							articles = articleRepo.Articles(o)
						}
					}
				}

				if len(articles) > 0 {
					firstId = articles[0].Data().Id
				}

				headlines := ttRssHeadlinesFromArticles(articles, feedTitle, req.ShowContent, req.ShowExcerpt)
				if req.IncludeHeader {
					header := ttRssHeadlinesHeader{Id: req.FeedId, FirstId: firstId, IsCat: req.IsCat}
					hContent := ttRssHeadlinesHeaderContent{}

					hContent = append(hContent, header)
					hContent = append(hContent, headlines)

					con = hContent
				} else {
					con = headlines
				}
			case "updateArticle":
				articles := user.ArticlesById(req.ArticleIds, data.ArticleQueryOptions{SkipSessionProcessors: true})
				updateCount := int64(0)

				switch req.Field {
				case 0, 2:
					for _, a := range articles {
						d := a.Data()
						updated := false

						switch req.Field {
						case 0:
							switch req.Mode {
							case 0:
								if d.Favorite {
									updated = true
									d.Favorite = false
								}
							case 1:
								if !d.Favorite {
									updated = true
									d.Favorite = true
								}
							case 2:
								updated = true
								d.Favorite = !d.Favorite
							}
							if updated {
								a.Favorite(d.Favorite)
							}
						case 2:
							switch req.Mode {
							case 0:
								if !d.Read {
									updated = true
									d.Read = true
								}
							case 1:
								if d.Read {
									updated = true
									d.Read = false
								}
							case 2:
								updated = true
								d.Read = !d.Read
							}
							if updated {
								a.Read(d.Read)
							}
						}

						if updated {
							if a.HasErr() {
								err = a.Err()
								break
							}

							updateCount++
						}
					}

					if err != nil {
						break
					}

					con = ttRssGenericContent{Status: "OK", Updated: updateCount}
				}
			case "getArticle":
				articles := user.ArticlesById(req.ArticleId, data.ArticleQueryOptions{SkipSessionProcessors: true})
				feedTitles := map[data.FeedId]string{}

				for _, a := range articles {
					d := a.Data()
					if _, ok := feedTitles[d.FeedId]; !ok {
						f := repo.FeedById(d.FeedId)
						feedTitles[d.FeedId] = f.Data().Title
					}
				}

				cContent := ttRssArticlesContent{}

				for _, a := range articles {
					d := a.Data()
					title := feedTitles[d.FeedId]
					h := ttRssArticle{
						Id:        strconv.FormatInt(int64(d.Id), 10),
						Unread:    !d.Read,
						Marked:    d.Favorite,
						Updated:   d.Date.Unix(),
						Title:     d.Title,
						Link:      d.Link,
						FeedId:    strconv.FormatInt(int64(d.FeedId), 10),
						FeedTitle: title,
						Content:   d.Description,
					}

					cContent = append(cContent, h)
				}

				con = cContent
			case "getConfig":
				con = ttRssConfigContent{DaemonIsRunning: true, NumFeeds: len(user.AllFeeds())}
			case "updateFeed":
				con = ttRssGenericContent{Status: "OK"}
			case "catchupFeed":
				var ar content.ArticleRepo
				o := data.ArticleUpdateStateOptions{BeforeDate: time.Now()}

				if req.IsCat {
					tagId := data.TagId(req.FeedId)
					ar = user.TagById(tagId)

					if tagId == TTRSS_CAT_UNCATEGORIZED {
						o.UntaggedOnly = true
					}
				} else {
					ar = user.FeedById(req.FeedId)
				}

				if ar != nil {
					ar.ReadState(true, o)

					if e, ok := ar.(content.Error); ok {
						if e.HasErr() {
							err = e.Err()
							break
						}
					}

					con = ttRssGenericContent{Status: "OK"}
				}
			case "getPref":
				switch req.PrefName {
				case "DEFAULT_UPDATE_INTERVAL":
					con = ttRssGenericContent{Value: int(config.FeedManager.Converted.UpdateInterval.Minutes())}
				case "DEFAULT_ARTICLE_LIMIT":
					con = ttRssGenericContent{Value: 200}
				case "HIDE_READ_FEEDS":
					con = ttRssGenericContent{Value: user.Data().ProfileData["unreadOnly"]}
				case "FEEDS_SORT_BY_UNREAD", "ENABLE_FEED_CATS", "SHOW_CONTENT_PREVIEW":
					con = ttRssGenericContent{Value: true}
				case "FRESH_ARTICLE_MAX_AGE":
					con = ttRssGenericContent{Value: (-1 * TTRSS_FRESH_DURATION).Hours()}
				}
			case "getLabels":
				con = []interface{}{}
			case "setArticleLabel":
				con = ttRssGenericContent{Status: "OK", Updated: 0}
			case "shareToPublished":
				errType = "Publishing failed"
			case "subscribeToFeed":
				f := repo.FeedByLink(req.FeedUrl)
				for _, u := range f.Users() {
					if u.Data().Login == user.Data().Login {
						con = ttRssSubscribeContent{Status: struct {
							Code int `json:"code"`
						}{0}}
						break
					}
				}

				if f.HasErr() {
					err = f.Err()
					break
				}

				f, err := controller.fm.AddFeedByLink(req.FeedUrl)
				if err != nil {
					errType = "INCORRECT_USAGE"
					break
				}

				uf := user.AddFeed(f)
				if uf.HasErr() {
					err = uf.Err()
					break
				}

				con = ttRssSubscribeContent{Status: struct {
					Code int `json:"code"`
				}{1}}
			case "unsubscribeFeed":
				f := user.FeedById(req.FeedId)
				f.Detach()
				users := f.Users()

				if f.HasErr() {
					err = f.Err()
					if err == content.ErrNoContent {
						errType = "FEED_NOT_FOUND"
					}
					break
				}

				if len(users) == 0 {
					controller.fm.RemoveFeed(f)
				}

				con = ttRssGenericContent{Status: "OK"}
			case "getFeedTree":
				items := []ttRssCategory{}

				special := ttRssCategory{Id: "CAT:-1", Items: []ttRssCategory{}, Name: "Special", Type: "category", BareId: -1}

				special.Items = append(special.Items, ttRssFeedListCategoryFeed(user, nil, TTRSS_ALL_ID, false))
				special.Items = append(special.Items, ttRssFeedListCategoryFeed(user, nil, TTRSS_FRESH_ID, false))
				special.Items = append(special.Items, ttRssFeedListCategoryFeed(user, nil, TTRSS_FAVORITE_ID, false))
				special.Items = append(special.Items, ttRssFeedListCategoryFeed(user, nil, TTRSS_PUBLISHED_ID, false))
				special.Items = append(special.Items, ttRssFeedListCategoryFeed(user, nil, TTRSS_ARCHIVED_ID, false))
				special.Items = append(special.Items, ttRssFeedListCategoryFeed(user, nil, TTRSS_RECENTLY_READ_ID, false))

				items = append(items, special)

				tf := user.AllTaggedFeeds()

				uncat := ttRssCategory{Id: "CAT:0", Items: []ttRssCategory{}, BareId: 0, Name: "Uncategorized", Type: "category"}
				tagCategories := map[content.Tag]ttRssCategory{}

				for _, f := range tf {
					tags := f.Tags()

					item := ttRssFeedListCategoryFeed(user, f, f.Data().Id, true)
					if len(tags) > 0 {
						for _, t := range tags {
							var c ttRssCategory
							if cached, ok := tagCategories[t]; ok {
								c = cached
							} else {
								c = ttRssCategory{
									Id:     "CAT:" + strconv.FormatInt(int64(t.Data().Id), 10),
									BareId: data.FeedId(t.Data().Id),
									Name:   string(t.Data().Value),
									Type:   "category",
									Items:  []ttRssCategory{},
								}
							}

							c.Items = append(c.Items, item)
							tagCategories[t] = c
						}
					} else {
						uncat.Items = append(uncat.Items, item)
					}
				}

				categories := []ttRssCategory{uncat}
				for _, c := range tagCategories {
					categories = append(categories, c)
				}

				for _, c := range categories {
					if len(c.Items) == 1 {
						c.Param = "(1 feed)"
					} else {
						c.Param = fmt.Sprintf("(%d feed)", len(c.Items))
					}
					items = append(items, c)
				}

				fl := ttRssCategory{Identifier: "id", Label: "name"}
				fl.Items = items

				if user.HasErr() {
					err = user.Err()
				} else {
					con = ttRssFeedTreeContent{Categories: fl}
				}
			default:
				errType = "UNKNOWN_METHOD"
				con = ttRssGenericContent{Method: req.Op}
			}
		}

		if err == nil && errType == "" {
			resp.Status = TTRSS_API_STATUS_OK
		} else {
			logger.Infof("Error processing TT-RSS API request: %s %v\n", errType, err)
			resp.Status = TTRSS_API_STATUS_ERR
			con = ttRssErrorContent{Error: errType}
		}

		var b []byte
		b, err = json.Marshal(con)
		if err == nil {
			resp.Content = json.RawMessage(b)
		}

		b, err = json.Marshal(&resp)

		if err == nil {
			w.Header().Set("Content-Type", "text/json")
			w.Header().Set("Api-Content-Length", strconv.Itoa(len(b)))
			w.Write(b)

			logger.Debugf("Output for %s: %s\n", req.Op, string(b))
		} else {
			logger.Print(fmt.Errorf("TT-RSS error %s: %v", req.Op, err))

			w.WriteHeader(http.StatusInternalServerError)
		}

	})
}
Example #26
0
func (con Feed) Handler(c context.Context) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		action := webfw.GetMultiPatternIdentifier(c, r)
		user := readeef.GetUser(c, r)

		r.ParseForm()

		var resp responseError
		var feedId int64

		params := webfw.GetParams(c, r)

		switch action {
		case "list":
			resp = listFeeds(user)
		case "discover":
			link := r.FormValue("url")
			resp = discoverFeeds(user, con.fm, link)
		case "opml-export":
			resp = exportOpml(user)
		case "opml":
			buf := util.BufferPool.GetBuffer()
			defer util.BufferPool.Put(buf)

			buf.ReadFrom(r.Body)

			resp = parseOpml(user, con.fm, buf.Bytes())
		case "add":
			links := r.Form["url"]
			resp = addFeeds(user, con.fm, links)
		case "remove":
			if feedId, resp.err = strconv.ParseInt(params["feed-id"], 10, 64); resp.err == nil {
				resp = removeFeed(user, con.fm, data.FeedId(feedId))
			}
		case "tags":
			if feedId, resp.err = strconv.ParseInt(params["feed-id"], 10, 64); resp.err == nil {
				if r.Method == "GET" {
					resp = getFeedTags(user, data.FeedId(feedId))
				} else if r.Method == "POST" {
					if b, err := ioutil.ReadAll(r.Body); err == nil {
						tags := []data.TagValue{}
						if err = json.Unmarshal(b, &tags); err != nil {
							resp.err = fmt.Errorf("Error decoding request body: %s", err)
							break
						}

						resp = setFeedTags(user, data.FeedId(feedId), tags)
					} else {
						resp.err = fmt.Errorf("Error reading request body: %s", err)
						break
					}
				}
			}
		case "read":
			var timestamp, beforeId int64

			if bid, ok := params["before-id"]; ok {
				beforeId, resp.err = strconv.ParseInt(bid, 10, 64)
			} else {
				timestamp, resp.err = strconv.ParseInt(params["timestamp"], 10, 64)
			}

			if resp.err == nil {
				resp = readState(user, params["feed-id"], data.ArticleId(beforeId), timestamp)
			}
		case "articles":
			var limit, offset int

			if limit, resp.err = strconv.Atoi(params["limit"]); resp.err == nil {
				if offset, resp.err = strconv.Atoi(params["offset"]); resp.err == nil {
					minId, _ := strconv.ParseInt(params["min-id"], 10, 64)
					maxId, _ := strconv.ParseInt(params["max-id"], 10, 64)

					resp = getFeedArticles(user, con.sp, params["feed-id"],
						data.ArticleId(minId), data.ArticleId(maxId),
						limit, offset, params["older-first"] == "true", params["unread-only"] == "true")
				}
			}
		}

		switch resp.err {
		case readeef.ErrNoAbsolute:
			resp.val["Error"] = true
			resp.val["ErrorType"] = errTypeNoAbsolute
			resp.err = nil
		case readeef.ErrNoFeed:
			resp.val["Error"] = true
			resp.val["ErrorType"] = errTypeNoFeed
			resp.err = nil
		}

		var b []byte
		if resp.err == nil {
			b, resp.err = json.Marshal(resp.val)
		}

		if resp.err == nil {
			w.Write(b)
		} else {
			webfw.GetLogger(c).Print(resp.err)

			w.WriteHeader(http.StatusInternalServerError)
		}

	})
}
Example #27
0
func (con UserSettings) Handler(c context.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var err error

		db := readeef.GetDB(c)
		user := readeef.GetUser(c, r)

		params := webfw.GetParams(c, r)
		attr := params["attribute"]

		resp := map[string]interface{}{"Login": user.Login}
		if r.Method == "GET" {
			switch attr {
			case "FirstName":
				resp[attr] = user.FirstName
			case "LastName":
				resp[attr] = user.LastName
			case "Email":
				resp[attr] = user.Email
			case "ProfileData":
				resp[attr] = user.ProfileData
			default:
				err = errors.New("Error getting user attribute: unknown attribute " + attr)
			}
		} else if r.Method == "POST" {
			buf := util.BufferPool.GetBuffer()
			defer util.BufferPool.Put(buf)

			buf.ReadFrom(r.Body)

			switch attr {
			case "FirstName":
				user.FirstName = buf.String()
			case "LastName":
				user.LastName = buf.String()
			case "Email":
				user.Email = buf.String()
			case "ProfileData":
				err = json.Unmarshal(buf.Bytes(), &user.ProfileData)
			case "password":
				data := struct {
					Current string
					New     string
				}{}
				err = json.Unmarshal(buf.Bytes(), &data)
				if err == nil {
					/* TODO: non-fatal error */
					if user.Authenticate(data.Current) {
						err = user.SetPassword(data.New)
					} else {
						err = errors.New("Error change user password: current password is invalid")
					}
				}
			default:
				err = errors.New("Error getting user attribute: unknown attribute " + attr)
			}

			if err == nil {
				err = db.UpdateUser(user)
			}

			if err == nil {
				resp["Success"] = true
				resp["Attribute"] = attr
			}
		}

		var b []byte
		if err == nil {
			b, err = json.Marshal(resp)
		}
		if err != nil {
			webfw.GetLogger(c).Print(err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.Write(b)
	}
}
Example #28
0
func (con Proxy) Handler(c context.Context) http.Handler {
	logger := webfw.GetLogger(c)
	config := readeef.GetConfig(c)
	client := readeef.NewTimeoutClient(config.Timeout.Converted.Connect, config.Timeout.Converted.ReadWrite)

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		sess := webfw.GetSession(c, r)

		if _, ok := sess.Get(readeef.AuthNameKey); !ok {
			w.WriteHeader(http.StatusForbidden)
			return
		}

		if r.Method == "HEAD" {
			return
		}

		r.ParseForm()
		var err error

		switch {
		default:
			var u *url.URL

			u, err = url.Parse(r.Form.Get("url"))
			if err != nil {
				err = fmt.Errorf("Error parsing url to proxy (%s): %v", r.Form.Get("url"), err)
				break
			}
			if u.Scheme == "" {
				u.Scheme = "http"
			}

			var req *http.Request

			req, err = http.NewRequest("GET", u.String(), nil)
			if err != nil {
				err = fmt.Errorf("Error creating proxy request to %s: %v", u, err)
				break
			}

			var resp *http.Response

			resp, err = client.Do(req)
			if err != nil {
				err = fmt.Errorf("Error getting proxy response from %s: %v", u, err)
				break
			}

			defer resp.Body.Close()

			for k, values := range resp.Header {
				for _, v := range values {
					w.Header().Add(k, v)
				}
			}

			var b []byte

			b, err = ioutil.ReadAll(resp.Body)
			if err != nil {
				err = fmt.Errorf("Error reading proxy response from %s: %v", u, err)
				break
			}

			_, err = w.Write(b)
		}

		if err != nil {
			logger.Infoln(err)
			w.WriteHeader(http.StatusNotAcceptable)
			return
		}

		return
	})
}
Example #29
0
func (con HubbubController) Handler(c context.Context) http.Handler {
	logger := webfw.GetLogger(c)
	repo := readeef.GetRepo(c)

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		params := r.URL.Query()
		pathParams := webfw.GetParams(c, r)
		feedId, err := strconv.ParseInt(pathParams["feed-id"], 10, 64)

		if err != nil {
			logger.Print(err)
			return
		}

		f := repo.FeedById(data.FeedId(feedId))
		s := f.Subscription()

		err = s.Err()

		if err != nil {
			logger.Print(err)
			return
		}

		logger.Infoln("Receiving hubbub event " + params.Get("hub.mode") + " for " + f.String())

		data := s.Data()
		switch params.Get("hub.mode") {
		case "subscribe":
			if lease, err := strconv.Atoi(params.Get("hub.lease_seconds")); err == nil {
				data.LeaseDuration = int64(lease) * int64(time.Second)
			}
			data.VerificationTime = time.Now()

			w.Write([]byte(params.Get("hub.challenge")))
		case "unsubscribe":
			// Nothing to do here, the subscription will be removed along with the feed by the manager
			w.Write([]byte(params.Get("hub.challenge")))
		case "denied":
			w.Write([]byte{})
			logger.Printf("Unable to subscribe to '%s': %s\n", params.Get("hub.topic"), params.Get("hub.reason"))
		default:
			w.Write([]byte{})

			buf := util.BufferPool.GetBuffer()
			defer util.BufferPool.Put(buf)

			if _, err := buf.ReadFrom(r.Body); err != nil {
				logger.Print(err)
				return
			}

			newArticles := false

			if pf, err := parser.ParseFeed(buf.Bytes(), parser.ParseRss2, parser.ParseAtom, parser.ParseRss1); err == nil {
				f.Refresh(pf)
				f.Update()

				if f.HasErr() {
					logger.Print(f.Err())
					return
				}

				newArticles = len(f.NewArticles()) > 0
			} else {
				logger.Print(err)
				return
			}

			if newArticles {
				for _, m := range con.hubbub.FeedMonitors() {
					if err := m.FeedUpdated(f); err != nil {
						logger.Printf("Error invoking monitor '%s' on updated feed '%s': %v\n",
							reflect.TypeOf(m), f, err)
					}
				}
			}

			return
		}

		switch params.Get("hub.mode") {
		case "subscribe":
			data.SubscriptionFailure = false
		case "unsubscribe", "denied":
			data.SubscriptionFailure = true
		}

		s.Data(data)
		s.Update()
		if s.HasErr() {
			logger.Print(fmt.Errorf("Error updating subscription %s: %v\n", s, s.Err()))
			return
		}

		if data.SubscriptionFailure {
			con.removeFeed <- f
		} else {
			con.addFeed <- f
		}
	})
}
Example #30
0
func (con HubbubController) Handler(c context.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		defer r.Body.Close()

		params := r.URL.Query()
		pathParams := webfw.GetParams(c, r)
		feedId, err := strconv.ParseInt(pathParams["feed-id"], 10, 64)

		if err != nil {
			webfw.GetLogger(c).Print(err)
			return
		}

		s, err := con.hubbub.db.GetHubbubSubscription(feedId)

		if err != nil {
			webfw.GetLogger(c).Print(err)
			return
		}

		f, err := con.hubbub.db.GetFeed(s.FeedId)
		if err != nil {
			webfw.GetLogger(c).Print(err)
			return
		}

		Debug.Println("Receiving hubbub event " + params.Get("hub.mode") + " for " + f.Link)

		switch params.Get("hub.mode") {
		case "subscribe":
			if lease, err := strconv.Atoi(params.Get("hub.lease_seconds")); err == nil {
				s.LeaseDuration = int64(lease) * int64(time.Second)
			}
			s.VerificationTime = time.Now()

			w.Write([]byte(params.Get("hub.challenge")))
		case "unsubscribe":
			w.Write([]byte(params.Get("hub.challenge")))
		case "denied":
			w.Write([]byte{})
			webfw.GetLogger(c).Printf("Unable to subscribe to '%s': %s\n", params.Get("hub.topic"), params.Get("hub.reason"))
		default:
			w.Write([]byte{})

			buf := util.BufferPool.GetBuffer()
			defer util.BufferPool.Put(buf)

			if _, err := buf.ReadFrom(r.Body); err != nil {
				webfw.GetLogger(c).Print(err)
				return
			}

			newArticles := false

			if pf, err := parser.ParseFeed(buf.Bytes(), parser.ParseRss2, parser.ParseAtom, parser.ParseRss1); err == nil {
				f = f.UpdateFromParsed(pf)

				_, newArticles, err = con.hubbub.db.UpdateFeed(f)
				if err != nil {
					webfw.GetLogger(c).Print(err)
					return
				}
			} else {
				webfw.GetLogger(c).Print(err)
				return
			}

			if newArticles {
				con.hubbub.updateFeed <- f
			}

			return
		}

		switch params.Get("hub.mode") {
		case "subscribe":
			s.SubscriptionFailure = false
		case "unsubscribe", "denied":
			s.SubscriptionFailure = true
		}

		if err := con.hubbub.db.UpdateHubbubSubscription(s); err != nil {
			webfw.GetLogger(c).Print(err)
			return
		}

		if s.SubscriptionFailure {
			con.hubbub.removeFeed <- f
		} else {
			con.hubbub.addFeed <- f
		}
	}
}