Because users authenticated with an external auth mechanism are not very
likely to have an INBOX created for them... and because INBOX, as a
hard-coded feature of the IMAP specification, must exist, here's a new
db_insert_message function which calls db_createmailbox if needed:
Aaron
/*
* inserts into inbox !
*/
u64_t db_insert_message (u64_t useridnr, const char *deliver_to_mailbox,
const char *uniqueid)
{
char timestr[30];
time_t td;
struct tm tm;
u64_t mailboxid;
time(&td); /* get time */
tm = *localtime(&td); /* get components */
strftime(timestr, sizeof(timestr), "%G-%m-%d %H:%M:%S", &tm);
/* if there isn't a mailbox specified, default to INBOX */
mailboxid = db_get_mailboxid( useridnr, ( deliver_to_mailbox ?
deliver_to_mailbox : "INBOX" ) );
/* check to see if the desired mailbox exists */
if( mailboxid == 0 )
{
/* see if we're configured to automatically create mailboxes */
if( 1 )
{
trace( TRACE_DEBUG, "db_insert_message(): creating mailbox as it does
not exist" );
if( db_createmailbox( ( deliver_to_mailbox ? deliver_to_mailbox :
"INBOX" ), useridnr ) != 0 )
{
return -1;
}
else
{
/* if only db_create_mailbox returned the mailboxid... */
mailboxid = db_get_mailboxid( useridnr, ( deliver_to_mailbox ?
deliver_to_mailbox : "INBOX" ) );
}
}
else
{
trace( TRACE_ERROR, "db_insert_message(): mailbox does not exist,
failing" );
return -1;
}
}
snprintf (query, DEF_QUERYSIZE,"INSERT INTO
messages(mailbox_idnr,messagesize,unique_id,"
"internal_date,recent_flag,status)"
" VALUES (%llu, 0, \"%s\", \"%s\", 1, '005')",
mailboxid, uniqueid ? uniqueid : "", timestr);
if (db_query (query)==-1)
{
trace(TRACE_STOP,"db_insert_message(): dbquery failed");
}
return db_insert_result("");
}