diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..ad19e53 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,26 @@ +--- +name: "Lint" + +# yamllint disable-line rule:truthy +on: [push, pull_request] + +jobs: + lint-yaml: + name: "YAML" + runs-on: ubuntu-22.04 + env: + YAML_FILES: | + .github/workflows/lint.yml + action.yml + steps: + - uses: actions/checkout@v4 + - name: Install Dependencies + run: sudo apt-get update && sudo apt-get install -y yamllint + - name: Validate YAML Files + run: yamllint $YAML_FILES + + lint-python: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + - uses: chartboost/ruff-action@v1 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..5383229 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,20 @@ +--- +# yamllint disable rule:line-length +name: "Tests" + +# yamllint disable-line rule:truthy +on: [push, pull_request] + +jobs: + run: + runs-on: ubuntu-22.04 + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: run + uses: "./" + with: + irc-server: "irc.hackint.org" + irc-nickname: "workflow-test" + irc-target: "#ffda-github-irc" + message: "${{ github.head_ref }} ${{ github.sha }}" diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..859ac3d --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,7 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.3.0 + hooks: + - id: check-yaml + - id: end-of-file-fixer + - id: trailing-whitespace diff --git a/README.md b/README.md index 462d210..70f87e0 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# action-irc-message \ No newline at end of file +# action-irc-message diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..9a08362 --- /dev/null +++ b/action.yml @@ -0,0 +1,31 @@ +--- +# yamllint disable rule:line-length + +name: "Send message to IRC" +inputs: + irc-server: + required: true + irc-nickname: + required: true + irc-target: + description: 'nickname or channel' + required: true + irc-port: + default: 6697 + message: + required: true + +runs: + using: 'composite' + steps: + - name: Install Python + uses: actions/setup-python@v4 + with: + python-version: '3.12' + - name: Install Dependencies + run: pip install irc + shell: bash + - name: Send message + run: | + python irc-message.py -p "${{ inputs.irc-port }}" "${{ inputs.irc-server }}" "${{ inputs.irc-nickname }}" "${{ inputs.irc-target }}" "${{ inputs.message }}" + shell: bash diff --git a/irc-message.py b/irc-message.py new file mode 100644 index 0000000..5990496 --- /dev/null +++ b/irc-message.py @@ -0,0 +1,63 @@ +#! /usr/bin/env python + +import sys +import ssl +import argparse + +import irc.client + +target = None +"The nick or channel to which to send messages" +message = None + +def on_connect(connection, event): + if irc.client.is_channel(target): + connection.join(target) + return + +def on_join(connection, event): + connection.privmsg(target, message) + connection.quit("Bye") + + +def on_disconnect(connection, event): + raise SystemExit() + + +def get_args(): + parser = argparse.ArgumentParser() + parser.add_argument('server') + parser.add_argument('nickname') + parser.add_argument('target', help="a nickname or channel") + parser.add_argument('message') + parser.add_argument('-p', '--port', default=6697, type=int) + return parser.parse_args() + + +def main(): + global target + global message + + args = get_args() + target = args.target + message = args.message + + ssl_factory = irc.connection.Factory(wrapper=ssl.wrap_socket) + reactor = irc.client.Reactor() + try: + c = reactor.server().connect( + args.server, args.port, args.nickname, connect_factory=ssl_factory + ) + except irc.client.ServerConnectionError: + print(sys.exc_info()[1]) + raise SystemExit(1) + + c.add_global_handler("welcome", on_connect) + c.add_global_handler("join", on_join) + c.add_global_handler("disconnect", on_disconnect) + + reactor.process_forever() + + +if __name__ == '__main__': + main()