Using Net::IMAP::Simple to move mailboxes around

In the past I have described using Net::POP3 (combined with tmail from UW-IMAP and a Perl script) to move inboxes from an old (and proprietary) mail server to a new one. However, if your users keep folders like Sent, Drafts and Trash on the mail server, you have to move these also. And this is where Net::IMAP::Simple enters the picture:

#!/usr/bin/perl

use Net::IMAP::Simple;
use Sys::Syslog;

$host = shift or die;
$user = shift or die;
$pass = shift or die;

openlog("imapcat-tmail", 'pid', 'mail') or die;
syslog('info', 'fetching mail for user %s', $user);
closelog();

# When debugging experiment only on myself
## if ($user ne "yiorgos") { exit 0; }

# Folders that we migrate.  CAUTION:  The Trash folder has to be the last in
# line!  That is because $imap->quit expunges the last open folder.  We want 
# this to be Trash and no other.

# For the time being, INBOX is being carried over by pop3cat-tmail.
## push @folder_list, "INBOX";
push @folder_list, "Sent";
push @folder_list, "Drafts";
push @folder_list, "Trash";

$imap = new Net::IMAP::Simple($host) or die;
$imap->login($user, $pass) or die;

while ($folder = shift(@folder_list)) {
        $nm = $imap->select($folder);
        next unless $nm;
        for ($i = 1; $i < = $nm; $i++) {
                $msg = $imap->get($i);
                open T, "| /storage/bin/tmail2 -I $folder $user";
                print T for @{$msg};
                close T;
                $imap->delete($i);
        }
        $imap->expunge_mailbox($folder);
}

$imap->quit;

## print "Bye.\n";
exit 0;

Some notes:

  1. tmail2 is a local version of tmail with a local quota function implemented in tquota.c. It also allows for the -I switch to be used by ordinary users.
  2. This script should be used with caution. When using it to move mailboxes around, keep in mind that all messages will be marked as new when moved.
  3. I must find time to do the same thing forking a version of mailutil that accepts the user password via a command line switch instead of the terminal.

O AliasFile=null: and Lenny’s sendmailconfig

As is described in the bat book, when you set:

O AliasFile=null:

no aliases will ever be found. However, if you put in your sendmail.mc:

define(`ALIAS_FILE’, `null:’)dnl

and the run sendmailconfig on a Debian Lenny, update_db will complain because it cannot handle the null: declaration. If you are bothered by this warning message, the solution is simple. Do not declare null: and instead:

# cp /dev/null /etc/mail/aliases
# newaliases

Ουρές

Τη Δευτέρα έζησα ακόμα ένα ΤΣΜΕΔΕ experience. Πιο δυνατό αυτή τη φορά, μια και από την 1η Οκτωβρίου 2008 χάρη στην ενοποίηση των ταμείων τα πράγματα έχουν γίνει πιο σκληρά. Αυτή τη φορά όμως πήγα κατάλληλα προετοιμασμένος με το “The Psychology of Waiting Lines” του Donald A. Norman (το οποίο “πατάει” πάνω στο ομότιτλο, αλλά παλαιότερο του David Maister) στο χέρι για να περάσει και η ώρα.

