Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/tclsh
- #
- # Goes through Empathy Logs and Converts it into HTML in a Pidgin Friendly
- # format.
- #
- # file format in pidgin: 2011-02-19.000000+0000GMT.html
- # Empathy logs in ~/.local/share/Empathy/
- # file format in empathy: 20110219.log
- # we require path to look into.
- if { $argc != 1 } {
- puts "Requires a file path."
- exit
- }
- set filepath [lindex $argv 0]
- puts "File path: $filepath"
- if { ![file isdirectory $filepath] } {
- puts "File path is not a directory."
- exit
- }
- proc pidginfilename { date } {
- return "${date}.000000+0000GMT.html"
- }
- proc color1 { } {
- return {#16569E}
- }
- proc color2 { } {
- return {#A82F2F}
- }
- proc formatDate { date } {
- # if it's a filename log
- if { [regexp {(\d\d\d\d)(\d\d)(\d\d).log} $date match yr mo dy] } {
- return "${yr}-${mo}-${dy}"
- }
- # if it's a date string
- if { [regexp {(\d\d\d\d)(\d\d)(\d\d)} $date match yr mo dy] } {
- return "${yr}-${mo}-${dy}"
- }
- puts "Error in formatting. Returning $date"
- return $date
- }
- proc getFileName { file } {
- if { [regexp {.+/(.+)} $file match filename] } {
- return $filename
- }
- puts "Error in regexp check for $file"
- return {}
- }
- proc empty_string_p { string } {
- return [string eq $string {}]
- }
- proc formatParamData { paramdata } {
- # time='20110218T16:36:09' cm_id='0' id='xxx@gmail.com' name='johndoe' token='xxxxxxxxx' isuser='true' type='normal'
- set data [list]
- if { [regexp {time='(.+?)'} $paramdata match timedata] } {
- if { [regexp {T(.+)} $timedata match time] } {
- lappend data "$time GMT"
- } else {
- lappend data $timedata
- }
- } else {
- lappend data {}
- }
- if { [regexp {name='(.+?)'} $paramdata match name] } {
- lappend data $name
- } else {
- lappend data {}
- }
- return $data
- }
- foreach file [lsort [glob -nocomplain -directory $filepath "*\.log"]] {
- # clean up the file path.
- if { ![file isfile $file] } {
- puts "Skipping $file"
- continue
- }
- set filename [getFileName $file]
- set chat_date [formatDate $filename]
- puts "Reading $file"
- set fp [open $file r]
- set converted_html "<html><head><title><h3>Exported From Empathy: $chat_date</h3></title></head><body>\n"
- # read contents
- set lastname {}
- while { [gets $fp line] != -1 } {
- if { [regexp {<message(.*)>(.+)</message>} $line match params msg] } {
- set messageData [formatParamData $params]
- set time [lindex $messageData 0]
- set name [lindex $messageData 1]
- if { ![empty_string_p $time] && ![empty_string_p $name] } {
- if { ![info exists color] } {
- set color [color1]
- }
- if { [empty_string_p $lastname] } {
- set lastname $name
- set color [color1]
- } else {
- if { ![string eq $lastname $name] } {
- set lastname $name
- # must update color.
- if { [string eq $color [color1]] } {
- set color [color2]
- } else {
- set color [color1]
- }
- }
- }
- append converted_html "<font color=\"$color\"><font size=\"2\">($time)</font> <b> $name:</b></font> $msg<br/>\n"
- }
- }
- }
- close $fp
- append converted_html "</body></html>"
- set newfile [pidginfilename $chat_date]
- set newpath "$filepath/$newfile"
- puts "Writing $newpath"
- set wp [open $newpath w]
- puts $wp $converted_html
- close $wp
- # success. We should remove the old log file
- file delete $file
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement