cleanup
This commit is contained in:
parent
b07eab5135
commit
53eb25ae33
101
irc.go
101
irc.go
@ -1,101 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/irc.v3"
|
||||
"suah.dev/mcchunkie/plugins"
|
||||
)
|
||||
|
||||
func ircConnect(store *FStore, plugins *plugins.Plugins) error {
|
||||
var ircServer, _ = store.Get("irc_server")
|
||||
var ircPort, _ = store.Get("irc_port")
|
||||
var ircNick, _ = store.Get("irc_nick")
|
||||
var ircPass, _ = store.Get("irc_pass")
|
||||
var ircRooms, _ = store.Get("irc_rooms")
|
||||
//var toRE = regexp.MustCompile(`^:(\w+)\s`)
|
||||
|
||||
if ircServer != "" {
|
||||
|
||||
log.Printf("IRC: connecting to %q\n", ircServer)
|
||||
|
||||
dialStr := fmt.Sprintf("%s:%s", ircServer, ircPort)
|
||||
conn, err := tls.Dial("tcp", dialStr, &tls.Config{
|
||||
ServerName: ircServer,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
config := irc.ClientConfig{
|
||||
Nick: ircNick,
|
||||
Pass: ircPass,
|
||||
User: ircNick,
|
||||
Name: "McChunkie",
|
||||
Handler: irc.HandlerFunc(func(c *irc.Client, m *irc.Message) {
|
||||
switch m.Command {
|
||||
case "001":
|
||||
for _, r := range strings.Split(ircRooms, ",") {
|
||||
log.Printf("IRC: joining %q\n", r)
|
||||
c.Write(fmt.Sprintf("JOIN %s", r))
|
||||
}
|
||||
case "PING":
|
||||
server := m.Trailing()
|
||||
log.Printf("IRC: pong %q\n", server)
|
||||
c.Write(fmt.Sprintf("PONG %s", server))
|
||||
case "INVITE":
|
||||
room := m.Trailing()
|
||||
log.Printf("IRC: joining %q\n", room)
|
||||
c.Write(fmt.Sprintf("JOIN %s", room))
|
||||
case "PRIVMSG":
|
||||
msg := m.Trailing()
|
||||
from := m.Prefix.Name
|
||||
to := m.Params[0]
|
||||
|
||||
if from == c.CurrentNick() {
|
||||
// Ignore messages from ourselves
|
||||
return
|
||||
}
|
||||
|
||||
resp := ""
|
||||
for _, p := range *plugins {
|
||||
if p.Match(c.CurrentNick(), msg) {
|
||||
p.SetStore(store)
|
||||
|
||||
resp = p.Process(from, msg)
|
||||
}
|
||||
}
|
||||
|
||||
if !c.FromChannel(m) {
|
||||
// in a private chat
|
||||
to = from
|
||||
|
||||
}
|
||||
|
||||
if resp != "" {
|
||||
log.Printf("IRC: sending: %q to %q\n", resp, to)
|
||||
c.WriteMessage(&irc.Message{
|
||||
Command: "PRIVMSG",
|
||||
Params: []string{
|
||||
to,
|
||||
resp,
|
||||
},
|
||||
})
|
||||
}
|
||||
default:
|
||||
log.Printf("IRC: unhandled - %q", m.String())
|
||||
}
|
||||
}),
|
||||
}
|
||||
|
||||
client := irc.NewClient(conn, config)
|
||||
err = client.Run()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
93
sms.go
93
sms.go
@ -1,93 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"suah.dev/mcchunkie/plugins"
|
||||
)
|
||||
|
||||
func smsCanSend(number string, numbers []string) bool {
|
||||
for _, s := range numbers {
|
||||
if number == s {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func smsListen(store *FStore, plugins *plugins.Plugins) {
|
||||
var smsPort, _ = store.Get("sms_listen")
|
||||
var smsAllowed, _ = store.Get("sms_users")
|
||||
var smsUsers = strings.Split(smsAllowed, ",")
|
||||
|
||||
if smsPort != "" {
|
||||
var htpass, _ = store.Get("sms_htpass")
|
||||
|
||||
log.Printf("SMS: listening on %q\n", smsPort)
|
||||
|
||||
http.HandleFunc("/_sms", func(w http.ResponseWriter, r *http.Request) {
|
||||
var msg, from string
|
||||
user, pass, ok := r.BasicAuth()
|
||||
err := bcrypt.CompareHashAndPassword([]byte(htpass), []byte(pass))
|
||||
if !(ok && err == nil && user == "sms") {
|
||||
log.Printf("SMS: failed auth %q %q\n", user, pass)
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="sms notify"`)
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
err = r.ParseForm()
|
||||
if err != nil {
|
||||
http.Error(w, "invalid request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println(r.Method)
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodPost:
|
||||
msg = r.Form.Get("Body")
|
||||
from = r.Form.Get("From")
|
||||
default:
|
||||
http.Error(
|
||||
w,
|
||||
fmt.Sprintf("method %q not implemented", r.Method),
|
||||
http.StatusMethodNotAllowed,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if smsCanSend(from, smsUsers) {
|
||||
msg = strings.TrimSuffix(msg, "\n")
|
||||
|
||||
if msg == "" {
|
||||
fmt.Fprintf(w, "empty message")
|
||||
return
|
||||
}
|
||||
|
||||
for _, p := range *plugins {
|
||||
if p.Match(from, msg) {
|
||||
log.Printf("%s: responding to '%s'", p.Name(), from)
|
||||
p.SetStore(store)
|
||||
|
||||
resp := p.Process(from, msg)
|
||||
fmt.Fprint(w, resp)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.Printf("number not allowed (%q)", from)
|
||||
http.Error(
|
||||
w,
|
||||
fmt.Sprintf("number not allowed (%q)", from),
|
||||
http.StatusMethodNotAllowed,
|
||||
)
|
||||
return
|
||||
}
|
||||
})
|
||||
log.Fatal(http.ListenAndServe(smsPort, nil))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user