all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Li Ian-Xue <b4283@bephor.org>
To: 12051@debbugs.gnu.org
Subject: bug#12051: 24.1; rcirc-send-message doesn't take multibyte into account.
Date: Thu, 26 Jul 2012 00:18:29 +0800	[thread overview]
Message-ID: <87boj3byqy.fsf@acerpad.localdomain> (raw)

[-- Attachment #1: Type: text/plain, Size: 892 bytes --]


Hello developers,

I discovered recently that the irc client `rcirc', although has an
max-message-length set, but it simply uses (length str) for detecting
the output length, which is not desirable for multibyte users because
usually our characters encode to more than one byte, and this causes an
error that the client actually sends out more bytes than the standard
has required (512 bytes to my understanding).

This limit is easily reached since chinese characters are usually
encoded with 3 bytes for one character.

By this error, if the server truncates the result string simply by
bytes, then it's known to cause the string to become entirely scrambles
for xchat.

I'm attaching a patch to perform an binary search for multibyte strings,
and this patch should not have any penalties for original ascii users
since it begins with a (multibyte-string-p) to decide which style to use.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: rcirc-fix-multibyte-overflow.patch --]
[-- Type: text/x-patch, Size: 2136 bytes --]

--- rcirc.el	2012-07-25 23:52:41.813226461 +0800
+++ rcirc-1.el	2012-07-25 23:55:20.813220626 +0800
@@ -792,21 +792,40 @@
 (defvar rcirc-max-message-length 420
   "Messages longer than this value will be split.")
 
+(defun rcirc-multibyte-position-at-byte (str bytes)
+  (if (multibyte-string-p str)
+      (rcirc-multibyte-position-at-byte-1 str bytes 0 0)
+      bytes))
+
+(defun rcirc-multibyte-position-at-byte-1 (str bytes now-chars now-bytes)
+  (let ((len (length str)))
+    (if (<= len 1)
+        now-chars
+      (let* ((half-len (/ len 2))
+             (lstr (substring str 0 half-len))
+             (rstr (substring str half-len len))
+             (now-bytes-1 (+ now-bytes (string-bytes lstr))))
+        (if (> now-bytes-1 bytes)
+            (rcirc-multibyte-position-at-byte-1 lstr bytes now-chars now-bytes)
+          (rcirc-multibyte-position-at-byte-1 rstr bytes (+ half-len now-chars) now-bytes-1))))))
+
 (defun rcirc-send-message (process target message &optional noticep silent)
   "Send TARGET associated with PROCESS a privmsg with text MESSAGE.
 If NOTICEP is non-nil, send a notice instead of privmsg.
 If SILENT is non-nil, do not print the message in any irc buffer."
   ;; max message length is 512 including CRLF
   (let* ((response (if noticep "NOTICE" "PRIVMSG"))
-         (oversize (> (length message) rcirc-max-message-length))
+         (oversize (> (string-bytes message) rcirc-max-message-length))
+         (adjusted-pos (if oversize
+                            (rcirc-multibyte-position-at-byte message rcirc-max-message-length)))
          (text (if oversize
-                   (substring message 0 rcirc-max-message-length)
+                   (substring message 0 adjusted-pos)
                  message))
          (text (if (string= text "")
                    " "
                  text))
          (more (if oversize
-                   (substring message rcirc-max-message-length))))
+                   (substring message adjusted-pos))))
     (rcirc-get-buffer-create process target)
     (rcirc-send-string process (concat response " " target " :" text))
     (unless silent

             reply	other threads:[~2012-07-25 16:18 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-25 16:18 Li Ian-Xue [this message]
2012-08-13  1:53 ` bug#12051: 24.1; rcirc-send-message doesn't take multibyte into account Leo
2012-08-14 13:11   ` Leo
2012-08-14 15:45   ` Eli Zaretskii
     [not found]     ` <874no4apau.fsf@acerpad.localdomain>
2012-08-15  2:57       ` Eli Zaretskii
2012-08-15 13:10         ` Leo
2012-08-15 13:29           ` Chong Yidong
2012-08-15 15:45             ` Chong Yidong
2012-08-15 22:55               ` Leo
2012-08-15 16:59           ` Eli Zaretskii
2012-08-15 17:53             ` Li Ian-Xue
2012-08-15 22:54               ` Leo
2012-08-15 23:00             ` Leo
2012-08-16  2:50               ` Eli Zaretskii
2012-08-16  3:16                 ` Leo
2012-08-16 15:20                   ` Eli Zaretskii
2012-08-16 17:17                     ` Leo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87boj3byqy.fsf@acerpad.localdomain \
    --to=b4283@bephor.org \
    --cc=12051@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.