-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathGetNonDomainFilterForwards.py
More file actions
executable file
·46 lines (41 loc) · 1.78 KB
/
GetNonDomainFilterForwards.py
File metadata and controls
executable file
·46 lines (41 loc) · 1.78 KB
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
#!/usr/bin/env python
"""
# Purpose: For Gmail User(s), list/delete all filters that forward email outside of a list of specified domains
# Note: This script can use basic GAM: https://github.com/jay0lee/GAM or advanced GAM: https://github.com/taers232c/GAMADV-X
# Usage:
# 1: Get filters, if you don't want all users, replace all users with your user selection in the command below
# $ Example, basic GAM: gam all users print filters > filters.csv
# $ Example, advanced GAM: gam config auto_batch_min 1 redirect csv ./filters.csv multiprocess all users print filters
# 2: From that list of filters, output a CSV file that lists the filters that forward email outside of the specified domains.
# $ python GetNonDomainFilterForwards.py filters.csv outsidefilters.csv
# 3: Inspect outsidefilters.csv, verify that it makes sense and then proceed
# 4: Delete the filters
# $ gam csv outsidefilters.csv gam user "~User" delete filter "~id"
"""
import csv
import re
import sys
forward_domain = re.compile(r"^forward .*@(.*)$")
# Substitute your domain(s) in the list below, e.g., domainList = ['domain.com',] domainList = ['domain1.com', 'domain2.com',]
domainList = []
if (len(sys.argv) > 2) and (sys.argv[2] != '-'):
outputFile = open(sys.argv[2], 'wb')
else:
outputFile = sys.stdout
if (len(sys.argv) > 1) and (sys.argv[1] != '-'):
inputFile = open(sys.argv[1], 'rb')
else:
inputFile = sys.stdin
filters = csv.DictReader(inputFile)
outsidefilters = csv.DictWriter(outputFile, filters.fieldnames, lineterminator='\n')
outsidefilters.writeheader()
for row in filters:
v = row.get('forward', '')
if v:
mg = forward_domain.match(v)
if mg and mg.group(1) not in domainList:
outsidefilters.writerow(row)
if inputFile != sys.stdin:
inputFile.close()
if outputFile != sys.stdout:
outputFile.close()