~gjabell/irc-ntfy

314ce122099008c90fed63cdb9bf216ef331cdcb — Galen Abell 2 months ago a2d0aec main
Allow disabling timeout and READ publishing
1 files changed, 39 insertions(+), 30 deletions(-)

M main.go
M main.go => main.go +39 -30
@@ 130,20 130,21 @@ func normalizeChannel(channel string) string {
}

type IRCClient struct {
	conn          *irc.Conn
	nick          string
	upstream      string
	channels      map[string]bool
	users         map[string]bool
	readReceipts  *ReadReceiptStore
	notifier      Notifier
	notifyTimeout time.Duration
	quitChan      chan bool
	conn                *irc.Conn
	nick                string
	upstream            string
	channels            map[string]bool
	users               map[string]bool
	readReceipts        *ReadReceiptStore
	notifier            Notifier
	notifyTimeout       time.Duration
	markReadAfterNotify bool
	quitChan            chan bool
}

func NewIRCClient(nick, username, password, server, upstream string,
	channels, users map[string]bool, notifier Notifier,
	notifyTimeout time.Duration) (*IRCClient, error) {
	notifyTimeout time.Duration, markReadAfterNotify bool) (*IRCClient, error) {
	if nick == "" {
		return nil, errors.New("nick is required")
	}


@@ 182,15 183,16 @@ func NewIRCClient(nick, username, password, server, upstream string,
	cfg.EnableCapabilityNegotiation = true

	return &IRCClient{
		conn:          irc.Client(cfg),
		nick:          nick,
		upstream:      upstream,
		channels:      normalizedChannels,
		users:         users,
		readReceipts:  NewReadReceiptStore(),
		notifier:      notifier,
		notifyTimeout: notifyTimeout,
		quitChan:      make(chan bool),
		conn:                irc.Client(cfg),
		nick:                nick,
		upstream:            upstream,
		channels:            normalizedChannels,
		users:               users,
		readReceipts:        NewReadReceiptStore(),
		notifier:            notifier,
		notifyTimeout:       notifyTimeout,
		markReadAfterNotify: markReadAfterNotify,
		quitChan:            make(chan bool),
	}, nil
}



@@ 229,13 231,15 @@ func (c *IRCClient) onPrivMsg(conn *irc.Conn, line *irc.Line) {
	}

	go func() {
		// Wait for a little while to see if another client reads the message.
		time.Sleep(c.notifyTimeout)

		lineTime := line.Time.UTC()
		lastReadTimestamp := c.readReceipts.Get(channel).Add(c.notifyTimeout)
		if lastReadTimestamp.After(lineTime) {
			return
		if c.notifyTimeout > 0 {
			// Wait for a little while to see if another client reads the message.
			time.Sleep(c.notifyTimeout)

			lineTime := line.Time.UTC()
			lastReadTimestamp := c.readReceipts.Get(channel).Add(c.notifyTimeout)
			if lastReadTimestamp.After(lineTime) {
				return
			}
		}

		uri := fmt.Sprintf("irc://%s/%s", c.upstream, channel)


@@ 248,8 252,10 @@ func (c *IRCClient) onPrivMsg(conn *irc.Conn, line *irc.Line) {
			log.Printf("Notify failed: %s\n", err)
		}

		// Mark the message as read so it doesn't ping other clients.
		conn.MarkRead(channel, line.Time)
		if c.markReadAfterNotify {
			// Mark the message as read so it doesn't ping other clients.
			conn.MarkRead(channel, line.Time)
		}
	}()
}



@@ 306,7 312,9 @@ func main() {
		"IRC public server url (optional, use if connecting to a bouncer)")

	timeout := flag.Duration("timeout", time.Second*2,
		"Time to wait before sending a notification")
		"Time to wait before sending a notification (0 to disable and always send)")
	noMarkRead := flag.Bool("no-mark-read", false,
		"Don't send a READ response for the message after sending a notification")
	ircChannels := csvMap{}
	flag.Var(&ircChannels, "irc-channels",
		"Comma-separated list of channels to monitor")


@@ 333,7 341,8 @@ func main() {
	}

	ircClient, err := NewIRCClient(*ircNick, *ircUsername, *ircPassword,
		*ircServer, *ircUpstream, ircChannels, ircUsers, ntfyClient, *timeout)
		*ircServer, *ircUpstream, ircChannels, ircUsers, ntfyClient, *timeout,
		!*noMarkRead)
	if err != nil {
		fmt.Printf("Invalid irc config: %s\n", err)
		os.Exit(1)