96 lines
1.9 KiB
Go
96 lines
1.9 KiB
Go
package adapter
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
. "forge.lightcrystal.systems/lightcrystal/underbbs/models"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
type HonkAdapter struct {
|
|
data *chan SocketData
|
|
nickname string
|
|
server string
|
|
username string
|
|
password string
|
|
token string
|
|
}
|
|
|
|
func (self *HonkAdapter) send(data SocketData) {
|
|
if self.data != nil {
|
|
*self.data <- data
|
|
} else {
|
|
fmt.Fprintln(os.Stdout, string(data.ToDatagram()))
|
|
}
|
|
}
|
|
|
|
func (self *HonkAdapter) Name() string {
|
|
return self.nickname
|
|
}
|
|
|
|
func (self *HonkAdapter) Init(settings Settings, data *chan SocketData) error {
|
|
// separate name and server in handle
|
|
parts := strings.Split(*settings.Handle, "@")
|
|
self.username = parts[1]
|
|
self.server = "https://" + parts[2]
|
|
self.password = *settings.Password
|
|
self.nickname = settings.Nickname
|
|
// store all the settings
|
|
// make a request to get the token
|
|
r, err := http.PostForm(self.server+"/dologin", url.Values{
|
|
"username": []string{self.username},
|
|
"password": []string{self.password},
|
|
"gettoken": []string{"1"},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var buf [32]byte
|
|
_, err = r.Body.Read(buf[:])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
self.token = string(buf[:])
|
|
fmt.Println(self.token)
|
|
return nil
|
|
}
|
|
|
|
func (self *HonkAdapter) Subscribe(string) []error {
|
|
return nil
|
|
}
|
|
|
|
func (self *HonkAdapter) Fetch(etype string, ids []string) error {
|
|
return nil
|
|
}
|
|
|
|
func (self *HonkAdapter) Do(action string, data map[string]string) error {
|
|
switch action {
|
|
case "post":
|
|
res, err := http.PostForm(self.server+"/api", url.Values{
|
|
"action": []string{"honk"},
|
|
"token": []string{self.token},
|
|
"noise": []string{data["content"]},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var buf [256]byte
|
|
_, err = res.Body.Read(buf[:])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(string(buf[:]))
|
|
default:
|
|
return errors.New("Do: unknown action")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (self *HonkAdapter) DefaultSubscriptionFilter() string {
|
|
return ""
|
|
}
|