⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mlist2jgossip.java

📁 jGossip是一个简单而功能强大的Java论坛软件(消息板)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * $Id: MList2JGossip.java,v 1.1 2004/06/12 23:25:34 bel70 Exp $
 *
 * ***** BEGIN LICENSE BLOCK *****
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in 
 * compliance with the License. You may obtain a copy of the License 
 * at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and 
 * limitations under the License.
 *
 * The Original Code is JGossip forum code.
 *
 * The Initial Developer of the Original Code is the JResearch, Org. 
 * Portions created by the Initial Developer are Copyright (C) 2004 
 * the Initial Developer. All Rights Reserved. 
 * 
 * Contributor(s): 
 *              Tim Endres <timendres@users.sourceforge.net>
 *               .
 * * ***** END LICENSE BLOCK ***** */
/*
 * Created on 13.06.2004
 *
 */
package org.jresearch.gossip.contrib.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * This class will read a directory of MH mail messages, and insert those
 * messages into a jGossip forum. The messages will be sorted as best as
 * possible based on the Subjects.
 * 
 * The resulting SQL for MySQL is output to stdout. This can be processed using
 * the mysql command line client by passing the SQL into mysql's stdin.
 * 
 * Usage: MList2Gossip [options...] --archive path [--archive path]...
 * 
 * Options:
 * 
 * --archive path Path to a directory of MH mail messages to insert into a
 * forum. You can specify any number of archive options to process.
 * 
 * --forum id Id number of forum into which the messages will be inserted.
 * 
 * --sender name Name of sender of inserted messages.
 * 
 * --ipaddr id IP address of inserted messages.
 * 
 * --msgid id Starting id for inserted messages.
 * 
 * --threadid id Starting id for inserted threads.
 * 
 * --debug Turn on debugging.
 * 
 * --verbose Turn on verbosity.
 */

public class MList2JGossip {

    private static String VERSION_STR = "1.1";

    private boolean debug = false;

    private boolean verbose = false;

    private Session session = null;

    private int fForumId = 25;

    private int fThreadId = 100;

    private int fMsgId = 500;

    private String fSender = "anonymous";

    private String fIpAddress = "127.0.0.1";

    private int errDupes = 0;

    private HashMap allMsgs = null;

    private HashMap subjects = null;

    private HashMap bySubject = null;

    private HashMap byMsgId = null;

    private SimpleDateFormat dateFmt = null;

    public static void main(String[] argv) {
        MList2JGossip app = new MList2JGossip();
        app.instanceMain(argv);
    }

    public void instanceMain(String[] argv) {
        this.allMsgs = new HashMap(1024);
        this.subjects = new HashMap(1024);
        this.bySubject = new HashMap(1024);
        this.byMsgId = new HashMap(1024);

        this.dateFmt = new SimpleDateFormat("yyyyMMddHHmmss");

        this.session = Session.getDefaultInstance(System.getProperties(), null);

        this.processArguments(argv);

        try {
            this.processArchives();
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
        }
    }

    private String escapeString(String str) {
        StringBuffer buf = new StringBuffer();

        int chIdx = 0;
        int offset = 0;
        int strLen = str.length();
        for (chIdx = 0; chIdx < strLen; ++chIdx) {
            char ch = str.charAt(chIdx);

            if (ch == '\'') {
                buf.append("''");
            } else if (ch == '\\') {
                buf.append("\\\\");
            } else {
                buf.append(ch);
            }
        }

        return buf.toString();
    }

    private void processArchives() throws IOException, MessagingException {
        Iterator threads = this.bySubject.values().iterator();
        for (; threads.hasNext();) {
            boolean insertedThread = false;
            ArrayList thread = (ArrayList) threads.next();

            for (int ti = 0; ti < thread.size(); ++ti) {
                String msgId = (String) thread.get(ti);

                try {
                    MimeMessage msg = (MimeMessage) this.allMsgs.get(msgId);
                    String subject = msg.getSubject();
                    Date sentDate = msg.getSentDate();
                    String dateStr = this.dateFmt.format(sentDate);

                    if (msg != null) {
                        if (!insertedThread) {
                            ++this.fThreadId;
                            insertedThread = true;

                            System.out
                                    .println("INSERT INTO jrf_thread "
                                            + "( threadid, forumid, lintime, locked, sortby )");

                            System.out.println("   VALUES " + "( "
                                    + this.fThreadId + ", " + this.fForumId
                                    + ", " + "'" + dateStr + "', " + "0, "
                                    + "9" + " ); ");
                        }

                        if (subject == null) subject = "";

                        if (subject.length() > 252)
                                subject = subject.substring(0, 252);

                        subject = this.escapeString(subject);

                        String bodyStr = null;
                        Object bodyObj = msg.getContent();
                        if (bodyObj instanceof String) {
                            bodyStr = this.escapeString((String) bodyObj);
                        } else if (bodyObj instanceof MimeMultipart) {
                            System.err.println("MULTIPART [" + msgId + "] ");

                            MimeMultipart multi = (MimeMultipart) bodyObj;
                            int cnt = multi.getCount();
                            for (int bpi = 0; bpi < cnt; ++bpi) {
                                BodyPart bp = multi.getBodyPart(bpi);
                                if (bp instanceof MimeBodyPart) {
                                    MimeBodyPart mbp = (MimeBodyPart) bp;
                                    String cType = mbp.getContentType();
                                    if (cType.startsWith("text/")) {
                                        bodyObj = mbp.getContent();
                                        bodyStr = this
                                                .escapeString((String) bodyObj);
                                        break;
                                    } else {
                                        System.err.println("   SKIP " + cType);
                                    }
                                } else {
                                    System.err.println("MSG [" + msgId

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -