From 53ba996ec9c5c8fc64f66934d8c98509bd7ed06d Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Tue, 2 Apr 2024 16:44:01 +0200 Subject: Add a simple IRC notifier utility --- LICENSE | 2 +- NEWS | 2 + README.adoc | 4 + xN/.gitignore | 3 + xN/Makefile | 19 ++++ xN/go.mod | 3 + xN/xN.adoc | 86 ++++++++++++++++++ xN/xN.go | 279 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ xN/xN_test.go | 98 +++++++++++++++++++++ xS/irc.go | 132 +++++++++++++++++++++++++++ xS/xS.go | 126 -------------------------- 11 files changed, 627 insertions(+), 127 deletions(-) create mode 100644 xN/.gitignore create mode 100644 xN/Makefile create mode 100644 xN/go.mod create mode 100644 xN/xN.adoc create mode 100644 xN/xN.go create mode 100644 xN/xN_test.go create mode 100644 xS/irc.go diff --git a/LICENSE b/LICENSE index 0610bbd..d58be36 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2014 - 2023, Přemysl Eric Janouch
+Copyright (c) 2014 - 2024, Přemysl Eric Janouch
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
diff --git a/NEWS b/NEWS
index 70f3849..7ee97ec 100644
--- a/NEWS
+++ b/NEWS
@@ -37,6 +37,8 @@
* Added a Go port of xD called xS
+ * Added a simple notifier called xN
+
1.5.0 (2021-12-21) "The Show Must Go On"
diff --git a/README.adoc b/README.adoc
index 6784e84..ed915dc 100644
--- a/README.adoc
+++ b/README.adoc
@@ -56,6 +56,10 @@ https://github.com/kiwiirc/webircgateway[].
Any further development, such as P10 or TS6 linking for IRC services,
or plugin support for arbitrary bridges, will happen here.
+xN
+--
+The IRC notifier, should you ever need to send automated messages from a script.
+
xB
--
The IRC bot. While originally intended to be a simple rewrite of my old GNU AWK
diff --git a/xN/.gitignore b/xN/.gitignore
new file mode 100644
index 0000000..86ad66d
--- /dev/null
+++ b/xN/.gitignore
@@ -0,0 +1,3 @@
+/xN
+/xN.1
+/irc.go
diff --git a/xN/Makefile b/xN/Makefile
new file mode 100644
index 0000000..5340b55
--- /dev/null
+++ b/xN/Makefile
@@ -0,0 +1,19 @@
+.POSIX:
+.SUFFIXES:
+AWK = env LC_ALL=C awk
+
+outputs = irc.go xN xN.1
+all: $(outputs)
+
+# If we want to keep module dependencies separate, we don't have many options.
+# Symlinking seems to work good enough.
+irc.go: ../xS/irc.go
+ ln -sf ../xS/irc.go $@
+
+xN: xN.go ../xK-version irc.go
+ go build -ldflags "-X 'main.projectVersion=$$(cat ../xK-version)'" -o $@
+xN.1: ../xK-version ../liberty/tools/asciiman.awk xN.adoc
+ env "asciidoc-release-version=$$(cat ../xK-version)" \
+ $(AWK) -f ../liberty/tools/asciiman.awk xN.adoc > $@
+clean:
+ rm -f $(outputs)
diff --git a/xN/go.mod b/xN/go.mod
new file mode 100644
index 0000000..50ff293
--- /dev/null
+++ b/xN/go.mod
@@ -0,0 +1,3 @@
+module janouch.name/xK/xN
+
+go 1.19
diff --git a/xN/xN.adoc b/xN/xN.adoc
new file mode 100644
index 0000000..1f5404f
--- /dev/null
+++ b/xN/xN.adoc
@@ -0,0 +1,86 @@
+xN(1)
+=====
+:doctype: manpage
+:manmanual: xK Manual
+:mansource: xK {release-version}
+
+Name
+----
+xN - IRC notifier
+
+Synopsis
+--------
+*xN* [_OPTION_]... IRC-URL...
+
+Description
+-----------
+*xN* is a simple IRC notifier, sending the text it receives on its standard
+input to all IRC targets specified by its command line arguments.
+
+The input text is forced to validate as UTF-8, and it is _not_ split
+automatically to comply with the maximum IRC message length.
+Thus, make sure to make the lines short, or they will be trimmed by the servers.
+
+*xN* does not attempt to appease flood detectors.
+
+Options
+-------
+*-debug*::
+ Print incoming IRC traffic, which may help in debugging any issues.
+
+*-version*::
+ Output version information and exit.
+
+URL format
+----------
+*xN* accepts URLs describing IRC users and channels, roughly as specified by
+the Internet Draft _draft-butcher-irc-url-04.txt_. Note, however, that close
+to no validation is done on these, and you should not pass URLs from untrusted
+sources, so as to avoid command or parameter injection.
+
+Any provided username will be propagated to the nickname, username,
+and realname. The default value for these is the name of your system user.
+
+As an extension, you may use the following options:
+
+*skipjoin*::
+ Do not JOIN channels before sending messages to them.
+ This requires channels to allow external messages
+ (which are disabled by channel mode *+n*).
+*usenotice*::
+ Send a NOTICE rather than a PRIVMSG, which distinguishes automated messages,
+ and is more appropriate for bots.
+
+Examples
+--------
+ $ uptime | xN 'irc://uptime@localhost/%23watch?skipjoin&usenotice'
+
+Send *uptime*(1) information as an external notice to channel *#watch*
+on the local server, using the standard port *6667*.
+
+ $ fortune -s | xN ircs://ohayou@irc.libera.chat/john,isuser
+
+Greet user *john* with a fortune for this day. In compliance with _RFC 7194_,
+the default TLS port is assumed to be *6697*.
+
+ $ xN 'ircs://agent:Password123@irc.cia.gov:1337/#hq?key=123456' <
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+//
+
+package main
+
+import "testing"
+
+func TestParseURL(t *testing.T) {
+ for _, rawURL := range []string{
+ `irc:server/channel`,
+ `irc://server/channel,isnetwork`,
+ `ircs://ssl.ircnet.io`,
+ `ircs://ssl.ircnet.io/`,
+ `http://server/path`,
+ } {
+ if _, _, err := parse(rawURL, nil); err == nil {
+ t.Errorf("%q should not parse\n", rawURL)
+ }
+ }
+
+ for _, test := range []struct {
+ rawURL string
+ p parameters
+ }{
+ {
+ rawURL: `irc://uptime@localhost/%23watch?skipjoin&usenotice`,
+ p: parameters{
+ username: "uptime",
+ target: "#watch",
+ skipjoin: true,
+ usenotice: true,
+ },
+ },
+ {
+ rawURL: `ircs://ohayou@irc.libera.chat/john,isuser,isserver`,
+ p: parameters{
+ username: "ohayou",
+ target: "john",
+ isuser: true,
+ },
+ },
+ {
+ rawURL: `ircs://agent:Password123@irc.cia.gov:1337/#hq?key=123456`,
+ p: parameters{
+ username: "agent",
+ password: "Password123",
+ target: "#hq",
+ chankey: "123456",
+ },
+ },
+ } {
+ p, _, err := parse(test.rawURL, nil)
+ if err != nil {
+ t.Errorf("%q should parse, got: %s\n", test.rawURL, err)
+ continue
+ }
+ if p.username != test.p.username {
+ t.Errorf("%q: username: %v ≠ %v\n",
+ test.rawURL, p.username, test.p.username)
+ }
+ if p.password != test.p.password {
+ t.Errorf("%q: password: %v ≠ %v\n",
+ test.rawURL, p.password, test.p.password)
+ }
+ if p.target != test.p.target {
+ t.Errorf("%q: target: %v ≠ %v\n",
+ test.rawURL, p.target, test.p.target)
+ }
+ if p.isuser != test.p.isuser {
+ t.Errorf("%q: isuser: %v ≠ %v\n",
+ test.rawURL, p.isuser, test.p.isuser)
+ }
+ if p.chankey != test.p.chankey {
+ t.Errorf("%q: chankey: %v ≠ %v\n",
+ test.rawURL, p.chankey, test.p.chankey)
+ }
+ if p.skipjoin != test.p.skipjoin {
+ t.Errorf("%q: skipjoin: %v ≠ %v\n",
+ test.rawURL, p.skipjoin, test.p.skipjoin)
+ }
+ if p.usenotice != test.p.usenotice {
+ t.Errorf("%q: usenotice: %v ≠ %v\n",
+ test.rawURL, p.usenotice, test.p.usenotice)
+ }
+ }
+}
diff --git a/xS/irc.go b/xS/irc.go
new file mode 100644
index 0000000..71bf961
--- /dev/null
+++ b/xS/irc.go
@@ -0,0 +1,132 @@
+package main
+
+import (
+ "path/filepath"
+ "regexp"
+ "strings"
+)
+
+func ircToLower(c byte) byte {
+ switch c {
+ case '[':
+ return '{'
+ case ']':
+ return '}'
+ case '\\':
+ return '|'
+ case '~':
+ return '^'
+ }
+ if c >= 'A' && c <= 'Z' {
+ return c + ('a' - 'A')
+ }
+ return c
+}
+
+func ircToUpper(c byte) byte {
+ switch c {
+ case '{':
+ return '['
+ case '}':
+ return ']'
+ case '|':
+ return '\\'
+ case '^':
+ return '~'
+ }
+ if c >= 'a' && c <= 'z' {
+ return c - ('a' - 'A')
+ }
+ return c
+}
+
+// Convert identifier to a canonical form for case-insensitive comparisons.
+// ircToUpper is used so that statically initialized maps can be in uppercase.
+func ircToCanon(ident string) string {
+ var canon []byte
+ for _, c := range []byte(ident) {
+ canon = append(canon, ircToUpper(c))
+ }
+ return string(canon)
+}
+
+func ircEqual(s1, s2 string) bool {
+ return ircToCanon(s1) == ircToCanon(s2)
+}
+
+func ircFnmatch(pattern string, s string) bool {
+ pattern, s = ircToCanon(pattern), ircToCanon(s)
+ // FIXME: This should not support [] ranges and handle '/' specially.
+ // We could translate the pattern to a regular expression.
+ matched, _ := filepath.Match(pattern, s)
+ return matched
+}
+
+var reMsg = regexp.MustCompile(
+ `^(?:@([^ ]*) +)?(?::([^! ]*)(?:!([^@]*)@([^ ]*))? +)?([^ ]+)(.*)?$`)
+var reArgs = regexp.MustCompile(`:.*| [^: ][^ ]*`)
+
+type message struct {
+ tags map[string]string // IRC 3.2 message tags
+ nick string // optional nickname
+ user string // optional username
+ host string // optional hostname or IP address
+ command string // command name
+ params []string // arguments
+}
+
+func ircUnescapeMessageTag(value string) string {
+ var buf []byte
+ escape := false
+ for i := 0; i < len(value); i++ {
+ if escape {
+ switch value[i] {
+ case ':':
+ buf = append(buf, ';')
+ case 's':
+ buf = append(buf, ' ')
+ case 'r':
+ buf = append(buf, '\r')
+ case 'n':
+ buf = append(buf, '\n')
+ default:
+ buf = append(buf, value[i])
+ }
+ escape = false
+ } else if value[i] == '\\' {
+ escape = true
+ } else {
+ buf = append(buf, value[i])
+ }
+ }
+ return string(buf)
+}
+
+func ircParseMessageTags(tags string, out map[string]string) {
+ for _, tag := range strings.Split(tags, ";") {
+ if tag == "" {
+ // Ignore empty.
+ } else if equal := strings.IndexByte(tag, '='); equal < 0 {
+ out[tag] = ""
+ } else {
+ out[tag[:equal]] = ircUnescapeMessageTag(tag[equal+1:])
+ }
+ }
+}
+
+func ircParseMessage(line string) *message {
+ m := reMsg.FindStringSubmatch(line)
+ if m == nil {
+ return nil
+ }
+
+ msg := message{nil, m[2], m[3], m[4], m[5], nil}
+ if m[1] != "" {
+ msg.tags = make(map[string]string)
+ ircParseMessageTags(m[1], msg.tags)
+ }
+ for _, x := range reArgs.FindAllString(m[6], -1) {
+ msg.params = append(msg.params, x[1:])
+ }
+ return &msg
+}
diff --git a/xS/xS.go b/xS/xS.go
index 25db005..69a9b36 100644
--- a/xS/xS.go
+++ b/xS/xS.go
@@ -456,132 +456,6 @@ func (fd *floodDetector) check() bool {
return count <= fd.limit
}
-// --- IRC protocol ------------------------------------------------------------
-
-func ircToLower(c byte) byte {
- switch c {
- case '[':
- return '{'
- case ']':
- return '}'
- case '\\':
- return '|'
- case '~':
- return '^'
- }
- if c >= 'A' && c <= 'Z' {
- return c + ('a' - 'A')
- }
- return c
-}
-
-func ircToUpper(c byte) byte {
- switch c {
- case '{':
- return '['
- case '}':
- return ']'
- case '|':
- return '\\'
- case '^':
- return '~'
- }
- if c >= 'a' && c <= 'z' {
- return c - ('a' - 'A')
- }
- return c
-}
-
-// Convert identifier to a canonical form for case-insensitive comparisons.
-// ircToUpper is used so that statically initialized maps can be in uppercase.
-func ircToCanon(ident string) string {
- var canon []byte
- for _, c := range []byte(ident) {
- canon = append(canon, ircToUpper(c))
- }
- return string(canon)
-}
-
-func ircEqual(s1, s2 string) bool {
- return ircToCanon(s1) == ircToCanon(s2)
-}
-
-func ircFnmatch(pattern string, s string) bool {
- pattern, s = ircToCanon(pattern), ircToCanon(s)
- // FIXME: This should not support [] ranges and handle '/' specially.
- // We could translate the pattern to a regular expression.
- matched, _ := filepath.Match(pattern, s)
- return matched
-}
-
-var reMsg = regexp.MustCompile(
- `^(?:@([^ ]*) +)?(?::([^! ]*)(?:!([^@]*)@([^ ]*))? +)?([^ ]+)(.*)?$`)
-var reArgs = regexp.MustCompile(`:.*| [^: ][^ ]*`)
-
-type message struct {
- tags map[string]string // IRC 3.2 message tags
- nick string // optional nickname
- user string // optional username
- host string // optional hostname or IP address
- command string // command name
- params []string // arguments
-}
-
-func ircUnescapeMessageTag(value string) string {
- var buf []byte
- escape := false
- for i := 0; i < len(value); i++ {
- if escape {
- switch value[i] {
- case ':':
- buf = append(buf, ';')
- case 's':
- buf = append(buf, ' ')
- case 'r':
- buf = append(buf, '\r')
- case 'n':
- buf = append(buf, '\n')
- default:
- buf = append(buf, value[i])
- }
- escape = false
- } else if value[i] == '\\' {
- escape = true
- } else {
- buf = append(buf, value[i])
- }
- }
- return string(buf)
-}
-
-func ircParseMessageTags(tags string, out map[string]string) {
- for _, tag := range splitString(tags, ";", true /* ignoreEmpty */) {
- if equal := strings.IndexByte(tag, '='); equal < 0 {
- out[tag] = ""
- } else {
- out[tag[:equal]] = ircUnescapeMessageTag(tag[equal+1:])
- }
- }
-}
-
-func ircParseMessage(line string) *message {
- m := reMsg.FindStringSubmatch(line)
- if m == nil {
- return nil
- }
-
- msg := message{nil, m[2], m[3], m[4], m[5], nil}
- if m[1] != "" {
- msg.tags = make(map[string]string)
- ircParseMessageTags(m[1], msg.tags)
- }
- for _, x := range reArgs.FindAllString(m[6], -1) {
- msg.params = append(msg.params, x[1:])
- }
- return &msg
-
-}
-
// --- IRC token validation ----------------------------------------------------
// Everything as per RFC 2812
--
cgit v1.2.3-70-g09d2