added channel support for IRC messages

This commit is contained in:
A_D 2018-11-17 21:03:51 +02:00
parent 4e219d1f0e
commit 47c2d78b62
No known key found for this signature in database
GPG Key ID: C242F3DD220FA945
3 changed files with 68 additions and 33 deletions

View File

@ -16,6 +16,7 @@ type Bot struct {
NSPasswd string NSPasswd string
NSNick string NSNick string
Logger log.Logger Logger log.Logger
MsgChan chan string
} }
type BotConfig struct { type BotConfig struct {
@ -25,6 +26,7 @@ type BotConfig struct {
ChansToJoin []string ChansToJoin []string
NSNick string NSNick string
NSPasswd string NSPasswd string
MsgChan chan string
} }
func newBot(config BotConfig, host, port, connectionName string, ssl bool) *Bot { func newBot(config BotConfig, host, port, connectionName string, ssl bool) *Bot {
@ -39,6 +41,7 @@ func newBot(config BotConfig, host, port, connectionName string, ssl bool) *Bot
ChansToJoin: config.ChansToJoin, ChansToJoin: config.ChansToJoin,
Server: *NewServer(host, port, connectionName, ssl, logger), Server: *NewServer(host, port, connectionName, ssl, logger),
Logger: logger, Logger: logger,
MsgChan: config.MsgChan,
} }
} }
@ -48,11 +51,17 @@ func (b *Bot) run() {
b.Logger.Fatal(err) b.Logger.Fatal(err)
} }
go b.sendLoop()
b.sendRaw(MakeMessage("NICK", b.Nick)) b.sendRaw(MakeMessage("NICK", b.Nick))
b.sendRaw(MakeMessage("USER", b.Ident, "0", "0", b.Realname)) b.sendRaw(MakeMessage("USER", b.Ident, "0", "0", b.Realname))
b.readLoop() b.readLoop()
} }
func (b *Bot) send(message string) {
b.MsgChan <- message
}
func (b *Bot) sendRaw(message ircmsg.IrcMessage) { func (b *Bot) sendRaw(message ircmsg.IrcMessage) {
line, err := message.LineBytes() line, err := message.LineBytes()
if err != nil { if err != nil {
@ -75,5 +84,20 @@ func (b *Bot) joinChannel(channelName string) {
} }
func (b *Bot) sendPrivMessage(target, message string) { func (b *Bot) sendPrivMessage(target, message string) {
b.sendRaw(MakeMessage("PRIVMSG", message)) b.sendRaw(MakeMessage("PRIVMSG", target, message))
}
func (b *Bot) fanOutMsg(message string) {
for _, c := range b.ChansToJoin {
b.sendPrivMessage(c, message)
}
}
func (b *Bot) sendLoop() {
for {
select {
case msg := <- b.MsgChan:
b.fanOutMsg(msg)
}
}
} }

View File

@ -12,15 +12,17 @@ CONFIG:
func main() { func main() {
config := getConfig() config := getConfig()
fmt.Printf(configPrint, config.Secret, config.BindHost, config.BindPort) fmt.Printf(configPrint, config.Secret, config.BindHost, config.BindPort)
msgChan := make(chan string)
bc := BotConfig{ bc := BotConfig{
Nick: config.Nick, Nick: config.Nick,
Ident: config.Ident, Ident: config.Ident,
Realname: config.Realname, Realname: config.Realname,
ChansToJoin: config.ChansToJoin, ChansToJoin: config.ChansToJoin,
MsgChan: msgChan,
} }
bot := newBot(bc, "chimera.snoonet.org", "6697", "snoonet", true) bot := newBot(bc, "chimera.snoonet.org", "6697", "snoonet", true)
go bot.run() go bot.run()
ListenForWebHook(config) ListenForWebHook(config, msgChan)
} }

View File

@ -10,8 +10,8 @@ import (
const INDENT = " " const INDENT = " "
func ListenForWebHook(config Config) { func ListenForWebHook(config Config, msgChan chan string) {
http.HandleFunc("/", handleWebHook) http.HandleFunc("/", CreateHandler(msgChan))
err := http.ListenAndServe(config.BindHost+":"+config.BindPort, nil) err := http.ListenAndServe(config.BindHost+":"+config.BindPort, nil)
@ -19,8 +19,8 @@ func ListenForWebHook(config Config) {
fmt.Println(err) fmt.Println(err)
} }
} }
func CreateHandler(msgChan chan string) http.HandlerFunc {
func handleWebHook(writer http.ResponseWriter, request *http.Request) { return func (writer http.ResponseWriter, request * http.Request) {
defer request.Body.Close() defer request.Body.Close()
data, err := ioutil.ReadAll(request.Body) data, err := ioutil.ReadAll(request.Body)
if err != nil { if err != nil {
@ -30,40 +30,42 @@ func handleWebHook(writer http.ResponseWriter, request *http.Request) {
switch eventType := request.Header.Get("X-Gitea-Event"); eventType { switch eventType := request.Header.Get("X-Gitea-Event"); eventType {
default: default:
fmt.Printf("Unknown event type: %q\n", eventType) fmt.Printf("Unknown event type: %q\n", eventType)
handleUnknown(data) handleUnknown(data, msgChan)
case "": case "":
// Not a gitea event. // Not a gitea event.
fmt.Printf("Got a non-event HTTP request %+v\n", request) fmt.Printf("Got a non-event HTTP request %+v\n", request)
case "push": case "push":
handlePush(data) handlePush(data, msgChan)
case "issues": case "issues":
handleIssueAction(data) handleIssueAction(data, msgChan)
case "issue_comment": case "issue_comment":
handleIssueComment(data) handleIssueComment(data, msgChan)
case "create": case "create":
handleCreate(data) handleCreate(data, msgChan)
} }
} }
}
func printSep() { func printSep() {
fmt.Println("---") fmt.Println("---")
} }
func handleUnknown(data []byte) { func handleUnknown(data []byte, msgChan chan string) {
printSep() printSep()
fmt.Println(string(data)) fmt.Println(string(data))
} }
func handlePush(data []byte) { func handlePush(data []byte, msgChan chan string) {
printSep() printSep()
pushData := Push{} pushData := Push{}
json.Unmarshal(data, &pushData) json.Unmarshal(data, &pushData)
outIrcMessage := strings.Builder{}
fmt.Printf( fmt.Printf(
"Got a push for repo %q owned by %q. Pushed by %q (%q)\n", "Got a push for repo %q owned by %q. Pushed by %q (%q)\n",
pushData.Repository.FullName, pushData.Repository.FullName,
@ -71,6 +73,12 @@ func handlePush(data []byte) {
pushData.Pusher.Login, pushData.Pusher.Login,
pushData.Pusher.Email, pushData.Pusher.Email,
) )
outIrcMessage.WriteString(fmt.Sprintf(
"[%s] %s pushed %d commits",
pushData.Repository.FullName,
pushData.Pusher.Login,
len(pushData.Commits),
))
for _, commit := range pushData.Commits { for _, commit := range pushData.Commits {
fmt.Println(INDENT + "--") fmt.Println(INDENT + "--")
@ -86,10 +94,11 @@ func handlePush(data []byte) {
msg := strings.Replace(commit.Message, "\n", strings.Repeat(INDENT, 2)+"\n", -1) msg := strings.Replace(commit.Message, "\n", strings.Repeat(INDENT, 2)+"\n", -1)
fmt.Println(strings.Repeat(INDENT, 2) + msg) fmt.Println(strings.Repeat(INDENT, 2) + msg)
} }
msgChan <- outIrcMessage.String()
} }
func handleIssueAction(data []byte) { func handleIssueAction(data []byte, msgChan chan string) {
action := IssueAction{} action := IssueAction{}
json.Unmarshal(data, &action) json.Unmarshal(data, &action)
printSep() printSep()
@ -103,7 +112,7 @@ func handleIssueAction(data []byte) {
) )
} }
func handleIssueComment(data []byte) { func handleIssueComment(data []byte, msgChan chan string) {
action := IssueAction{} action := IssueAction{}
json.Unmarshal(data, &action) json.Unmarshal(data, &action)
printSep() printSep()
@ -122,19 +131,19 @@ func handleIssueComment(data []byte) {
fmt.Printf(strings.Repeat(INDENT, 2) + msg) fmt.Printf(strings.Repeat(INDENT, 2) + msg)
} }
func handleCreate(data []byte) { func handleCreate(data []byte, msgChan chan string) {
created := CreateAction{} created := CreateAction{}
json.Unmarshal(data, &created) json.Unmarshal(data, &created)
switch created.RefType { switch created.RefType {
case "branch": case "branch":
handleBranchCreate(created) handleBranchCreate(created, msgChan)
default: default:
fmt.Printf("Unknown create reftype %q:\n", created.RefType) fmt.Printf("Unknown create reftype %q:\n", created.RefType)
fmt.Println(string(data)) fmt.Println(string(data))
} }
} }
func handleBranchCreate(branchAction CreateAction) { func handleBranchCreate(branchAction CreateAction, msgChan chan string) {
printSep() printSep()
fmt.Printf( fmt.Printf(
"%q (%q) created branch %q on repo %q (owned by %q)\n", "%q (%q) created branch %q on repo %q (owned by %q)\n",