/*-------------------------------------------------------*/
/* schedule.c ( NTHU CS MapleBBS Ver 2.36 ) */
/*-------------------------------------------------------*/
/* target : schedule for SOB routines */
/* create : 98/02/04 Gene */
/* update : 98/02/15 by SiEpthero */
/*-------------------------------------------------------*/
#include "bbs.h"
#define SCH_YEAR 98 /* 隨著時間調整年份內定值 */
typedef struct {
time_t time;
char reason[96];
} sch;
static int
time_cmp(i, j)
sch *i, *j;
{
register int mount;
mount = i->time - j->time;
return mount;
}
char *
C_date(clock)
time_t *clock;
{
static char foo[24];
static char myweek[] = "天一二三四五六";
struct tm *mytm = localtime(clock);
sprintf(foo,"[%d/%d]星期%c%c[%d:%02d]",
mytm->tm_mon+1,mytm->tm_mday,myweek[mytm->tm_wday<<1],
myweek[(mytm->tm_wday<<1)+1],mytm->tm_hour,mytm->tm_min);
return (foo);
}
time_t
get_sch_time()
{
char buf[8],ch;
char *t_max = "19392959";
time_t sch_time;
struct tm ptime;
int i;
move(3,0);
clrtobot();
show_picture("etc/sch_help");
move(3,0);outs("請輸入您預定的時間(請用24小時制): 月 日 時 分");
for(i=0;i<8;i++){
move(3,(33+i/2*4+i%2));
ch = egetch();
if(ch == 'q' || ch == 'Q') return 0;
else if ( ch == '\r') { i = -1; continue;}
else if ( ch == '\177' || ch == Ctrl('H'))
{
if(i)i -= 2;
else i = -1;
continue;
} /*允許倒退鍵及ENTER重新輸入*/
if(ch >= '0' && ch <= t_max[i]){
outc(ch);
buf[i] = ch - '0';
}
else{ bell(); --i;}
}
ptime.tm_year = SCH_YEAR;ptime.tm_sec=0;
ptime.tm_mon = buf[0]*10+buf[1]-1;
ptime.tm_mday = buf[2]*10+buf[3];
ptime.tm_hour = buf[4]*10+buf[5]+1;
ptime.tm_min = buf[6]*10+buf[7];
sch_time = mktime(&ptime);
move(4,0);
prints("您剛剛輸入的時間是:%s",C_date(&sch_time));
return sch_time;
}
void
refresh_sch(char *fpath,int mode,int s_no)
{
FILE *fp,*nfp;
char fn[80];
sch temp;
int count=0;
sprintf(fn, "%s_", fpath);
if ( (fp = fopen(fpath, "r")) && (nfp = fopen(fn, "w")) )
{
while (fread( &temp, sizeof(temp), 1, fp))
{
count++;
if ((mode && count!=s_no)|| !mode)
if(temp.time > currutmp->uptime)
fwrite(&temp, sizeof(sch), 1, nfp);
}
fclose(fp);
fclose(nfp);
Rename(fn, fpath);
}
}
int
sch_count(char *fname)
{
FILE *fp;
int count = 0;
sch tmp;
if(fp=fopen(fname,"r"))
{
move(4,0);
outs("\033[1m==============================\033[33m[\033[31m行事曆資料介面\
\033[33m]\033[37m================================\n\
\033[35;44m[編號] \033[33m[時 間] \033[36m[事 件 敘 述]\
\033[32m[Written by SiEpthero]\033[m\n");
while(fread( &tmp, sizeof(sch), 1, fp))
{
count++;
if ( 5 + count < b_lines)
{
move(5 + count, 0);
prints("\033[1m(\033[36m%2d\033[37m) \033[32m%s\033[33m %s\033[m",count,
C_date(&tmp.time), tmp.reason);
} /* SiE: 超過畫面就不顯示了 */
}
fclose(fp);
}
return count;
}
int
edit_sch(char *fname)
{
int s_num;
sch temp;
char fpath[80];
sethomefile(fpath, cuser.userid,fname);
for(;;)
{
stand_title("編輯行事曆");
s_num=sch_count(fpath); move(1,0);
prints("您目前的行事曆資料有%d筆,您要 1)新增 2)刪除資料?[q離開]",s_num);
switch(igetch())
{
case '1':
do{
/* if(++s_num>SCH_LIM){yypresskey("行事曆超過上限");break;}*/
move(2,0);
prints("新增事件[%d]:",s_num);
temp.time=get_sch_time();
if(!temp.time) break;
else if ( temp.time < currutmp->uptime )
{
yypresskey("時間過期了");
continue;
s_num--;
}
getdata(4,0,"請輸入事件:",temp.reason,60,DOECHO,0);
if (!temp.reason[0])break;
rec_add(fpath, &temp, sizeof(temp));
}while(getans("還要新增嗎?[N]")=='y');
break;
case '2':
{
int d_no;
char buf[3];
getdata(2,0,"您要刪除哪筆資料?",buf,3,DOECHO,0);
d_no=atoi(buf);
if(d_no>0 && d_no <=s_num)
refresh_sch(fpath,1,d_no);break;
}
case 'q':
case 'Q':
return 0;
}
};
return 0;
}
int
read_schedule(s_in,s_out,s_type)
FILE *s_in,*s_out;
char *s_type;
{
int k=0;
sch tmp;
while(fread( &tmp, sizeof(tmp), 1, s_in) )
{
sprintf(tmp.reason + strlen(tmp.reason), "(%s)", s_type);
fwrite( &tmp, sizeof(tmp), 1, s_out);
k++;
}
fclose(s_in);
return k;
}
/*************************/
/* schedule init */
/*************************/
/* Gene */
time_t schedule_time;
char schedule_string[100];
void
init_schedule()
{
FILE *in_file, *out_file, *user_file;
char fname[100];
char user_list[100];
sch *schedule;
int i=0, j=1;
sethomefile(fname, cuser.userid, "alarm");
out_file=fopen(fname, "w");
sethomefile(fname, cuser.userid, "private");
refresh_sch(fname,0,0);
if(in_file = fopen(fname, "r"))
i += read_schedule( in_file, out_file, "私人");
sethomefile(fname, cuser.userid, "userlist");
if (user_file = fopen(fname, "r"))
{
while(!(fscanf(user_file, "%s", &user_list)==EOF))
{
strcpy(user_list, strtok(user_list, str_space));
sethomefile(fname, user_list, "public");
if(in_file = fopen(fname,"r"))
i += read_schedule(in_file, out_file, user_list);
}
fclose(user_file);
}
fclose(out_file);
sethomefile(fname, cuser.userid, "alarm");
schedule = (sch *)malloc( sizeof(sch) * ( i + 2) );
i = 0;
if(in_file = fopen(fname, "r"))
{
while(fread( &schedule[i], sizeof(sch), 1, in_file))
{
if (schedule[i].time > currutmp->uptime) i++;
}
fclose(in_file);
qsort( schedule, i, sizeof(sch), time_cmp);
schedule_time = schedule[0].time;
strcpy(schedule_string, schedule[0].reason);
sethomefile(fname, cuser.userid, "schedule");
out_file = fopen(fname, "w");
for(j=0;j<i;j++)
{
fprintf(out_file, "%s %s\n",
C_date(&schedule[j].time), schedule[j].reason);
}
fclose(out_file);
}
if (schedule_time < currutmp->uptime)schedule_time+=86400;
}
void
check_schedule()
{
time_t now = time(0);
if (difftime(now, schedule_time) > 0)
{
init_schedule();
}
if ((difftime(schedule_time, now) < MOVIE_INT) &&
difftime(schedule_time, now) > 0)
{
extern screenline* big_picture;
screenline* screen0 = calloc(24, sizeof(screenline));
int y, x;
char buf[100];
getyx(&y, &x);
memcpy(screen0, big_picture, 24 * sizeof(screenline));
sethomefile(buf, cuser.userid, "schedule");
more(buf, NA);
sprintf(buf, "[%s] 提醒您%s的時間快到了!!", Cdate(&now),
&schedule_string);
move(b_lines - 1, 0);
outs(buf);
pressanykey();
memcpy(big_picture, screen0, 24 * sizeof(screenline));
move(y, x);
free(screen0);
redoscr();
}
}
int s_private()
{
edit_sch("private");
return -1;
}
int s_public()
{
edit_sch("public");
return -1;
}
int s_userlist()
{
char user_name[100];
sethomefile(user_name, cuser.userid, "userlist");
vedit(user_name, NA);
return -1;
}
int s_alarm()
{
char genbuf[100];
sprintf(genbuf, "到%s的時間是還剩 %d 分",
schedule_string, (schedule_time - currutmp->uptime)/60);
move(b_lines - 1, 0);
outs(genbuf);
pressanykey();
return 0;
}
int
s_schedule()
{
char fn_name[80];
init_schedule();
sethomefile(fn_name, cuser.userid, "schedule");
more(fn_name, YEA);
return 0;
}