Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2011
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 3.92 KB | None | 0 0
  1. #!/usr/bin/tclsh
  2. #
  3. # Goes through Empathy Logs and Converts it into HTML in a Pidgin Friendly
  4. # format.
  5. #
  6.  
  7. # file format in pidgin: 2011-02-19.000000+0000GMT.html
  8. # Empathy logs in ~/.local/share/Empathy/
  9. # file format in empathy: 20110219.log
  10.  
  11. # we require path to look into.
  12. if { $argc != 1 } {
  13.     puts "Requires a file path."
  14.     exit
  15. }
  16.  
  17. set filepath [lindex $argv 0]
  18. puts "File path: $filepath"
  19.  
  20. if { ![file isdirectory $filepath] } {
  21.     puts "File path is not a directory."
  22.     exit
  23. }
  24.  
  25. proc pidginfilename { date } {
  26.     return "${date}.000000+0000GMT.html"
  27. }
  28.  
  29. proc color1 { } {
  30.     return {#16569E}
  31. }
  32.  
  33. proc color2 { } {
  34.     return {#A82F2F}
  35. }
  36.  
  37. proc formatDate { date } {
  38.  
  39.     # if it's a filename log
  40.     if { [regexp {(\d\d\d\d)(\d\d)(\d\d).log} $date match yr mo dy] } {
  41.         return "${yr}-${mo}-${dy}"
  42.     }
  43.  
  44.     # if it's a date string
  45.     if { [regexp {(\d\d\d\d)(\d\d)(\d\d)} $date match yr mo dy] } {
  46.         return "${yr}-${mo}-${dy}"
  47.     }
  48.  
  49.     puts "Error in formatting.  Returning $date"
  50.     return $date
  51. }
  52.  
  53. proc getFileName { file } {
  54.     if { [regexp {.+/(.+)} $file match filename] } {
  55.         return $filename
  56.     }
  57.  
  58.     puts "Error in regexp check for $file"
  59.     return {}
  60. }
  61.  
  62. proc empty_string_p { string } {
  63.     return [string eq $string {}]
  64. }
  65.  
  66. proc formatParamData { paramdata } {
  67.     # time='20110218T16:36:09' cm_id='0' id='xxx@gmail.com' name='johndoe' token='xxxxxxxxx' isuser='true' type='normal'
  68.  
  69.     set data [list]
  70.     if { [regexp {time='(.+?)'} $paramdata match timedata] } {
  71.  
  72.         if { [regexp {T(.+)} $timedata match time] } {
  73.             lappend data "$time  GMT"
  74.         } else {
  75.             lappend data $timedata
  76.         }
  77.  
  78.     } else {
  79.         lappend data {}
  80.     }
  81.  
  82.     if { [regexp {name='(.+?)'} $paramdata match name] } {
  83.         lappend data $name
  84.     } else {
  85.         lappend data {}
  86.     }
  87.  
  88.     return $data
  89. }
  90.  
  91. foreach file [lsort [glob -nocomplain -directory $filepath "*\.log"]] {
  92.     # clean up the file path.
  93.  
  94.     if { ![file isfile $file] } {
  95.         puts "Skipping $file"
  96.         continue
  97.     }
  98.  
  99.     set filename [getFileName $file]
  100.  
  101.     set chat_date [formatDate $filename]
  102.     puts "Reading $file"
  103.  
  104.     set fp [open $file r]
  105.  
  106.     set converted_html "<html><head><title><h3>Exported From Empathy: $chat_date</h3></title></head><body>\n"
  107.  
  108.     # read contents
  109.     set lastname {}
  110.  
  111.     while { [gets $fp line] != -1 } {
  112.         if { [regexp {<message(.*)>(.+)</message>} $line match params msg] } {
  113.             set messageData [formatParamData $params]
  114.  
  115.             set time [lindex $messageData 0]
  116.             set name [lindex $messageData 1]
  117.  
  118.             if { ![empty_string_p $time] && ![empty_string_p $name] } {
  119.                 if { ![info exists color] } {
  120.                     set color [color1]
  121.                 }
  122.  
  123.                 if { [empty_string_p $lastname] } {
  124.                     set lastname $name
  125.                     set color [color1]                
  126.                 } else {
  127.                     if { ![string eq $lastname $name] } {
  128.                         set lastname $name
  129.                         # must update color.
  130.                         if { [string eq $color [color1]] } {
  131.                             set color [color2]
  132.                         } else {
  133.                             set color [color1]
  134.                         }
  135.                     }
  136.                 }
  137.                
  138.                 append converted_html "<font color=\"$color\"><font size=\"2\">($time)</font> <b> $name:</b></font> $msg<br/>\n"
  139.             }
  140.         }
  141.     }
  142.  
  143.     close $fp
  144.     append converted_html "</body></html>"
  145.  
  146.     set newfile [pidginfilename $chat_date]
  147.  
  148.     set newpath "$filepath/$newfile"
  149.  
  150.     puts "Writing $newpath"
  151.  
  152.     set wp [open $newpath w]
  153.  
  154.     puts $wp $converted_html
  155.  
  156.     close $wp
  157.  
  158.     # success.  We should remove the old log file
  159.  
  160.     file delete $file
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement