Chilkat IMAP - how to check "Sent" mailbox name - autoit

How to check how "Sent" mailbox is named ?
Somtimes I have to use "INBOX.Sent" sometimes "Sent",
I do can not relay on guess I must have a way to check what current name is.
Do you have a way to check it with Chilkat ?

Mailboxes contain flags such as "\Sent", "\Trash", "\Junk", etc to indicate the special use mailboxes.
See this example: https://www.example-code.com/csharp/imap_find_sent_mailbox.asp

Finally I get answer/solution to my problem:
I use This example (PowerShell) Find the "Sent" IMAP Mailbox
Here is my version:
Add-Type -Path "c:\PS1\ChilkatDotNet48_x64.dll"
$glob = New-Object Chilkat.Global
$success = $glob.UnlockBundle("Anything for 30-day trial")
if ($success -ne $true) {
$($glob.LastErrorText)
exit
}
$status = $glob.UnlockStatus
if ($status -eq 2) {
$("Unlocked using purchased unlock code.")
}
else {
$("Unlocked in trial mode.")
}
$($glob.LastErrorText)
$imap = New-Object Chilkat.Imap
$imap.Ssl = $true
$imap.Port = 993
# connect to server
$success = $imap.Connect("******")
if ($success -ne $true) {
$($imap.LastErrorText)
exit
}
# Login or authenticate in some way..
$success = $imap.Login("******","******")
if ($success -ne $true) {
$($imap.LastErrorText)
exit
}
# Get the list of mailboxes.
$refName = ""
$wildcardedMailbox = "*"
$mboxes = $imap.ListMailboxes($refName,$wildcardedMailbox)
if ($imap.LastMethodSuccess -eq $false) {
$($imap.LastErrorText)
exit
}
# The mailbox with the "/Sent" flag is the "Sent" mailbox.
# Likewise for Junk and Trash..
$i = 0
$("")
$("")
while ($i -lt $mboxes.Count) {
if ($mboxes.GetName($i) -eq "Sent") {
$("Sent mailbox (1): " + $mboxes.GetName($i))
$(" >> mboxes.GetNthFlag(i,0)=" + $mboxes.GetNthFlag($i, 0))
$(" >> mboxes.GetNthFlag(i,1)=" + $mboxes.GetNthFlag($i, 1))
$("")
}
if ($mboxes.HasFlag($i,"\Sent") -eq $true) {
$("Sent mailbox (2): " + $mboxes.GetName($i))
$(" >> mboxes.GetNthFlag(i,0)=" + $mboxes.GetNthFlag($i, 0))
$(" >> mboxes.GetNthFlag(i,1)=" + $mboxes.GetNthFlag($i, 1))
$("")
}
if ($mboxes.HasFlag($i,"\Junk") -eq $true) {
$("Junk mailbox: " + $mboxes.GetName($i))
$(" >> mboxes.GetNthFlag(i,0)=" + $mboxes.GetNthFlag($i, 0))
$(" >> mboxes.GetNthFlag(i,1)=" + $mboxes.GetNthFlag($i, 1))
$("")
}
if ($mboxes.HasFlag($i,"\Trash") -eq $true) {
$("Trash mailbox: " + $mboxes.GetName($i))
$(" >> mboxes.GetNthFlag(i,0)=" + $mboxes.GetNthFlag($i, 0))
$(" >> mboxes.GetNthFlag(i,1)=" + $mboxes.GetNthFlag($i, 1))
$("")
}
$i = $i + 1
}
# Disconnect from the IMAP server.
$success = $imap.Disconnect()
And my results are:
Junk mailbox: Junk
>> mboxes.GetNthFlag(i,1)=\HasNoChildren
>> mboxes.GetNthFlag(i,2)=\Junk
Sent mailbox (1): Sent
>> mboxes.GetNthFlag(i,1)=\HasChildren
>> mboxes.GetNthFlag(i,2)=
Conclusion: This mean that sometimes Sent should be recognized by a Name not by Flag.
btw.
In Polish version of ThunderBird this Mailbox have different name "Wysłane" but location is: similar to "imap://user#domain.com/Sent"

Related

Making a script for sorting photos based on tags faster

