Mini Shell

Direktori : /proc/thread-self/root/proc/thread-self/root/proc/self/root/opt/sharedrads/
Upload File :
Current File : //proc/thread-self/root/proc/thread-self/root/proc/self/root/opt/sharedrads/send_pp_email

#!/opt/imh-python/bin/python3
"""This will allow you to send common PP emails from
the command line"""
__author__ = 'chases'

import sys
import logging
import argparse
from platform import node
import pp_api
import rads
from rads.color import red

sys.path.insert(0, '/opt/sharedrads/python')
import picker  # pylint: disable=wrong-import-position,wrong-import-order


def get_inputs():
    """Will take input in the form of -u username or $1"""
    parser = argparse.ArgumentParser()
    parser.add_argument('-u', dest='user', help='Set user to send mail to')
    args, other = parser.parse_known_args()
    if args.user:
        if rads.is_cpuser(args.user):
            user = args.user
        else:
            sys.exit("Not a valid cPanel user")
    elif other:
        if rads.is_cpuser(other[0]):
            user = other[0]
        else:
            sys.exit("Not a valid cPanel user")
    else:
        parser.print_usage()
        sys.exit("No user provided")
    if rads.IMH_CLASS == 'reseller' and rads.is_child(user):
        try:
            user = rads.get_owner(user)
        except rads.CpuserError as exc:
            sys.exit(exc)
    return user


def set_available_emails():
    """Dictionary with values for subject, eid, and vars from PP emails"""
    if 'inmotionhosting' in node() or 'servconfig' in node():
        email_template_info = [
            {
                'subject': "wp-cron.php Disabled Due to High Resource Usage",
                'eid': 446,
                'var1': None,
                'var2': None,
                'var3': None,
            },
            {
                'subject': "WordPress Performance and Caching",
                'eid': 117,
                'var1': None,
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Account Resource Overage",
                'eid': 445,
                'var1': None,
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Hacked Account Notification",
                'eid': 45,
                'var1': 'List of hacked files: ',
                'var2': None,
                'var3': None,
            },
            {
                'subject': "XMLRPC Abuse",
                'eid': 669,
                'var1': 'Location of updated .htaccess file: ',
            },
            {
                'subject': "Phishing/Spamming due to Hacked Account",
                'eid': 127,
                'var1': 'Organization Targeted: ',
                'var2': 'List of files that were found: ',
                'var3': None,
            },
            {
                'subject': "Unapproved Bulk Email",
                'eid': 96,
                'var1': 'From email address',
                'var2': None,
            },
            {
                'subject': "Spamvertisement Complaint Received",
                'eid': 146,
                'var1': 'Email address sending the spamvertisement',
                'var2': 'Domain name sending the spamvertisement',
                'var3': None,
            },
            {
                'subject': "Long-Running MySQL Queries",
                'eid': 22,
                'var1': 'MySQL Query Info',
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Storage of Backups",
                'eid': 104,
                'var1': 'List location(s) where backups are being stored',
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Email Password Reset",
                'eid': 617,
                'var1': 'Reason: ',
                'var2': None,
                'var3': None,
            },
            {
                'subject': "cPanel Password Reset",
                'eid': 615,
                'var1': 'Reason: ',
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Unauthenticated Email Script",
                'eid': 318,
                'var1': 'Spam Subject: ',
                'var2': 'URL or Sending Path: ',
                'var3': None,
            },
            {
                'subject': "Terms of Service Violation",
                'eid': 647,
                'var1': 'Type of Violation (not legal): ',
                'var2': 'Location: ',
                'var3': None,
            },
            {
                'subject': "Caching Recommended (CMS-agnostic)",
                'eid': 304,
                'var1': 'Reason: ',
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Child cPanel Password Reset",
                'eid': 620,
                'var1': 'CMS Name: ',
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Web Spiders and Robots.txt",
                'eid': 121,
                'var1': None,
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Bulk Mailing Information Request",
                'eid': 39,
                'var1': None,
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Unattended Forum",
                'eid': 310,
                'var1': 'Location of forum: ',
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Forwarders Disabled",
                'eid': 447,
                'var1': 'Forwarders that were disabled: ',
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Scripting Issue - Core Dumping",
                'eid': 109,
                'var1': 'Location of core dumps: ',
                'var2': 'Process causing core dumps: ',
                'var3': None,
            },
        ]
    elif 'webhostinghub' in node():
        email_template_info = [
            {
                'subject': "Caching Recommended (CMS-agnostic)",
                'eid': 372,
                'var1': 'CMS Name: ',
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Spamvertisement Complaint Received",
                'eid': 365,
                'var1': 'Website sending spam',
                'var2': 'Address sending spam',
                'var3': None,
            },
            {
                'subject': "XMLRPC Abuse",
                'eid': 670,
                'var1': 'Location of updated .htaccess file: ',
            },
            {
                'subject': "Bulk Mailing Information Request",
                'eid': 185,
                'var1': None,
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Hacked Account Notification",
                'eid': 334,
                'var1': 'List of hacked files',
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Unattended Forum",
                'eid': 439,
                'var1': 'Location of forum: ',
                'var2': None,
                'var3': None,
            },
            {
                'subject': "WordPress Performance and Caching",
                'eid': 374,
                'var1': None,
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Web Spiders and Robots.txt",
                'eid': 343,
                'var1': None,
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Storage of Backups",
                'eid': 379,
                'var1': 'List location(s) where backups are being stored',
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Scripting Issue - Core Dumping",
                'eid': 363,
                'var1': 'Location of core dumps',
                'var2': 'Script causing core dump',
                'var3': None,
            },
            {
                'subject': "Long-Running MySQL Queries",
                'eid': 332,
                'var1': 'MySQL Query Info: ',
                'var2': None,
                'var3': None,
            },
            {
                'subject': "wp-cron.php Disabled Due to High Resource Usage",
                'eid': 837,
                'var1': None,
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Account Resource Overage",
                'eid': 336,
                'var1': None,
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Phishing/Spamming due to Hacked Account",
                'eid': 388,
                'var1': 'Organization Targeted: ',
                'var2': 'List of files that were found: ',
                'var3': None,
            },
            {
                'subject': "Unapproved Bulk Email",
                'eid': 381,
                'var1': 'From email address: ',
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Email Password Reset",
                'eid': 618,
                'var1': 'Reason: ',
                'var2': 'List of users: ',
                'var3': None,
            },
            {
                'subject': "Unauthenticated Email Script",
                'eid': 734,
                'var1': 'Spam Subject: ',
                'var2': 'URL or Sending Path: ',
                'var3': None,
            },
            {
                'subject': "cPanel Password Reset",
                'eid': 616,
                'var1': 'Reason: ',
                'var2': None,
                'var3': None,
            },
            {
                'subject': "Terms of Service Violation",
                'eid': 648,
                'var1': 'Type of Violation (not legal): ',
                'var2': 'Location: ',
                'var3': None,
            },
            {
                'subject': "Forwarders Disabled",
                'eid': 448,
                'var1': 'Forwarders that were disabled: ',
                'var2': None,
                'var3': None,
            },
        ]
    else:
        sys.exit('Error: Unable to find brand from hostname')
    return email_template_info


