mirror of
https://github.com/ZwareBear/awx.git
synced 2026-04-07 08:31:47 -05:00
* Job object can now control the output and generate K:V output for notification types that can support it * Notifications store the body as json/dict now to encode more information * Notification Type can further compose the message based on what is sensible for the notification type * This will also allow customizing the message template in the future * All notification types use sane defaults for the level of detail now
94 lines
3.3 KiB
Python
94 lines
3.3 KiB
Python
# Copyright (c) 2016 Ansible, Inc.
|
|
# All Rights Reserved.
|
|
|
|
import time
|
|
import ssl
|
|
import logging
|
|
|
|
import irc.client
|
|
|
|
from awx.main.notifications.base import TowerBaseEmailBackend
|
|
|
|
logger = logging.getLogger('awx.main.notifications.irc_backend')
|
|
|
|
class IrcBackend(TowerBaseEmailBackend):
|
|
|
|
init_parameters = {"server": {"label": "IRC Server Address", "type": "string"},
|
|
"port": {"label": "IRC Server Port", "type": "int"},
|
|
"nickname": {"label": "IRC Nick", "type": "string"},
|
|
"password": {"label": "IRC Server Password", "type": "password"},
|
|
"use_ssl": {"label": "SSL Connection", "type": "bool"},
|
|
"targets": {"label": "Destination Channels or Users", "type": "list"}}
|
|
recipient_parameter = "targets"
|
|
sender_parameter = None
|
|
|
|
def __init__(self, server, port, nickname, password, use_ssl, fail_silently=False, **kwargs):
|
|
super(IrcBackend, self).__init__(fail_silently=fail_silently)
|
|
self.server = server
|
|
self.port = port
|
|
self.nickname = nickname
|
|
self.password = password if password != "" else None
|
|
self.use_ssl = use_ssl
|
|
self.connection = None
|
|
|
|
def open(self):
|
|
if self.connection is not None:
|
|
return False
|
|
if self.use_ssl:
|
|
connection_factory = irc.connection.Factory(wrapper=ssl.wrap_socket)
|
|
else:
|
|
connection_factory = irc.connection.Factory()
|
|
try:
|
|
self.reactor = irc.client.Reactor()
|
|
self.connection = self.reactor.server().connect(
|
|
self.server,
|
|
self.port,
|
|
self.nickname,
|
|
password=self.password,
|
|
connect_factory=connection_factory,
|
|
)
|
|
except irc.client.ServerConnectionError as e:
|
|
logger.error("Exception connecting to irc server: {}".format(e))
|
|
if not self.fail_silently:
|
|
raise
|
|
return True
|
|
|
|
def close(self):
|
|
if self.connection is None:
|
|
return
|
|
self.connection = None
|
|
|
|
def on_connect(self, connection, event):
|
|
for c in self.channels:
|
|
if irc.client.is_channel(c):
|
|
connection.join(c)
|
|
else:
|
|
for m in self.channels[c]:
|
|
connection.privmsg(c, m.subject)
|
|
self.channels_sent += 1
|
|
|
|
def on_join(self, connection, event):
|
|
for m in self.channels[event.target]:
|
|
connection.privmsg(event.target, m.subject)
|
|
self.channels_sent += 1
|
|
|
|
def send_messages(self, messages):
|
|
if self.connection is None:
|
|
self.open()
|
|
self.channels = {}
|
|
self.channels_sent = 0
|
|
for m in messages:
|
|
for r in m.recipients():
|
|
if r not in self.channels:
|
|
self.channels[r] = []
|
|
self.channels[r].append(m)
|
|
self.connection.add_global_handler("welcome", self.on_connect)
|
|
self.connection.add_global_handler("join", self.on_join)
|
|
start_time = time.time()
|
|
process_time = time.time()
|
|
while self.channels_sent < len(self.channels) and (process_time-start_time) < 60:
|
|
self.reactor.process_once(0.1)
|
|
process_time = time.time()
|
|
self.reactor.disconnect_all()
|
|
return self.channels_sent
|