Το κείμενο του Norman χωρίζεται σε δύο ενότητες, από τις οποίες την πρώτη (Eight Design Principles for Waiting Lines) την διάβασα περιμένοντας. Για να δούμε αυτές τις αρχές:

  1. Emotions Dominate

    “Emotions color the experience and, more importantly, how the experience will be remembered.”

    – Δεν κάνετε ούτε για εργαζόμενος στο Δήμο Αθηναίων για τη συλλογή των σκουπιδιών!

    Είπε στον αρχίατρο ένας φανερά εκνευρισμένος ασφαλισμένος. Είπε και άλλα περισσότερα στον σεκιουριτά που τον απομάκρυνε από τον αρχίατρο μεν, αλλά έκανε το λάθος να ανοίξει κουβέντα μαζί του προσπαθώντας να δικαιολογήσει τον αρχίατρο. Παιδί μου, ο ασφαλισμένος έχει το πρόβλημά του στο μυαλό και μόνο αυτό. Δεν τον ενδιαφέρει το ζόρι που τραβάει ο άλλος, παρά μόνο εάν πρόκειται για κλέψιμο σειράς στην ουρά (και αυτό μόνο εάν χάνει και αυτός).

    “Make the surrounds bright and cheery, attractive and inviting.”

    Βέβαια, δε μας κάνει καρδιά να φύγουμε από εκεί μέσα.

  2. Eliminate confusion: Provide a Conceptual Model, Feedback and Explanation

    – Μα έχω δώσει το βιβλιάριό μου πολύ πριν από αυτά που μοιράζετε τώρα. Γιατί αργεί το δικό μου;

    “One of the major determiners of emotional unhappiness is fear of the unknown and uncertainty.”

    Υπάρχει μία ουρά με αριθμημένα χαρτάκια. Εξυπηρετείται συνήθως από τρία ταμεία. Οι υπάλληλοι μαζεύουν στοίβες από βιβλιάρια και τις προωθούν στους γιατρούς. Και ενώ η πρώτη ουρά είναι FIFO, το πως θα βγουν από τους γιατρούς είναι καθαρά τυχαίο. Ο ασφαλισμένος δεν το ξέρει αυτό και σίγουρα δεν το θέλει αυτό. Την τελευταία φορά είδα να περνάνε στους γιατρούς και το αριθμημένο χαρτάκι. Ελπίζω να χρησιμοποιείται η πληροφορία, αν και τα “μα άλλοι που ήρθαν μετά από εμένα έχουν φύγει” δεν έλλειψαν και τότε.

  3. The Wait Must Be Appropriate

    “when there is a large crowd awaiting service, yet only a few people providing it, then the tolerance shifts to blame the service provider for not reacting to the need”

    Είπαμε τρεις ταμίες. Απίστευτος κόσμος και έκδοση αριθμών μέχρι τις 12:00 (προ ενοποίησης μέχρι τις 13:00).

  4. Set expectations, Then Meet or Exceed Them

    Το χαρτάκι γράφει εκτιμώμενο χρόνο εξυπηρέτησης. Αλλά αυτός είναι μέχρι να φτάσει το βιβλιάριο στον υπάλληλο. Από τη στιγμή εκείνη και μετά δεν υπάρχει κανένας τρόπος να ξέρεις πότε θα το ξαναπάρεις στα χέρια σου. Δεν υπάρχει τέλος, μόνο νεύρα.

  5. Keep People Occupied: Filled Time Passes More Quickly Than Unfilled Time

    Μερικοί παίρνουν το χαρτάκι και επιστρέφουν σε μία ώρα για να εξυπηρετηθούν. Άλλοι έχουν ραδιοφωνάκια. Άλλοι εφημερίδες ή βιβλία. Και άλλοι πιάνουν την κουβέντα χωρίς να τους νοιάζει εάν ο διπλανός θέλει να μιλήσει ή όχι. Άλλοι είναι βυθισμένοι στο πρόβλημά τους και ο χρόνος δεν περνάει.

  6. Be Fair

    “If a waiting line seems reasonable, even though it is a negative experience, it will not necessarily trigger a strong negative emotion. The emotion comes if the line appears to be arbitrary, unpredictable, and worst of all, unfair”

    Είπαμε, δεν υπάρχει τρόπος να ξέρεις πως θα πάρεις το βιβλιάριο πίσω πριν από τον επόμενο από εσένα.

    “It is also very important not to let people cut ahead of others”

    – Ρε παιδιά μια ερώτηση έχω να κάνω, δε σας παίρνω τη σειρά

    Υπάρχουν όμως κάτι αξιοπρεπείς αρχαίοι μηχανικοί που διδάσκουν υπομονή και σεβασμό στην ουρά. Και ενώ λένε τα ίδια λόγια που θα έλεγες (ή είπες) κι εσύ, ο “μια ερώτηση” τους ακούει.

  7. End Strong, Start Strong

    Υπάρχει μια ουρά που όσο μεγάλη κι αν είναι κανείς δεν διαμαρτύρεται: Είναι η ουρά του ταμείου που εξαργυρώνει τα εντάλματα πληρωμής. Υπάρχει τέλος και ανταμοιβή.

    Αντίθετα η απογοήτευση ξεκινάει από την ώρα που κόβεις το χαρτάκι και διαπιστώνεις πως είσαι 120 νούμερα πίσω: “Πόση ώρα θα κάτσω στο αχούρι;”

  8. Memory of an Event Is More Important Than the Experience

    Αυτό το post είναι μια απόδειξη. Οι απαντήσεις που παίρνει κανείς όταν λέει “Έχω να πάω στο ΤΣΜΕΔΕ” επίσης.

