PHP iChat到Adium Chat Transcript Converter

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP iChat到Adium Chat Transcript Converter相关的知识,希望对你有一定的参考价值。

<?php

  // This is the most inefficient piece of shit you will ever see

  require_once('Date.php');

  $base = getcwd();
  $infile = $base . '/iChat Export.txt';
  $outdir = $base . '/Converted Transcripts';
  $myscreenname = 'theshane100';
  $myname = 'Shaun Chapman';
  $abort = false;

  // Create the output directory if it doesn't exist
  if (!file_exists($outdir)) {
    mkdir($outdir);
  }

  $file = file_get_contents($infile);
  $lines = explode("\r", $file);

  $last_alias = '';

  foreach ($lines as $line) {
    $pieces = explode("\t", $line);
    $count = count($pieces);
    $valid = false;

    if ($count == 5) {
      $valid = true;
    } elseif ($count == 4) {
      // Likely a return
      $pieces[4] = "\n";
      $valid = true;
    }

    if ($valid) {
      $alias = $pieces[0];
      $info['sender'] = $pieces[1];
      $info['time'] = date('Y-m-d', strtotime($pieces[2])) . "T{$pieces[3]}-05:00";
      $info['message'] = $pieces[4];

      if ($info['sender'] == $myscreenname) {
        $info['alias'] = $myname;
      } else {
        $info['alias'] = $alias;
      }

      $alias_arr[$alias][] = $info;
    }
  }


  // Build the dictionary
  foreach ($alias_arr as $messages) {
    foreach ($messages as $message) {
      if (!isset($dictionary[$message['alias']])) {
        $dictionary[$message['alias']] = $message['sender'];
      } elseif ($dictionary[$message['alias']] != $message['sender']) {
        echo "Warning:  {$message['alias']} matches with both {$dictionary[$message['alias']]} and {$message['sender']}.\n";
        //$abort = true;
      }
    }
  }

  if ($abort) {
    echo "Aborting...\n";
    exit;
  }

  // This takes a long time
  foreach ($alias_arr as $alias => $messages) {
    $last_time = new Date();
    $log_num = 0;

    foreach ($messages as $message) {
      $this_time = new Date($message['time']);
      $span = new Date_Span();
      $span->setFromDateDiff($last_time, $this_time);

      if ((int)$span->toHours() >= 1) {
        $last_time = $this_time;
        $log_num++;
      }

      $log_arr[$alias][$log_num][] = $message;
    }
  }


  // Write matched files
  foreach ($log_arr as $alias => $logs) {
    $matched_folder = "$outdir/Matched";
    $not_matched_folder = "$outdir/Not Matched";

    if (!file_exists($matched_folder))
      mkdir($matched_folder);

    if (!file_exists($not_matched_folder))
      mkdir($not_matched_folder);

    foreach ($logs as $log) {
      // Find the other screenname
      $screenname = $myscreenname;

      foreach ($log as $message) {
        if ($message['sender'] != $myscreenname) {
          $screenname = $message['sender'];
        }
      }

      if ($screenname != $myscreenname) {
        $sn_folder = "$matched_folder/$screenname";
      } else {
        // Try to match the screenname
        if (!empty($dictionary[$alias])) {
          $screenname = $dictionary[$alias];
          echo "Matched $alias to $screenname.\n";
          $sn_folder = "$matched_folder/{$dictionary[$alias]}";
        } else {
          $screenname = '[Unknown]';
          echo "Could not find match for $alias.\n";
          $sn_folder = "$not_matched_folder/$alias";
        }
      }

      if (!file_exists($sn_folder)) {
        mkdir($sn_folder);
      }

      $start_time = $log[0]['time'];
      $end_time = $log[count($log) - 1]['time'];
      $log_folder = "$sn_folder/$screenname (" . str_replace(':', '.', str_replace('-05:00', '-0500', $start_time)) . ").chatlog";
      $log_file = "$log_folder/$screenname (" . str_replace(':', '.', str_replace('-05:00', '-0500', $start_time)) . ").xml";
      $output = '';

      // Create log folder if it doesn't exist
      if (!file_exists($log_folder)) {
        mkdir($log_folder);
      }

      $output .= '<?xml version="1.0" encoding="UTF-8" ?>' . "\r";
      $output .= "<!-- Converted from iChat Transcript -->\r";
      $output .= '<chat xmlns="http://purl.org/net/ulf/ns/0.4-02" account="' . $myscreenname . '" service="AIM">';
      $output .= '<event type="windowOpened" sender="' . $myscreenname . '" time="' . $start_time . '"/>' . "\r";

      foreach ($log as $message) {
        $output .= '<message sender="' . $message['sender'] . '" time="' . $message['time'] . '" alias="' . $message['alias'] . '">';
        $output .= '<div><span style="font-family: Helvetica; font-size: 12pt;">';
        $output .= str_replace("\n", '', nl2br(htmlspecialchars($message['message'])));
        $output .= '</span></div></message>' . "\r";
      }

      $output .= '<event type="windowClosed" sender="' . $myscreenname . '" time="' . $end_time . '"/>' . "\r";
      $output .= "</chat>\r";

      // Write to file
      file_put_contents($log_file, $output);
    }
  }

?>

以上是关于PHP iChat到Adium Chat Transcript Converter的主要内容,如果未能解决你的问题,请参考以下文章

Smack 连接到服务器失败

通过 iChatAgent 以编程方式在 iChat 中设置状态

恢复我的“com.apple.iChat”文件

iChat 如何开始对话?

更改iChat状态终端.app

将iChat状态设置为“最近”最后一个.fm轨道