Class: Pidgin2Adium::LogFile

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Pidgin2Adium
Defined in:
lib/pidgin2adium/log_file.rb

Overview

A holding object for the result of LogParser.parse. It makes the instance variable @chat_lines available, which is an array of Message subclass instances (XMLMessage, Event, etc.) Here is a list of the instance variables for each class in @chat_lines:

All of these variables are read/write. All:: sender, time, buddy_alias XMLMessage:: body

AutoReplyMessage

body

Event:: body, event_type StatusMessage:: status

Constant Summary

Constants included from Pidgin2Adium

ADIUM_LOG_DIR, BAD_DIRS, FILE_EXISTS, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Pidgin2Adium

balance_tags_c, delete_search_indexes, error, log_msg, oops, parse, parse_and_generate

Constructor Details

#initialize(chat_lines, service, user_SN, partner_SN, adium_chat_time_start) ⇒ LogFile

Returns a new instance of LogFile.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pidgin2adium/log_file.rb', line 18

def initialize(chat_lines, service, user_SN, partner_SN, adium_chat_time_start)
  @chat_lines = chat_lines
  @user_SN = user_SN
  @partner_SN = partner_SN
  @adium_chat_time_start = adium_chat_time_start

  # @chat_str is generated when to_s is called
  @chat_str = nil

  # key is for Pidgin, value is for Adium
  # Just used for <service>.<screenname> in directory structure
  service_name_map = {'aim' => 'AIM',
    'jabber' =>'Jabber',
    'gtalk'=> 'GTalk',
    'icq' => 'ICQ',
    'qq' => 'QQ',
    'msn' => 'MSN',
    'yahoo' => 'Yahoo!'}

  @service = service_name_map[service.downcase]
end

Instance Attribute Details

#adium_chat_time_startObject (readonly)

Returns the value of attribute adium_chat_time_start.



40
41
42
# File 'lib/pidgin2adium/log_file.rb', line 40

def adium_chat_time_start
  @adium_chat_time_start
end

#chat_linesObject (readonly)

Returns the value of attribute chat_lines.



40
41
42
# File 'lib/pidgin2adium/log_file.rb', line 40

def chat_lines
  @chat_lines
end

#partner_SNObject (readonly)

Returns the value of attribute partner_SN.



40
41
42
# File 'lib/pidgin2adium/log_file.rb', line 40

def partner_SN
  @partner_SN
end

#serviceObject (readonly)

Returns the value of attribute service.



40
41
42
# File 'lib/pidgin2adium/log_file.rb', line 40

def service
  @service
end

#user_SNObject (readonly)

Returns the value of attribute user_SN.



40
41
42
# File 'lib/pidgin2adium/log_file.rb', line 40

def user_SN
  @user_SN
end

Instance Method Details

#each(&blk) ⇒ Object



48
49
50
# File 'lib/pidgin2adium/log_file.rb', line 48

def each(&blk)
  @chat_lines.each{|l| yield l }
end

#to_sObject

Returns contents of log file



43
44
45
46
# File 'lib/pidgin2adium/log_file.rb', line 43

def to_s
  # Faster than inject() or each()
  @chat_str ||= @chat_lines.map{|l| l.to_s }.join
end

#write_out(overwrite = false, output_dir_base = ADIUM_LOG_DIR) ⇒ Object

Set overwrite=true to create a logfile even if logfile already exists. Returns one of:

  • false (if an error occurred),

  • Pidgin2Adium::FILE_EXISTS if the file to be generated already exists and overwrite=false, or

  • the path to the new Adium log file.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pidgin2adium/log_file.rb', line 57

def write_out(overwrite = false, output_dir_base = ADIUM_LOG_DIR)
  # output_dir_base + "/buddyname (2009-08-04T18.38.50-0700).chatlog"
  output_dir = File.join(output_dir_base, "#{@service}.#{@user_SN}", @partner_SN, "#{@partner_SN} (#{@adium_chat_time_start}).chatlog")
  # output_dir + "/buddyname (2009-08-04T18.38.50-0700).chatlog/buddyname (2009-08-04T18.38.50-0700).xml"
  output_path = output_dir + '/' + "#{@partner_SN} (#{@adium_chat_time_start}).xml"
  begin
    FileUtils.mkdir_p(output_dir)
  rescue => bang
    error "Could not create destination directory for log file. (Details: #{bang.class}: #{bang.message})"
    return false
  end
  if overwrite
    unless File.exist?(output_path)
      # File doesn't exist, but maybe it does with a different
      # time zone. Check for a file that differs only in time
      # zone and, if found, change @output_path to target it.
      maybe_matches = Dir.glob(output_dir_base + '/' << File.basename(output_path).sub(/-\d{4}\)\.chatlog$/, '') << '/*')
      unless maybe_matches.empty?
        output_path = maybe_matches[0]
      end
    end
  else
    if File.exist?(output_path)
      return FILE_EXISTS
    end
  end

  begin
    outfile = File.new(output_path, 'w')
  rescue => bang
    error "Could not open log file for writing. (Details: #{bang.class}: #{bang.message})"
    return false
  end

  # no \n before </chat> because @chat_str (from to_s) has it already
  outfile.printf('<?xml version="1.0" encoding="UTF-8" ?>'<<"\n"+
                 '<chat xmlns="http://purl.org/net/ulf/ns/0.4-02" account="%s" service="%s">'<<"\n"<<'%s</chat>',
                 @user_SN, @service, self.to_s)
  outfile.close

  return output_path
end