# mbox 가져오기: charset=windows-1252가 ?(물음표)로 표시됩니다

**URL:** https://meta.discourse.org/t/import-mbox-maps-charset-windows-1252-to/168908
**Category:** Support
**Created:** [11월 1, 2020, 10:12오전 UTC](https://meta.discourse.org/t/import-mbox-maps-charset-windows-1252-to/168908 "2020-11-01T10:12:07Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![dachary](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dachary/32/119731_2.png) [@dachary](https://meta.discourse.org/u/dachary)
#### Post date: [11월 1, 2020, 10:12오전 UTC](https://meta.discourse.org/t/import-mbox-maps-charset-windows-1252-to/168908/1 "2020-11-01T10:12:07Z")

</div>

안녕하세요,

이 메시지를 포함하고 있는 [mbox 파일을 가져오는](https://meta.discourse.org/t/importing-mailing-lists-mbox-listserv-google-groups-emails/79773) 과정에서

[windows.txt](https://meta.discourse.org/uploads/short-url/6ae5Bvsh1jSybVunvlYB3omfT29.txt) (3.7 KB)

다음과 같이 표시됩니다:

 ![fail](https://global.discourse-cdn.com/meta/original/3X/5/e/5ef8e1322600728deb4e6d6e00a69470f6b3144b.png)

아래와 같은 인코딩이 포함되어 있기 때문에 인코딩 문제일 가능성이 높습니다:

```plaintext
Content-Type: text/plain; charset=windows-1252; format=flowed

```

**iso-8859-1** 과 같은 UTF-8이 아닌 다른 문자 인코딩을 가진 메시지는 정상적으로 가져와집니다.

[script/import\_scripts/mbox/support/indexer.rb](https://github.com/discourse/discourse/blob/master/script/import_scripts/mbox/support/indexer.rb#L198)부터 소스를 탐색하여 문제의 근본 원인을 파악해 보기 전에, 혹시 이 문제를 알고 계신 분이 계신가요? 이는 코드베이스가 아닌 환경적인 문제일 수도 있나요? 메일링 리스트 모드로 실행 중인 사용자가 이 인코딩으로 답장을 보낼 때에도 동일한 문제가 발생하는가요?

미리 감사드립니다 🙂

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [11월 1, 2020, 8:39오후 UTC](https://meta.discourse.org/t/import-mbox-maps-charset-windows-1252-to/168908/2 "2020-11-01T20:39:17Z")

</div>

간단한 테스트를 해보았는데 `Email::Receiver`는 정상적으로 작동하는 것 같습니다. 입력을 UTF-8로 변환합니다. 이후 인코딩이 왜 잘못될 수 있는지 이유를 찾을 수 없습니다.

```ruby
[1] pry(main)> raw_email = File.read("/tmp/windows.txt");
[2] pry(main)> receiver = Email::Receiver.new(raw_email, convert_plaintext: true, skip_trimming: false);
[3] pry(main)> body = receiver.select_body;
[4] pry(main)> receiver.mail.charset
=> "windows-1252"
[5] pry(main)> body.first.encoding
=> #<Encoding:UTF-8>
[6] pry(main)> puts body.first;
cette réflexion me fait penser : y\-a\-il une obligation/raison \(en dehors du coup de maintenannce\) à avoir un même outil pour les 2 fonctionnalités \(interactions vs galerie\) ?

```

---

<div class="post-metadata">

### Author: ![dachary](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dachary/32/119731_2.png) [@dachary](https://meta.discourse.org/u/dachary)
#### Post date: [11월 1, 2020, 8:47오후 UTC](https://meta.discourse.org/t/import-mbox-maps-charset-windows-1252-to/168908/3 "2020-11-01T20:47:27Z")

</div>

빠른 테스트를 해주셔서 감사합니다. 제가 직접 해보는 법을 몰랐습니다 🙂 가져오기 컨테이너에 뭔가 누락된 건 아닌가요? 여러분이 하신 내용을 재현해서 거기서부터 탐색해 보고 싶습니다. 만약 아무것도 찾지 못하면, 이 메일 하나만 포함된 받은편지함으로 mbox 가져오기 절차를 사용하여 문제를 재현하는 방법을 안내해 드리겠습니다.

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [11월 1, 2020, 8:52오후 UTC](https://meta.discourse.org/t/import-mbox-maps-charset-windows-1252-to/168908/4 "2020-11-01T20:52:11Z")

</div>

컨테이너에서 `rails console`을 실행해 직접 사용해 볼 수 있습니다.

---

<div class="post-metadata">

### Author: ![dachary](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dachary/32/119731_2.png) [@dachary](https://meta.discourse.org/u/dachary)
#### Post date: [11월 1, 2020, 8:58오후 UTC](https://meta.discourse.org/t/import-mbox-maps-charset-windows-1252-to/168908/5 "2020-11-01T20:58:56Z")

</div>

저도 동일한 결과를 얻었으니 문제는 거기에 있지 않습니다. 이것이 어떤 부수적인 영향인지 확인하기 위해, 이 메일만 가지고 새 카테고리로 가져오기를 실행해 보겠습니다.

---

<div class="post-metadata">

### Author: ![dachary](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dachary/32/119731_2.png) [@dachary](https://meta.discourse.org/u/dachary)
#### Post date: [11월 1, 2020, 9:09오후 UTC](https://meta.discourse.org/t/import-mbox-maps-charset-windows-1252-to/168908/6 "2020-11-01T21:09:46Z")

</div>

2.5.4 설치 환경에서 제가 수행한 작업은 다음과 같습니다:

- `shared/standalone/import/settings.yml` 파일은 수정하지 않았습니다.
- 이전 가져오기(import)에서 생성된 `shared/standalone/import/data/index.db` 파일을 삭제했습니다.
- **Message-ID:** 헤더를 변경했습니다.
- [windows.txt](https://meta.discourse.org/uploads/short-url/6ae5Bvsh1jSybVunvlYB3omfT29.txt) 파일을 `shared/standalone/import/data/windows4/windows.mbox`로 복사했습니다.
- `./launcher enter import` 명령을 실행했습니다.
- 다음 명령으로 가져오기를 실행했습니다.

```plaintext
root@forum:/var/www/discourse# import_mbox.sh 
The mbox import is starting...

Loading existing groups...
Loading existing users...
Loading existing categories...
Loading existing posts...
Loading existing topics...

creating index
indexing files in /shared/import/data/windows4
indexing /shared/import/data/windows4/windows.mbox

indexing replies and users

creating categories
        1 / 1 (100.0%) [8121278 items/min]  
creating users
Skipping 1 already imported users

creating topics and posts
        1 / 1 (100.0%) [219 items/min]  

Updating topic status

Updating bumped_at on topics

Updating last posted at on users

Updating last seen at on users

Updating first_post_created_at...

Updating user post_count...

Updating user topic_count...

Updating topic users

Updating post timings

Updating featured topic users

Updating featured topics in categories
        9 / 9 (100.0%) [1562 items/min] ]  
Resetting topic counters

Done (00h 00min 09sec)

```

- 위에서 언급한 것과 동일한 결과가 나왔으며, 이는 [여기](https://forum.the.re/t/spip-dev-refonte-mediabox-wip/17338)에서 확인할 수 있습니다.

---

<div class="post-metadata">

### Author: ![dachary](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dachary/32/119731_2.png) [@dachary](https://meta.discourse.org/u/dachary)
#### Post date: [11월 1, 2020, 10:05오후 UTC](https://meta.discourse.org/t/import-mbox-maps-charset-windows-1252-to/168908/7 "2020-11-01T22:05:47Z")

</div>

**Email::Receiver** 가 가져오기(importer)에서 [같은 방식으로 호출되지 않기 때문일까요?](https://github.com/discourse/discourse/blob/master/script/import_scripts/mbox/importer.rb#L131-L146)

> Email::Receiver.new(row[‘raw\_message’])

대신에

> receiver = Email::Receiver.new(raw\_email, convert\_plaintext: true, skip\_trimming: false);

---

<div class="post-metadata">

### Author: ![dachary](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dachary/32/119731_2.png) [@dachary](https://meta.discourse.org/u/dachary)
#### Post date: [11월 2, 2020, 11:25오전 UTC](https://meta.discourse.org/t/import-mbox-maps-charset-windows-1252-to/168908/8 "2020-11-02T11:25:28Z")

</div>

또는 mbox 파일에서 메시지가 추출되는 방식에 차이가 있을 수 있습니다. 여기가 코드 경로가 다른 부분입니다. 위의 `raw_email = File.read("/tmp/windows.mbox")`는 [정규 표현식으로 파일을 분할하는 방식](https://github.com/discourse/discourse/blob/master/script/import_scripts/mbox/support/indexer.rb#L118-L145)과 다르며, 아마도 문제가 여기서 발생했을 가능성이 있습니다.

---

<div class="post-metadata">

### Author: ![dachary](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dachary/32/119731_2.png) [@dachary](https://meta.discourse.org/u/dachary)
#### Post date: [11월 2, 2020, 11:41오전 UTC](https://meta.discourse.org/t/import-mbox-maps-charset-windows-1252-to/168908/9 "2020-11-02T11:41:18Z")

</div>

실제로, [이 라인](https://github.com/discourse/discourse/blob/master/script/import_scripts/mbox/support/indexer.rb#L72) 뒤에 `File.open('/tmp/message.txt', 'w') { |file| file.write(receiver.raw_email) }`를 추가하면 원본 파일과 **다르다** 는 것을 알 수 있는 다음과 같은 파일이 생성됩니다.

[message.txt](https://meta.discourse.org/uploads/short-url/tCFH8T6bjd8eXi4LmcVGK6eqmcq.txt) (3.7 KB)

레일스 콘솔에서 실행할 경우에도 `receiver.raw_email`은 원본 파일과 다릅니다: UTF-8로 올바르게 인코딩되어 있습니다.

이러한 잘못된 수정이 어디서 일어나는지 아이디어가 있으신가요?

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [11월 2, 2020, 12:32오후 UTC](https://meta.discourse.org/t/import-mbox-maps-charset-windows-1252-to/168908/10 "2020-11-02T12:32:20Z")

</div>

이메일 파일의 인코딩을 Ruby에 알리기 위해 파일을 읽은 후 `.force_encoding`을 호출하는 것이 필요할 수 있습니다.

---

<div class="post-metadata">

### Author: ![dachary](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dachary/32/119731_2.png) [@dachary](https://meta.discourse.org/u/dachary)
#### Post date: [11월 2, 2020, 12:42오후 UTC](https://meta.discourse.org/t/import-mbox-maps-charset-windows-1252-to/168908/11 "2020-11-02T12:42:22Z")

</div>

초보자의 질문일 수 있어 죄송하지만, 코드베이스에 익숙하지 않습니다. 🙂 이런 변경이 어떤 부분에서 도움이 될지 제안해 주실 수 있을까요?

---

<div class="post-metadata">

### Author: ![dachary](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dachary/32/119731_2.png) [@dachary](https://meta.discourse.org/u/dachary)
#### Post date: [11월 2, 2020, 12:56오후 UTC](https://meta.discourse.org/t/import-mbox-maps-charset-windows-1252-to/168908/12 "2020-11-02T12:56:00Z")

</div>

불필요한 변환이 발생하는 위치를 좁혀본 결과, 여기인 것 같습니다:

> <https://github.com/discourse/discourse/blob/main/script/import_scripts/mbox/support/indexer.rb#L163-L165>

[line.scrub](https://ruby-doc.org/core-2.4.0/String.html#method-i-scrub)가 원본과 다른 내용으로 변환되도록 하는 책임이 있습니다. 이 부분을 제거하면 정규 표현식이 다음과 같은 오류를 발생시킵니다:

```plaintext
...
         1: from /var/www/discourse/script/import_scripts/mbox/support/indexer.rb:174:in `block in each_mail'                                                                         
/var/www/discourse/script/import_scripts/mbox/support/indexer.rb:174:in `=~': invalid byte sequence in UTF-8 (ArgumentError)

```

실제로 UTF-8이 아니기 때문이죠 🙂

이 문제를 어떻게 해결해야 할지 아이디어가 있으신가요? 아마 메일 헤더에 대해 문자 집합(charset)만 먼저 확인하는 패스를 거치는 것이 좋을지도 모르겠습니다. 여기에는 달걀과 닭 문제(🐔 과 🥚)가 있는 것 같습니다.

---

<div class="post-metadata">

### Author: ![dachary](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dachary/32/119731_2.png) [@dachary](https://meta.discourse.org/u/dachary)
#### Post date: [11월 2, 2020, 1:15오후 UTC](https://meta.discourse.org/t/import-mbox-maps-charset-windows-1252-to/168908/13 "2020-11-02T13:15:32Z")

</div>

> <https://github.com/discourse/discourse/blob/main/script/import_scripts/mbox/support/indexer.rb#L163-L165>

다음 코드를

```plaintext

    line = line.scrub

    if line =~ @split_regex

```

다음 코드로

```plaintext
    if line.scrub =~ @split_regex

```

교체하면 동작하는 것 같습니다:

 ![good](https://global.discourse-cdn.com/meta/original/3X/0/6/06a74e01d6964e7ea1d676d9d8e1b5c96b93a59d.png)

하지만 이것이 올바른 수정 방법인지 확실하지 않습니다.

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [11월 2, 2020, 1:50오후 UTC](https://meta.discourse.org/t/import-mbox-maps-charset-windows-1252-to/168908/14 "2020-11-02T13:50:20Z")

</div>

문제를 해결하는 완벽한 방법인 것 같아요.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [12월 2, 2020, 2:03오후 UTC](https://meta.discourse.org/t/import-mbox-maps-charset-windows-1252-to/168908/15 "2020-12-02T14:03:02Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
