diff options
| -rw-r--r-- | Makefile | 17 | ||||
| -rw-r--r-- | config.go | 33 | ||||
| -rw-r--r-- | env.sh | 6 | ||||
| -rw-r--r-- | handler.go | 40 | ||||
| -rw-r--r-- | main.go | 31 | ||||
| -rw-r--r-- | struct.go | 7 |
6 files changed, 134 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f23f492 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +include env.sh + +APP=web2irc + +all: kill build run + +build: + go build -o $(APP) + +run: + ./$(APP) & + +kill: + pkill $(APP) || exit 0 + +clean: + rm ./$(APP) || exit 0 diff --git a/config.go b/config.go new file mode 100644 index 0000000..04caaf0 --- /dev/null +++ b/config.go @@ -0,0 +1,33 @@ +package main + +import ( + "log" + "os" +) + +func checkConfig() { + check := os.Getenv("WEB2IRC_HTTP_IP") + if check == "" { + log.Fatal("Env WEB2IRC_HTTP_IP not found.") + } + check = os.Getenv("WEB2IRC_HTTP_PORT") + if check == "" { + log.Fatal("Env WEB2IRC_HTTP_PORT not found.") + } + check = os.Getenv("WEB2IRC_IRC_NICK") + if check == "" { + log.Fatal("Env WEB2IRC_IRC_NICK not found.") + } + check = os.Getenv("WEB2IRC_IRC_REAL_NAME") + if check == "" { + log.Fatal("Env WEB2IRC_IRC_REAL_NAME not found.") + } + check = os.Getenv("WEB2IRC_IRC_SERVER") + if check == "" { + log.Fatal("Env WEB2IRC_IRC_SERVER not found.") + } + check = os.Getenv("WEB2IRC_IRC_SERVER_PORT") + if check == "" { + log.Fatal("Env WEB2IRC_IRC_SERVER_PORT not found.") + } +} @@ -0,0 +1,6 @@ +export WEB2IRC_HTTP_IP=127.0.0.1 +export WEB2IRC_HTTP_PORT=8000 +export WEB2IRC_IRC_NICK=bot +export WEB2IRC_IRC_REAL_NAME=bot +export WEB2IRC_IRC_SERVER=irc.iamfabulous.de +export WEB2IRC_IRC_SERVER_PORT=6667 diff --git a/handler.go b/handler.go new file mode 100644 index 0000000..e0d40b8 --- /dev/null +++ b/handler.go @@ -0,0 +1,40 @@ +package main + +import ( + "encoding/json" + "log" + "net/http" +) + +func IndexHandler(w http.ResponseWriter, r *http.Request) { + 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(err) + w.WriteHeader(400) + w.Write([]byte("Wrong JSON.")) + return + } + if hook.Join != "" { + con.Join("#" + hook.Join) + con.Privmsg("#"+hook.Join, hook.Message) + con.Part("#" + hook.Join) + if hook.Target != "" { + con.Privmsg(hook.Target, hook.Message) + } + } else { + con.Privmsg(hook.Target, hook.Message) + } +} @@ -0,0 +1,31 @@ +package main + +import ( + "github.com/gorilla/mux" + "github.com/thoj/go-ircevent" + "log" + "net/http" + "os" +) + +var con = irc.IRC(os.Getenv("WEB2IRC_IRC_NICK"), os.Getenv("WEB2IRC_IRC_REAL_NAME")) + +func main() { + checkConfig() + + err := con.Connect(os.Getenv("WEB2IRC_IRC_SERVER") + ":" + os.Getenv("WEB2IRC_IRC_SERVER_PORT")) + if err != nil { + log.Fatal(err) + } + r := mux.NewRouter() + r.HandleFunc("/", IndexHandler) + r.HandleFunc("/webhook", WebhookHandler).Methods("POST") + r.HandleFunc("/webhook", WebhookFailureHandler) + + http.Handle("/", r) + + ip := os.Getenv("WEB2IRC_HTTP_IP") + port := os.Getenv("WEB2IRC_HTTP_PORT") + + http.ListenAndServe(ip+":"+port, nil) +} diff --git a/struct.go b/struct.go new file mode 100644 index 0000000..6cf92f4 --- /dev/null +++ b/struct.go @@ -0,0 +1,7 @@ +package main + +type Webhook struct { + Target string + Join string + Message string +} |