Δεν θέλω να κάνω τον έξυπνο. Υπάρχουν μέρες που και οι δικές μας ουρές είναι ανυπόφορες. Δεν τα βάζω με τους υπάλληλους. Αυτοί άλλωστε βρίσκονται σε αυτές τις συνθήκες καθημερινά και επί ώρες και με το πρόβλημα του καθενός μπροστά τους. Αλλά κάποιος θα πρέπει λίγο να σκεφτεί από το πως είναι το κτίριο που εξυπηρετεί τον κόσμο, τις διαδρομές που κάνει ένας ασφαλισμένος ή ένας υπάλληλος για να γίνει μία εργασία (π.χ. εξυπηρετείσαι για τις εισφορές στον 2ο, αλλά πρέπει ενδιάμεσα στη διαδικασία να πρωτοκολλήσεις εσύ και όχι η υπηρεσία, πηγαίνοντας στο 3ο), μέχρι εάν μπορούν οι υπάλληλοι στον ημιόροφο να κάνουν δουλειά με τόσο κόσμο κυριολεκτικά πάνω από το κεφάλι τους. “It is all a matter of design” που λέει και ο Norman στην τελευταία του πρόταση.

Γραπτή ψυχοθεραπεία τέλος.

The ethics of jumping the queue.

#include <std/disclaimer.h>

(previously on ΤΣΜΕΔΕ) (next)

on problem solving

– Λογικά συμβαίνει αυτό και δεν παίζει

Πάνω στη δουλειά είμαι φωνακλάς. Τις περισσότερες φορές ίσως αδικαιολόγητα. Υπάρχει όμως ένα πράγμα για το οποίο φωνάζω συνέχεια:

– Μην κάνεις λογικές υποθέσεις!

Έχω παρατηρήσει πως στην διαδικασία επίλυσης προβλημάτων, όταν η πρόταση ξεκινάει με την λέξη “λογικά”, τότε από υπόθεση αυτόματα γίνεται απόδειξη με όλα τα διαθέσιμα στοιχεία να αγνοούνται και όλα τα ενδιάμεσα βήματα να παραλείπονται. Όταν όμως το guesstimation πέσει έξω (και είναι θέμα τεράστιας εμπειρίας να μην πέφτει κανείς έξω) αυτό σημαίνει πως πολύς χρόνος έχει περάσει χωρίς αποτέλεσμα.

Επειδή ονομάζουμε κάτι λογικό, δεν σημαίνει πως είναι κιόλας!

Αντιγράφω από το “Forensic Engineering Investigation“:

There should be a large foundation of evidence and facts at the bottom. These facts then form the basis for analysis according to proven scientific principles. The facts and analysis, taken together, support a small number of conclusions that form the apex of the pyramid.

Conclusions should be directly based on the facts and analysis, and not on other conclusions or hypotheses. If the facts are arranged logically and systematically, the conclusions should be almost self-evident. Conclusions based on other conclusions or hypotheses, that in turn are only based upon a few selected facts and very generalized principles, are a house of cards. When one point is proven wrong, the logical construct collapses.

Αποφύγετε να χρησιμοποιείτε τη λέξη “λογικά” όταν προσπαθείτε να βρείτε τι συμβαίνει:

– Λογικά συμβαίνει το Α επειδή έχω το Β

