summaryrefslogtreecommitdiff
path: root/handler.go
blob: 4124b081e7e740a764e505f966f023b407c61613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main

import (
	"encoding/json"
	"log"
	"net/http"
)

func IndexHandler(w http.ResponseWriter, r *http.Request) {
	log.Println("Wrong URL.")
	w.WriteHeader(400)
	w.Write([]byte("Wrong URL."))
}

func WebhookFailureHandler(w http.ResponseWriter, r *http.Request) {
	log.Println("Getting not-Post request on /webhook")
	w.WriteHeader(400)
	w.Write([]byte("Accepts only POST requests."))
}

func WebhookHandler(w http.ResponseWriter, r *http.Request) {
	decoder := json.NewDecoder(r.Body)
	var hook Webhook
	err := decoder.Decode(&hook)
	if err != nil {
		log.Print("Wrong JSON.", err)
		w.WriteHeader(400)
		w.Write([]byte("Wrong JSON."))
		return
	}
	if hook.Join != "" {
		log.Println("Joining #" + hook.Join)
		con.Join("#" + hook.Join)

		log.Println("Sending private message to " + hook.Join + ": " + hook.Message)
		con.Privmsg("#"+hook.Join, hook.Message)

		log.Println("Leaving #" + hook.Join)
		con.Part("#" + hook.Join)

		if hook.Target != "" {
			log.Println("Sending private message to " + hook.Target + ": " + hook.Message)
			con.Privmsg(hook.Target, hook.Message)
		}
	} else {
		log.Println("Sending private message to " + hook.Target + ": " + hook.Message)
		con.Privmsg(hook.Target, hook.Message)
	}
}