<commit_before>package srnd

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"net/http"
	"path/filepath"
	"strconv"
	"strings"
	"sync"
)

type NullCache struct {
	handler *nullHandler
}

type nullHandler struct {
	database       Database
	attachments    bool
	requireCaptcha bool
	name           string
	prefix         string
	translations   string
	i18n           map[string]*I18N
	access         sync.Mutex
}

func (self *nullHandler) ForEachI18N(v func(string)) {
	self.access.Lock()
	for lang := range self.i18n {
		v(lang)
	}
	self.access.Unlock()
}

func (self *nullHandler) GetI18N(r *http.Request) *I18N {
	lang := r.URL.Query().Get("lang")
	if lang == "" {
		lang = I18nProvider.Name
	}
	self.access.Lock()
	i, ok := self.i18n[lang]
	if !ok {
		var err error
		i, err = NewI18n(lang, self.translations)
		if err != nil {
			log.Println(err)
		}
		if i != nil {
			self.i18n[lang] = i
		}
	}
	self.access.Unlock()
	return i
}

func (self *nullHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

	i18n := self.GetI18N(r)

	path := r.URL.Path
	_, file := filepath.Split(path)

	isjson := strings.HasSuffix(path, "/json") || strings.HasSuffix(path, "/json/")

	if strings.HasPrefix(path, "/t/") {
		// thread handler
		parts := strings.Split(path[3:], "/")
		hash := parts[0]
		msg, err := self.database.GetMessageIDByHash(hash)
		if err == nil {

			if !self.database.HasArticleLocal(msg.MessageID()) {
				goto notfound
			}

			template.genThread(self.attachments, self.requireCaptcha, msg, self.prefix, self.name, w, self.database, isjson, i18n)
			return
		} else {
			goto notfound
		}
	}
	if strings.Trim(path, "/") == "overboard" {
		// generate ukko aka overboard
		template.genUkko(self.prefix, self.name, w, self.database, isjson, i18n)
		return
	}

	if strings.HasPrefix(path, "/b/") {
		// board handler
		parts := strings.Split(path[3:], "/")
		page := 0
		group := parts[0]
		if len(parts) > 1 && parts[1] != "" && parts[1] != "json" {
			var err error
			page, err = strconv.Atoi(parts[1])
			if err != nil {
				goto notfound
			}
		}
		hasgroup := self.database.HasNewsgroup(group)
		if !hasgroup {
			goto notfound
		}

		banned, _ := self.database.NewsgroupBanned(group)
		if banned {
			goto notfound
		}

		pages := self.database.GetGroupPageCount(group)
		if page >= int(pages) {
			goto notfound
		}
		template.genBoardPage(self.attachments, self.requireCaptcha, self.prefix, self.name, group, page, w, self.database, isjson, i18n)
		return
	}

	if strings.HasPrefix(path, "/o/") {
		page := 0
		parts := strings.Split(path[3:], "/")
		if parts[0] != "json" && parts[0] != "" {
			var err error
			page, err = strconv.Atoi(parts[0])
			if err != nil {
				goto notfound
			}<commit_msg>fix /b/ug<commit_after>package srnd

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"net/http"
	"path/filepath"
	"strconv"
	"strings"
	"sync"
)

type NullCache struct {
	handler *nullHandler
}

type nullHandler struct {
	database       Database
	attachments    bool
	requireCaptcha bool
	name           string
	prefix         string
	translations   string
	i18n           map[string]*I18N
	access         sync.Mutex
}

func (self *nullHandler) ForEachI18N(v func(string)) {
	self.access.Lock()
	for lang := range self.i18n {
		v(lang)
	}
	self.access.Unlock()
}

func (self *nullHandler) GetI18N(r *http.Request) *I18N {
	lang := r.URL.Query().Get("lang")
	if lang == "" {
		lang = I18nProvider.Name
	}
	self.access.Lock()
	i, ok := self.i18n[lang]
	if !ok {
		var err error
		i, err = NewI18n(lang, self.translations)
		if err != nil {
			log.Println(err)
		}
		if i != nil {
			self.i18n[lang] = i
		}
	}
	self.access.Unlock()
	return i
}

func (self *nullHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

	i18n := self.GetI18N(r)

	path := r.URL.Path
	_, file := filepath.Split(path)

	isjson := strings.HasSuffix(path, "/json") || strings.HasSuffix(path, "/json/")

	if strings.HasPrefix(path, "/t/") {
		// thread handler
		parts := strings.Split(path[3:], "/")
		hash := parts[0]
		msg, err := self.database.GetMessageIDByHash(hash)
		if err == nil {

			if !self.database.HasArticleLocal(msg.MessageID()) {
				goto notfound
			}

			template.genThread(self.attachments, self.requireCaptcha, msg, self.prefix, self.name, w, self.database, isjson, i18n)
			return
		} else {
			goto notfound
		}
	}
	if strings.Trim(path, "/") == "overboard" {
		// generate ukko aka overboard
		template.genUkko(self.prefix, self.name, w, self.database, isjson, i18n)
		return
	}

	if strings.HasPrefix(path, "/b/") {
		// board handler
		parts := strings.Split(path[3:], "/")
		page := 0
		group := parts[0]
		if len(parts) > 1 && parts[1] != "" && parts[1] != "json" {
			var err error
			page, err = strconv.Atoi(parts[1])
			if err != nil {
				goto notfound
			}
		}
		if group == "" {
			goto notfound
		}
		hasgroup := self.database.HasNewsgroup(group)
		if !hasgroup {
			goto notfound
		}

		banned, _ := self.database.NewsgroupBanned(group)
		if banned {
			goto notfound
		}

		pages := self.database.GetGroupPageCount(group)
		if page >= int(pages) {
			goto notfound
		}
		template.genBoardPage(self.attachments, self.requireCaptcha, self.prefix, self.name, group, page, w, self.database, isjson, i18n)
		return
	}

	if strings.HasPrefix(path, "/o/") {
		page := 0
		parts := strings.Split(path[3:], "/")
		if parts[0] != "json" && parts[0] != "" {
			var err error
			page, err = strconv.Atoi(parts[0])
			if err != nil {
				goto notfound
			}
{'fraction_non_alphanumeric': 0.15463165572574764, 'fraction_numerical': 0.016046681254558718, 'mean_word_length': 3.108614232209738, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2393530', 'n_tokens_mistral': 2314, 'n_tokens_neox': 2076, 'n_words': 461}