void CreateAlias::execute() { if ( !d->address ) { Utf8Codec c; parseOptions(); d->address = nextAsAddress(); EString * first = args()->firstElement(); if ( first && !first->startsWith( "/" ) && first->contains( "@" ) ) d->destination = nextAsAddress(); else d->mailboxName = c.toUnicode( next() ); end(); if ( !c.valid() ) error( "Argument encoding: " + c.error() ); database( true ); if ( !d->mailboxName.isEmpty() ) Mailbox::setup( this ); } if ( !choresDone() ) return; if ( !d->t ) { if ( !d->mailboxName.isEmpty() ) { d->mailbox = Mailbox::obtain( d->mailboxName, false ); if ( !d->mailbox || d->mailbox->deleted() ) error( "No mailbox named " + d->mailboxName.utf8() ); } d->t = new Transaction( this ); List< Address > l; l.append( d->address ); if ( d->destination ) l.append( d->destination ); AddressCreator * ac = new AddressCreator( &l, d->t ); ac->execute(); } if ( !d->address->id() || ( d->destination && !d->destination->id() ) ) return; if ( !d->q ) { if ( d->destination ) { d->q = new Query( "insert into aliases (address, mailbox) " "select $1, mailbox from aliases al " "join addresses a on (al.address=a.id) " "where a.localpart=$2" " and a.domain=$3 " "limit 1", this ); d->q->bind( 1, d->address->id() ); d->q->bind( 2, d->destination->localpart() ); d->q->bind( 3, d->destination->domain() ); } else { d->q = new Query( "insert into aliases (address, mailbox) " "values ($1, $2)", this ); d->q->bind( 1, d->address->id() ); d->q->bind( 2, d->mailbox->id() ); } d->t->enqueue( d->q ); d->t->execute(); } if ( !d->q->done() ) return; if ( d->q->failed() ) error( "Couldn't create alias: " + d->q->error() ); if ( d->q->rows() < 1 ) error( "Could not locate destination for alias" ); else if ( d->q->rows() > 1 ) error( "Internal error: Inserted " + fn( d->q->rows() ) + " instead of 1. Not committing." ); d->t->commit(); finish(); }
void CreateView::execute() { if ( d->name.isEmpty() ) { parseOptions(); Utf8Codec c; d->name = c.toUnicode( next() ); d->source = c.toUnicode( next() ); UString owner( c.toUnicode( next() ) ); d->selector = parseSelector( args() ); if ( !c.valid() ) error( "Argument encoding: " + c.error() ); if ( d->name.isEmpty() ) error( "No name supplied for the view." ); if ( d->source.isEmpty() ) error( "No source mailbox name supplied." ); if ( !d->selector ) error( "Invalid search expression supplied." ); database( true ); Mailbox::setup( this ); if ( !owner.isEmpty() ) { d->user = new User; d->user->setLogin( owner ); d->user->refresh( this ); } } if ( !choresDone() ) return; if ( !d->t ) { if ( d->user ) { if ( d->user->state() == User::Unverified ) return; if ( d->user->state() == User::Nonexistent ) error( "No user named " + d->user->login().utf8() ); if ( !d->name.startsWith( "/" ) ) d->name = d->user->home()->name() + "/" + d->name; if ( !d->source.startsWith( "/" ) ) d->source = d->user->home()->name() + "/" + d->source; } d->ms = Mailbox::obtain( d->source ); if ( !d->ms || d->ms->deleted() ) error( "Can't create view on " + d->source.utf8() ); d->t = new Transaction( this ); d->mv = Mailbox::obtain( d->name ); Query * q = d->mv->create( d->t, d->user ); if ( !q ) error( "Couldn't create view named " + d->name.utf8() ); q = new Query( "insert into views " "(view, selector, source, nextmodseq) values " "((select id from mailboxes where name=$1)," "$2, " "((select id from mailboxes where name=$3), " "1::bigint)", this ); q->bind( 1, d->name ); q->bind( 2, d->selector->string() ); q->bind( 3, d->ms->name() ); d->t->enqueue( q ); d->t->commit(); } if ( !d->t->done() ) return; if ( d->t->failed() ) error( "Couldn't create view" ); finish(); }