Deleting Email. Review this Script?
[1/3] from: tim-johnsons:web at: 11-Aug-2006 13:12
Hi:
For several years, I've been using this script to run as both a CGI
process and a cron process from some server while I am traveling. What
it does is delete any email not sent to my email address. I.E. any mail
sent to a mailing list is deleted. Doesn't take care of spam, but I can
handle that thru webmail.
The problem is that when I run this manually, I note that have to run it
more that once to "clean out" the email.
I'm presenting the script (with user and password obfuscated) for review
perhaps someone can point out code changes that will delete all ML
messages in one pass.
TIA
Tim
;; Code follows
#!/usr/bin/rebol -cs
REBOL[
Title: "popscan"
Date:
File: %popscan.r
Purpose: {scan mailbox for mailing list messages. Delete where found.}
URL: http://localhost/cgi-bin/AIS/popscan.r
]
;; =======================================================
print "Content-Type: text/html^/" ;
email-address: "tim-johnsons-web.com"
user: "********************"
pwd: "******************"
server: "postman.johnsons-web.com"
;; =======================================================
print "<pre>"
url: to-url rejoin ["pop://" user ":" pwd "-" server]
?? url
main: func[
/local mailbox deleted msg recipient ML
][
deleted: 0
ML: false
print "Connecting ......"
mailbox: open url
print rejoin[(length? mailbox) " messages found."]
while [not tail? mailbox][
msg: import-email first mailbox ; imports the email message
recipient: to-string msg/to ; sets the recipient ('TO:') value in the message
either all[(recipient = email-address) ][
print "<font color=^"blue^">------- KEEPING ----------------------"
print rejoin[ "From: " msg/from newline "Subject: " msg/subject </font>]
][
print "<font color=^"red^">------- DELETING ----------------------"
print rejoin[ "From: " msg/from newline "Subject: " msg/subject </font>]
ML: true
deleted: deleted + 1
]
mailbox: next mailbox ; next message
if any[ML][
print rejoin["deleting number " deleted]
remove skip mailbox -1
ML: false
]
]
print rejoin[deleted " message(s) were deleted"]
]
main
print "</pre>"
--
Tim Johnson <tim-johnsons-web.com>
http://www.alaska-internet-solutions.com
[2/3] from: rebol::ccsducto::com::ar at: 11-Aug-2006 18:59
Hi Tim,
I'm not a super specialist, but what happen if you try this?:
to be brief
either ok? [
keeping procedure
mailbox: next mailbox
][
deleting procedure
remove mailbox
]
Perhaps "remove skip mailbox -1" is not working well
Regards
César
[3/3] from: Izkata::Comcast::net at: 12-Aug-2006 1:21
I didn't take too close a look at it (and don't really have something to
test it on at the moment), but take a close look at the "mailbox: next
mailbox ; next message" line, and how series look in general:
>> while [not tail? S][
[ A: first S
[ if #"3" = first S [remove S]
[ B: first S
[ S: next S
[ print rejoin [A {, } B {, } first S]
[ ]
1, 1, 2
2, 2, 3
3, 4, 5
5, 5, 6
6, 6, 7
7, 7, 8
8, 8, 9
9, 9, 0
** Script Error: Out of range or past end
** Where: rejoin
** Near: first S
See how it skipped weirdly when it hit the character being checked for?
When your script finds something to be remove, it immediately removes it -
which automatically means "first mailbox" has already advanced to the next
one, making the "mailbox: next mailbox" line skip the email that goes after
the removed one.
It doesn't look like it because of the way yours skips backwards to remove
it, but it does. I would suggest cleaning up the function's code like this:
main: func[
/local mailbox deleted msg recipient
][
deleted: 0
print "Connecting ......"
mailbox: open url
print rejoin [(length? mailbox) " messages found."]
while [not tail? mailbox][
msg: import-email first mailbox ; imports the email message
recipient: to-string msg/to ; sets the recipient ('TO:') value in
the message
either recipient = email-address [ ;You only have one condition,
so 'all []' is not needed
print "<font color=^"blue^">-------
KEEPING ----------------------"
print rejoin[ "From: " msg/from newline "Subject: " msg/subject
</font>]
mailbox: next mailbox ; next message ;You're keeping it, so
advance to the next message
][
print "<font color=^"red^">-------
DELETING ----------------------"
print rejoin[ "From: " msg/from newline "Subject: " msg/subject
</font>]
remove mailbox ;remove current message ;You're deleting it and
advancing to the next message with this here.
deleted: deleted + 1
print rejoin["deleting number " deleted]
]
]
print rejoin[deleted " message(s) were deleted"]
]