Browse Source

Fix wordWrap function so it doesn't drop chars, and fix client.Notice() to automagically split very long lines.

tags/v0.8.0
Daniel Oaks 7 years ago
parent
commit
d847d55c06
2 changed files with 27 additions and 7 deletions
  1. 9
    2
      irc/client.go
  2. 18
    5
      irc/server.go

+ 9
- 2
irc/client.go View File

623
 }
623
 }
624
 
624
 
625
 // Notice sends the client a notice from the server.
625
 // Notice sends the client a notice from the server.
626
-//TODO(dan): Make this handle message splitting.
627
 func (client *Client) Notice(text string) {
626
 func (client *Client) Notice(text string) {
628
-	client.Send(nil, client.server.name, "NOTICE", client.nick, text)
627
+	limit := 400
628
+	if client.capabilities[MaxLine] {
629
+		limit = client.server.limits.LineLen.Rest - 110
630
+	}
631
+	lines := wordWrap(text, limit)
632
+
633
+	for _, line := range lines {
634
+		client.Send(nil, client.server.name, "NOTICE", client.nick, line)
635
+	}
629
 }
636
 }

+ 18
- 5
irc/server.go View File

886
 	return false
886
 	return false
887
 }
887
 }
888
 
888
 
889
+// wordWrap wraps the given text into a series of lines that don't exceed lineWidth characters.
889
 func wordWrap(text string, lineWidth int) []string {
890
 func wordWrap(text string, lineWidth int) []string {
890
 	var lines []string
891
 	var lines []string
891
 	var cacheLine, cacheWord string
892
 	var cacheLine, cacheWord string
892
 
893
 
893
 	for _, char := range text {
894
 	for _, char := range text {
894
-		if (char == ' ' || char == '-') && len(cacheLine)+len(cacheWord)+1 < lineWidth {
895
+		if char == '\r' {
896
+			continue
897
+		} else if char == '\n' {
898
+			cacheLine += cacheWord
899
+			lines = append(lines, cacheLine)
900
+			cacheWord = ""
901
+			cacheLine = ""
902
+		} else if (char == ' ' || char == '-') && len(cacheLine)+len(cacheWord)+1 < lineWidth {
903
+			// natural word boundary
895
 			cacheLine += cacheWord + string(char)
904
 			cacheLine += cacheWord + string(char)
896
 			cacheWord = ""
905
 			cacheWord = ""
897
-		} else if len(cacheLine)+len(cacheWord)+1 >= lineWidth {
906
+		} else if lineWidth <= len(cacheLine)+len(cacheWord)+1 {
907
+			// time to wrap to next line
898
 			if len(cacheLine) < (lineWidth / 2) {
908
 			if len(cacheLine) < (lineWidth / 2) {
899
-				// there must be a really long word or something, just split on word boundary
909
+				// this word takes up more than half a line... just split in the middle of the word
900
 				cacheLine += cacheWord + string(char)
910
 				cacheLine += cacheWord + string(char)
901
 				cacheWord = ""
911
 				cacheWord = ""
912
+			} else {
913
+				cacheWord += string(char)
902
 			}
914
 			}
903
 			lines = append(lines, cacheLine)
915
 			lines = append(lines, cacheLine)
904
 			cacheLine = ""
916
 			cacheLine = ""
905
 		} else {
917
 		} else {
918
+			// normal character
906
 			cacheWord += string(char)
919
 			cacheWord += string(char)
907
 		}
920
 		}
908
 	}
921
 	}
909
-	if len(cacheWord) > 0 {
922
+	if 0 < len(cacheWord) {
910
 		cacheLine += cacheWord
923
 		cacheLine += cacheWord
911
 	}
924
 	}
912
-	if len(cacheLine) > 0 {
925
+	if 0 < len(cacheLine) {
913
 		lines = append(lines, cacheLine)
926
 		lines = append(lines, cacheLine)
914
 	}
927
 	}
915
 
928
 

Loading…
Cancel
Save