Class: Pidgin2Adium::LogFile

Inherits:
Object
  • Object
show all
Includes:
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 objects which each have at least the instance variables sender, time, and buddy_alias available. Some objects in @chat_lines have more variables available, specifically:

  • XMLMessage, AutoReplyMessage, and Event

    body

  • Event

    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, delete_search_indexes, 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.



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

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.



38
39
40
# File 'lib/pidgin2adium/log_file.rb', line 38

def adium_chat_time_start
  @adium_chat_time_start
end

#chat_linesObject (readonly)

Returns the value of attribute chat_lines.



38
39
40
# File 'lib/pidgin2adium/log_file.rb', line 38

def chat_lines
  @chat_lines
end

#partner_SNObject (readonly)

Returns the value of attribute partner_SN.



38
39
40
# File 'lib/pidgin2adium/log_file.rb', line 38

def partner_SN
  @partner_SN
end

#serviceObject (readonly)

Returns the value of attribute service.



38
39
40
# File 'lib/pidgin2adium/log_file.rb', line 38

def service
  @service
end

#user_SNObject (readonly)

Returns the value of attribute user_SN.



38
39
40
# File 'lib/pidgin2adium/log_file.rb', line 38

def user_SN
  @user_SN
end

Instance Method Details

#each(&blk) ⇒ Object



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

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

#to_sObject

Returns contents of log file



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

def to_s
    if @chat_str.nil?
  # Faster than inject() or each()
  @chat_str = @chat_lines.map{|l| l.to_s }.join
    end
    return @chat_str
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.



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
99
# File 'lib/pidgin2adium/log_file.rb', line 58

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