-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodifyNotificationCenterCasper.py
299 lines (250 loc) · 12.9 KB
/
modifyNotificationCenterCasper.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/python
import os, commands, sqlite3, sys, pwd, time, getopt, getpass, re
##########################################################################################
# Name the Casper Parameters:
# 4 Apps to effect (ALL, APPLE, NONAPPLE, NONSYSTEM, CASPER)
# 5 Alert Style (BANNER, ALERT, OFF)
# 6 Show Previews (ALWAYS, UNLOCKED, OFF)
# 7 Show on Lock Screen (ON, OFF)
# 8 Show in Notification Center: 0,1,5,10,20
# 9 Show Badges (ON, OFF)
# 10 Play Sounds (ON, OFF)
#
# When creating a policy in the JSS, be sure that every parameter has SOMETHING in it.
# For Example if you want to set all Notification Items to have the sound off but keep
# all other settings the same, you can put a dot or "Same" in the other parameters to keep
# them how the user has them.
#
# The policy cannot be triggered to run during startup, login or logout
# (and probably not during enrollment complete)
# It needs a logged in user to function.
#
##########################################################################################
#########################################################################################
# Get Current Finder User
#########################################################################################
CURRENT_SCRIPT_USERID = os.getuid()
CURRENT_SCRIPT_USERNAME = getpass.getuser()
CURRENT_FINDER_USERNAME = os.getlogin()
CURRENT_FINDER_USERID = pwd.getpwnam(CURRENT_FINDER_USERNAME).pw_uid
CURRENT_FINDER_HOME = pwd.getpwnam(CURRENT_FINDER_USERNAME).pw_dir
#########################################################################################
# Switch to Current Finder User to run the rest of the script
#########################################################################################
os.setuid(CURRENT_FINDER_USERID)
#########################################################################################
# Global Variables
#########################################################################################
_THIS_SCRIPT = str(sys.argv[0])
_DEBUG = False
_VERBOSE = True
APPS_IN_QUESTION = ""
APPS_LIST = []
ALERT_STYLE = "SAME"
SHOW_ON_LOCK = "SAME"
SHOW_IN_CENTER = "SAME"
RECENT_ITEMS = 0
BADGE_ICONS = "SAME"
SOUNDS = "SAME"
PREVIEWS = "SAME"
APPS_WHERE_DICT = { "ALL" : "",
"APPLE" : "WHERE bundleid LIKE 'COM.APPLE.%'",
"NONAPPLE": "WHERE bundleid NOT LIKE '%COM.APPLE.%' AND bundleid NOT LIKE '_SYSTEM_CENTER_%'",
"NONSYSTEM" : "WHERE bundleid NOT LIKE '_SYSTEM_CENTER_%'",
"CASPER" : "WHERE bundleid LIKE 'com.jamfsoftware.Management-Action'"
}
#########################################################################################
# Get the Current Finder User's Cache Folder
#########################################################################################
CURRENT_FINDER_CACHE_FOLDER = commands.getoutput("getconf DARWIN_USER_DIR")
NOTIFICATION_DATABASE = CURRENT_FINDER_CACHE_FOLDER + "com.apple.notificationcenter/db/db"
#########################################################################################
# Check for existance of Current Finder User's Cache Folder
#########################################################################################
if os.path.exists(NOTIFICATION_DATABASE):
pass
else:
if _DEBUG: print "Can not find Notification Database in /var/folders/ checking home folder"
CURRENT_FINDER_CACHE_FOLDER = CURRENT_FINDER_HOME + "/Library/Application Support/NotificationCenter/"
FILES = os.listdir(CURRENT_FINDER_CACHE_FOLDER)
for file in FILES:
if re.match("\d*.*\.db",file):
NOTIFICATION_DATABASE = CURRENT_FINDER_CACHE_FOLDER + file
if os.path.exists(NOTIFICATION_DATABASE):
if _DEBUG | _VERBOSE: print "Found Notification Database at " + NOTIFICATION_DATABASE
pass
else:
print "Can not find Notification Database."
exit(1)
#########################################################################################
# Connect to the Database and Pull App Bundle List
#########################################################################################
con = sqlite3.connect(NOTIFICATION_DATABASE)
cur = con.cursor()
cur.execute("SELECT bundleid FROM app_info")
BUNDLESTUP = cur.fetchall()
for bun in BUNDLESTUP:
APPS_LIST.append(bun[0].upper())
#########################################################################################
# Print some Debug info if needed
#########################################################################################
def SHOW_DEBUG(currentArgs):
if _DEBUG: print "\nCurrent options: %s" % str(currentArgs)
if _DEBUG: print "Database:\t", NOTIFICATION_DATABASE
if _DEBUG: print "Process Owner:\t", CURRENT_SCRIPT_USERNAME, CURRENT_SCRIPT_USERID
if _DEBUG: print "Finder User:\t", CURRENT_FINDER_USERNAME, CURRENT_FINDER_USERID
if _DEBUG: print "\n"
#########################################################################################
# Parse our arguments and options
#########################################################################################
if len(sys.argv) < 4:
print "Missing Options"
exit()
#EFFECT
if sys.argv[4].upper() in ("ALL","APPLE","NONAPPLE","NONSYSTEM","CASPER"):
APPS_IN_QUESTION = sys.argv[4].upper()
if _DEBUG | _VERBOSE: print "Modifying " + APPS_IN_QUESTION
elif sys.argv[4].upper() in APPS_LIST:
APPS_IN_QUESTION = sys.argv[4].upper()
if _DEBUG | _VERBOSE: print "Modifying " + APPS_IN_QUESTION
else:
print "\n\n****Missing or Incorrect Application"
exit(2)
#ALERT
try:
if sys.argv[5].upper() in ("BANNER","ALERT","OFF"):
ALERT_STYLE = sys.argv[5].upper()
if _DEBUG | _VERBOSE:print "Alert Style: " + ALERT_STYLE
except:
pass
#PREVIEWS
try:
if sys.argv[6].upper() in ("ALWAYS","UNLOCKED","OFF"):
PREVIEWS = sys.argv[6].upper()
if _DEBUG | _VERBOSE:print "Preview Style: " + PREVIEWS
except:
pass
#LOCK
try:
if sys.argv[7].upper() in ("ON","OFF"):
SHOW_ON_LOCK = sys.argv[7].upper()
if _DEBUG | _VERBOSE:print "Lock Screen: " + SHOW_ON_LOCK
except:
pass
#CENTER
try:
if sys.argv[8] == "0":
SHOW_IN_CENTER = "OFF"
if _DEBUG | _VERBOSE:print "Not Showing in Notification Center"
except:
pass
try:
if sys.argv[8] in ("1","5","10","20"):
SHOW_IN_CENTER = "ON"
RECENT_ITEMS = sys.argv[8]
if _DEBUG | _VERBOSE:print "Showing " + RECENT_ITEMS + " Recent Items in Notification Center "
except:
pass
#BADGE
try:
if sys.argv[9].upper() in ("ON","OFF"):
BADGE_ICONS = sys.argv[9].upper()
if _DEBUG | _VERBOSE:print "Badge Icons: " + BADGE_ICONS
except:
pass
#SOUND
try:
if sys.argv[10].upper() in ("ON","OFF"):
SOUNDS = sys.argv[10].upper()
if _DEBUG | _VERBOSE:print "Sounds: ", SOUNDS
except:
pass
#########################################################################################
# Figure out what App or Apps we are working with
#########################################################################################
if APPS_IN_QUESTION == "ALL": cur.execute("SELECT * FROM app_info %s"% APPS_WHERE_DICT["ALL"])
elif APPS_IN_QUESTION == "APPLE": cur.execute("SELECT * FROM app_info %s"% APPS_WHERE_DICT["APPLE"])
elif APPS_IN_QUESTION == "NONAPPLE": cur.execute("SELECT * FROM app_info %s"% APPS_WHERE_DICT["NONAPPLE"])
elif APPS_IN_QUESTION == "NONSYSTEM": cur.execute("SELECT * FROM app_info %s"% APPS_WHERE_DICT["NONSYSTEM"])
elif APPS_IN_QUESTION == "CASPER": cur.execute("SELECT * FROM app_info %s"% APPS_WHERE_DICT["CASPER"])
else: cur.execute("SELECT * FROM app_info WHERE bundleid LIKE '%s'"% APPS_IN_QUESTION)
WORKING_APP_LIST = cur.fetchall()
#########################################################################################
# Determine the current Settings
#########################################################################################
for workingApp in WORKING_APP_LIST:
currentBundleID = workingApp[1]
currentSettings = workingApp[2]
if _DEBUG: print "Bundle ID:\t\t" + currentBundleID + "\nCurrent Settings:\t" + str(currentSettings)
newSettings = currentSettings
# Find out what settings are currently on
currentSHOW_IN_CENTER = "ON" if currentSettings & 1 == 0 else "OFF"
currentBADGE_ICONS = "OFF" if currentSettings & 1 << 1 == 0 else "ON"
currentSOUNDS = "OFF" if currentSettings & 1 << 2 == 0 else "ON"
if (currentSettings & 1 << 3 == 0) and (currentSettings & 1 << 4 == 0):
currentALERT_STYLE = "OFF"
elif (currentSettings & 1 << 3 != 0) and (currentSettings & 1 << 4 == 0):
currentALERT_STYLE = "BANNER"
else:
currentALERT_STYLE = "ALERT"
currentSHOW_ON_LOCK = "ON" if currentSettings & 1 << 12 == 0 else "OFF"
if (currentSettings & (1 << 13) == 0) and (currentSettings & (1 << 14) != 0) :
currentSHOWPREVIEWS = "OFFUNLOCKED"
elif (currentSettings & (1 << 13) != 0) and (currentSettings & (1 << 14) != 0) :
currentSHOWPREVIEWS = "OFFALWAYS"
elif (currentSettings & 1 << 13 != 0) and (currentSettings & 1 << 14 == 0):
currentSHOWPREVIEWS = "ALWAYS"
elif (currentSettings & 1 << 13 == 0) and (currentSettings & 1 << 14 == 0):
currentSHOWPREVIEWS = "UNLOCKED"
if _DEBUG : print "Center: " + currentSHOW_IN_CENTER + "\nBadge: " + currentBADGE_ICONS + "\nSounds: " + currentSOUNDS + "\nAlert Style: " + currentALERT_STYLE + "\nLock: " + currentSHOW_ON_LOCK + "\nPreviews: " + currentSHOWPREVIEWS
#########################################################################################
# Make Changes
#########################################################################################
if SHOW_IN_CENTER == "OFF" and currentSHOW_IN_CENTER == "ON": newSettings = newSettings ^ 1
if SHOW_IN_CENTER == "ON" and currentSHOW_IN_CENTER == "OFF": newSettings = newSettings ^ 1
if BADGE_ICONS == "OFF" and currentBADGE_ICONS == "ON": newSettings = newSettings ^ 1 << 1
if BADGE_ICONS == "ON" and currentBADGE_ICONS == "OFF": newSettings = newSettings ^ 1 << 1
if SOUNDS == "OFF" and currentSOUNDS == "ON": newSettings = newSettings ^ (1 << 2)
if SOUNDS == "ON" and currentSOUNDS == "OFF": newSettings = newSettings ^ (1 << 2)
if SHOW_ON_LOCK == "OFF" and currentSHOW_ON_LOCK == "ON": newSettings = newSettings ^ (1 << 12)
if SHOW_ON_LOCK == "ON" and currentSHOW_ON_LOCK == "OFF": newSettings = newSettings ^ (1 << 12)
if ALERT_STYLE == "OFF" and currentALERT_STYLE == "BANNER": newSettings = newSettings ^ (1 << 3)
if ALERT_STYLE == "OFF" and currentALERT_STYLE == "ALERT": newSettings = newSettings ^ (1 << 4)
if ALERT_STYLE == "BANNER" and currentALERT_STYLE == "ALERT":
newSettings = newSettings ^ (1 << 3)
newSettings = newSettings ^ (1 << 4)
if ALERT_STYLE == "BANNER" and currentALERT_STYLE == "OFF":
newSettings = newSettings ^ (1 << 3)
if ALERT_STYLE == "ALERT" and currentALERT_STYLE == "BANNER":
newSettings = newSettings ^ (1 << 3)
newSettings = newSettings ^ (1 << 4)
if ALERT_STYLE == "ALERT" and currentALERT_STYLE == "OFF":
newSettings = newSettings ^ (1 << 4)
if PREVIEWS == "OFF" and currentSHOWPREVIEWS != "OFF": newSettings = newSettings ^ (1 << 14)
if PREVIEWS == "UNLOCKED" and currentSHOWPREVIEWS == "OFFUNLOCKED" : newSettings = newSettings ^ (1 << 14)
if PREVIEWS == "UNLOCKED" and currentSHOWPREVIEWS == "OFFALWAYS" :
newSettings = newSettings ^ (1 << 14)
newSettings = newSettings ^ (1 << 13)
if PREVIEWS == "ALWAYS" and currentSHOWPREVIEWS == "OFFALWAYS" : newSettings = newSettings ^ (1 << 14)
if PREVIEWS == "ALWAYS" and currentSHOWPREVIEWS == "OFFUNLOCKED" :
newSettings = newSettings ^ (1 << 14)
newSettings = newSettings ^ (1 << 13)
if _DEBUG: print "New Settings: ", newSettings
#########################################################################################
# Commit Changes to Database
#########################################################################################
cur.execute("UPDATE app_info SET flags = %s WHERE bundleid LIKE '%s'"% (newSettings,currentBundleID))
if RECENT_ITEMS != 0 : cur.execute("UPDATE app_info SET show_count = %s WHERE bundleid LIKE '%s'"% (RECENT_ITEMS,currentBundleID))
#########################################################################################
# Close Connection to the Database
#########################################################################################
con.commit()
con.close()
#########################################################################################
# Unload and Reload the usernoted and notificationcenterui
#########################################################################################
commands.getoutput("launchctl unload /System/Library/LaunchAgents/com.apple.usernoted.plist")
commands.getoutput("launchctl unload /System/Library/LaunchAgents/com.apple.notificationcenterui.plist")
time.sleep(2)
commands.getoutput("launchctl load /System/Library/LaunchAgents/com.apple.usernoted.plist")
commands.getoutput("launchctl load /System/Library/LaunchAgents/com.apple.notificationcenterui.plist")