// IfsMailInit.bcpl - Mail system initialization
// Copyright Xerox Corporation 1979, 1980, 1981
// Last modified October 4, 1981  4:15 PM by Taft

get "Pup0.decl"
get "Pup1.decl"
get "IfsFtpProt.decl"
get "IfsFiles.decl"
get "IfsMail.decl"
get "Ifs.decl"
get "IfsSystemInfo.decl"

external
[
// outgoing procedures
InitMail; InitMailStats

// incoming procedures
CreateEvent; MailEvent; IfsFtpServ
Unqueue; Enqueue; Allocate; Zero; ReadCalendar
ExtractSubstring; OpenLevel1Socket
MtpSStore; MtpSStoreMail; MtpSStoreCleanup
MtpSRetrieve; MtpSRetrieveMail; MtpSRetrieveCleanup
LockCell; UnlockCell; VFileWritePage

// outgoing statics
mail

// incoming statics
sysZone; rsQ; dFTPI; infoVMD; socketQ
]

static mail

//----------------------------------------------------------------------------
let InitMailStats(ms) be
//----------------------------------------------------------------------------
// Assumes the page pointed at by ms is locked and marked dirty.
[
Zero(lv ms>>MS.stats, lenMSStats)
ms>>MS.statsVersion = msStatsVersion
ReadCalendar(lv ms>>MS.timeReset)
let mse = lv ms>>MS.mseLen; mse>>MSE.max = 7;
mse>>MSE.grain = 1; mse>>MSE.scale = 6; mse>>MSE.scaleTotal = 0
mse = lv ms>>MS.mseMlbx; mse>>MSE.max = 7;
mse>>MSE.grain = 1; mse>>MSE.scale = 1; mse>>MSE.scaleTotal = 0
mse = lv ms>>MS.mseSort; mse>>MSE.max = 7;
mse>>MSE.grain = 1; mse>>MSE.scale = 0; mse>>MSE.scaleTotal = 0
mse = lv ms>>MS.mseRetr; mse>>MSE.max = 7;
mse>>MSE.grain = 2; mse>>MSE.scale = 8; mse>>MSE.scaleTotal = 6
mse = lv ms>>MS.mseFwd; mse>>MSE.max = 7;
mse>>MSE.grain = 1; mse>>MSE.scale = 0; mse>>MSE.scaleTotal = 0
]

//----------------------------------------------------------------------------
and InitMail() be
//----------------------------------------------------------------------------
// This should be called AFTER InitFtpServ and OpenSystemInfo.
[
// Build the mail system state block
mail = Allocate(sysZone, lenMail); Zero(mail, lenMail)

// Register the Mail socket with the socket manager
let rs = Allocate(sysZone, lenRS); Zero(rs, lenRS)
rs>>RS.proc = IfsFtpServ
rs>>RS.soc = Allocate(sysZone, lenPupSoc)
OpenLevel1Socket(rs>>RS.soc, table [ 0; 0; socketMail ])
rs>>RS.extraSpace = lenExtraFtpCtx
rs>>RS.type = jobTypeMTP
Enqueue(rsQ, rs)
mail>>Mail.rs = rs

// Build a user info block for internal mail operations
let ui = Allocate(sysZone, lenUserInfo); Zero(ui, lenUserInfo)
ui>>UserInfo.userName = ExtractSubstring("System")
ui>>UserInfo.connName = ExtractSubstring("Mail")
mail>>Mail.ui = ui

// Install mail operations in default FTP Interface
dFTPI>>FTPI.StoreMail = MtpSStore
dFTPI>>FTPI.StoreMailMessage = MtpSStoreMail
dFTPI>>FTPI.StoreMailCleanup = MtpSStoreCleanup
dFTPI>>FTPI.RetrieveMail = MtpSRetrieve
dFTPI>>FTPI.RetrieveMailMessage = MtpSRetrieveMail
dFTPI>>FTPI.RetrieveMailCleanup = MtpSRetrieveCleanup

// Check long term mail system state
let ms = VFileWritePage(infoVMD, msPage)
LockCell(lv ms)
if ms>>MS.version ne msVersion then
   [  //Hmmm.  Doesn't seem to be set up
   Zero(ms, lenMS)
   ms>>MS.version = msVersion
   ]
if ms>>MS.statsVersion ne msStatsVersion then InitMailStats(ms)
mail>>Mail.enabled = ms>>MS.enabled
if ms>>MS.enabled eq 0 then Unqueue(socketQ, rs>>RS.soc)
mail>>Mail.forward = ms>>MS.forward
UnlockCell(lv ms)
ReadCalendar(lv mail>>Mail.msgID)

// Start the mail event
mail>>Mail.wake = 60/eventInterval  // 1 minute
CreateEvent(MailEvent)
]