2013-07-17 22:37:04 +02:00
|
|
|
#!/usr/bin/python
|
2013-05-08 13:24:31 +02:00
|
|
|
# -*- 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
|
2013-11-19 00:55:49 +01:00
|
|
|
version_added: "1.2"
|
2013-05-08 13:24:31 +02:00
|
|
|
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:
|
|
|
|
- Nickname
|
|
|
|
required: false
|
|
|
|
default: ansible
|
|
|
|
msg:
|
|
|
|
description:
|
|
|
|
- The message body.
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
color:
|
|
|
|
description:
|
|
|
|
- Text color for the message. Default is black.
|
|
|
|
required: false
|
|
|
|
default: black
|
|
|
|
choices: [ "yellow", "red", "green", "blue", "black" ]
|
|
|
|
channel:
|
|
|
|
description:
|
|
|
|
- Channel name
|
|
|
|
required: true
|
2013-06-26 16:16:59 +02:00
|
|
|
passwd:
|
|
|
|
description:
|
|
|
|
- Server password
|
|
|
|
required: false
|
2013-12-05 20:03:42 +01:00
|
|
|
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
|
2013-05-08 13:24:31 +02:00
|
|
|
|
|
|
|
# informational: requirements for nodes
|
|
|
|
requirements: [ socket ]
|
2013-12-05 20:03:42 +01:00
|
|
|
author: Jan-Piet Mens, Matt Martz
|
2013-05-08 13:24:31 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2013-06-17 08:53:46 +02:00
|
|
|
- irc: server=irc.example.net channel="#t1" msg="Hello world"
|
2013-05-11 22:25:22 +02:00
|
|
|
|
2013-06-17 08:53:46 +02:00
|
|
|
- local_action: irc port=6669
|
|
|
|
channel="#t1"
|
|
|
|
msg="All finished at {{ ansible_date_time.iso8601 }}"
|
|
|
|
color=red
|
|
|
|
nick=ansibleIRC
|
2013-05-08 13:24:31 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# IRC module support methods.
|
|
|
|
#
|
|
|
|
|
2013-12-05 20:03:42 +01:00
|
|
|
import re
|
2013-05-08 13:24:31 +02:00
|
|
|
import socket
|
|
|
|
|
2013-12-05 20:03:42 +01:00
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
|
2013-05-08 13:24:31 +02:00
|
|
|
def send_msg(channel, msg, server='localhost', port='6667',
|
2013-12-05 20:03:42 +01:00
|
|
|
nick="ansible", color='black', passwd=False, timeout=30):
|
2013-05-08 13:24:31 +02:00
|
|
|
'''send message to IRC'''
|
|
|
|
|
|
|
|
colornumbers = {
|
2013-12-05 20:03:42 +01:00
|
|
|
'black': "01",
|
|
|
|
'red': "04",
|
|
|
|
'green': "09",
|
|
|
|
'yellow': "08",
|
|
|
|
'blue': "12",
|
2013-05-08 13:24:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
|
|
|
colornumber = colornumbers[color]
|
|
|
|
except:
|
|
|
|
colornumber = "01" # black
|
|
|
|
|
|
|
|
message = "\x03" + colornumber + msg
|
|
|
|
|
2013-12-05 20:03:42 +01:00
|
|
|
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
irc.connect((server, int(port)))
|
2013-06-26 16:16:59 +02:00
|
|
|
if passwd:
|
2013-12-05 20:03:42 +01:00
|
|
|
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)
|
|
|
|
if re.search('^:\S+ 00[1-4] %s :' % nick, motd, flags=re.M):
|
|
|
|
break
|
|
|
|
elif time.time() - start > timeout:
|
2013-12-06 18:39:39 +01:00
|
|
|
raise Exception('Timeout waiting for IRC server welcome response')
|
2013-12-05 20:03:42 +01:00
|
|
|
time.sleep(0.5)
|
|
|
|
|
|
|
|
irc.send('JOIN %s\r\n' % channel)
|
|
|
|
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:
|
2013-12-06 18:39:39 +01:00
|
|
|
raise Exception('Timeout waiting for IRC JOIN response')
|
2013-12-05 20:03:42 +01:00
|
|
|
time.sleep(0.5)
|
|
|
|
|
|
|
|
irc.send('PRIVMSG %s :%s\r\n' % (channel, message))
|
2013-06-06 02:10:26 +02:00
|
|
|
time.sleep(1)
|
2013-12-05 20:03:42 +01:00
|
|
|
irc.send('PART %s\r\n' % channel)
|
|
|
|
irc.send('QUIT\r\n')
|
2013-06-06 02:10:26 +02:00
|
|
|
time.sleep(1)
|
2013-05-08 13:24:31 +02:00
|
|
|
irc.close()
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# Main
|
|
|
|
#
|
|
|
|
|
|
|
|
|
2013-12-05 20:03:42 +01:00
|
|
|
def main():
|
2013-05-08 13:24:31 +02:00
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2013-12-05 20:03:42 +01:00
|
|
|
server=dict(default='localhost'),
|
|
|
|
port=dict(default=6667),
|
|
|
|
nick=dict(default='ansible'),
|
|
|
|
msg=dict(required=True),
|
|
|
|
color=dict(default="black", choices=["yellow", "red", "green",
|
|
|
|
"blue", "black"]),
|
|
|
|
channel=dict(required=True),
|
|
|
|
passwd=dict(),
|
|
|
|
timeout=dict(type='int', default=30)
|
2013-05-08 13:24:31 +02:00
|
|
|
),
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
2013-12-05 20:03:42 +01:00
|
|
|
server = module.params["server"]
|
|
|
|
port = module.params["port"]
|
|
|
|
nick = module.params["nick"]
|
|
|
|
msg = module.params["msg"]
|
|
|
|
color = module.params["color"]
|
2013-05-08 13:24:31 +02:00
|
|
|
channel = module.params["channel"]
|
2013-12-05 20:03:42 +01:00
|
|
|
passwd = module.params["passwd"]
|
|
|
|
timeout = module.params["timeout"]
|
2013-05-08 13:24:31 +02:00
|
|
|
|
|
|
|
try:
|
2013-12-05 20:03:42 +01:00
|
|
|
send_msg(channel, msg, server, port, nick, color, passwd, timeout)
|
2013-05-08 13:24:31 +02:00
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(msg="unable to send to IRC: %s" % e)
|
|
|
|
|
2013-05-15 07:36:23 +02:00
|
|
|
module.exit_json(changed=False, channel=channel, nick=nick,
|
2013-05-08 13:24:31 +02:00
|
|
|
msg=msg)
|
|
|
|
|
2013-12-02 21:13:49 +01:00
|
|
|
# import module snippets
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2013-05-08 13:24:31 +02:00
|
|
|
main()
|