summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/notification/irc.py
blob: 51809d82a022e6fc30179e28822bfdbe25c4735c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2013, Jan-Piet Mens <jpmens () gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type


ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['stableinterface'],
                    'supported_by': 'community'}


DOCUMENTATION = '''
---
module: irc
version_added: "1.2"
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 to send the message from. May be shortened, depending on server's NICKLEN setting.
    required: false
    default: ansible
  msg:
    description:
      - The message body.
    required: true
    default: null
  topic:
    description:
      - Set the channel topic
    required: false
    default: null
    version_added: "2.0"
  color:
    description:
      - 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").
        Added 11 more colors in version 2.0.
    required: false
    default: "none"
    choices: [ "none", "white", "black", "blue", "green", "red", "brown", "purple", "orange", "yellow", "light_green", "teal", "light_cyan",
               "light_blue", "pink", "gray", "light_gray"]
  channel:
    description:
      - Channel name.  One of nick_to or channel needs to be set.  When both are set, the message will be sent to both of them.
    required: true
  nick_to:
    description:
      - A list of nicknames to send the message to. One of nick_to or channel needs to be set.  When both are defined, the message will be sent to both of them.
    required: false
    default: null
    version_added: "2.0"
  key:
    description:
      - Channel key
    required: false
    version_added: "1.7"
  passwd:
    description:
      - Server password
    required: false
  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"
  use_ssl:
    description:
      - Designates whether TLS/SSL should be used when connecting to the IRC server
    default: False
    version_added: "1.8"
  part:
    description:
      - Designates whether user should part from channel after sending message or not.
        Useful for when using a faux bot and not wanting join/parts between messages.
    default: True
    version_added: "2.0"
  style:
    description:
      - Text style for the message. Note italic does not work on some clients
    default: None
    required: False
    choices: [ "bold", "underline", "reverse", "italic" ]
    version_added: "2.0"

# informational: requirements for nodes
requirements: [ socket ]
author:
    - '"Jan-Piet Mens (@jpmens)"'
    - '"Matt Martz (@sivel)"'
'''

EXAMPLES = '''
- irc:
    server: irc.example.net
    channel: #t1
    msg: Hello world

- local_action:
    module: irc
    port: 6669
    server: irc.example.net
    channel: #t1
    msg: 'All finished at {{ ansible_date_time.iso8601 }}'
    color: red
    nick: ansibleIRC

- local_action:
    module: 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
'''

# ===========================================
# IRC module support methods.
#

import re
import socket
import ssl
import time
import traceback

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native


def send_msg(msg, server='localhost', port='6667', channel=None, nick_to=None, key=None, topic=None,
             nick="ansible", color='none', passwd=False, timeout=30, use_ssl=False, part=True, style=None):
    '''send message to IRC'''
    nick_to = [] if nick_to is None else nick_to

    colornumbers = {
        'white': "00",
        'black': "01",
        'blue': "02",
        'green': "03",
        'red': "04",
        'brown': "05",
        'purple': "06",
        'orange': "07",
        'yellow': "08",
        'light_green': "09",
        'teal': "10",
        'light_cyan': "11",
        'light_blue': "12",
        'pink': "13",
        'gray': "14",
        'light_gray': "15",
    }

    stylechoices = {
        'bold': "\x02",
        'underline': "\x1F",
        'reverse': "\x16",
        'italic': "\x1D",
    }

    try:
        styletext = stylechoices[style]
    except:
        styletext = ""

    try:
        colornumber = colornumbers[color]
        colortext = "\x03" + colornumber
    except:
        colortext = ""

    message = styletext + colortext + msg

    irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    if use_ssl:
        irc = ssl.wrap_socket(irc)
    irc.connect((server, int(port)))
    if passwd:
        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)
        # 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).
        match = re.search(r'^:\S+ 00[1-4] (?P<nick>\S+) :', motd, flags=re.M)
        if match:
            nick = match.group('nick')
            break
        elif time.time() - start > timeout:
            raise Exception('Timeout waiting for IRC server welcome response')
        time.sleep(0.5)

    if key:
        irc.send('JOIN %s %s\r\n' % (channel, key))
    else:
        irc.send('JOIN %s\r\n' % channel)

    join = ''
    start = time.time()
    while 1:
        join += irc.recv(1024)
        if re.search(r'^:\S+ 366 %s %s :' % (nick, channel), join, flags=re.M):
            break
        elif time.time() - start > timeout:
            raise Exception('Timeout waiting for IRC JOIN response')
        time.sleep(0.5)

    if topic is not None:
        irc.send('TOPIC %s :%s\r\n' % (channel, topic))
        time.sleep(1)

    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))
    time.sleep(1)
    if part:
        irc.send('PART %s\r\n' % channel)
        irc.send('QUIT\r\n')
        time.sleep(1)
    irc.close()

# ===========================================
# Main
#


def main():
    module = AnsibleModule(
        argument_spec=dict(
            server=dict(default='localhost'),
            port=dict(type='int', default=6667),
            nick=dict(default='ansible'),
            nick_to=dict(required=False, type='list'),
            msg=dict(required=True),
            color=dict(default="none", aliases=['colour'], choices=["white", "black", "blue",
                                                                    "green", "red", "brown",
                                                                    "purple", "orange", "yellow",
                                                                    "light_green", "teal", "light_cyan",
                                                                    "light_blue", "pink", "gray",
                                                                    "light_gray", "none"]),
            style=dict(default="none", choices=["underline", "reverse", "bold", "italic", "none"]),
            channel=dict(required=False),
            key=dict(no_log=True),
            topic=dict(),
            passwd=dict(no_log=True),
            timeout=dict(type='int', default=30),
            part=dict(type='bool', default=True),
            use_ssl=dict(type='bool', default=False)
        ),
        supports_check_mode=True,
        required_one_of=[['channel', 'nick_to']]
    )

    server = module.params["server"]
    port = module.params["port"]
    nick = module.params["nick"]
    nick_to = module.params["nick_to"]
    msg = module.params["msg"]
    color = module.params["color"]
    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"]
    passwd = module.params["passwd"]
    timeout = module.params["timeout"]
    use_ssl = module.params["use_ssl"]
    part = module.params["part"]
    style = module.params["style"]

    try:
        send_msg(msg, server, port, channel, nick_to, key, topic, nick, color, passwd, timeout, use_ssl, part, style)
    except Exception as e:
        module.fail_json(msg="unable to send to IRC: %s" % to_native(e), exception=traceback.format_exc())

    module.exit_json(changed=False, channel=channel, nick=nick,
                     msg=msg)


if __name__ == '__main__':
    main()