Skip to content

Instantly share code, notes, and snippets.

@mrhalix
Created June 20, 2023 12:27
Show Gist options
  • Save mrhalix/1fcaec01c0198cf377c88c68be7212d7 to your computer and use it in GitHub Desktop.
Save mrhalix/1fcaec01c0198cf377c88c68be7212d7 to your computer and use it in GitHub Desktop.
IceWarp - Sort disabled users by their usage
#-----------------
# I know this is a mess, but it works
#-----------------
import xml.etree.ElementTree as ET
def convert_quota(kb):
if kb > 1024 * 1024:
return str(round(kb / (1024 * 1024), 2)) + " GB"
elif kb > 1024:
return str(round(kb / 1024, 2)) + " MB"
# Emails list, open chrome dev tools and copy the response of the request
# API command: `GetAccountsInfoList``
f = open("response.xml", "r")
res = f.read()
f.close()
# IceWarp response namespaces
namespace = {"ns": "admin:iq:rpc"}
root = ET.fromstring(res)
items = root.findall(".//ns:item", namespace)
users = {}
users_names = {}
for item in items:
displayemail = item.find("ns:displayemail", namespace).text
name = item.find("ns:name", namespace).text
accountstate_state = item.find("ns:accountstate/ns:state", namespace).text
quota = item.find("ns:quota/ns:mailboxsize", namespace).text
# calculate quota if user is not enabled
if accountstate_state == "1":
users[displayemail] = int(quota)
users_names[displayemail] = name
users = {k: v for k, v in sorted(users.items(), key=lambda item: item[1], reverse=True)}
csv = "email,name,quota\n"
for i in users:
csv += i+","+users_names[i]+","+convert_quota(users[i])+"\n"
# write results to file
print("writing to file")
output = open("users-usage.csv", "w")
output.write(csv)
output.close()
print(csv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment