aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorla-ninpre <aaoth@aaoth.xyz>2023-12-02 13:06:12 +0300
committerla-ninpre <aaoth@aaoth.xyz>2023-12-02 13:06:12 +0300
commit4f479f90c06b24f57a4b28f7f1e8a2550da713cf (patch)
tree044a0ae0f9eddaaaff26c3adc9b8f639dede872c
parentcd0118a677e8f1f3b1f6a4a89311a3fae9d7c349 (diff)
downloadtelebonk-4f479f90c06b24f57a4b28f7f1e8a2550da713cf.tar.gz
telebonk-4f479f90c06b24f57a4b28f7f1e8a2550da713cf.zip
rename go module root
-rw-r--r--go.mod2
-rw-r--r--main.go87
2 files changed, 27 insertions, 62 deletions
diff --git a/go.mod b/go.mod
index 7d014b4..e23a126 100644
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,3 @@
-module git.aaoth.xyz/la-ninpre/telebonk
+module aaoth.xyz/la-ninpre/telebonk
go 1.19
diff --git a/main.go b/main.go
index 4709756..6a078a6 100644
--- a/main.go
+++ b/main.go
@@ -15,48 +15,9 @@ import (
"strconv"
"strings"
"time"
-)
-
-// A Config is holding configuration of telebonk.
-type Config struct {
- TgBotToken string
- TgChatID string
- TgApiURL string
- HonkAuthToken string
- HonkPage string
- HonkURL string
-}
-
-// Check makes sure that no Config fields are set to empty strings.
-func (c *Config) Check() error {
- var what string
- if c.TgBotToken == "" {
- what = "bot_token"
- }
- if c.TgChatID == "" {
- what = "chat_id"
- }
- if c.TgApiURL == "" {
- what = "tgapi_url"
- }
- if c.HonkAuthToken == "" {
- what = "honk_token"
- }
- if c.HonkURL == "" {
- what = "honk_url"
- }
- switch c.HonkPage {
- case "atme", "longago", "home", "myhonks":
- default:
- return fmt.Errorf("bad page type: %s", c.HonkPage)
- }
- if what != "" {
- return fmt.Errorf("'%s' shouldn't be empty", what)
- }
- return nil
-}
-var config = &Config{}
+ "git.aaoth.xyz/la-ninpre/telebonk/config"
+)
// A Honk is a post from honk.
type Honk struct {
@@ -133,7 +94,7 @@ func (h *Honk) Check() error {
// Decide sets the Action of a Honk.
//
-// It sets HonkIgnore to those honks that are: 1) old; 2) already sent and not edits.
+// It sets HonkIgnore to those honks that are: 1) old; 2) already sent and aren't edits.
func (h *Honk) Decide() {
oldhonk, ok := honkMap[h.XID]
if ok {
@@ -198,10 +159,10 @@ type TelegramResponse struct {
}
// NewMess creates and populates a new Mess with default values.
-func NewMess(parseMode string) *Mess {
+func NewMess(parseMode, chatID string) *Mess {
return &Mess{
ParseMode: parseMode,
- ChatID: config.TgChatID,
+ ChatID: chatID,
kind: messHonk,
}
}
@@ -314,8 +275,9 @@ const (
messDonkDoc
)
-func botAPIMethod(method string) string {
- return fmt.Sprintf("%s/bot%s/%s", config.TgApiURL, config.TgBotToken, method)
+// XXX: figure out how to retreive these args without passing config object thousand times
+func botAPIMethod(url, token, method string) string {
+ return fmt.Sprintf("%s/bot%s/%s", url, token, method)
}
func checkTgAPI() error {
@@ -341,18 +303,18 @@ const (
)
// getHonks receives and unmarshals some honks from a Honk instance.
-func getHonks(page string, after int) ([]*Honk, error) {
+func getHonks(from, page, token string, after int) ([]*Honk, error) {
query := url.Values{}
query.Set("action", "gethonks")
query.Set("page", page)
query.Set("after", strconv.Itoa(after))
- apiurl := config.HonkURL + "/api?" + query.Encode()
+ apiurl := from + "/api?" + query.Encode()
req, err := http.NewRequest("GET", apiurl, nil)
if err != nil {
return nil, err
}
- req.Header.Add("Authorization", "Bearer " + config.HonkAuthToken)
+ req.Header.Add("Authorization", "Bearer "+token)
resp, err := client.Do(req)
if err != nil {
@@ -364,7 +326,7 @@ func getHonks(page string, after int) ([]*Honk, error) {
var honkJunk map[string][]*Honk
err = json.NewDecoder(resp.Body).Decode(&honkJunk)
if err != nil {
- // FIXME: honk tokens last for a week or so. when one expires, shouldn't this say something meaningful instead of `unexpected v in blah-blah'?
+ // XXX: honk tokens last for a week or so. when one expires, shouldn't this say something meaningful instead of `unexpected v in blah-blah'?
log.Print("gethonks: ", resp.Status)
return nil, err
}
@@ -432,27 +394,30 @@ func TruncateNoise(s, with string, length int) string {
return b.String()
}
+// XXX: global and mutable
+var conf = &config.Config{}
+
func init() {
- flag.StringVar(&config.TgBotToken, "bot_token", "", "Telegram bot token")
- flag.StringVar(&config.TgChatID, "chat_id", "", "Telegram chat_id")
- flag.StringVar(&config.TgApiURL, "tgapi_url", "https://api.telegram.org", "Telegram API URL")
- flag.StringVar(&config.HonkAuthToken, "honk_token", "", "Honk auth token")
- flag.StringVar(&config.HonkPage, "honk_page", "myhonks", "Page to get honks from. Should be one of [atme, longago, home, myhonks]")
- flag.StringVar(&config.HonkURL, "honk_url", "", "URL of a Honk instance")
+ flag.StringVar(&conf.TgBotToken, "bot_token", "", "Telegram bot token")
+ flag.StringVar(&conf.TgChatID, "chat_id", "", "Telegram chat_id")
+ flag.StringVar(&conf.TgApiURL, "tgapi_url", "https://api.telegram.org", "Telegram API URL")
+ flag.StringVar(&conf.HonkAuthToken, "honk_token", "", "Honk auth token")
+ flag.StringVar(&conf.HonkPage, "honk_page", "myhonks", "Page to get honks from. Should be one of [atme, longago, home, myhonks]")
+ flag.StringVar(&conf.HonkURL, "honk_url", "", "URL of a Honk instance")
flag.Parse()
- if err := config.Check(); err != nil {
- log.Fatal("config:", err) // fail
+ if err := conf.Check(); err != nil {
+ log.Fatal("conf:", err) // fail
}
- config.TgApiURL = strings.TrimRight(config.TgApiURL, "/")
+ conf.TgApiURL = strings.TrimRight(conf.TgApiURL, "/")
if err := checkTgAPI(); err != nil {
log.Fatal("tgAPI:", err) // fail
}
}
var client = http.DefaultClient
-var honkMap = make(map[string]*Honk) // FIXME: not safe for use by multiple goroutines!
+var honkMap = make(map[string]*Honk) // XXX: not safe for use by multiple goroutines!
var now = time.Now()
func main() {
@@ -460,7 +425,7 @@ func main() {
log.Print("starting telebonk") // info
for {
- honks, err := getHonks(config.HonkPage, 0)
+ honks, err := getHonks(conf.HonkURL, conf.HonkPage, conf.HonkAuthToken, 0)
if err != nil {
log.Print("gethonks:", err) // error
retry--