re-add irc notifications

This commit is contained in:
Jörg Thalheim 2020-01-11 08:52:28 +00:00
parent 0996106bf7
commit 6b4b37af2d
No known key found for this signature in database
GPG key ID: B3F5D81B0C6967C4
6 changed files with 110 additions and 2 deletions

View file

@ -50,7 +50,9 @@ git config --global commit.gpgsign true
git clone git@github.com:nix-community/nur-combined
result/bin/nur combine nur-combined
result/bin/nur combine \
--irc-notify nur-bot@chat.freenode.net:6697/nixos-nur \
nur-combined
if [[ -z "$(git diff --exit-code)" ]]; then
echo "No changes to the output on this push; exiting."

View file

@ -28,6 +28,11 @@ def parse_arguments(argv: List[str]) -> argparse.Namespace:
subparsers = parser.add_subparsers(description="subcommands")
combine = subparsers.add_parser("combine")
combine.add_argument(
"--irc-notify",
type=str,
help="Example nur-bot@chat.freenode.net:6697/nixos-nur",
)
combine.add_argument("directory")
combine.set_defaults(func=combine_command)

View file

@ -182,4 +182,16 @@ def combine_command(args: Namespace) -> None:
with chdir(combined_path):
setup_combined()
update_combined(combined_path)
notifications = update_combined(combined_path)
if args.irc_notify:
try:
from .irc_notify import send
except ImportError as e:
print(f"failed to import irc_notify, skipping notification: {e}")
return
try:
send(args.irc_notify, notifications)
except Exception as e:
print(f"failed to send irc notifications: {e}")

86
nur/irc_notify.py Normal file
View file

@ -0,0 +1,86 @@
import ssl
from typing import List, Optional
from urllib.parse import urlparse
from irc.client import Connection, Event, Reactor, ServerConnectionError, is_channel
from irc.connection import Factory
class Exit(SystemExit):
pass
def send(url: str, notifications: List[str]) -> None:
parsed = urlparse(f"http://{url}")
username = parsed.username or "nur-bot"
server = parsed.hostname or "chat.freenode.de"
if parsed.path != "/" or parsed.path == "":
channel = f"#{parsed.path[1:]}"
else:
channel = "#nixos-nur"
port = parsed.port or 6697
password = parsed.password
if len(notifications) == 0:
return
_send(
notifications=notifications,
nickname=username,
password=password,
server=server,
channel=channel,
port=port,
)
class _send:
def __init__(
self,
notifications: List[str],
server: str,
nickname: str,
port: int,
channel: str,
password: Optional[str] = None,
use_ssl: bool = True,
) -> None:
self.notifications = notifications
self.channel = channel
ssl_factory = None
if use_ssl:
ssl_factory = Factory(wrapper=ssl.wrap_socket)
reactor = Reactor()
try:
s = reactor.server()
c = s.connect(
server, port, nickname, password=password, connect_factory=ssl_factory
)
except ServerConnectionError as e:
print(f"error sending irc notification {e}")
return
c.add_global_handler("welcome", self.on_connect)
c.add_global_handler("join", self.on_join)
c.add_global_handler("disconnect", self.on_disconnect)
try:
reactor.process_forever()
except Exit:
pass
def on_connect(self, connection: Connection, event: Event) -> None:
if is_channel(self.channel):
connection.join(self.channel)
return
self.main_loop(connection)
def on_join(self, connection: Connection, event: Event) -> None:
self.main_loop(connection)
def on_disconnect(self, connection: Connection, event: Event) -> None:
raise Exit()
def main_loop(self, connection: Connection) -> None:
for notification in self.notifications:
connection.privmsg(self.channel, notification)
connection.quit("Bye")

View file

@ -6,6 +6,8 @@ python3Packages.buildPythonApplication {
name = "nur";
src = ./.;
propagatedBuildInputs = [ python3Packages.irc ];
makeWrapperArgs = [
"--prefix" "PATH" ":" "${stdenv.lib.makeBinPath [ nix-prefetch-git git nix ]}"
"--set" "LOCALE_ARCHIVE" "${glibcLocales}/lib/locale/locale-archive"

View file

@ -14,6 +14,7 @@ setup(
license="MIT",
packages=find_packages(),
entry_points={"console_scripts": ["nur = nur:main"]},
install_requires=["irc"],
extras_require={"dev": ["mypy", "flake8>=3.5,<3.6", "black"]},
classifiers=[
"Development Status :: 5 - Production/Stable",