#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2012, Jim Richardson <weaselkeeper@gmail.com>
# All rights reserved.
#
# 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: pushover
version_added: "2.0"
short_description: Send notifications via u(https://pushover.net)
description:
   - Send notifications via pushover, to subscriber list of devices, and email
   addresses. Requires pushover app on devices.
notes:
   - You will require a pushover.net account to use this module. But no account
   is required to receive messages.
options:
  msg:
    description:
      What message you wish to send.
    required: true
  app_token:
    description:
      Pushover issued token identifying your pushover app.
    required: true
  user_key:
    description:
      Pushover issued authentication key for your user.
    required: true
  pri:
    description: Message priority (see u(https://pushover.net) for details.)
    required: false

author: Jim Richardson
'''

EXAMPLES = '''
- local_action: pushover msg="{{inventory_hostname}} has exploded in flames,
  It is now time to panic" app_token=wxfdksl user_key=baa5fe97f2c5ab3ca8f0bb59
'''

import urllib
import httplib


class pushover(object):
    ''' Instantiates a pushover object, use it to send notifications '''

    def __init__(self):
        self.host, self.port = 'api.pushover.net', 443

    def run(self):
        ''' Do, whatever it is, we do. '''
        # parse config
        conn = httplib.HTTPSConnection(self.host, self.port)
        conn.request("POST", "/1/messages.json",
                     urllib.urlencode(self.options),
                     {"Content-type": "application/x-www-form-urlencoded"})
        conn.getresponse()
        return


def main():

    module = AnsibleModule(
        argument_spec=dict(
            msg=dict(required=True),
            app_token=dict(required=True),
            user_key=dict(required=True),
            pri=dict(required=False, default=0),
        ),
    )

    msg_object = pushover()
    msg_object.options = {}
    msg_object.options['user'] = module.params['user_key']
    msg_object.options['token'] = module.params['app_token']
    msg_object.options['priority'] = module.params['pri']
    msg_object.options['message'] = module.params['msg']
    try:
        msg_object.run()
    except:
        module.fail_json(msg='Unable to send msg via pushover')

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

# import module snippets
from ansible.module_utils.basic import *
main()
