mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
216 lines
5.9 KiB
Python
216 lines
5.9 KiB
Python
12 years ago
|
#!/usr/bin/python
|
||
12 years ago
|
# -*- coding: utf-8 -*-
|
||
|
|
||
|
# (c) 2013, Jan-Piet Mens <jpmens () gmail.com>
|
||
|
#
|
||
|
# This file is part of Ansible
|
||
|
#
|
||
|
# Ansible is free software: you can redistribute it and/or modify
|
||
|
# it under the terms of the GNU General Public License as published by
|
||
|
# the Free Software Foundation, either version 3 of the License, or
|
||
|
# (at your option) any later version.
|
||
|
#
|
||
|
# Ansible is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||
|
#
|
||
|
|
||
|
DOCUMENTATION = '''
|
||
|
---
|
||
|
module: irc
|
||
11 years ago
|
version_added: "1.2"
|
||
12 years ago
|
short_description: Send a message to an IRC channel
|
||
|
description:
|
||
|
- Send a message to an IRC channel. This is a very simplistic implementation.
|
||
|
options:
|
||
|
server:
|
||
|
description:
|
||
|
- IRC server name/address
|
||
|
required: false
|
||
|
default: localhost
|
||
|
port:
|
||
|
description:
|
||
|
- IRC server port number
|
||
|
required: false
|
||
|
default: 6667
|
||
|
nick:
|
||
|
description:
|
||
11 years ago
|
- Nickname. May be shortened, depending on server's NICKLEN setting.
|
||
12 years ago
|
required: false
|
||
|
default: ansible
|
||
|
msg:
|
||
|
description:
|
||
|
- The message body.
|
||
|
required: true
|
||
|
default: null
|
||
|
color:
|
||
|
description:
|
||
11 years ago
|
- Text color for the message. ("none" is a valid option in 1.6 or later, in 1.6 and prior, the default color is black, not "none").
|
||
12 years ago
|
required: false
|
||
11 years ago
|
default: "none"
|
||
11 years ago
|
choices: [ "none", "yellow", "red", "green", "blue", "black" ]
|
||
12 years ago
|
channel:
|
||
|
description:
|
||
|
- Channel name
|
||
|
required: true
|
||
11 years ago
|
key:
|
||
|
description:
|
||
|
- Channel key
|
||
|
required: false
|
||
|
version_added: 1.7
|
||
12 years ago
|
passwd:
|
||
|
description:
|
||
|
- Server password
|
||
|
required: false
|
||
11 years ago
|
timeout:
|
||
|
description:
|
||
|
- Timeout to use while waiting for successful registration and join
|
||
|
messages, this is to prevent an endless loop
|
||
|
default: 30
|
||
|
version_added: 1.5
|
||
10 years ago
|
use_ssl:
|
||
|
description:
|
||
|
- Designates whether TLS/SSL should be used when connecting to the IRC server
|
||
|
default: False
|
||
10 years ago
|
version_added: 1.8
|
||
12 years ago
|
|
||
|
# informational: requirements for nodes
|
||
|
requirements: [ socket ]
|
||
11 years ago
|
author: Jan-Piet Mens, Matt Martz
|
||
12 years ago
|
'''
|
||
|
|
||
|
EXAMPLES = '''
|
||
12 years ago
|
- irc: server=irc.example.net channel="#t1" msg="Hello world"
|
||
12 years ago
|
|
||
12 years ago
|
- local_action: irc port=6669
|
||
|
channel="#t1"
|
||
|
msg="All finished at {{ ansible_date_time.iso8601 }}"
|
||
|
color=red
|
||
|
nick=ansibleIRC
|
||
12 years ago
|
'''
|
||
|
|
||
|
# ===========================================
|
||
|
# IRC module support methods.
|
||
|
#
|
||
|
|
||
11 years ago
|
import re
|
||
12 years ago
|
import socket
|
||
10 years ago
|
import ssl
|
||
12 years ago
|
|
||
11 years ago
|
from time import sleep
|
||
|
|
||
|
|
||
11 years ago
|
def send_msg(channel, msg, server='localhost', port='6667', key=None,
|
||
10 years ago
|
nick="ansible", color='none', passwd=False, timeout=30, use_ssl=False):
|
||
12 years ago
|
'''send message to IRC'''
|
||
|
|
||
|
colornumbers = {
|
||
11 years ago
|
'black': "01",
|
||
|
'red': "04",
|
||
|
'green': "09",
|
||
|
'yellow': "08",
|
||
|
'blue': "12",
|
||
12 years ago
|
}
|
||
|
|
||
|
try:
|
||
|
colornumber = colornumbers[color]
|
||
11 years ago
|
colortext = "\x03" + colornumber
|
||
12 years ago
|
except:
|
||
11 years ago
|
colortext = ""
|
||
12 years ago
|
|
||
11 years ago
|
message = colortext + msg
|
||
12 years ago
|
|
||
11 years ago
|
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||
10 years ago
|
if use_ssl:
|
||
|
irc = ssl.wrap_socket(irc)
|
||
11 years ago
|
irc.connect((server, int(port)))
|
||
12 years ago
|
if passwd:
|
||
11 years ago
|
irc.send('PASS %s\r\n' % passwd)
|
||
|
irc.send('NICK %s\r\n' % nick)
|
||
|
irc.send('USER %s %s %s :ansible IRC\r\n' % (nick, nick, nick))
|
||
|
motd = ''
|
||
|
start = time.time()
|
||
|
while 1:
|
||
|
motd += irc.recv(1024)
|
||
11 years ago
|
# The server might send back a shorter nick than we specified (due to NICKLEN),
|
||
|
# so grab that and use it from now on (assuming we find the 00[1-4] response).
|
||
11 years ago
|
match = re.search('^:\S+ 00[1-4] (?P<nick>\S+) :', motd, flags=re.M)
|
||
|
if match:
|
||
|
nick = match.group('nick')
|
||
11 years ago
|
break
|
||
|
elif time.time() - start > timeout:
|
||
11 years ago
|
raise Exception('Timeout waiting for IRC server welcome response')
|
||
11 years ago
|
sleep(0.5)
|
||
11 years ago
|
|
||
11 years ago
|
if key:
|
||
|
irc.send('JOIN %s %s\r\n' % (channel, key))
|
||
|
else:
|
||
|
irc.send('JOIN %s\r\n' % channel)
|
||
|
|
||
11 years ago
|
join = ''
|
||
|
start = time.time()
|
||
|
while 1:
|
||
|
join += irc.recv(1024)
|
||
|
if re.search('^:\S+ 366 %s %s :' % (nick, channel), join, flags=re.M):
|
||
|
break
|
||
|
elif time.time() - start > timeout:
|
||
11 years ago
|
raise Exception('Timeout waiting for IRC JOIN response')
|
||
11 years ago
|
sleep(0.5)
|
||
11 years ago
|
|
||
|
irc.send('PRIVMSG %s :%s\r\n' % (channel, message))
|
||
11 years ago
|
sleep(1)
|
||
11 years ago
|
irc.send('PART %s\r\n' % channel)
|
||
|
irc.send('QUIT\r\n')
|
||
11 years ago
|
sleep(1)
|
||
12 years ago
|
irc.close()
|
||
|
|
||
|
# ===========================================
|
||
|
# Main
|
||
|
#
|
||
|
|
||
|
|
||
11 years ago
|
def main():
|
||
12 years ago
|
module = AnsibleModule(
|
||
|
argument_spec=dict(
|
||
11 years ago
|
server=dict(default='localhost'),
|
||
|
port=dict(default=6667),
|
||
|
nick=dict(default='ansible'),
|
||
|
msg=dict(required=True),
|
||
11 years ago
|
color=dict(default="none", choices=["yellow", "red", "green",
|
||
|
"blue", "black", "none"]),
|
||
11 years ago
|
channel=dict(required=True),
|
||
11 years ago
|
key=dict(),
|
||
11 years ago
|
passwd=dict(),
|
||
10 years ago
|
timeout=dict(type='int', default=30),
|
||
|
use_ssl=dict(type='bool', default=False)
|
||
12 years ago
|
),
|
||
|
supports_check_mode=True
|
||
|
)
|
||
|
|
||
11 years ago
|
server = module.params["server"]
|
||
|
port = module.params["port"]
|
||
|
nick = module.params["nick"]
|
||
|
msg = module.params["msg"]
|
||
|
color = module.params["color"]
|
||
12 years ago
|
channel = module.params["channel"]
|
||
11 years ago
|
key = module.params["key"]
|
||
11 years ago
|
passwd = module.params["passwd"]
|
||
|
timeout = module.params["timeout"]
|
||
10 years ago
|
use_ssl = module.params["use_ssl"]
|
||
12 years ago
|
|
||
|
try:
|
||
10 years ago
|
send_msg(channel, msg, server, port, key, nick, color, passwd, timeout, use_ssl)
|
||
12 years ago
|
except Exception, e:
|
||
|
module.fail_json(msg="unable to send to IRC: %s" % e)
|
||
|
|
||
12 years ago
|
module.exit_json(changed=False, channel=channel, nick=nick,
|
||
12 years ago
|
msg=msg)
|
||
|
|
||
11 years ago
|
# import module snippets
|
||
11 years ago
|
from ansible.module_utils.basic import *
|
||
12 years ago
|
main()
|