2020-02-16 15:26:55 +00:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2020-02-16 21:07:41 +00:00
|
|
|
_ "os"
|
2020-02-16 15:26:55 +00:00
|
|
|
"strings"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/lrstanley/girc"
|
2020-02-17 08:41:08 +00:00
|
|
|
|
|
|
|
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
|
2020-02-16 15:26:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// User id format: nickname@server
|
2020-02-16 16:53:31 +00:00
|
|
|
|
2020-02-16 15:26:55 +00:00
|
|
|
// Room id format: #room_name@server
|
|
|
|
|
|
|
|
type IRC struct {
|
|
|
|
handler Handler
|
|
|
|
|
|
|
|
connected bool
|
|
|
|
timeout int
|
|
|
|
|
|
|
|
nick string
|
|
|
|
name string
|
|
|
|
server string
|
|
|
|
conn *girc.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRC) SetHandler(h Handler) {
|
|
|
|
irc.handler = h
|
|
|
|
}
|
|
|
|
|
|
|
|
func(irc *IRC) Protocol() string {
|
|
|
|
return "irc"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRC) Configure(c Configuration) error {
|
2020-02-16 15:41:13 +00:00
|
|
|
if irc.conn != nil {
|
|
|
|
irc.Close()
|
|
|
|
}
|
|
|
|
|
2020-02-16 16:53:31 +00:00
|
|
|
var err error
|
2020-02-16 15:26:55 +00:00
|
|
|
|
2020-02-16 16:53:31 +00:00
|
|
|
irc.nick, err = c.GetString("nick")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-16 15:26:55 +00:00
|
|
|
|
2020-02-16 16:53:31 +00:00
|
|
|
irc.server, err = c.GetString("server")
|
2020-02-16 15:26:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-16 16:53:31 +00:00
|
|
|
|
2020-02-16 18:30:49 +00:00
|
|
|
port, err := c.GetInt("port", 6667)
|
2020-02-16 16:53:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-16 18:30:49 +00:00
|
|
|
ssl, err := c.GetBool("ssl", false)
|
2020-02-16 16:53:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-02-16 15:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client := girc.New(girc.Config{
|
|
|
|
Server: irc.server,
|
|
|
|
Port: port,
|
|
|
|
Nick: irc.nick,
|
|
|
|
User: irc.nick,
|
2020-02-16 21:07:41 +00:00
|
|
|
//Out: os.Stderr,
|
2020-02-16 16:53:31 +00:00
|
|
|
SSL: ssl,
|
2020-02-16 15:26:55 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
client.Handlers.Add(girc.CONNECTED, irc.ircConnected)
|
|
|
|
//client.Handlers.Add(girc.DISCONNECTED, irc.ircDisconnected)
|
|
|
|
//client.Handlers.Add(girc.NICK, irc.ircNick)
|
|
|
|
client.Handlers.Add(girc.PRIVMSG, irc.ircPrivmsg)
|
|
|
|
client.Handlers.Add(girc.JOIN, irc.ircJoin)
|
|
|
|
client.Handlers.Add(girc.PART, irc.ircPart)
|
|
|
|
client.Handlers.Add(girc.RPL_NAMREPLY, irc.ircNamreply)
|
2020-02-17 14:30:01 +00:00
|
|
|
client.Handlers.Add(girc.TOPIC, irc.ircTopic)
|
|
|
|
client.Handlers.Add(girc.RPL_TOPIC, irc.ircRplTopic)
|
2020-02-16 15:26:55 +00:00
|
|
|
|
|
|
|
irc.conn = client
|
|
|
|
go irc.connectLoop(client)
|
|
|
|
|
|
|
|
for i := 0; i < 42; i++ {
|
|
|
|
time.Sleep(time.Duration(1)*time.Second)
|
|
|
|
if irc.conn != client {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if irc.connected {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2020-02-16 16:53:31 +00:00
|
|
|
return fmt.Errorf("Failed to connect after 42s attempting")
|
2020-02-16 15:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRC) User() UserID {
|
|
|
|
return UserID(irc.nick + "@" + irc.server)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRC) checkRoomId(id RoomID) (string, error) {
|
|
|
|
x := strings.Split(string(id), "@")
|
|
|
|
if len(x) != 2 || x[1] != irc.server || x[0][0] != '#' {
|
|
|
|
return "", fmt.Errorf("Invalid room ID: %s", id)
|
|
|
|
}
|
|
|
|
return x[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRC) checkUserId(id UserID) (string, error) {
|
|
|
|
x := strings.Split(string(id), "@")
|
|
|
|
if len(x) != 2 || x[1] != irc.server || x[0][0] == '#' {
|
|
|
|
return "", fmt.Errorf("Invalid user ID: %s", id)
|
|
|
|
}
|
|
|
|
return x[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRC) SetUserInfo(info *UserInfo) error {
|
|
|
|
return fmt.Errorf("Not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRC) SetRoomInfo(roomId RoomID, info *RoomInfo) error {
|
|
|
|
ch, err := irc.checkRoomId(roomId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Name != "" && info.Name != ch {
|
|
|
|
return fmt.Errorf("May not change IRC room name to other than %s", ch)
|
|
|
|
}
|
|
|
|
if info.Picture != nil {
|
|
|
|
return fmt.Errorf("Room picture not supported on IRC")
|
|
|
|
}
|
2020-02-17 14:30:01 +00:00
|
|
|
if info.Topic != "" {
|
|
|
|
irc.conn.Cmd.Topic(ch, info.Topic)
|
|
|
|
}
|
2020-02-16 15:26:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRC) Join(roomId RoomID) error {
|
|
|
|
ch, err := irc.checkRoomId(roomId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
irc.conn.Cmd.Join(ch)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-16 15:41:13 +00:00
|
|
|
func (irc *IRC) Invite(userId UserID, roomId RoomID) error {
|
|
|
|
ch, err := irc.checkRoomId(roomId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
who, err := irc.checkUserId(userId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
irc.conn.Cmd.Invite(ch, who)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-16 15:26:55 +00:00
|
|
|
func (irc *IRC) Leave(roomId RoomID) {
|
|
|
|
ch, err := irc.checkRoomId(roomId)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
irc.conn.Cmd.Part(ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRC) Send(event *Event) error {
|
2020-02-16 22:27:03 +00:00
|
|
|
// Workaround girc bug
|
|
|
|
if event.Text[0] == ':' {
|
|
|
|
event.Text = " " + event.Text
|
|
|
|
}
|
|
|
|
|
2020-02-16 15:26:55 +00:00
|
|
|
dest := ""
|
|
|
|
if event.Room != "" {
|
|
|
|
ch, err := irc.checkRoomId(event.Room)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dest = ch
|
|
|
|
} else if event.Recipient != "" {
|
|
|
|
ui, err := irc.checkUserId(event.Recipient)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dest = ui
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid target")
|
|
|
|
}
|
|
|
|
|
|
|
|
if event.Attachements != nil && len(event.Attachements) > 0 {
|
|
|
|
// TODO find a way to send them using some hosing of some kind
|
|
|
|
return fmt.Errorf("Attachements not supported on IRC")
|
|
|
|
}
|
|
|
|
|
|
|
|
if event.Type == EVENT_MESSAGE {
|
2020-02-16 16:53:31 +00:00
|
|
|
irc.conn.Cmd.Message(dest, event.Text)
|
2020-02-16 15:26:55 +00:00
|
|
|
} else if event.Type == EVENT_ACTION {
|
2020-02-16 16:53:31 +00:00
|
|
|
irc.conn.Cmd.Action(dest, event.Text)
|
2020-02-16 15:26:55 +00:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Invalid event type")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRC) Close() {
|
2020-02-16 16:53:31 +00:00
|
|
|
conn := irc.conn
|
2020-02-16 15:26:55 +00:00
|
|
|
irc.conn = nil
|
2020-02-16 16:53:31 +00:00
|
|
|
conn.Close()
|
2020-02-16 15:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRC) connectLoop(c *girc.Client) {
|
|
|
|
irc.timeout = 10
|
|
|
|
for {
|
|
|
|
if irc.conn != c {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := c.Connect(); err != nil {
|
|
|
|
irc.connected = false
|
2020-02-16 16:53:31 +00:00
|
|
|
fmt.Printf("IRC failed to connect / disconnected: %s\n", err)
|
|
|
|
fmt.Printf("Retrying in %ds\n", irc.timeout)
|
2020-02-16 15:26:55 +00:00
|
|
|
time.Sleep(time.Duration(irc.timeout) * time.Second)
|
|
|
|
irc.timeout *= 2
|
2020-02-16 16:53:31 +00:00
|
|
|
if irc.timeout > 600 {
|
|
|
|
irc.timeout = 600
|
|
|
|
}
|
2020-02-16 15:26:55 +00:00
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRC) ircConnected(c *girc.Client, e girc.Event) {
|
|
|
|
fmt.Printf("ircConnected ^^^^\n")
|
|
|
|
irc.timeout = 10
|
|
|
|
irc.connected = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRC) ircPrivmsg(c *girc.Client, e girc.Event) {
|
|
|
|
ev := &Event{
|
|
|
|
Type: EVENT_MESSAGE,
|
|
|
|
Author: UserID(e.Source.Name + "@" + irc.server),
|
2020-02-16 16:53:31 +00:00
|
|
|
Text: e.Last(),
|
2020-02-16 15:26:55 +00:00
|
|
|
}
|
|
|
|
if e.IsFromChannel() {
|
|
|
|
ev.Room = RoomID(e.Params[0] + "@" + irc.server)
|
|
|
|
}
|
|
|
|
if e.IsAction() {
|
|
|
|
ev.Type = EVENT_ACTION
|
|
|
|
}
|
|
|
|
irc.handler.Event(ev)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRC) ircJoin(c *girc.Client, e girc.Event) {
|
|
|
|
room := RoomID(e.Params[0] + "@" + irc.server)
|
|
|
|
if e.Source.Name == irc.nick {
|
|
|
|
irc.handler.Joined(room)
|
|
|
|
} else {
|
|
|
|
ev := &Event{
|
|
|
|
Type: EVENT_JOIN,
|
|
|
|
Author: UserID(e.Source.Name + "@" + irc.server),
|
|
|
|
Room: room,
|
|
|
|
}
|
|
|
|
irc.handler.Event(ev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRC) ircPart(c *girc.Client, e girc.Event) {
|
|
|
|
room := RoomID(e.Params[0] + "@" + irc.server)
|
|
|
|
if e.Source.Name == irc.nick {
|
|
|
|
irc.handler.Left(room)
|
|
|
|
} else {
|
|
|
|
ev := &Event{
|
|
|
|
Type: EVENT_LEAVE,
|
|
|
|
Author: UserID(e.Source.Name + "@" + irc.server),
|
|
|
|
Room: room,
|
|
|
|
}
|
|
|
|
irc.handler.Event(ev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRC) ircNamreply(c *girc.Client, e girc.Event) {
|
2020-02-16 15:41:13 +00:00
|
|
|
room := RoomID(e.Params[2] + "@" + irc.server)
|
|
|
|
names := strings.Split(e.Last(), " ")
|
|
|
|
for _, name := range names {
|
|
|
|
if name[0] == '@' {
|
|
|
|
name = name[1:]
|
|
|
|
}
|
|
|
|
src := girc.ParseSource(name)
|
|
|
|
if src.Name != irc.nick {
|
|
|
|
irc.handler.Event(&Event{
|
|
|
|
Type: EVENT_JOIN,
|
|
|
|
Author: UserID(src.Name + "@" + irc.server),
|
|
|
|
Room: room,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-02-16 15:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRC) ircTopic(c *girc.Client, e girc.Event) {
|
2020-02-17 14:30:01 +00:00
|
|
|
source := UserID(e.Source.Name + "@" + irc.server)
|
|
|
|
room := RoomID(e.Params[0] + "@" + irc.server)
|
|
|
|
topic := e.Last()
|
|
|
|
irc.handler.RoomInfoUpdated(room, source, &RoomInfo{
|
|
|
|
Topic: topic,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *IRC) ircRplTopic(c *girc.Client, e girc.Event) {
|
2020-02-16 15:41:13 +00:00
|
|
|
room := RoomID(e.Params[1] + "@" + irc.server)
|
|
|
|
topic := e.Last()
|
2020-02-17 14:30:01 +00:00
|
|
|
irc.handler.RoomInfoUpdated(room, "", &RoomInfo{
|
|
|
|
Topic: topic,
|
2020-02-16 15:41:13 +00:00
|
|
|
})
|
2020-02-16 15:26:55 +00:00
|
|
|
}
|