Προγραμματίζεται το μυαλό σε λανθασμένη πορεία. Αντί το Β να είναι πιθανό αίτιο, μετατρέπεται σε βεβαιότητα. Και αντί να ψάξει κανείς να βρει όλα τα πιθανά αίτια, προσπαθεί να αποδείξει πως το συγκεκριμένο Β προκαλεί το Α, ακόμα κι αν δεν το προκαλεί.

Update 2009/08/05: Έτυχε να διαβάσω το “Strong Inference” [pdf] το οποίο περιέχει αναφορές και από το “The Method of Multiple Working Hypotheses” του T. C. Chamberlin. Αντιγράφω σχετικά:

Chamberlin says our trouble is that when we make a single hypothesis, we become attached to it.

“The moment one has offered an original explanation for a phenomenon which seems satisfactory, that moment affection for his intellectual child springs into existence and as the explanation grows into a definite theory his parental affections cluster about his offspring and it grows more and more dear to him […] There springs up, also, an unconscious pressing of the theory to make it fit the facts to make them fit the theory.”

To avoid this grave danger, the method of multiple hypotheses is urged. It differs from the simple working hypothesis in that it distributes the effort and divides the affections.

Each hypothesis suggests its own criteria, its own means of proof, its own method of developing the truth, and if a group of hypotheses encompass the subject on all sides, the total outcome of means and of methods is full and rich.

Chamberlin thinks the method “leads to certain distinct habits of mind” and is of prime value in education. When faithfully followed for a sufficient time, it develops a mode of thought of its own kind which may be designated the habit of complex thought.

(previous)

#define SYSTEM_ADMINISTRATOR

SysAdminDay.comΈχω περιγράψει μια διαδρομή που μπορεί να ακολουθήσει κανείς για να γίνει system administrator. Πολλές φορές όμως είναι πιο εύκολο να εξηγεί κανείς πως μπορεί να γίνει αυτό παρά να εξηγεί τι είναι. Στο Jargon File για παράδειγμα, ο system administrator ορίζεται ως:

admin: /ad·min΄/n.

Short for ‘administrator’; very commonly used in speech or on-line to refer to the systems person in charge on a computer. Common constructions on this include sysadmin and site admin (emphasizing the administrator’s role as a site contact for email and news) or newsadmin (focusing specifically on news). Compare postmaster, sysop, system mangler.

Αυτός ο ορισμός δεν είναι όμως αρκετός για να απαντήσει στον πατέρα μου την ερώτηση:

– Τελικά παιδί μου εσύ τι δουλειά κάνεις;

Για να φτάσει σε αυτή την ερώτηση έχουν προηγηθεί μια σειρά από άλλες. Η σειρά πάει κάπως έτσι:

– Δηλαδή εσύ γράφεις προγράμματα;
– Ναι, αλλά δεν είναι αυτή η δουλειά μου.
– Φτιάχνεις υπολογιστές;
(εννοεί με το κατσαβίδι και όλα)
– Ναι, αλλά δεν είναι αυτή η δουλειά μου.
– Ναι, αλλά τους διορθώνεις.
– Ναι, αλλά δεν είναι η δουλειά μου το service!
– Τελικά παιδί μου εσύ τι δουλειά κάνεις;

Ούτε μπορεί να απαντήσει στη γυναίκα μου την ερώτηση:

– Μα καλά εσύ γιατί δεν μπορείς να έχεις ένα κανονικό ωράριο;

Ούτε φυσικά μπορεί να απαντήσει στην παρατήρηση της ξαδέρφης μου:

– Μα γιατί έχεις το laptop μαζί σου Γιώργο στις διακοπές; Στο Δημόσιο δε δουλεύεις;0

Θα μπορούσα φυσικά να απαντάω “Είμαι πιανίστας σε μπουρδέλο“. Δε βοηθάει όμως.

Ο φίλος μου ο Σ. εξήγησε μια φορά στη γυναίκα μου:

– Πες πως είναι πυροσβέστης και στη βάρδιά του δεν έχει πιάσει καμιά φωτιά. Και μετά τη βάρδια πυροσβέστης είναι, οπότε εάν υπάρχει φωτιά και δεν υπάρχει άλλη βάρδια, αυτός πρέπει να τη σβήσει.