def generate_menu(email_template_info, user):
    """Uses picker located in:
    /opt/imh-python/lib/python3/site-packages/
    As a wrapper for curses to create a menu"""
    email_subjects = []
    for email_type in email_template_info:
        email_subjects.append(email_type['subject'])
    opts = picker.Picker(
        title='Select PP email to send to %s' % user,
        options=sorted(email_subjects),
    ).get_selected()
    if opts:
        return opts
    sys.exit("No email selected")


def mail_users(message_id, cpuser):
    """This part sends the message to the user.
    Reference for pp_api emails"""
    try:
        if rads.IMH_CLASS == 'RESELLER' and rads.is_child(cpuser):
            cpuser = rads.get_owner(cpuser)
    except rads.CpuserError as exc:
        sys.exit(exc)
    pp_connect = pp_api.PowerPanel()
    template_id = message_id['eid']
    data = {
        'template': template_id,
        'cpanelUser': cpuser,
        'variable_1': message_id['var1'],
        'variable_2': message_id['var2'],
        'variable_3': message_id['var3'],
    }

    results = pp_connect.call("notification.send", **data)
    if results.status != 0:
        log_error = f"Failed to send notification to {cpuser}"
        logging.error(log_error)
        print(red('Failed'))
        if hasattr(results, 'message'):
            print(results.message)
        sys.exit(1)

    else:
        whoami = rads.get_login()
        log_success = "Success: {} sent to {} by {}".format(
            message_id['subject'], cpuser, whoami
        )
        logging.info(log_success)
        print("Message Sent Successfully")


def parse_opts_and_get_variables(opts, email_template_info, user):
    """Takes the messages, finds the coresponding eid,
    collects required variables, and sends to mail_users()."""
    for value in opts:
        for message in email_template_info:
            if value == message['subject']:
                if message['var1'] is not None:
                    print(
                        "Gathering Variables for email %s" % message['subject']
                    )
                    var = input("%s : " % message['var1'])
                    message['var1'] = var
                    if message['var2'] is not None:
                        var2 = input("%s : " % message['var2'])
                        message['var2'] = var2
                    if message['var3'] is not None:
                        var3 = input("%s : " % message['var3'])
                        message['var3'] = var3
                print("Connecting to AMP to send Message")
                mail_users(message, user)


def main():
    """Main function: collect args, post email"""
    rads.setup_logging(
        path='/var/log/ppnotifications.log',
        loglevel=logging.INFO,
        print_out=False,
    )
    user = get_inputs()
    email_template_info = set_available_emails()
    opts = generate_menu(email_template_info, user)
    parse_opts_and_get_variables(opts, email_template_info, user)


if __name__ == "__main__":
    main()

Zerion Mini Shell 1.0