Hi I have no idea if this is a legitimate question for SO.
But I have a script i made for sorting photos into folders based on tags. It uses the exifr package to do this.
However, it's running very slow. I've tried to improve it using guides but what I ever I make ends up not working. Is there someone with understanding of vectorization and or optimization that could give point to some suggestions.
Thanks!
#----- Imports ----
library(exifr)
# ---------- Functions ----------
'%!in%' <- function(x,y)!('%in%'(x,y))
tagcatcher <- function(dat){
tags <- c()
for (tagNameTry in keywords_names ) {
if (tagNameTry %in% names(dat)) {
xs <- dat[tagNameTry]
if (typeof(xs) == "list") {
xs <- xs[[1]]
l <- length(xs[[1]])
x <- c()
for (i in 1:l) {
x <- c(x,xs[[1]][i])
}
} else {
x <- xs
}
tags <- c(tags,x)
}
}
tags <- unique(tags)
return(tags)
}
# ----------- Settings ----------
ss <- "/"
haystacks <- c("H:MyPhotos")
organizedMediaPhotos <- "V:/Photos"
all_files <- list.files(haystacks,recursive = TRUE, full.names = TRUE)
keywords_names <- c("Category","XPKeywords","Keywords")
ctags <- list.dirs(organizedMediaPhotos)[list.dirs(organizedMediaPhotos) %!in% organizedMediaPhotos]
current_tags <- c()
for (ctag in ctags) {
x <- strsplit(ctag,"/")
x <- x[[1]]
x <- x[length(x)]
current_tags <- c(current_tags,x)
}
# Main Loop - That Needs to be faster
for (cur_file in all_files) {
print(cur_file)
cur_dat <- read_exif(cur_file,tags=keywords_names)
tags <- tagcatcher(cur_dat)
for (tag in tags) {
tag_folder <- paste(organizedMediaPhotos,ss,tag,sep="")
if (tag %!in% current_tags) {
dir.create(tag_folder)
print(paste("creating tag folder: ",tag_folder))
}
pic_path <- paste(tag_folder,ss,basename(cur_file),sep="")
if (!file.exists(pic_path)) {
file.copy(cur_file,pic_path)
print(paste("moved file from ",cur_file, " to ", pic_path))
}
}
}
You can give this a try
for x in *.jpg; do
d=$(date -r "$x" +%Y-%m-%d)
mkdir -p "$d"
mv -- "$x" "$d/"
done
For powershell:
Param(
[string]$source,
[string]$dest,
[string]$format = "yyyy/yyyy_MM/yyyy_MM_dd"
)
$shell = New-Object -ComObject Shell.Application
function Get-File-Date {
[CmdletBinding()]
Param (
$object
)
$dir = $shell.NameSpace( $object.Directory.FullName )
$file = $dir.ParseName( $object.Name )
# First see if we have Date Taken, which is at index 12
$date = Get-Date-Property-Value $dir $file 12
if ($null -eq $date) {
# If we don't have Date Taken, then find the oldest date from all date properties
0..287 | ForEach-Object {
$name = $dir.GetDetailsof($dir.items, $_)
if ( $name -match '(date)|(created)') {
# Only get value if date field because the GetDetailsOf call is expensive
$tmp = Get-Date-Property-Value $dir $file $_
if ( ($null -ne $tmp) -and (($null -eq $date) -or ($tmp -lt $date))) {
$date = $tmp
}
}
}
}
return $date
}
function Get-Date-Property-Value {
[CmdletBinding()]
Param (
$dir,
$file,
$index
)
$value = ($dir.GetDetailsof($file, $index) -replace "`u{200e}") -replace "`u{200f}"
if ($value -and $value -ne '') {
return [DateTime]::ParseExact($value, "g", $null)
}
return $null
}
Get-ChildItem -Attributes !Directory $source -Recurse |
Foreach-Object {
Write-Host "Processing $_"
$date = Get-File-Date $_
if ($date) {
$destinationFolder = Get-Date -Date $date -Format $format
$destinationPath = Join-Path -Path $dest -ChildPath $destinationFolder
# See if the destination file exists and rename until we get a unique name
$newFullName = Join-Path -Path $destinationPath -ChildPath $_.Name
if ($_.FullName -eq $newFullName) {
Write-Host "Skipping: Source file and destination files are at the same location. $_"
return
}
$newNameIndex = 1
$newName = $_.Name
while (Test-Path -Path $newFullName) {
$newName = ($_.BaseName + "_$newNameIndex" + $_.Extension)
$newFullName = Join-Path -Path $destinationPath -ChildPath $newName
$newNameIndex += 1
}
# If we have a new name, then we need to rename in current location before moving it.
if ($newNameIndex -gt 1) {
Rename-Item -Path $_.FullName -NewName $newName
}
Write-Host "Moving $_ to $newFullName"
# Create the destination directory if it doesn't exist
if (!(Test-Path $destinationPath)) {
New-Item -ItemType Directory -Force -Path $destinationPath
}
robocopy $_.DirectoryName $destinationPath $newName /mo
PS: I had tried this a few years back and it worked like a charm
You can change your if command to something like this:
if [[ "$t" =~ IMG_+[0-9]{8}[a-zA-Z]*$ ]]
The =~ is a regular expression comparison operator which is introduced in bash version 3 and above.
By using this if statement you can catch names like IMG_11111111alphabets.ext. You can play with it and customize it according to your needs. For more information have a look at this: Bash's regular expression

Moving Users Homefolders to Archive after they leave the business

Hi I am not a Scripter but trying to learn.Trying to find out why this script won't work I found it on here but I am unable to identify how to fix the issues it has.
PS C:\windows> Param (
[array]$Users = #(Get-Content "C:\Temp\Users.txt"),
[string]$oldserver = srvmanfiler
[string]$Newserver = SrvmanArchive
)
$Users | ForEach { Robocopy "\\$oldserver\Users\Homedrives\$_" "\\$newserver\Archive\\Homedrives\" /COPY:DAT /v }
At line:3 char:22
+ [string]$oldserver = srvmanfiler
+ ~
Missing expression after '='.
At line:3 char:20
+ [string]$oldserver = srvmanfiler
+ ~
Missing ')' in function parameter list.
At line:5 char:1
+ )
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingExpressionAfterToken
PS C:\windows> enter code here
Fixed it now does what I require which is Move the folders using SamAccount Names to the Destination the full syntax is :
Param (
[array]$Users = #(Get-Content "C:\Temp\Aliases.txt"),
[string]$oldserver = "srvmanFiler",
[string]$Newserver = "SrvmanArchive"
)
$Users | ForEach { Robocopy "\\$oldserver\Users\Homedrives\$_" "\\$newserver\Archive\\Homedrives\$_" /COPY:DAT /E /Move /V }

Searching throughout telnet output result while doing parallel telnet to multiple network devices

I am trying to find a string (e.g.: ZNTS) from telnet output to a host.
I want to telnet to multiple hosts at the same time because waiting to finish from host to host takes a long time. The host can not response fast enough, so I need to sleep after every command line.
Below is my code:
import sys
import telnetlib
import time
MXK_LIST = ['172.16.32.15',
'172.16.33.30',
'192.168.55.3',
'192.168.52.3',
'192.168.54.3',
'192.168.42.25',
'192.168.43.3',
'192.168.44.3',
'192.168.45.3',
'192.168.46.3',
'192.168.47.3',
'192.168.48.3',
'192.168.49.9']
TOTAL_MXK = len(MXK_LIST)
ZNTS = str(sys.argv[1])
DEBUG = str(sys.argv[2])
username = "admin"
password = "4n0cmek0n9net"
I = 0
C = 0
print "\n Finding ZNTS = " + ZNTS
print "\n It will take around 5 minutes to complete the searching !!\n"
for MXK in MXK_LIST:
I = I + 1
OUTPUT = ""
try:
tn = telnetlib.Telnet(MXK)
except:
print "Bad Connection. Please verify IP again."
sys.exit(0)
tn.read_until(b"login: ",1)
tn.write(username + "\n")
time.sleep(5)
tn.read_until(b"password: ",1)
tn.write(password + "\n")
time.sleep(5)
OUTPUT = tn.read_until(b">",1)
if DEBUG == 1: print OUTPUT
time.sleep(5)
tn.write("onu showall 1" + "\n")
time.sleep(5)
tn.read_until(b"[no] ",1)
tn.write(b"yes" + "\n")
time.sleep(15)
OUTPUT = tn.read_until(b"quit",1)
time.sleep(5)
if DEBUG == 1: print OUTPUT
tn.write("A" + "\n")
time.sleep(5)
OUTPUT = tn.read_until(b">",1)
if DEBUG == 1: print OUTPUT
tn.write("exit\n")
if ZNTS in OUTPUT:
print str(I) + "/" + str(TOTAL_MXK) + " : FOUND " + ZNTS + " IN " + MXK
C = C + 1
else:
print str(I) + "/" + str(TOTAL_MXK) + " : NOT FOUND " + ZNTS + " IN " + MXK
print "\n"+ZNTS + " is found in " + str(C) + " MXK"
You can try to spawn a thread for each telnet session and parallelize the search that way.
Check out the multi-thread sample code here:
http://stackoverflow.com/questions/6286235/multiple-threads-in-python

WhoIs servers are not responding

I'm trying to realize a little script in Autoit that retrieves all IPs used by Facebook servers (query "-i origin AS32934" to whois.radb.net, as developer page says), but I don't understand why WhoIs servers are not responding to my queries.
This is the script:
Local $host = "whois.radb.net", $query = "-i origin AS32934"
If Ping($host) Then
$ws = DllOpen("ws2_32.dll")
TCPStartup()
$ip = TCPNameToIP($host)
If #error Then _err("TCPNameToIP", #error)
Global $socket = TCPConnect($ip, 43)
If #error Then _err("TCPConnect", #error)
TCPSend($socket, $query)
If #error Then _err("TCPSend", #error, 1)
Local $text = "", $t = TimerInit(), $counter = 0
While 1
$recv = TCPRecv($socket, 2048)
If #error And #error <> -1 Then
$aRet = DllCall($ws, "int", "WSAGetLastError")
MsgBox(16,"ERROR", "Function: TCPRecv" & #CRLF & "Last data received: " & $recv & #CRLF & "Winsock error: " & $aRet[0] & #CRLF & "Loop executed " & $counter & " times")
ExitLoop
EndIf
$text &= $recv
$counter += 1
If TimerDiff($t) > 4999 Then ExitLoop
WEnd
If $text = "" Then
MsgBox(48, "RESULT", "EMPTY" & #CRLF & "Loop executed " & $counter & " times")
Else
MsgBox(0, "RESULT", $text)
EndIf
TCPCloseSocket($socket)
TCPShutdown()
Else
_err("Ping", #error, 0)
EndIf
Func _err($func, $err, $opt = 2)
MsgBox(16, "ERROR", "Function: " & $func & #CRLF & "Error: " & $err)
If $opt = 1 Then TCPCloseSocket($socket)
If $opt > 0 Then TCPShutdown()
Exit
EndFunc
The output is always "RESULT:EMPTY".
I've tried to query other WhoIs services with various queries (for example whois.verisign-grs.com with "=facebook.com") but I get no response.
It's not a problem about my network, because I've tried a Nirsoft WhoIs tool ad it works.
I've downloaded a sniffer and this is the output when I start my script:
==================================================
Protocol : TCP
Local Address : 192.168.1.101
Remote Address : 198.108.0.18
Local Port : 23509
Remote Port : 43
Remote Host : whois.radb.net
Service Name : nicname
Packets : 5 {5 ; 0}
Data Size : 17 Bytes {17 ; 0}
Total Size : 274 Bytes {217 ; 57}
Data Speed : 0.0 KB/Sec
Capture Time : 07/07/2014 15:18:37:313
Last Packet Time : 07/07/2014 15:18:45:837
Duration : 00:00:08.523
==================================================
Content:
-i origin AS32934
It seems the packet is sent but no packet is received.
I just found out the solution: adding a #crlf at the end of TCPSend() request
$query = "-i origin AS32934" & #crlf

If Then statement in a loop

I'm creating a script that will open a program , login , and do specific tasks .
I created the login script with a variables adding and loopin through .
The problem is with the program , sometimes it just gives you an error on connection or a network problem .
What I need is IF the script gets the problem it'll reset it self and go through the account it got the error on , This is what I have so far and I just can't make it work
$t = 0
$n = "account"
$p = "password"
For $r = X To X
Run("program")
AutoITSetOption("MouseCoordMode", 0)
AutoITSetOption("WinTitleMatchMode", 3)
Do
Sleep(1000)
$t = $t + 1
Until WinActive("program") Or $t = 15
$t = 0
Sleep(1500)
Send("{TAB}")
Sleep(100)
Send("{TAB}")
Sleep(100)
Send("{Enter}")
Sleep(100)
Send($n & $r)
Sleep(200)
Send("{TAB}")
Sleep(200)
Send($p & $r)
Sleep(100)
Send("{Enter}")
Sleep(5500)
If $t > 14 Then
$r = $r - 1
Run(#ComSpec & " /c taskkill /F /im program.exe")
Do
Sleep(500)
$t = $t + 1
Until WinActive("Program - Update News") Or $t = 15
$t = 0
WinActivate("Program")
Sleep(2000)
MouseClick("Primary", 28, 12)
Sleep(1000)
MouseClick("Primary", 35, 125)
Sleep(1000)
MouseClick("Primary", 360, 175)
Sleep(2000)
Send("{ENTER}")
Sleep(2500)
Run(#ComSpec & " /c taskkill /F /im program.exe")
; EndIf
Next
Right now what it does is reruns the program without actually closing the error window
Hopefully this will give you a better idea of how to create a script and fix your problem. It is commented to give you an understanding of the steps taken.
#include <msgboxconstants.au3>
Local $t = 0, $n = "account", $p = "password" ; $n & $p are your own data
Local $r = ["X", "X", "X"], $e, $msg, $w ;$r is filled with your own data
AutoItSetOption("MouseCoordMode", 0)
AutoItSetOption("WinTitleMatchMode", 3)
HotKeySet("{ESC}", "Quit") ;If you want to exit the script, press the ESC key
Sender() ;call the function to run
While 1
If $e <> 0 Then ;if #error is set to a non-zero variable, wait 1 sec (1000 millisec) and run again
Sleep(1000)
Sender()
ElseIf $w = 0 Then ;if msgbox value is 0 AND retry OR ignore has been pressed, wait 1 second and run again
Sleep(1000)
$w += 1
Sender()
EndIf
WEnd
Func Sender() ;wrapped in function to make returning out of the function easier
For $r = "X" To UBound($r) ; for the first value in $r to the length of the array
Run("program")
$e = #error ;if Run sets #error then put #error and check the value with an if statement
If $e <> 0 Then
$msg = MsgBox(2, "ERROR", "error running program, exiting...")
If $msg = $IDABORT Then ;if we don't want to run the program again we abort
Quit()
ElseIf $msg = $IDRETRY Then ;if we want to retry then we hit retry
Return
ElseIf $msg = $IDIGNORE Then ;if we want to retry then we hit ignore as well
Return ;go out of the function because we have recieved an error and want to restart the function
EndIf
EndIf
SetError(0) ;sets #error to 0 so that we don't accidentally restart our program
Do
Sleep(1000)
$t += 1 ;add 1 to $t
Sleep(1500)
Send("{TAB}")
Sleep(100)
Send("{TAB}")
Sleep(100)
Send("{Enter}")
Sleep(100)
Send($n & $r)
Sleep(200)
Send("{TAB}")
Sleep(200)
Send($p & $r)
Sleep(100)
Send("{Enter}")
Sleep(5500)
If $t > 14 Then
$r -= 1 ;minus 1 from $r
Run(#ComSpec & " /c taskkill /F /im program.exe")
$e = #error
If $e <> 0 Then
$msg = MsgBox(2, "ERROR", "error running " & #ComSpec & " /c taskkill /F /im program.exe, Abort to Quit; Retry & Ignore to RETRY.")
If $msg = $IDABORT Then
Quit()
ElseIf $msg = $IDRETRY Then
Return
ElseIf $msg = $IDIGNORE Then
Return
EndIf
EndIf
EndIf
SetError(0)
Until WinActive("program") Or $t = 15
$t = 0 ;set $t to 0
Do
Sleep(500)
$t += 1
$w = WinActivate("Program")
If $w = 0 Then
$msg = MsgBox(2, "ERROR", "error activating program, Abort to Quit; Retry & Ignore to RETRY.")
If $msg = $IDABORT Then
Quit()
ElseIf $msg = $IDRETRY Then
Return
ElseIf $msg = $IDIGNORE Then
Return
EndIf
EndIf
$w += 1
Sleep(2000)
MouseClick("Primary", 28, 12)
Sleep(1000)
MouseClick("Primary", 35, 125)
Sleep(1000)
MouseClick("Primary", 360, 175)
Sleep(2000)
Send("{ENTER}")
Sleep(2500)
Run(#ComSpec & " /c taskkill /F /im program.exe")
$e = #error
If $e <> 0 Then
$msg = MsgBox(2, "ERROR", "error running " & #ComSpec & " /c taskkill /F /im program.exe, Abort to Quit; Retry & Ignore to RETRY.")
If $msg = $IDABORT Then
Quit()
ElseIf $msg = $IDRETRY Then
Return
ElseIf $msg = $IDIGNORE Then
Return
EndIf
EndIf
SetError(0)
Until WinActive("Program - Update News") Or $t = 15
Next
EndFunc ;==>Sender
Func Quit() ;will be called when we want to completely stop the script
Exit
EndFunc ;==>Quit

Resources