WernerScheduler.borg

WernerScheduler.borg


{
` ----------------------------
`   GLOBAL VARIABLES
` ----------------------------
FREE:0;
VUB:1;
JETTE:2;
TRAVEL:3;
AWAY:4;
HOME:5;
NAME:"W.E.R.N.E.R.";    `Persons Name
MAINURL:"/Werner";        `URL to subscribe to
START:9;            `start hour
STOP:24;            `stop hour
EMPTYFIELD:"";           `the empty field in the schedule

` ----------------------------
`   DATA STORAGE
` ----------------------------
makeappointement(color,str):: clone();
makeday(dag,maand,jaar)::
    {
    uren[STOP-START]:makeappointement(FREE,EMPTYFIELD);
    setappointement(from,to,app)::
        {
        while(from1]:=app;
            from:=from+1
            })
        };
    clone()
    };

day_equals(a,d):: (((a.dag)=(d.dag)) & ((a.maand)=(d.maand)) & ((a.jaar)=(d.jaar)));
day_greater(a,d)::
        {
        if ((a.jaar)>(d.jaar), true,
            if ((a.jaar)=(d.jaar),
                if ((a.maand)>(d.maand), true,
                    if ((a.maand)=(d.maand),
                        (a.dag)>(d.dag),
                        false)),
                false))
        };

nextday(day)::
    {
    d:(day.dag)+1;
    m:day.maand;
    j:day.jaar;
    if (d>monthtab[m],
        {d:=1;
        m:=m+1});
    if (m>12,
        {m:=1;
        j:=k+1});
    makeday(d,m,j)
    };

days:[];
save_sched()::saveexp(days,agentself,"schedule.exp");
load_sched()::
    {
    display("loading the last known schedule",eoln);        
    newdays:loadexp(agentself,"hborg/schedule.exp");
    if(is_void(newdays),
        {display("failed",eoln); false},
        {days:=newdays; true})
    };

findday(d)::
    {
    D:void;
    for(i:1,i<=size(days),i:=i+1,
        if (day_equals(d,days[i]), D:=days[i]));
    D
    };

checkcalendar()::
    {
    `check wether the current day is in the list or not...
    `remove all old days
    currentdate:date();
    today:makeday(currentdate[1],currentdate[2],currentdate[3]);
    x:nrofdays:1;
    found:false;
    while(xvoid,
            {
            nrofdays:=nrofdays+1;
            found := found | day_equals(today,days[x])
            });
        x:=x+1
        });
    `calculate the size of the new array
    if (!found, nrofdays:=nrofdays+1);
    if (nrofdays<14, nrofdays:=14);
    `allocate the new array, and say how to write
    tays[nrofdays]:void;
    writingat:1;
    todayinserted:false;
    lastwritten:void;
    write(d)::
        {
        if ((!todayinserted) & (!found) & day_greater(d,today),
            {tays[writingat]:=today;
            todayinserted:=true;
            writingat:=writingat+1});
        tays[writingat]:=d;
        lastwritten:=d;
        writingat:=writingat+1
        };
    `copy all existing days
    for(readingat:1,readingat1,
        {
        d:days[readingat];
        if (!is_void(d),write(d))
        });
    `insert today if necesary
    if ((!todayinserted) & (!found),
        {write(today);
        todayinserted:=true});
    `insert next days till end of matrix
    while(writingat<=nrofdays,
        write(nextday(lastwritten)));
    `reasign days
    days:=tays
    };


` ----------------------------
`   HTML OUTPUT
` ----------------------------
pagetext:"";
cleartext()::pagetext:="";
flushtext(to)::{to->display(pagetext); cleartext()};
disp(txt)::pagetext := pagetext + text(txt);
href(anker,txt)::"+anker+'"'+">"+txt+"";
appointement_html(obj,url)::
    {
    if ((obj.color)=FREE,"",
    if ((obj.color)=JETTE,"",
    if ((obj.color)=VUB,"",
    if ((obj.color)=AWAY,"",
    if ((obj.color)=HOME,"",
    if ((obj.color)=TRAVEL,"",<"td bgcolor=#000000>"))))))+
    href(url,".")+(obj.str)+""
    };

day_url_html(day)::"get"+MAINURL+"?DAY="+text(day.dag)+"&MONTH="+text(day.maand)+"&YEAR="+text(day.jaar);

day_html(day)::
    {
    header:text(day.dag)+"/"+text(day.maand)+"/"+text(day.jaar);
    href(day_url_html(day),header)
    };

printheader()::
    {
    disp("

Hello "+NAME+"

I'm agent "+text(agentname)); disp("
Please feel free to debug me :-)

"
) }; printcalendar():: { disp(""); disp(""); x:1; while(x<=size(days), { day:days[x]; disp(""); x := x + 1 }); disp(""+eoln); uur:1; while(uur<=(STOP-START), { disp(""); x : 1; while(x<=size(days), { day:days[x]; uren:day.uren; appointement:uren[uur]; url:day_url_html(day)+"&FROM="+text(START-1+uur); disp(appointement_html(appointement,url)); x := x + 1 }); uur := uur + 1; disp("") }); disp("
Uur"+day_html(day)+"
"); disp(href("get"+MAINURL+"?FROM="+text(START-1+uur),text(START-1+uur)+" - "+text(START+uur))); disp("
") }; printform(formdata):: { txt(t):: if (is_void(t), "", if (is_number(t), text(t), t)); disp("

+"/get"+MAINURL+'"'+">"); disp(""+ ""+ ""); disp(""); disp(""); disp(""); disp(""); disp("
Date:
From:
To:
I am:
Comments:
") }; printfooter()::disp("

+MAINURL+">Back to Main Schedule"); printerror(text)::disp("+"#ff0000"+'"'+">Error: "+text+"

"
); ` ---------------------------- ` HTML LOGIC ` ---------------------------- monthtab:[31,28,31,30,31,30,31,31,30,31,30,31]; parseformdata(formdata):: { dag:maand:jaar:from:to:state:comments:void; ` we lopen al de data af en assignen fields zoals nodig for(i:1,i<=size(formdata),i:=i+1, { field:formdata[i,1]; value:formdata[i,2]; if (field = "FROM", from:=number(value)); if (field = "TO", to:=number(value)); if (field = "STATE", state:=number(value)); if (field = "DAY", dag:=number(value)); if (field = "MONTH", maand:=number(value)); if (field = "YEAR", jaar:=number(value)); if (field = "COMMENTS", comments:=value) }); cleardag(err)::{dag:=void; printerror(err)}; clearmaand(err)::{maand:=void; printerror(err)}; clearjaar(err)::{jaar:=void; printerror(err)}; clearfrom(err)::{from:=void; printerror(err)}; clearto(err)::{to:=void; printerror(err)}; clone() }; handleformdata(formdata):: { day:void; ` valid date ? if (!is_void(formdata.dag), if ((formdata.dag)<1, formdata.cleardag("Day must be >= 1"))); if (!is_void(formdata.maand), if (((formdata.maand)<1) | ((formdata.maand)>12), formdata.clearmaand("Month must be between 1 and 12"))); if (!is_void(formdata.dag) & !is_void(formdata.maand), if((formdata.dag)>monthtab[formdata.maand], formdata.cleardag("Not that much days in given month"))); if (!is_void(formdata.jaar), if ((formdata.jaar)<2000, formdata.clearjaar("Years start from 2000"))); ` check de uren als ze gegeven zijn if (!is_void(formdata.from), if (((formdata.from)=STOP), formdata.clearfrom("From must be between "+text(START)+" and "+text(STOP-1)))); if (!is_void(formdata.to), if ((((formdata.to)<=START) | ((formdata.to)>STOP)), formdata.clearto("To must be between "+text(START+1)+" and "+text(STOP)))); if (!is_void(formdata.from) & !is_void(formdata.to), if((formdata.from)>=(formdata.to), formdata.clearto("From must be smaller than To"))); ` als we dag, maand, jaar ` from, to, state hebben kunnen we een appointement invullen if (!is_void(formdata.dag) & !is_void(formdata.maand) & !is_void(formdata.jaar) & !is_void(formdata.from) & !is_void(formdata.to), { if (is_void(formdata.comments), formdata.comments:=EMPTYFIELD); day:=makeday(formdata.dag,formdata.maand,formdata.jaar); day:=findday(day); if (is_void(day), printerror("Day is not available"), day.setappointement(formdata.from,formdata.to,makeappointement(formdata.state,formdata.comments))) }) }; respondto(req,line,formdata):: { display(text(req)+": "+line); ` log a message formdata:=parseformdata(formdata); ` parse the formdata checkcalendar(); ` check the calendar to see wether all dates are online including today. cleartext(); ` clear outgoing text printheader(); flushtext(req); ` print the header handleformdata(formdata); ` is er formdata geattached, zoja, fix de agenda save_sched(); ` save the new agenda printcalendar(); flushtext(req); ` print the calendar printform(formdata); ` print from printfooter(); flushtext(req); ` print footer req->finish(); ` finish display(" [done]",eoln) ` log a message }; ` ---------------------------- ` DEBUGGING LOGIC ` ---------------------------- connect_ui(agent)::eval(read("uiagent:agent("+'"'+text(agent)+'"'+")"),agentself); disconnect_ui()::eval(read("uiagent:0"),agentself); away():: { save_sched(); agentmove("homer/webdispatcher"); if (!load_sched(), save_sched()) }; ` ---------------------------- ` INITIALISATION ` ---------------------------- init():: { load_sched(); `Load schedule display("announcing "+MAINURL+" at webdispatcher",eoln); `Subscribe to URL a:agent(router_placename()+"/webdispatcher"); ping(a); a->forgethandler(MAINURL); a->announcehandler(MAINURL,agentself); display("initialization complete",eoln) }; loadall():: new("htmlportal",load("examples/WebServer/HtmlPortal.borg")); display("loadall()"+eoln); display("init()") }