From 0128502950ac17da7f5992d57fc2d9d261f25de2 Mon Sep 17 00:00:00 2001
From: Jer Noble <jer.noble@apple.com>
Date: Tue, 26 Mar 2013 11:35:07 -0700
Subject: [PATCH] Retain existing nickname suffixes when adding idle suffix.

---
 BetterAway/BetterAway/TPIBetterAway.m | 32 +++++++++++++++++++++------
 1 file changed, 25 insertions(+), 7 deletions(-)

diff --git a/BetterAway/BetterAway/TPIBetterAway.m b/BetterAway/BetterAway/TPIBetterAway.m
index e9c1b8e..b33f610 100644
--- a/BetterAway/BetterAway/TPIBetterAway.m
+++ b/BetterAway/BetterAway/TPIBetterAway.m
@@ -44,20 +44,38 @@ static NSString* kBetterAwayIdleCommand = @"betteraway";
     [_idleSuffix release];
     _idleSuffix = idleSuffix;
     [[NSUserDefaults standardUserDefaults] setObject:idleSuffix forKey:kBetterAwayIdleSuffix];
+
+    for (IRCClient* client in _world.clients) {
+        NSMutableString* nick = [NSMutableString stringWithString:client.localNickname];
+        NSString* suffix = [@"|" stringByAppendingString:[self idleSuffix]];
+        [nick replaceOccurrencesOfString:suffix withString:@"" options:0 range:NSMakeRange(0, [nick length])];
+        [client changeNick:nick];
+    }
 }
 
 #pragma mark -- NSWorkspace Notifications --
 
 - (void)onDisplaySleep:(id)sender
 {
-    for (IRCClient* client in _world.clients)
-        [client changeNick:[NSString stringWithFormat:@"%@|%@", client.config.nick, [self idleSuffix]]];
+    for (IRCClient* client in _world.clients) {
+        NSMutableString* nick = [NSMutableString stringWithString:client.localNickname];
+        NSString* suffix = [@"|" stringByAppendingString:[self idleSuffix]];
+        [nick replaceOccurrencesOfString:suffix withString:@"" options:0 range:NSMakeRange(0, [nick length])];
+        [nick appendString:suffix];
+        [client printDebugInformation:[@"BetterAway setting nickname to " stringByAppendingString:nick]];
+        [client changeNick:nick];
+    }
 }
 
 - (void)onDisplayWake:(id)sender
 {
-    for (IRCClient* client in _world.clients)
-        [client changeNick:client.config.nick];
+    for (IRCClient* client in _world.clients) {
+        NSMutableString* nick = [NSMutableString stringWithString:client.localNickname];
+        NSString* suffix = [@"|" stringByAppendingString:[self idleSuffix]];
+        [nick replaceOccurrencesOfString:suffix withString:@"" options:0 range:NSMakeRange(0, [nick length])];
+        [client printDebugInformation:[@"BetterAway setting nickname to " stringByAppendingString:nick]];
+        [client changeNick:nick];
+    }
 }
 
 #pragma mark -- Plugin API --
@@ -70,13 +88,13 @@ static NSString* kBetterAwayIdleCommand = @"betteraway";
 				  message:(NSString *)messageString
 				  command:(NSString *)commandString
 {
-    if ([[commandString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualTo:kBetterAwayIdleCommand]) {
+    if ([[commandString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] compare:kBetterAwayIdleCommand options:NSCaseInsensitiveSearch] == NSOrderedSame) {
         NSString* idleSuffix = [messageString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
         if (![idleSuffix length])
-            [client printBoth:client.world.selectedChannel type:TVCLogLineSystemType text:[@"Idle suffix is currently " stringByAppendingString:[self idleSuffix]]];
+            [client printDebugInformation:[@"Idle suffix is currently " stringByAppendingString:[self idleSuffix]]];
         else {
             [self setIdleSuffix:idleSuffix];
-            [client printBoth:client.world.selectedChannel type:TVCLogLineSystemType text:[@"Set idle suffix to " stringByAppendingString:idleSuffix]];
+            [client printDebugInformation:[@"Set idle suffix to " stringByAppendingString:idleSuffix]];
         }
     }
 }
-- 
2.40.1