|
Server : Apache System : Linux ecngx264.inmotionhosting.com 4.18.0-553.77.1.lve.el8.x86_64 #1 SMP Wed Oct 8 14:21:00 UTC 2025 x86_64 User : lonias5 ( 3576) PHP Version : 7.3.33 Disable Function : NONE Directory : /proc/self/root/proc/thread-self/root/opt/sharedrads/ |
Upload File : |
#!/usr/lib/rads/venv/bin/python3
"""Send email to STR based on stdin or file input.
Optionally, direct that mail elsewhere."""
from platform import node
import argparse
import sys
import yaml
import rads
DEFAULT_DEST = "str@imhadmin.net"
DESTS = [
"sadmin@imhadmin.net",
"reclamations@imhadmin.net",
DEFAULT_DEST,
]
def get_args():
'''Get arguments:
--email(s) - email to send to if not str@
--user - user for which we're generating the str
--file - file containing the STR information'''
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"-s",
"--subject",
help="Ticket subject. If not provided, defaults to user @ hostname",
)
parser.add_argument(
'-e',
'--email',
dest='emails',
nargs='*',
default=[DEFAULT_DEST],
choices=DESTS,
help="Provide a different email address for the STR message to be sent."
f" If one is not provided, this script will send to {DEFAULT_DEST}.",
)
parser.add_argument(
'-f',
'--file',
type=argparse.FileType('r'),
dest='infile',
metavar='strinfo.txt',
nargs=1,
default=[sys.stdin],
help="Provide STR information via file",
)
parser.add_argument(
'-u',
'--user',
dest='user',
help="User for which the STR is being generated",
required=True,
)
args = parser.parse_args()
return args
def get_user_owners() -> dict[str, str]:
"""Get user -> owner map"""
with open('/etc/trueuserowners', encoding='utf-8') as handle:
data = yaml.load(handle, rads.DumbYamlLoader)
if data is None:
data = {}
return data
def main():
'''Main function:
Get args, get the platform, find the reseller if this is the STR for
a child account, email STR'''
args = get_args()
message = args.infile[0].read()
# get the shortname by cutting node() at the first .'s index
hostname = node()[: node().find(".")]
emails = args.emails
user = args.user
if not args.subject:
args.subject = f"{user} @ {hostname}" # "userna5 @ hostname"
if rads.IMH_CLASS == 'reseller':
# check reseller ownership from /etc/trueuserowners
# (faster than whmapi1)
owner = get_user_owners().get(user, 'root')
if owner not in rads.OUR_RESELLERS:
# "child (reseller) @ shortname"
args.subject = f"{user} ({owner}) @ {hostname}"
exit_code = 0
for email in emails:
try:
rads.make_ticket(
dest=email,
subject=args.subject,
body=message,
sender=f"{user}@{hostname}",
)
except rads.TicketError as exc:
exit_code = 1
print(
f"Failed creating {email} ticket. Please submit manually. "
f"(Creation error: {exc})"
)
else:
print(f"Successfully sent to {email} with subject {args.subject!r}")
sys.exit(exit_code)
if __name__ == "__main__":
main()