aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/delete-later
blob: 4db6973225c1494164e026a4d5bc5037c3fde86f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/expect -f

# MySQL database connection settings
set CRDB_host     localhost
set CRDB_DB       test
set CRDB_username root
set CRDB_password root

# set eiter one to 1 for verbose output
log_user 0
set comments 0

package require mysqltcl

# connect to MySQL database
set handle [::mysql::connect -host $CRDB_host -user $CRDB_username -password $CRDB_password]

# get server/usernames to clean up
set userlist [::mysql::sel $handle "SELECT UserID, server, username, password, retaindays from $CRDB_DB.fetchmail_user" -flatlist]

# loop through all users in database
foreach {userid server username password days} $userlist {
  if {$comments==1} { send_user "\r\nWorking on accound #$userid\r\n*******************************\r\n" }
  eval spawn telnet -l fetchmail_cleanup $server 110
  expect "ready"
  send "USER $username\r"
  expect "password"
  send "PASS $password\r"
  expect "OK"
  send "STAT\r"
  expect "+OK "
  expect -re "\[0-9]* "
  set anz $expect_out(0,string)
  if {$comments==1} { send_user "message count: $anz \r\n" }
  set i 0
  while { $i < $anz } {
    incr i
    send "UIDL $i\r"
    expect -re "\\\+OK $i \(.*\)\r"
    set uid $expect_out(1,string)
    ::mysql::exec $handle "insert ignore into $CRDB_DB.fetchmail values ($userid,'$uid',now());"
    set age [::mysql::sel $handle "SELECT DATEDIFF(now(),Fetchdate) from $CRDB_DB.fetchmail where UserID=$userid and UID='$uid'" -list]
    if {$comments==1} { send_user "Message #$i: UID: $uid , age: $age \r\n" }
    if {$age > $days} {
      send "DELE $i\r"
      expect "deleted"
      if {$comments==1} { send_user "Message $i deleted.\r\n" }
    }
  }
  send "quit\r"
  expect "signing off"
  ::mysql::exec $handle "delete from $CRDB_DB.fetchmail where DATEDIFF(now(),Fetchdate)>($days*2) and UserID=$userid;"
}
::mysql::close $handle
exit
t;Message-ID:" -i "X-Multidrop-Processing: <`hostname`>" >>$mbox rm -f $mbox.lock else (echo "Subject: Cannot lock mailbox for $user" & cat -) | /usr/lib/sendmail postmaster fi # # EOF # This obviously is very Linux (even RedHat?) dependant, locking mailboxes, creating mailboxes with the right permissions, probably even bash dependent. I would say that it should be fairly easy to port to other systems, but alas, my unix knowledge is lacking for that. I'll also rewrite it someday, a.o. that umask handling can be done much better and the location of the sendmail binairy should not be fixed. Now the only thing left to do is to retrieve the mail with fetchmail, using 'envelope "Delivered-To:"' in the poll line. The above script has added this line, so this is all that fetchmail needs. All parts of this solution need carefull examination. In particular I think the new rule lines may not catch all cases, although they worked for everything I threw at them and work satisfactorily in production. I'm also wondering if there is a more standard way to drop something in a mailbox. I yet have to investigate procmail, but all other MDA's mucked with the message and effectively undid my carefully added header. I'll experiment some more and rethink it all as I learn more. I'm still wondering, if I can get formail to include another received line.... "Received from localhost by dropmail for <user>...." to make it work without the envelope flag. Well I'll have to experiment. Do you know if there is a header I can add so fetchmail works out-of-the-box? Regards, Martijn Lievaart