-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
142 lines (110 loc) · 2.65 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"fmt"
"log"
"github.com/voldyman/ircbot"
"github.com/voldyman/slackbot"
)
func main() {
users := make(map[string]int)
ircNick := "slackTestBot"
ircPass := ""
slackToken := ""
// slack -> irc mapping
bridges := map[string]string{
"#django": "#botTestChan",
"#django2": "#botTestChan2",
}
slackBot := slackbot.New(slackToken)
slackEvents, err := slackBot.Start("https://ele.slack.com")
if err != nil {
fmt.Println("Could not start slack bot", err.Error())
return
}
ircBot := ircbot.New("irc.freenode.net:6667", ircNick, Values(bridges))
ircEvents, err := ircBot.Start()
if err != nil {
fmt.Println("Could not connect to IRC")
return
}
ircBot.SendRawMessage("identify "+ircPass, "NickServ")
for {
select {
case msg := <-ircEvents:
log.Printf("IRC: <%s@%s> %s\n", msg.Sender, msg.Channel, msg.Text)
if target, ok := KeyForValue(bridges, msg.Channel); ok {
slackBot.SendMessage(msg.Sender, target, msg.Text)
incUser(users, msg.Sender, target)
}
case ev := <-slackEvents:
switch ev.(type) {
case *slackbot.MessageEvent:
msg := ev.(*slackbot.MessageEvent)
// we don't handle named channels without '#'
msg.Channel = "#" + msg.Channel
if _, ok := bridges[msg.Channel]; !ok {
continue
}
log.Printf("slack: <%s@%s> %s\n", msg.Sender,
msg.Channel, msg.Text)
if shouldHandle(users, msg.Sender, msg.Channel) {
log.Println("Handling Message")
if target, ok := bridges[msg.Channel]; ok {
ircBot.SendMessage(msg.Sender, msg.Text, target)
}
}
//case error:
// err = ev.(error)
// fmt.Println("Error occured:", err.Error())
}
}
}
}
// Get All the keys of the map
func Keys(bridges map[string]string) []string {
vals := []string{}
for k := range bridges {
vals = append(vals, k)
}
return vals
}
// Get all values of the map
func Values(bridges map[string]string) []string {
vals := []string{}
for _, v := range bridges {
vals = append(vals, v)
}
return vals
}
// Get the key of a map for the given value
func KeyForValue(bridges map[string]string, val string) (string, bool) {
result := ""
for k, v := range bridges {
if v == val {
result = k
}
}
if result == "" {
return "", false
}
return result, true
}
// Semaphores to manages messages
func incUser(users map[string]int, user, channel string) {
key := user + channel
if val, ok := users[key]; ok {
users[key] = val + 1
} else {
users[key] = 1
}
}
func shouldHandle(users map[string]int, user, channel string) bool {
key := user + channel
if val, ok := users[key]; ok {
if val > 0 {
users[key] = val - 1
return false
}
}
return true
}