###
# Allgemeine Parameter
param(
[Parameter(Mandatory=$true)][string]$chat_file
)
[array]$csv_content_chat = Import-Csv $chat_file
[array]$content = $null
[string]$curDate = $null
[string]$oldDate = $null
[string]$chat_partner_name = $null
[string]$msg_status = $null
# Teste auf leere Chat-CSV-Datei
If ($csv_content_chat.Length -eq 0){
Write-Host "Chat-Datei hat keinen Inhalt."
Exit 1
}
Try { # Teste auf Vorhandensein der Umfrage-Dateien
[array]$csv_content_ballot = Import-Csv "ballot.csv"
[array]$csv_content_bvote = Import-Csv "ballot_vote.csv"
[array]$csv_content_bchoice = Import-Csv "ballot_choice.csv"
} Catch {
Write-Host "Nicht alle ""ballot*""-Dateien (für Umfragen) gefunden!"
Exit 3
}
Try { # Teste auf Vorhandensein der Kontakte- und Gruppen-Datei
[array]$csv_content_contacts = Import-Csv "contacts.csv"
[array]$csv_content_groups = Import-Csv "groups.csv"
} Catch {
Write-Host "Datei ""contacts.csv"" oder ""groups.csv"" nicht gefunden!"
Exit 2
}
# Prüfe auf Gruppe oder Einzelchat/Kanal
#If ($csv_content_chat[0].PSObject.Properties.Name[2] -eq "identity"){
If (($csv_content_chat[0] | Get-Member -Name identity).Name -eq "identity"){
[bool]$isGroup = $true
[string]$chat_id = (Get-ChildItem $chat_file).BaseName.Split("_")[2].Split("-")[0]
} Else {
[bool]$isGroup = $false
# Teste auf Kanal, ansonsten Einzelchat
If ((Get-ChildItem $chat_file).BaseName.Split("_").Count -eq 3){
[string]$chat_id = (Get-ChildItem $chat_file).BaseName.Split("_")[2]
} Else {
[string]$chat_id = (Get-ChildItem $chat_file).BaseName.Split("_")[1]
}
}
# Wenn Kanal (mit * beginnend), dann setze den in der ID auch bitte
If ($chat_id.Length -eq 7){ $chat_id = "*"+$chat_id }
# Medienordner mit "gc_" oder "id_" beginnend
If ($isGroup){
[string]$chat_id_folder = "gc_"+$chat_id
[string]$chat_id_identity = $chat_id
} Else {
[string]$chat_id_folder = "id_"+$chat_id.Replace("*", "_")
[string]$chat_id_identity = $chat_id.Replace("*", "_")
}
# Funktion zum Konvertieren der Sekunden-Zeitstempel
Function ConvertUnixTime ([string]$msg_timestamp_unix, [bool]$fulldatetime){
$msg_timestamp_unix = $msg_timestamp_unix.Substring(0, 10)
If ($fulldatetime -eq $false){
(([datetime]'1/1/1970 01:00').AddSeconds($msg_timestamp_unix)).ToString("dd.MM.yyyy")
} Else {
(([datetime]'1/1/1970 01:00').AddSeconds($msg_timestamp_unix)).ToString("dd.MM.yyyy, HH:mm:ss")
}
}
# Funktion zum Auslesen des Kontaktnamens
Function GetContactName ($ask_chat_id, $group_not_user, $memyself_word){
If ($group_not_user){ # Test auf Gruppe
For ([long]$i=0; $i -lt $csv_content_groups.Count; $i++){
If ($csv_content_groups[$i].id -eq $ask_chat_id){
# Gebe Gruppenname zurück
Return $csv_content_groups[$i].groupname
}
}
} Else { # Einzelchat oder Erwähnung
# Alle in einer Gruppe
If ($ask_chat_id -eq "@@@@@@@@"){Return "alle"}
For ([long]$i=0; $i -lt $csv_content_contacts.Count; $i++){
If ($csv_content_contacts[$i].identity -eq $ask_chat_id){
If ($csv_content_contacts[$i].firstname -ne "" -And $csv_content_contacts[$i].lastname -ne ""){
# Gebe vollständigen Namen zurück, wenn Vor- und Nachname gesetzt sind
$tmp_fullname = $csv_content_contacts[$i].firstname + " " + $csv_content_contacts[$i].lastname
Return $tmp_fullname
} ElseIf ($csv_content_contacts[$i].firstname -ne ""){
# Gebe nur den Vornamen zurück, wenn Nachname nicht gesetzt ist
Return $csv_content_contacts[$i].firstname
} ElseIf ($csv_content_contacts[$i].lastname -ne ""){
# Gebe nur den Nachnamen zurück, wenn Vorname nicht gesetzt ist
Return $csv_content_contacts[$i].lastname
} ElseIf ($csv_content_contacts[$i].nick_name -ne ""){
# Gebe den Nicknamen zurück, wenn Vor- und Nachname nicht gesetzt sind
Return $csv_content_contacts[$i].nick_name
} Else {
# Gebe die ID zurück, wenn keinerlei Name gesetzt ist
Return $ask_chat_id
}
}
}
# Kein Treffer? Gebe die übergebene Variable zurück (für eigene Zitate, ...)
Return $memyself_word
}
}
# Funktion zum Verarbeiten der Nachricht in die HTML-Datei
Function WriteMessage ($msg_content){
# allgemeine, temporäre Parameter in dieser Funktion
$tmp_quotecontact_id = $null
$tmp_quotecontact_name = $null
$tmp_content = $null
$tmp_content_from_user = $null
$tmp_msg_content = $null
$tmp_array = @()
$msg_content.body = $msg_content.body.Replace("<", "<") # Ersetze < Zeichen, um den HTML-Code nicht zu zerstören
If ($isGroup){ # Test auf Gruppe
$tmp_group_user_id = $msg_content.identity # Hole die ID des Senders
$tmp_group_user_name = GetContactName $tmp_group_user_id $false "Ich" # Setze den Namen zur Sender-ID
$tmp_filename = "group_message_media_"+$msg_content.uid # Setze mögliche Medien-ID
$tmp_filename_thumbnail = "group_message_thumbnail_"+$msg_content.uid # Setze mögliche Medien-ID zum Vorschaubild
} Else {
$tmp_filename = "message_media_"+$msg_content.uid # Setze mögliche Medien-ID für Einzelchat (auch Kanal?)
$tmp_filename_thumbnail = "message_thumbnail_"+$msg_content.uid # Setze mögliche Medien-ID für Einzelchat (auch Kanal?) zum Vorschaubild
}
# Wenn Nachrichtenempfang in einer Gruppe: setze Name und ID vorweg
If ($msg_content.isoutbox -eq 0 -And $msg_content.identity -ne "" -And $isGroup){
$tmp_content_from_user = ""+$tmp_group_user_name+" ("+$tmp_group_user_id+"):
"
}
# Verarbeitung je nach Nachrichtentyp
Switch ($msg_content.type){
"TEXT" {
# Ersetze Erwähnungen: @[ABCD1234]
While ($msg_content.body | Select-String -CaseSensitive "@\[........\]"){
$tmp_at_index = $msg_content.body.IndexOf("@[")
$tmp_at_full = $msg_content.body.Substring($tmp_at_index, 11)
$tmp_at_user_id = $msg_content.body.Substring($tmp_at_index + 2, 8)
$tmp_at_user_name = GetContactName $tmp_at_user_id $false "Du"
If ($tmp_at_user_id -eq "@@@@@@@@"){
$msg_content.body = $msg_content.body.Replace($tmp_at_full, "@" + $tmp_at_user_name + "")
} Else {
$msg_content.body = $msg_content.body.Replace($tmp_at_full, "@" + $tmp_at_user_name + " (" + $tmp_at_user_id + ")")
}
}
# Teste auf Zitat
If ($msg_content.body -like "> ????????: *"){
$tmp_quotecontact_id = $msg_content.body.Substring(2, 8)
$tmp_quotecontact_name = GetContactName $tmp_quotecontact_id $false "mir"
$tmp_content += "`t
"
$msg_content_split = $msg_content.body.Split("`n")
$msg_content_split[0] = $msg_content_split[0].Replace("> "+$tmp_quotecontact_id+": ", "Zitat von "+$tmp_quotecontact_name+" ("+$tmp_quotecontact_id+"):
")
$tmp_content += $msg_content_split[0]
$tmp_quote_closed = $false
For ([long]$i=1; $i -lt $msg_content_split.Count; $i++){
If ($msg_content_split[$i].StartsWith("> ") -eq $true){
$tmp_content += "
"+$msg_content_split[$i].Replace("> ", "")
} ElseIf ($tmp_quote_closed -And $i -lt $msg_content_split.Count){
If ($i -lt $msg_content_split.Count){
$tmp_content += $msg_content_split[$i]+"
"
} Else {
$tmp_content += $msg_content_split[$i]
}
} Else {
$tmp_content += "
"+$tmp_content_from_user $tmp_quote_closed = $true } } $tmp_content += "
" } Else { If ($isGroup -And $msg_content.isoutbox -eq 0){ $tmp_content += "`t"+$tmp_content_from_user+$msg_content.body.Replace("`n", "
")+"
"+$msg_content.body.Replace("`n", "
")+"
"+$tmp_content_from_user+"
"+$tmp_msg_content+"
"+$tmp_content_from_user+"
"+$tmp_msg_content+"
"+$tmp_content_from_user+"Bild nicht gefunden.
"+$tmp_msg_content+"
"+$tmp_content_from_user+"
" Move-Item $tmp_filename $chat_id_folder/$tmp_filename } Else { $tmp_content = "`t"+$tmp_content_from_user+"Audio nicht gefunden.
"+$tmp_content_from_user+"
" Move-Item $tmp_filename $chat_id_folder/$tmp_filename Remove-Item $tmp_filename_thumbnail 2>$null } ElseIf (Test-Path -Path $tmp_filename_thumbnail){ # Teste ob Thumbnail vorhanden $tmp_content = "`t"+$tmp_content_from_user+"Video nicht gefunden. Gespeichertes Vorschaubild:
"+$tmp_msg_content+"
"+$tmp_content_from_user+"Video nicht gefunden.
"+$tmp_msg_content+"
"+$tmp_content_from_user+"Datei: "+$tmp_array[4]+"
"+$tmp_msg_content+"
"+$tmp_content_from_user+"Bild/Video nicht gefunden. Gespeichertes Vorschaubild:
"+$tmp_msg_content+"
"+$tmp_content_from_user+"Datei nicht gefunden.
"+$tmp_array[4]+"
"+$tmp_msg_content+"
Umfrage """+$csv_content_ballot[$i].name+""" begonnen:
`n" For ([long]$j=0; $j -lt $csv_content_bchoice.Count; $j++){ If ($csv_content_bchoice[$j].ballot -eq $csv_content_ballot[$i].aid+"-"+$csv_content_ballot[$i].creator){ $tmp_content += "`t"+$csv_content_bchoice[$j].name+"
" } } # Umfrage geschlossen } ElseIf ($newballot[0] -eq "3"){ $tmp_content += "`tUmfrage """+$csv_content_ballot[$i].name+""" geschlossen:
`n" $ballotchoices = @() For ([long]$j=0; $j -lt $csv_content_bchoice.Count; $j++){ If ($csv_content_bchoice[$j].ballot -eq $csv_content_ballot[$i].aid+"-"+$csv_content_ballot[$i].creator){ $ballotchoices += $csv_content_bchoice[$j].aid, $csv_content_bchoice[$j].name, "" } } For ([long]$k=0; $k -lt $csv_content_bvote.Count; $k++){ If ($csv_content_bvote[$k].ballot_uid -eq $csv_content_ballot[$i].aid+"-"+$csv_content_ballot[$i].creator){ If ($csv_content_bvote[$k].choice -eq 1){ For ($l=0; $l -lt $ballotchoices.Count; $l+=3){ If ($ballotchoices[$l] -eq $csv_content_bvote[$k].choice_uid){ $tmp_username = GetContactName $csv_content_bvote[$k].identity $false "Ich" $ballotchoices[$l+2] += ", "+$tmp_username } } } } } For ($n=0; $n -lt $ballotchoices.Count; $n+=3){ If ($ballotchoices[$n+2].Length -gt 2){ $ballotchoices[$n+2] = $ballotchoices[$n+2].Substring(2) } } For ($m=0; $m -lt $ballotchoices.Count; $m+=3){ $tmp_content += "`t"+$ballotchoices[$m+1]+": "+$ballotchoices[$m+2]+"
" } } $msg_timestamp_readable = ConvertUnixTime $csv_content_ballot[$i].created_at $true } } break} "LOCATION" { $tmp_array = $msg_content.body.Replace("[", "").Replace("]", "").Split(",") $tmp_content = "`t"+$tmp_content_from_user+"Standort:
"+$msg_content.caption.Replace("`n", "
").Replace("*", "")+"
"+$msg_timestamp_readable+$msg_status+"
" } } Else { $content += ""+$msg_timestamp_readable+"
" } Else { $content += "`t"+$msg_status+" "+$msg_timestamp_readable+"
" } } $msg_status = $null $content += "