From 5ffb054dfaf47963c9800debdb6d9a83fa0030c9 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Tue, 9 Feb 2016 10:01:05 +0000 Subject: [PATCH] Basic mod client --- Makefile | 3 ++- broker/webhook.go | 23 +++++++---------------- mods/main.go | 21 +++++++++++++++++++++ mods/metafora.go | 35 +++++++++++++++++++++++++++++++++++ tg/broker.go | 42 ++++++++++++++++++++++++++++++++++++++++++ tg/client.go | 36 ++++++++++++++++++++++++++++++++++++ tg/command.go | 10 +++++----- 7 files changed, 148 insertions(+), 22 deletions(-) create mode 100644 mods/main.go create mode 100644 mods/metafora.go create mode 100644 tg/broker.go create mode 100644 tg/client.go diff --git a/Makefile b/Makefile index c67efd9..2d7232f 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ all: clessy-broker clessy-broker: go build -o clessy-broker ./broker + go build -o clessy-mods ./mods clean: - rm -f clessy-broker + rm -f clessy-broker clessy-mods diff --git a/broker/webhook.go b/broker/webhook.go index 54bafcb..ac7651e 100644 --- a/broker/webhook.go +++ b/broker/webhook.go @@ -1,26 +1,17 @@ package main import ( - "io" - "log" + "io/ioutil" "net/http" - "os" ) func webhook(rw http.ResponseWriter, req *http.Request) { - log.Println("Received request! Details follow:") + // Read entire request and broadcast to everyone + data, err := ioutil.ReadAll(req.Body) + if err != nil { + return + } defer req.Body.Close() - /* - var update tg.APIUpdate - err := json.NewDecoder(req.Body).Decode(&update) - if err != nil { - log.Println("ERR: Not JSON!") - return - } - - jenc, _ := json.Marshal(update) - log.Println(jenc) - */ - io.Copy(os.Stdout, req.Body) + broadcast(string(data)) } diff --git a/mods/main.go b/mods/main.go new file mode 100644 index 0000000..63b156f --- /dev/null +++ b/mods/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "flag" + + "../tg" +) + +func dispatch(broker *tg.Broker, update tg.APIMessage) { + metafora(broker, update) +} + +func main() { + brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port") + flag.Parse() + + err := tg.CreateBrokerClient(*brokerAddr, dispatch) + if err != nil { + panic(err) + } +} diff --git a/mods/metafora.go b/mods/metafora.go new file mode 100644 index 0000000..5717bba --- /dev/null +++ b/mods/metafora.go @@ -0,0 +1,35 @@ +package main + +import ( + "math/rand" + + "../tg" +) + +var actions []string = []string{ + "Puppami", "Degustami", "Lucidami", "Manipolami", "Disidratami", "Irritami", "Martorizzami", + "Lustrami", "Osannami", "Sorseggiami", "Assaporami", "Apostrofami", "Spremimi", "Dimenami", + "Agitami", "Stimolami", "Suonami", "Strimpellami", "Stuzzicami", "Spintonami", "Sguinzagliami", + "Modellami", "Sgrullami", "Cavalcami", "Perquotimi", "Misurami", "Sventolami", "Induriscimi", + "Accordami", "Debuggami", +} + +var objects []string = []string{ + "il birillo", "il bastone", "l'ombrello", "il malloppo", "il manico", "il manganello", + "il ferro", "la mazza", "l'archibugio", "il timone", "l'arpione", "il flauto", "la reliquia", + "il fiorino", "lo scettro", "il campanile", "la proboscide", "il pino", "il maritozzo", "il perno", + "il tubo da 100", "la verga", "l'idrante", "il pendolo", "la torre di Pisa", "la lancia", + "il cilindro", "il lampione", "il joystick", "il Wiimote", "il PSMove", "l'albero maestro", + "il trenino", +} + +func metafora(broker *tg.Broker, update tg.APIMessage) { + if update.Text != nil { + if *(update.Text) == "/metafora" { + n := rand.Intn(len(actions)) + m := rand.Intn(len(objects)) + broker.SendTextMessage(update.Chat, actions[n]+" "+objects[m]) + return + } + } +} diff --git a/tg/broker.go b/tg/broker.go new file mode 100644 index 0000000..6c4c7d5 --- /dev/null +++ b/tg/broker.go @@ -0,0 +1,42 @@ +package tg + +import ( + "encoding/json" + "fmt" + "log" + "net" +) + +type Broker struct { + Socket net.Conn +} + +func ConnectToBroker(brokerAddr string) (*Broker, error) { + sock, err := net.Dial("tcp", brokerAddr) + if err != nil { + return nil, err + } + + broker := new(Broker) + broker.Socket = sock + return broker, nil +} + +func (b *Broker) Close() { + b.Socket.Close() +} + +func (b *Broker) SendTextMessage(chat *APIChat, text string) { + cmd := ClientCommand{ + Type: CmdSendTextMessage, + TextMessageData: &ClientTextMessageData{ + Text: text, + }, + } + // Encode command and send to broker + err := json.NewEncoder(b.Socket).Encode(&cmd) + if err != nil { + log.Printf("[SendTextMessage] JSON Encode error: %s\n", err.Error()) + } + fmt.Fprintf(b.Socket, "\n") +} diff --git a/tg/client.go b/tg/client.go new file mode 100644 index 0000000..3dc12b8 --- /dev/null +++ b/tg/client.go @@ -0,0 +1,36 @@ +package tg + +import ( + "bufio" + "encoding/json" + "io" + "log" +) + +type UpdateHandler func(broker *Broker, message APIMessage) + +func CreateBrokerClient(brokerAddr string, updateFn UpdateHandler) error { + broker, err := ConnectToBroker(brokerAddr) + if err != nil { + return err + } + defer broker.Close() + + in := bufio.NewReader(broker.Socket) + for { + bytes, _, err := in.ReadLine() + if err != nil { + break + } + + var update APIUpdate + err = json.Unmarshal(bytes, &update) + if err != nil { + log.Printf("[tg - CreateBrokerClient] ERROR reading JSON: %s\r\n", err.Error()) + } + + // Dispatch to UpdateHandler + updateFn(broker, update.Message) + } + return io.EOF +} diff --git a/tg/command.go b/tg/command.go index 5b7d02c..8ea7993 100644 --- a/tg/command.go +++ b/tg/command.go @@ -3,14 +3,14 @@ package tg type ClientCommandType uint const ( - CmdSendMessage ClientCommandType = 1 + CmdSendTextMessage ClientCommandType = 1 ) -type ClientCommandMessageData struct { - MessageText string +type ClientTextMessageData struct { + Text string } type ClientCommand struct { - Type ClientCommandType - MessageData *ClientCommandMessageData + Type ClientCommandType + TextMessageData *ClientTextMessageData }