$tosub=$toisuserid?'$':'(SELECT userid FROM ^users WHERE handle=$ LIMIT 1)';
$source='^messages LEFT JOIN ^users ON fromuserid=^users.userid WHERE '.$where.' AND touserid='.$tosub.' ORDER BY ^messages.created DESC LIMIT #,#';
$source='^messages LEFT JOIN ^users ufrom ON fromuserid=ufrom.userid LEFT JOIN ^users uto ON touserid=uto.userid WHERE '.$where.' AND touserid='.$tosub.' ORDER BY ^messages.created DESC LIMIT #,#';
$where='touserid='.($toisuserid?'$':'(SELECT userid FROM ^users WHERE handle=$ LIMIT 1)').' AND type=$';
$source='^messages LEFT JOIN ^users ON fromuserid=^users.userid WHERE '.$where.' ORDER BY ^messages.created DESC LIMIT #,#';
$source='^messages LEFT JOIN ^users ufrom ON fromuserid=ufrom.userid LEFT JOIN ^users uto ON touserid=uto.userid WHERE '.$where.' ORDER BY ^messages.created DESC LIMIT #,#';
$where='fromuserid='.($fromisuserid?'$':'(SELECT userid FROM ^users WHERE handle=$ LIMIT 1)').' AND type=$';
$source='^messages LEFT JOIN ^users ON fromuserid=^users.userid WHERE '.$where.' ORDER BY ^messages.created DESC LIMIT #,#';
$source='^messages LEFT JOIN ^users ufrom ON fromuserid=ufrom.userid LEFT JOIN ^users uto ON touserid=uto.userid WHERE '.$where.' ORDER BY ^messages.created DESC LIMIT #,#';