Για να εισπράξει την απάντηση:

– Φίλος του δεν είσαι;

Αφού οι παραπάνω προσπάθειες δεν έφεραν αποτέλεσμα, δοκίμασα να δω το πρόβλημα από μια άλλη γωνία:

Τι είναι σύστημα;

Στο “What is a Systems Approach?” περιέχονται μια σειρά από ορισμοί που απαντάνε στο ερώτημα “Τι είναι Σύστημα;”. Για την περίπτωσή μας ο συνδιασμός των παρακάτω δύο δείχνει αρκετός:

A system is a set of variables sufficiently isolated to stay discussable while we discuss it. — W. Ross Ashby.

και:

A system is a representation of an entity as a complex whole open to feedback from its environment.

Και πράγματι τα συστήματα που διαχειριζόματε (εμείς οι system administrators) έχουν πολλές μεταβλητές (εξαρτήσεις εάν θέλετε) και είναι πολύπλοκες οντότητες που αλληλεπιδρούν με το περιβάλλον στο οποίο εντάσσονται (για οποιοδήποτε ορισμό του περιβάλλοντος, π.χ. εταιρικό περιβάλλον και ανάγκες, το Internet, οι ίδιοι οι χρήστες του συστήματος, κ.ο.κ.).

Ακόμα καλύτερα το ίδιο άρθρο μιλάει για “organized complexity” που είναι το ίδιο με αυτό που γράφει ο Tom Limoncelli στο “Time Management for System Administrators“: “I am a system administrator! I manage chaos for a living!”

Στο ίδιο άρθρο όμως υπάρχει ένα διαμάντι:

The responsibility of the systems engineer was the choice of the “technical path” between theory and application in order to create new services; improve the quality of existing services; or lower their cost.

Με το παραπάνω φαίνεται να συμφωνεί και ο θεωρητικός του επαγγέλματος, ο Mark Burgess ο οποίος στο “Analytical Network and System Administration” γράφει:

System administration is about the design, running and maintenance of human-computer systems. Human-computer systems are ‘communities’ of people and machines that collaborate actively to execute a common task1.
:
System administration is primarily about the technological side of a system: the architecture, construction and optimization of the collaborating parts, but it also occasionally touches on softer factors such as user assistance (help desks), ethical considerations in deploying a system, and the larger implications of its design for others who come into contact with it. System administration deals first and foremost with the system as a whole

Όσο βατοί και βολικοί κι αν φαίνονται οι παραπάνω ορισμοί και περιγραφές, πάσχουν: Είναι απλοί και γίνονται κατανοητοί από ανθρώπους που ήδη ξέρουν τι είναι ένα σύστημα ή τι δουλειά κάνει ένας system administrator, αντιλλαμβάνονται τις απαιτήσεις και την πολυπλοκότητα. Έτσι το ερώτημα παραμένει:

– Εσύ παιδί μου, τι δουλειά κάνεις;

Τελικά ίσως το μόνο που να χρειάζεται είναι κάποιος που πιάνει το χέρι του να κάνει μια παραλλαγή αυτού του διαγράμματος:

small talk with a web designer

Γιατί όπως φαίνεται δεν είμαι ο μόνος που δεν μπορεί να απαντήσει απλά και περιεκτικά το ερώτημα.

#include <std/disclaimer.h>


[0]- Οφείλω να ομολογήσω πως η ξαδέρφη διαθέτει εν αγνοία της διορατικότητα μια και ο system administrator στο Δημόσιο κατατάσσεται διακριτά σε μία από δύο κατηγορίες, αυτή και αυτή.

[1] – Όσοι ασχολείστε με τη θεωρία των MIS καλό είναι να διαβάσετε το βιβλίο του Burgess. Αυτό περιέχει όλη την απαιτούμενη θεωρία για αυτό που θέλετε: να φτιάχνετε δηλαδή συστήματα που να δουλεύουν και να δουλεύουν σωστά και αποδοτικά. Όλα τα άλλα βιβλία (π.χ. Turban) είναι για να περνάτε τα μαθήματα. Είμαι ισοπεδωτικός, αλλά έχω τα χιλιόμετρα για να είμαι.

