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
NSNick string
Logger log.Logger
MsgChan chan string
}
type BotConfig struct {
@ -25,6 +26,7 @@ type BotConfig struct {
ChansToJoin []string
NSNick string
NSPasswd string
MsgChan chan string
}
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,
Server: *NewServer(host, port, connectionName, ssl, logger),
Logger: logger,
MsgChan: config.MsgChan,
}
}
@ -48,11 +51,17 @@ func (b *Bot) run() {
b.Logger.Fatal(err)
}
go b.sendLoop()
b.sendRaw(MakeMessage("NICK", b.Nick))
b.sendRaw(MakeMessage("USER", b.Ident, "0", "0", b.Realname))
b.readLoop()
}
func (b *Bot) send(message string) {
b.MsgChan <- message
}
func (b *Bot) sendRaw(message ircmsg.IrcMessage) {
line, err := message.LineBytes()
if err != nil {
@ -75,5 +84,20 @@ func (b *Bot) joinChannel(channelName 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() {
config := getConfig()
fmt.Printf(configPrint, config.Secret, config.BindHost, config.BindPort)
msgChan := make(chan string)
bc := BotConfig{
Nick: config.Nick,
Ident: config.Ident,
Realname: config.Realname,
ChansToJoin: config.ChansToJoin,
MsgChan: msgChan,
}
bot := newBot(bc, "chimera.snoonet.org", "6697", "snoonet", true)
go bot.run()
ListenForWebHook(config)
ListenForWebHook(config, msgChan)
}

View File

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