-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo.go
44 lines (37 loc) · 839 Bytes
/
echo.go
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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
// initEcho initializes the default values for the echo route.
func (b *bot) initEcho() {
if b.HTTP.EchoRoute == "" {
b.HTTP.EchoRoute = "/echo"
}
if b.HTTP.EchoMethod == "" {
b.HTTP.EchoMethod = "POST"
}
}
// echoHandler takes the POST-data and sends it back to the IRC channel as a
// regular IRC message.
func (b *bot) echoHandler(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
b.HTTP.logger.Printf("echo: unable to read body, %v", err)
http.Error(w, "Error reading request body", http.StatusInternalServerError)
}
data := string(body)
if data == "" {
return
}
rows := strings.Split(data, "\n")
for _, o := range rows {
if o == "" {
continue
}
b.privmsg(o)
}
fmt.Fprintf(w, data)
}