sendmail: implementing a “catch all” address

You may find yourself in a situation where you may need to implement a “catch all” email address, i.e. every email that is directed to your domain regardless whether the user exits or not, not to be rejected but instead directed to a single mailbox. There are various approaches to the problem, and we will see some here:

First, the easy way:

Using FEATURE(`virtusertable’) one can do that in a single line:

@example.com          catch-all@delivery.host.name

You can even exclude some addresses and have email delivered to their own mailbox instead of catch-all:

user1@example.com          user1@delivery.host.name
user2@example.com          user2@delivery.host.name
@example.com                 catch-all@delivery.host.name

The sendmail.mc way:

Normally the above trick which is adequately described in cf/README and the bat book, should be enough. But there may be cases that it is not the solution that you want, or simply because it-is-not-invented-here. For example you may want to redirect to catch-all all email directed to existing users of the system, as opposed to the virtusertable trick which does this unconditionally:

LOCAL_CONFIG
Kuser user -m -a.FOUND

LOCAL_RULE_0
R$- < @ $=w . > $*        $: $(user $1 $) < @ $2 . > $3
R$- . FOUND < @ $=w . > $*          $@ catch-all < @ $2 . > $3

Or, you may want to redirect to the catch-all address all email directed to non-existing users of the system:

MODIFY_MAILER_FLAGS(`LOCAL', `-w')dnl
FEATURE(`local_procmail')dnl
MAILER(`smtp')dnl

LOCAL_CONFIG
Kuser user -m -a.FOUND

LOCAL_RULE_0
R$- < @ $=w . > $*        $(user $1 $)
R$- . FOUND          $#local $: $1
R$-                    $#local $: bit-bucket

In fact (with bit-bucket aliased to /dev/null) the above example silently discards every email not directed to an existing user.

The Phenomenon of Science

Internet Systematics is a blog maintained by Yannis Corovesis (a well known Engineer from the stone ages of the Greek Internet) and is the result of his observations as well as of his participation into the process of building the global Internet over the years.

In his latest post he mentions Turchin‘s book “The Phenomenon of Science” which apparently is out of print. But thanks to the Internet, not out of availability: You can read it from Scribd, or even download it from here.

sendmail: when local users are not users of the system – part 2

Continuing from the previous post in this series, let’s see how one can deal with incoming email that must be delivered both to physical users of the system and users not visible via /etc/passwd:

LOCAL_CONFIG
Kuser user -m -a.FOUND

LOCAL_RULE_0
# Unconditionally redirect email to abuse and Postmaster
RPostmaster  < @ $=w . > $*        $: Postmaster  < @example.com. > $3
Rabuse  < @ $=w . > $*        $: abuse  < @example.com. > $3

# Deliver email to yiorgos locally
Ryiorgos  < @ $=w . > $*        $# local $: $1

# Delete email directed to all other users in /etc/passwd
R$-  < @ $=w . > $*        $(user $1 $)
R$- . FOUND        $#local $: bit-bucket

# The following is valid only if sendmail is instructed to not check /etc/passwd.
# This is achieved with MODIFY_MAILER_FLAGS(`LOCAL', `-w')dnl
R$- < @ $=w . > $*        $#custom.local $: $1

What does the above snippet do? The first set of rules accepts all incoming email addressed to Postmaster and abuse and redirects it to Postmaster@example.com and abuse@example.com.

The second set of rules accepts and delivers locally all incoming email addressed to user yiorgos.

The third set deletes all incoming email for all other users listed in /etc/passwd. One may refine that using a (sendmail) class definition and decide to do so for incoming email addressed to users like man, daemon, lp etc. Remember that in Ruleset 0 you cannot call $#discard.

Assuming that you have written a special delivery agent (to save email in a database for example) for “local” users not found in /etc/passwd, the last rule calls that delivery agent for the given username.

Of course if you are in a certain mood of BOFHiness, you can add similar rules that return random error codes to the sender. The expressiveness of sendmail’s modem-noise is unlimited…

(part 1)