Skip to content

Instantly share code, notes, and snippets.

@i8degrees
Created July 22, 2017 14:28
Show Gist options
  • Save i8degrees/f0e3ac6e88c2a57ced8ade75a10fe457 to your computer and use it in GitHub Desktop.
Save i8degrees/f0e3ac6e88c2a57ced8ade75a10fe457 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# fix-imessage.sh:jeff
#
# http://www.tonymacx86.com/threads/how-to-fix-imessage.110471/#TOP3.3
# Execute a command
#
# This function supports "dry-run" execution; set the variable NOM_DRY_RUN
# before execution of this function to enable it.
function exec_cmd() {
local RET_CODE=-1
EXEC_CMD=$*
# Function argument list is empty
if [[ -z "${EXEC_CMD}" ]]; then
return "${RET_CODE}"
fi
# Fire in the hole!
if [[ -n "${NOM_DRY_RUN}" ]]; then
echo "DRY RUN: ${EXEC_CMD}"
RET_CODE=0
else
${EXEC_CMD}
RET_CODE=$?
fi
# if [[ "${RET_CODE}" != 0 ]]; then
# echo -e "ERROR: Failure to execute input: \n\t${EXEC_CMD}\n"
# # EX_UNAVAILABLE (69) as per man 3 sysexits (BSD)
# # exit 69
# exit "${RET_CODE}"
# fi
return "${RET_CODE}"
}
function term_daemons() {
local PATH
PATH=/usr/bin
exec_cmd "killall Messages"
exec_cmd "killall identityserviced"
exec_cmd "killall imagent"
exec_cmd "killall securityd"
exec_cmd "killall securityd_service"
}
function clean_caches() {
local PATH
local CACHE_DIR
PATH=/bin
CACHE_DIR=${HOME}/Library/Caches
exec_cmd "cd ${CACHE_DIR}"
if [[ $? -ne 0 ]]; then
echo "ERROR: Could not change directory to ${CACHE_DIR}."
return 1
fi
exec_cmd "rm -rfv com.apple.iCloudHelper*"
exec_cmd "rm -rfv com.apple.Messages*"
exec_cmd "rm -rfv com.apple.imfoundation.IMRemoteURLConnectionAgent*"
}
function clean_settings() {
local PATH
local PREFS_DIR
PATH=/bin:/usr/bin
PREFS_DIR=${HOME}/Library/Preferences
exec_cmd "cd ${PREFS_DIR}"
if [[ $? -ne 0 ]]; then
echo "ERROR: Could not change directory to ${PREFS_DIR}."
return 1
fi
exec_cmd "rm -rfv com.apple.iChat.*"
exec_cmd "rm -rfv com.apple.icloud.*"
exec_cmd "rm -rfv com.apple.imagent.*"
exec_cmd "rm -rfv com.apple.imessage.*"
exec_cmd "rm -rfv com.apple.imservice.*"
exec_cmd "rm -rfv com.apple.ids.service*"
exec_cmd "rm -rfv com.apple.madrid.plist*"
exec_cmd "rm -rfv com.apple.imessage.bag.plist.*"
exec_cmd "com.apple.identityserviced*"
exec_cmd "com.apple.security.*"
}
function clean_trash() {
local PATH
local TRASH_DIR
PATH=/bin:/usr/bin
TRASH_DIR=${HOME}/.Trash
exec_cmd "cd ${TRASH_DIR}"
if [[ $? -ne 0 ]]; then
echo "ERROR: Could not change directory to ${TRASH_DIR}."
return 1
fi
exec_cmd "rm -rf *"
}
# Enable dry-run mode by uncommenting the following line.
# NOM_DRY_RUN=1
# The iMessage clean up job consists of the following tasks:
#
# 1. Terminate daemons that are associated with iMessage.
# 2. Remove cache files that are associated with iMessage.
# 3. Remove preferences that are associated with iMessage.
# 4. Empty the trash bin.
#
# As absurd as it sounds, I've had it resolve my own issues many times now.
echo "This script bases its file removal off the currently logged in user, "
echo "and must be ran as the user with the iMessage problem."
echo -e
echo "Is $(whoami) this user? (y/N)"
read -r LOGGED_USER
if [[ $LOGGED_USER = "y" || $LOGGED_USER = "Y" ]]; then
echo "Terminating daemons associated with iMessage..."
term_daemons
echo "Removing user cached files associated with iMessage..."
clean_caches
echo "Removing user settings associated with iMessage..."
clean_settings
# echo "Emptying the user's trash bin..."
# clean_trash
echo "All done!"
else
echo "Terminating without repair. Re-run this program as the user with the problem."
fi
echo "You should empty your trash bin to ensure removal of everything associated with iMessage."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment