mirror of
https://git.ferricyanide.solutions/A_D/irc-webhooks.git
synced 2024-12-22 18:47:04 +00:00
added channel support for IRC messages
This commit is contained in:
parent
4e219d1f0e
commit
47c2d78b62
26
src/bot.go
26
src/bot.go
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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,8 +19,8 @@ func ListenForWebHook(config Config) {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func handleWebHook(writer http.ResponseWriter, request *http.Request) {
|
||||
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 {
|
||||
@ -30,40 +30,42 @@ func handleWebHook(writer http.ResponseWriter, request *http.Request) {
|
||||
switch eventType := request.Header.Get("X-Gitea-Event"); eventType {
|
||||
default:
|
||||
fmt.Printf("Unknown event type: %q\n", eventType)
|
||||
handleUnknown(data)
|
||||
handleUnknown(data, msgChan)
|
||||
|
||||
case "":
|
||||
// Not a gitea event.
|
||||
fmt.Printf("Got a non-event HTTP request %+v\n", request)
|
||||
|
||||
case "push":
|
||||
handlePush(data)
|
||||
handlePush(data, msgChan)
|
||||
|
||||
case "issues":
|
||||
handleIssueAction(data)
|
||||
handleIssueAction(data, msgChan)
|
||||
|
||||
case "issue_comment":
|
||||
handleIssueComment(data)
|
||||
handleIssueComment(data, msgChan)
|
||||
|
||||
case "create":
|
||||
handleCreate(data)
|
||||
handleCreate(data, msgChan)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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",
|
||||
|
Loading…
Reference in New Issue
Block a user