Merge pull request #608 from amuraru/irc

Add option to send a private message in irc module
reviewable/pr18780/r1
Toshio Kuratomi 10 years ago
commit 2799c48cf3

@ -39,7 +39,7 @@ options:
default: 6667 default: 6667
nick: nick:
description: description:
- Nickname. May be shortened, depending on server's NICKLEN setting. - Nickname to send the message from. May be shortened, depending on server's NICKLEN setting.
required: false required: false
default: ansible default: ansible
msg: msg:
@ -63,6 +63,11 @@ options:
description: description:
- Channel name - Channel name
required: true required: true
nick_to:
description:
- A list of nicknames to send the message to. When both channel and nick_to are defined, the message will be send to both of them.
required: false
version_added: 2.0
key: key:
description: description:
- Channel key - Channel key
@ -95,10 +100,19 @@ EXAMPLES = '''
- irc: server=irc.example.net channel="#t1" msg="Hello world" - irc: server=irc.example.net channel="#t1" msg="Hello world"
- local_action: irc port=6669 - local_action: irc port=6669
server="irc.example.net"
channel="#t1" channel="#t1"
msg="All finished at {{ ansible_date_time.iso8601 }}" msg="All finished at {{ ansible_date_time.iso8601 }}"
color=red color=red
nick=ansibleIRC nick=ansibleIRC
- local_action: irc port=6669
server="irc.example.net"
channel="#t1"
nick_to=["nick1", "nick2"]
msg="All finished at {{ ansible_date_time.iso8601 }}"
color=red
nick=ansibleIRC
''' '''
# =========================================== # ===========================================
@ -112,7 +126,7 @@ import ssl
from time import sleep from time import sleep
def send_msg(channel, msg, server='localhost', port='6667', key=None, topic=None, def send_msg(msg, server='localhost', port='6667', channel=None, nick_to=[], key=None, topic=None,
nick="ansible", color='none', passwd=False, timeout=30, use_ssl=False): nick="ansible", color='none', passwd=False, timeout=30, use_ssl=False):
'''send message to IRC''' '''send message to IRC'''
@ -173,7 +187,11 @@ def send_msg(channel, msg, server='localhost', port='6667', key=None, topic=None
irc.send('TOPIC %s :%s\r\n' % (channel, topic)) irc.send('TOPIC %s :%s\r\n' % (channel, topic))
sleep(1) sleep(1)
irc.send('PRIVMSG %s :%s\r\n' % (channel, message)) if nick_to:
for nick in nick_to:
irc.send('PRIVMSG %s :%s\r\n' % (nick, message))
if channel:
irc.send('PRIVMSG %s :%s\r\n' % (channel, message))
sleep(1) sleep(1)
irc.send('PART %s\r\n' % channel) irc.send('PART %s\r\n' % channel)
irc.send('QUIT\r\n') irc.send('QUIT\r\n')
@ -191,33 +209,38 @@ def main():
server=dict(default='localhost'), server=dict(default='localhost'),
port=dict(default=6667), port=dict(default=6667),
nick=dict(default='ansible'), nick=dict(default='ansible'),
nick_to=dict(required=False, type='list'),
msg=dict(required=True), msg=dict(required=True),
color=dict(default="none", choices=["yellow", "red", "green", color=dict(default="none", choices=["yellow", "red", "green",
"blue", "black", "none"]), "blue", "black", "none"]),
channel=dict(required=True), channel=dict(required=False),
key=dict(), key=dict(),
topic=dict(), topic=dict(),
passwd=dict(), passwd=dict(),
timeout=dict(type='int', default=30), timeout=dict(type='int', default=30),
use_ssl=dict(type='bool', default=False) use_ssl=dict(type='bool', default=False)
), ),
supports_check_mode=True supports_check_mode=True,
required_one_of=[['channel', 'nick_to']]
) )
server = module.params["server"] server = module.params["server"]
port = module.params["port"] port = module.params["port"]
nick = module.params["nick"] nick = module.params["nick"]
topic = module.params["topic"] nick_to = module.params["nick_to"]
msg = module.params["msg"] msg = module.params["msg"]
color = module.params["color"] color = module.params["color"]
channel = module.params["channel"] channel = module.params["channel"]
topic = module.params["topic"]
if topic and not channel:
module.fail_json(msg="When topic is specified, a channel is required.")
key = module.params["key"] key = module.params["key"]
passwd = module.params["passwd"] passwd = module.params["passwd"]
timeout = module.params["timeout"] timeout = module.params["timeout"]
use_ssl = module.params["use_ssl"] use_ssl = module.params["use_ssl"]
try: try:
send_msg(channel, msg, server, port, key, topic, nick, color, passwd, timeout, use_ssl) send_msg(msg, server, port, channel, nick_to, key, topic, nick, color, passwd, timeout, use_ssl)
except Exception, e: except Exception, e:
module.fail_json(msg="unable to send to IRC: %s" % e) module.fail_json(msg="unable to send to IRC: %s" % e)

Loading…
Cancel
Save