diff --git a/README.md b/README.md index a6d3f3d..4a493d4 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ slack-cleaner --token --message --group hr --user "*" slack-cleaner --token --message --direct sherry --user johndoe # Delete all messages from a multiparty direct message channel -slack-cleaner --token --message --mpdirect sherry,james --user "*" +slack-cleaner --token --message --mpdirect sherry,james --user "*" # Delete all messages from certain user slack-cleaner --token --message --channel gossip --user johndoe @@ -40,6 +40,9 @@ slack-cleaner --token --message --channel auto-build --bot # Delete all messages older than 2015/09/19 slack-cleaner --token --message --channel general --user "*" --before 20150919 +# Purge messages from all channels for a user +slack-cleaner --token --message --purge yes --user johndoe + # Always have a look at help message slack-cleaner --help ``` diff --git a/slack_cleaner/args.py b/slack_cleaner/args.py index 23f7883..4d7f0bc 100644 --- a/slack_cleaner/args.py +++ b/slack_cleaner/args.py @@ -28,6 +28,8 @@ def __init__(self): # Channel, DM or group g_chan = p.add_mutually_exclusive_group(required=True) + g_chan.add_argument('--purge', + help='Purge messages from all channels') g_chan.add_argument('--channel', help='Channel name\'s, e.g., general') g_chan.add_argument('--direct', @@ -61,11 +63,12 @@ def __init__(self): self.delete_message = args.message self.delete_file = args.file + self.purge_name = args.purge self.channel_name = args.channel self.direct_name = args.direct self.group_name = args.group self.mpdirect_name = args.mpdirect - + self.user_name = args.user self.bot = args.bot self.start_time = args.after diff --git a/slack_cleaner/cli.py b/slack_cleaner/cli.py index e788b9c..bd8bfc2 100644 --- a/slack_cleaner/cli.py +++ b/slack_cleaner/cli.py @@ -46,6 +46,9 @@ # User dict user_dict = {} +# Channel dict +channel_dict = {} + # Construct a local user dict for further usage def init_user_dict(): @@ -60,12 +63,29 @@ def init_user_dict(): # Init user dict init_user_dict() +# Construct a local channel dict for further usage +def init_channel_dict(): + res = slack.channels.list().body + if not res['ok']: + return + channels = res['channels'] + + for c in channels: + channel_dict[c['id']] = c['name'] + +# Init channel dict +init_channel_dict() def get_id_by_name(list_dict, key_name): for d in list_dict: if d['name'] == key_name: return d['id'] +def purge_channels(time_range, user_id=None, bot=False): + + for c in channel_dict: + print "Cleaning Channel: ",channel_dict[c] + clean_channel(c, time_range, user_id, args.bot) def clean_channel(channel_id, time_range, user_id=None, bot=False): # Setup time range for query @@ -74,7 +94,9 @@ def clean_channel(channel_id, time_range, user_id=None, bot=False): _api_end_point = None # Set to the right API end point - if args.channel_name: + if args.purge_name: + _api_end_point = slack.channels.history + elif args.channel_name: _api_end_point = slack.channels.history elif args.direct_name: _api_end_point = slack.im.history @@ -83,6 +105,7 @@ def clean_channel(channel_id, time_range, user_id=None, bot=False): elif args.mpdirect_name: _api_end_point = slack.mpim.history + has_more = True while has_more: res = _api_end_point(channel_id, latest, oldest).body @@ -169,12 +192,9 @@ def get_user_id_by_name(name): def get_channel_id_by_name(name): - res = slack.channels.list().body - if not res['ok']: - return - channels = res['channels'] - if len(channels) > 0: - return get_id_by_name(channels, name) + for k, v in channel_dict.iteritems(): + if v == name: + return k def get_direct_id_by_name(name): @@ -201,7 +221,7 @@ def get_mpdirect_id_by_name(name): if len(mpims) > 0: for mpim in mpims: - # match the mpdirect user ids + # match the mpdirect user ids if set(mpim['members']) == members: return mpim['id'] @@ -218,24 +238,6 @@ def message_cleaner(): _channel_id = None _user_id = None - # If channel's name is supplied - if args.channel_name: - _channel_id = get_channel_id_by_name(args.channel_name) - - # If DM's name is supplied - if args.direct_name: - _channel_id = get_direct_id_by_name(args.direct_name) - - # If channel's name is supplied - if args.group_name: - _channel_id = get_group_id_by_name(args.group_name) - - # If group DM's name is supplied - if args.mpdirect_name: - _channel_id = get_mpdirect_id_by_name(args.mpdirect_name) - - if _channel_id is None: - sys.exit('Channel, direct message or private group not found') # If user's name is also supplied if args.user_name: @@ -248,8 +250,31 @@ def message_cleaner(): if _user_id is None: sys.exit('User not found') - # Delete messages on certain channel - clean_channel(_channel_id, time_range, _user_id, args.bot) + if args.purge_name: + purge_channels(time_range, _user_id, args.bot) + else: + # If channel's name is supplied + if args.channel_name: + _channel_id = get_channel_id_by_name(args.channel_name) + + # If DM's name is supplied + if args.direct_name: + _channel_id = get_direct_id_by_name(args.direct_name) + + # If channel's name is supplied + if args.group_name: + _channel_id = get_group_id_by_name(args.group_name) + + # If group DM's name is supplied + if args.mpdirect_name: + _channel_id = get_mpdirect_id_by_name(args.mpdirect_name) + + if _channel_id is None: + sys.exit('Channel, direct message or private group not found') + + + # Delete messages on certain channel + clean_channel(_channel_id, time_range, _user_id, args.bot) def file_cleaner():