[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4688: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3823)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4690: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3823)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4691: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3823)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4692: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3823)
Poco Forums • View topic - Strip Attachments script, help pls

Strip Attachments script, help pls

Scripting questions and ideas

Moderators: Eric, Tomas, robin, Michael

Strip Attachments script, help pls

Postby spadesmaster » Mon Nov 15, 2004 10:46 am

I am having some trouble writing a script that will strip out text message attachments and store them directly into the body of the email message (so it prints, replies quoted, etc.). Don't ask me why, I just do...

At any rate, I could not find way to read a file in PocoScript so I did this via a simple external Perl script. This part I know works, but the part that doesn't is:
a) PasteFromClipboard doesn't seem to load the string with the clipboard, even when I verified the text is there.
b) I don't see to understand how AppendBody works, I just want to append this string to the end of the body, but I don't think I'm using it right.
c) DeleteAttached function just doesn't seem to delete the attachment from the current message as advertised.

The full script (and the perl utility routine) is below, can anyone help, please?

{### Strip Attachments and Edit Message
{ Script will append attached text to internal msg body (for Badhand reports)

{# STEP 1 : Read attachment file into a string (if attachment)}
ReadAttached $strAttachedFiles %message
If (CharCount($strAttachedFiles) = 0) Then Exit
Set $msg "Attachments found: "
AddStrings $msg $strAttachedFiles
MessageBox $msg

{## STEP 2: Suck text out of text attachment via clipboard}
{## do this via perl script since poco doesn't have file functions}
ExecuteAndWait "H:\pocomail\Scripts\filetoclip.pl" $strAttachedFiles
Set $strAttachText "Attachment text: "
PasteFromClipboard $strClipboard
AddStrings $strAttachText $strClipboard
MessageBox $strAttachText

{## STEP 3: Append string text to body of message}
AppendBody $strAttachText

{## STEP 4: Remove the text attachment from the original message}
DeleteAttached %message $strAttachedFiles




# filetoclip.pl
#
# Simply loads the file into the system clipboard.
# This is used by PocoMail script to suck attachment into message
#
# Created 11/15/04 by Christopher

# Load file provided in argument into a string
if ($#ARGV < 0) {
print "Usage: filetoclip.pl <file>\n";
exit;
} else {
$strFname = $ARGV[0];
}
open (FILE, "<$strFname") || die "FAILED to read: $strFname: $!.";
@file = <FILE>; $strFileText = join "", @file; close FILE;

# Now dump the file string out onto the system clipboard for PocoMail
use Win32::Clipboard;
$CLIP = Win32::Clipboard();
$CLIP->Set($strFileText);
spadesmaster
Poco Tourist
 
Posts: 20
Joined: Tue Aug 17, 2004 7:18 am

Postby Sandy » Mon Nov 15, 2004 12:10 pm

I can't take the time to walk thru your logic, but if it helps here is a script I wrote some time ago that re-embeds attachments and deletes the file from the attachment folder. It is NOT polished, nor is it set up for anyone but me, and it has a few quirks, but it does work. Perhaps you can steal ideas from this.

Code: Select all
{Takes a message whose attachments have been stripped to the poco defined attachments folder
{and re-embeds those attachments into the msg; the modified msg is saved back into the
{current mailbox; the original msg is deleted; and the stripped files remaining
{in the attachments folder can be deleted if desired


{Author: Sandy McKean (based on a script by noggin - I think it was)

{Written: 10/13/02

{Modified:
{   11/15/04 - since it appears that the problem with saving into the current mailbox was fixed
{            in Poco some time ago, I removed "safety" step of saving to the "Testing" mailbox;
{            the script now saves back into the current mailbox; note however, that the msg
{            will show up twice in the current mailbox while one is between the InputBox replies;
{            I will presume that the old problem of IDX file screw-ups when saving into the
{            current mailbox are gone, and this "double instance" is just a fluke of PocoScript;
{            however I will need to watch for a while to be sure there are no screw-ups

{Notes:
{      There can be multiple files as long as NONE are still embedded; note that
{        all files be handled independently with one running of the script; that is,
{        you can embed and/or delete stripped attachments all independently from each other

{Future enhancements:
{      Right now this code only looks at the the 1st attachment to determine
{        whether ALL attachments are either already embedded or stripped; it assumes that ALL
{        attachments are in the same state; if this were not so, which would be rare if ever, then
{        this code will not work properly; to fix this, I should loop thru all attachments and make
{        decisions on each individual attachment (note that there are script commands to manipulate
{        multi-line variables not now used that could help with this) 1/7/03


Set $OnErrorMessage "Error in sgmReEmbedAttachments.poc"
Set $OnErrorGoTo "DONE"

{extract header info for display in msg to user
ReadHeader $AttachHeader "X-Poco-Attachment:" %message
ReadHeader $Subject "Subject:" %message
Trim $Subject
ChopString $Subject 36 9999
Set $MessageTitle ""
AddStrings $MessageTitle "Enter y to re-embed this attachment.\n"
AddStrings $MessageTitle "MESSAGE: " $Subject "\n"
{get list of all attachments and set variables
ReadAttached $Attachments %Message
LineCount #Total $Attachments
Set &Unchanged True
Set #i 0

{do a check to insure that 1st attachment is in the Poco attachments folder; if not, exit
GetLine $TestIt 0 $Attachments
StringPos #n $attachpath $TestIt
If #n ! 0 then LOOP
MessageBox "Aborting, attachments are either already embedded or are not in Poco Attachment folder"
Goto DONE

:LOOP
{loop thru each attachment re-embbing it into the msg, also delete attachment header
{  associated with that attachment
If #i = #total Then SAVEIT
GetLine $TheFile #i $Attachments
Trim $TheFile
Inc #i
Set $Tmp ""
AddStrings $Tmp $MessageTitle "ATTACHMENT: \"" $TheFile "\""
InputBox $Response $Tmp
If $Response ! "y" Then LOOP
Attachfile %Message $TheFile
DeleteHeader "X-Poco-Attachment:" %message
Set &unchanged False
Goto LOOP

:SAVEIT
{now that the msg has all selected attachments re-embedded,
{  save newly formed msg back to the mailbox it came from
{  assuming at least one attachment was processed
If &unchanged Then DONE
AppendToFile true
SaveMessage %message $currentMailbox
DeleteMessage %Message

{reset variable for file delete loop
Set $messageTitle ""
AddStrings $MessageTitle "Enter y to delete old attachment file.\n"
AddStrings $MessageTitle "MESSAGE: " $subject "\n"
Set #i 0

:LOOP1
{loop thru and delete selected attachments that had been stripped to the Poco attachment folder
If #i = #total Then DONE
GetLine $TheFile #i $Attachments
Inc #i
Set $tmp ""
AddStrings $tmp $MessageTitle "ATTACHMENT: \"" $TheFile "\""
InputBox $response $tmp
If $response ! "y" Then LOOP1
{DeleteAttached %Message $TheFile............not necessary I think
DeleteFile $TheFile
Goto LOOP1

:DONE
Exit
Sandy
 

Postby Hogyt » Mon Nov 15, 2004 1:09 pm

Beaten to it ;-)

Here's what i knocked up, with no comments and little testing so don't blame me if it doesn't work ;-) Attachments with .txt extension are appended to the body of a new email and the original deleted. It would probably benefit from a content-type check and some error detection:

Code: Select all
ReadRawBody $body %message
ReadAttached $files %message
LineCount #n $files
If #n = 0 Then quit
Set #count 0

:loop
Dec #n
GetLine $file #n $files
FileExists &exist $file
Not &exist
If &exist Then skip
CharCount #len $file
SubIntegers #len 4
Set $ext $file
ChopString $ext 1 #len
If $ext ! ".txt" Then skip
Inc #count
OpenBody $text $file
AppendBody $body $text
DeleteAttached %message $file

:skip
If #n > 0 Then loop
If #count = 0 Then quit

AssignBody %message $body
SaveMessage %message $currentmailbox
DeleteMessage %message

:quit
Exit
Mat
Hogyt
Poco Enthusiast
 
Posts: 241
Joined: Thu Jul 29, 2004 11:22 am
Location: England

Postby spadesmaster » Fri Nov 19, 2004 10:27 am

This seems to do close to what I need, but it does odd things based on if message name, body, or text is highlighted. It also seems to sometimes grab other attachments from other messages instead.

I am trying to figure it out, but it seems like you have given me enough to work with here, so I will post it back when I get a cleaned up, working, version in case someone else could benefit from it.

Thanks!
Christopher
spadesmaster
Poco Tourist
 
Posts: 20
Joined: Tue Aug 17, 2004 7:18 am

Postby spadesmaster » Fri Nov 19, 2004 11:43 am

The main problem is that it trashes the newlines in the message, putting the text all on one single line.

For example if the body is:
1
2
3

I get "1 2 3" in the Stripped or Edited message...

I decided to simplify the problem by creating a simpler 'Edit Message' script first (which I kinda wanted anyways). It has the same problem, namely trashing the newlines in the messsage.

I must not be understanding something about the strings and newlines the way PocoScript uses them. The string usage in PocoScript just doesn't seem to make sense to me and I suspect there is some fundamental string thing I am just not getting...

Here is my 'Edit Message' script that works great other than the newlines getting trashed. Perhaps if you can help me correct this issue, I can figure out whats up with the Strip Attachments script and fix it as well:

Code: Select all
{### Edit Message
{ This script allows direct editing of the message body in Notepad

{## STEP 1: Dump out the message body to a temporary file
ReadBody $strOldBody %message   
SaveBody $strOldBody ~msg.tmp

{## STEP 2: Open up the message text in Notepad and wait
ExecuteAndWait notepad.exe ~msg.tmp

{## STEP 3: Suck the edited text back into a string
OpenBody $strNewBody ~msg.tmp

{## STEP 4: Final save of new body and message and remove old
AssignBody %message $strNewBody
SaveMessage %message $currentmailbox
DeleteMessage %message
spadesmaster
Poco Tourist
 
Posts: 20
Joined: Tue Aug 17, 2004 7:18 am

Postby Hogyt » Fri Nov 19, 2004 12:00 pm

Are you running the scripts on messages with an html content type? If so thats what i meant about the content type check, since if you're then adding text to html then you'll also need to add in line breaks. You can try using ReadRawBody to read in the body with html included for your edit message script.

This seems to do close to what I need, but it does odd things based on if message name, body, or text is highlighted.

If part of a message is highlighted then that text is treated as the body of the email. For example, select some text then hit reply and you'll see the selected text in your reply instead of the message body. In the same way it affects the ReadBody and ReadRawBody commands so that they contain the selected text. An easy workaround is not to select any text before running the script ;-)

It also seems to sometimes grab other attachments from other messages instead.

Compressing the mailbox might help but i don't know what is happening there :? Sorry!
Mat
Hogyt
Poco Enthusiast
 
Posts: 241
Joined: Thu Jul 29, 2004 11:22 am
Location: England

Postby spadesmaster » Fri Nov 19, 2004 12:14 pm

Thanks for your answer about the highlighting thing, that makes sense.

Don't think these are HTML messages, but I am not sure. Does the yellow versus white background on a message body have something to do with this?

You mentioned 'adding newlines' in, yet I dont know how to do that. Do I have to then load these in line by line and use some special coded character for a newline?

My simple Edit Message script has the same problem so I think if you can help me with that, I can figure out what's up with the more complicated Strip Attachments script...
spadesmaster
Poco Tourist
 
Posts: 20
Joined: Tue Aug 17, 2004 7:18 am

Postby spadesmaster » Fri Nov 19, 2004 12:17 pm

Ok using ReadRawBody and prepending '<PRE>' to the beginning of the raw text body I input from Notepad seems to have done the trick...

I think I can get this working now, thanks for your help!
spadesmaster
Poco Tourist
 
Posts: 20
Joined: Tue Aug 17, 2004 7:18 am

Postby Hogyt » Fri Nov 19, 2004 12:27 pm

I'm glad ReadRawBody helped. When you use ReadBody on an html email it takes the plain text and strips out the html, so when you save it back again it'll be missing the formatting and hence you get joined up lines.

Differing background colours are certainly a sign of an html email! To see you can look at the raw message (right click and view raw message) and have a look at the content-type header (if it's not visible, right click in an email message and select View Headers->Show Full Headers). If the content-type is text/plain then it's plain text, if it's text/html then it's html. There's lots of other content-types too, just to confuse the matter ;-)
Mat
Hogyt
Poco Enthusiast
 
Posts: 241
Joined: Thu Jul 29, 2004 11:22 am
Location: England

Postby Hogyt » Fri Nov 19, 2004 12:38 pm

Also look at AssignStyledBody for when dealing with html mail. And for the script i posted earlier, another good thing to add would be a check on #PocoScriptMode (see the variables section in the PocoScript reference) to make sure the script is run in the mode you want (eg. manually, on incoming messages, etc.)

Good luck with it! :)
Mat
Hogyt
Poco Enthusiast
 
Posts: 241
Joined: Thu Jul 29, 2004 11:22 am
Location: England

Postby spadesmaster » Wed Dec 08, 2004 7:32 am

Thanks for the help. I finally remembered to post my final source in hopes that it might help someone else with a similar problem:

Code: Select all
{### Strip Attachments and Edit Message
{ Script will append attached text to internal msg body (for Badhand reports)

{# STEP 1: Read attachments in message
ReadRawBody $body %message
ReadAttached $files %message
LineCount #n $files
If #n = 0 Then quit
Set #count 0

{# STEP 2: Loop through lines in attachments and append to a string
:loop
Dec #n
GetLine $file #n $files
FileExists &exist $file
Not &exist
If &exist Then skip
CharCount #len $file
SubIntegers #len 4
Set $ext $file
ChopString $ext 1 #len
If $ext ! ".txt" Then skip
Inc #count

{## STEP 3: Append string text to body of message}
OpenBody $text $file
AppendBody $body $text

{## STEP 4: Remove the text attachment from the original message}
DeleteAttached %message $file

{## STEP 5: Repeat if more than one attachment
:skip
If #n > 0 Then loop
If #count = 0 Then quit

{## STEP 6: Final save of new body and message
AssignBody %message $body
SaveMessage %message $currentmailbox
DeleteMessage %message

:quit
Exit
spadesmaster
Poco Tourist
 
Posts: 20
Joined: Tue Aug 17, 2004 7:18 am


Return to PocoScript Help and How-To

Who is online

Users browsing this forum: No registered users and 1 guest

cron