回文章列表
作者spock.bbs@bbs.csmc.edu.tw (打混的蟑螂史巴克), 信區: installbbs
標題[patch] FireBird v2.52 sendmsg.c 修改
時間中山醫學院BBS站 (Tue Jan 6 12:35:03 1998)
轉信站: GIBBS!ccnews.ncku!ctu-peer!news.nctu!news.iim.nctu!news.cis.nctu!csmcbb


以下為整個 sendmsg.c, 去掉簽名檔剪下來即可使用.
修改的部份為 do_sendmsg() 及 r_msg() 兩個函式, 多開了兩個檔案判斷訊息的
相關條件, 相對的磁碟讀寫負擔也加重許多.
修正: 訊息只能看到最後一個人發給收訊人的問題.
      紀錄自己的訊息.

#include "bbs.h"
#ifdef lint
#include <sys/uio.h>
#endif
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

extern int  RMSG;
extern int RUNSH;
char buf2[STRLEN];
struct user_info *t_search();

int
get_msg(uid,msg,line)
char *msg,*uid;
int line;
{
        char genbuf[3];

        move(line,0);
        clrtoeol();
        prints("送音信給:%s",uid);
        memset(msg,0,sizeof(msg));
        while(1)
        {
                getdata( line+1, 0, "音信 : ", msg, 50, DOECHO, NULL,NA);
                if(msg[0]=='\0')
                        return NA;
                getdata( line+2, 0, "確定要送出嗎(Y)是的 (N)不要 (E)再編輯? [Y],
                    genbuf, 2, DOECHO, NULL ,1);
                if(genbuf[0]=='e'||genbuf[0]=='E')
                        continue;
                if(genbuf[0]=='n'||genbuf[0]=='N')
                        return NA;
                else
                        return YEA;
        }
}

char
msgchar(uin)
struct user_info *uin;
{
    if ((uin->pager&ALLMSG_PAGER)) return ' ';
    if ((can_override(uin->userid,currentuser.userid)))
    {
        if((uin->pager&FRIENDMSG_PAGER))
                return 'O';
        else
                return '#';
    }
    return '*';
}

int
canmsg(uin)
struct user_info *uin;
{
    if ((uin->pager&ALLMSG_PAGER) || HAS_PERM(PERM_SYSOP)) return YEA;
    if ((uin->pager&FRIENDMSG_PAGER))
    {
        if(can_override(uin->userid,currentuser.userid))
                return YEA;
    }
    return NA;
}

s_msg()
{
      do_sendmsg(NULL,NULL,0,0);
}

int
show_allmsgs()
{
        char fname[STRLEN];

        setuserfile(fname,"msgfile");
        clear();
        modify_user_mode( LOOKMSGS);
        if(dashf(fname))
        {
                ansimore(fname,YEA);
                clear();
        }
        else
        {
                move(5,30);
                prints("沒有任何的訊息存在!!");
                pressanykey();
                clear();
        }
}

int
do_sendmsg(uentp,msgstr,mode,userpid)
struct user_info *uentp;
char msgstr[256];
int mode;
int userpid;/*檢查 PID, 以免送錯人 */
{
    char uident[STRLEN] ,ret_str[20];
    FILE *fp;
    time_t now;
    struct user_info *uin ;
    char buf[256], msgbuf[256], filebuf[256], *timestr;
    char msgbuf_me[256];
    char timeindex[10];
    int current_index = 0;

    if(mode==0)
    {
            modify_user_mode( MSG );
            move(2,0) ; clrtobot();
    }
  if(uentp==NULL)
  {
    prints("<輸入使用者代號>\n") ;
    move(1,0) ;
    clrtoeol() ;
    prints("送訊息給: ") ;
    creat_list() ;
    namecomplete(NULL,uident) ;
    if(uident[0] == '\0')
    {
        clear() ;
        return 0 ;
    }
/*    if(searchuser(uident)==0 || tuid == usernum)
    {
       if(uentp==NULL)
       {
        move(2,0) ;
        prints("錯誤的使用者 ID\n") ;
        pressreturn() ;
        move(2,0) ;
        clrtoeol() ;
       }
        return -1 ;
    }*/
    uin=t_search(uident,NA);
    if(uin==NULL)
    {
        move(2,0) ;
        prints("對方目前不在線上,或是使用者代號輸入錯誤...\n");
        pressreturn() ;
        move(2,0) ;
        clrtoeol() ;
        return -1 ;
    }
    if(!canmsg(uin))
    {
        move(2,0) ;
        prints("對方已經關閉接受訊息的呼叫器...\n");
        pressreturn() ;
        move(2,0) ;
        clrtoeol() ;
        return -1;
    }
  }else
  {
  if(!strcmp(uentp->userid,currentuser.userid))
      return 0;
   uin=uentp;
   strcpy(uident,uin->userid);
  }
    if(msgstr==NULL)
    {
        if (!get_msg(uident,buf,1) ){
            move(1,0); clrtoeol();
            move(2,0); clrtoeol();
            return 0;
        }
     }

        now=time(0);
        timestr=ctime(&now)+11;
        *(timestr+8)='\0';
        strcpy(ret_str,"R 回覆");
        if(msgstr==NULL||mode==2)
        {
/*
Time index in messages add by Spock.
We hide index number as false ANSI code.

lastindex records the last message someone sends to an user in user's home dir.

To prevent more than one message send in the same message got the same time
index, check lastindex first. If lastindex >= now == time(0), we add now by 1
to make sure no message index time will be the same.
*/
                sethomefile( filebuf, uident, "lastindex");
                if( fp = fopen( filebuf, "r" ) ) {
                        fgets( timeindex, 10, fp );
                        current_index = atoi( timeindex );
                        fclose( fp );
                }

                if( current_index >= now )
                        now = current_index + 1;

                if( ( fp = fopen( filebuf, "w" ) ) == NULL )
                        return;
                sprintf( timeindex, "%d", now );
                fputs( timeindex , fp );
                fclose( fp );

               sprintf(msgbuf,"%-12.12s(%-5.5s):%-51.51s(%s)d;%dm\n", currentus
               timestr, (msgstr==NULL)?buf:msgstr,ret_str,uinfo.pid+100, now );

               sprintf(msgbuf_me,"# To %-12.12s(%-5.5s):%-51.51s  #\n", uident,
               timestr, (msgstr==NULL)?buf:msgstr);

               setuserfile( filebuf, "msgfile" );
               if( ( fp = fopen( filebuf, "a" ) ) == NULL )
                   return -1;
               fputs( msgbuf_me, fp );
               fclose( fp );
        } else
        {
/*
Mode 4 is add by Spock in order to deal with messages to all friends on line.
*/
           if(mode==0)
                   sprintf(msgbuf,"站長 %-12.12s%-5.5s 昭告:%-51.51s\n",
                   currentuser.userid, timestr, msgstr );
           else if(mode==1)
           {
                   sprintf(msgbuf,"%-12.12s(%-5.5s) 邀請你%-43.43s(%s)dm\n",
                   currentuser.userid, timestr, msgstr,ret_str,uinfo.pid+100);
           }else if(mode==3)
           {
               sprintf(msgbuf,"BBS 系統通告(%-5.5s):%-51.51s(%s)\n",
               timestr, (msgstr==NULL)?buf:msgstr,ret_str);
           }
           else if(mode==4)
           {
               sprintf(msgbuf,"%-12.12s(%-5.5s):%-51.51s(%s)dm\n", currentuser.
               timestr, (msgstr==NULL)?buf:msgstr,ret_str,uinfo.pid+100 );
           }
        }
        if(userpid)
        {
                if(userpid!=uin->pid)
                {
                        saveline(0, 0); /* Save line */
                        move(0,0);
                        clrtoeol();
                        prints("對方已經離線...\n");
                        sleep(1);
                        saveline(0, 1); /* restore line */
                        return -1;
                }
        }
        sethomefile(buf,uident,"msgfile");
        if((fp=fopen(buf,"a"))==NULL)
                return -1;
        fputs(msgbuf,fp);
        fclose(fp);

        if(kill(uin->pid,SIGTTOU)==-1&&msgstr==NULL)
        {
            prints("\n對方已經離線....\n") ; pressreturn();
            clear();
            return -1;
        }
        if(msgstr==NULL)
        {
            prints("\n已送出訊息....\n") ; pressreturn();
            clear() ;
        }
    return 1 ;
}

int
dowall(uin)
struct user_info *uin;
{
        if (!uin->active || !uin->pid) return -1;
        move(1,0);
        clrtoeol();
        prints("正對 %s 廣播.... Ctrl-D 停止對此位 User 廣播。",uin->userid);
        refresh();
        do_sendmsg(uin,buf2,0,uin->pid);
}


int
wall()
{
    modify_user_mode( MSG );
    move(2,0) ; clrtobot();
    if (!get_msg("所有使用者",buf2,1) ){
         move(1,0); clrtoeol();
         move(2,0); clrtoeol();
         return 0;
    }
    if( apply_ulist( dowall ) == -1 ) {
        move(2,0);
        prints( "沒有任何使用者上線\n" );
        pressanykey();
    }
    prints("\n已經廣播完畢....\n");
    pressanykey();
}


void
r_msg()
{
    FILE *fp, *fp_ind;
    char buf[256] ;
    char msg[256] ;
    char fname[STRLEN], fname_ind[STRLEN];
    char timeindex[10], timebuf[10], *ptr;
    int line,tmpansi;
    int y,x,ch;
    int next_msg_flag = 0;
    int msgtime;

    signal(SIGTTOU,r_msg) ;
    getyx(&y,&x);
    tmpansi=showansi;
    showansi=1;
    if(uinfo.mode==TALK)
        line=t_lines/2-1;
    else
        line=0;
    if(DEFINE(DEF_SOUNDMSG))
    {
            bell();
            bell();
    }

/*
The message time index is added by Spock.
*/

    setuserfile( fname_ind, "msgtimeindex");
    if( ( fp_ind = fopen( fname_ind, "r" ) ) == NULL )
    {
        setuserfile(fname,"msgfile");
        if( ( fp = fopen( fname, "r" ) ) == NULL )
            return;
        while( fgets( buf, 256, fp ) != NULL )
        {
            strcpy(msg,buf);
            ptr = strrchr( msg, ';' );
            msgtime = atoi( ptr + 1 );

            if( ( fp_ind = fopen( fname_ind, "w" ) ) == NULL )
                return;
            sprintf( timeindex, "%d", msgtime );
            fputs( timeindex , fp_ind );
            fclose( fp_ind );
        }
        fclose( fp );
    }
    else
    {
        fgets( timebuf, 10, fp_ind );
        strcpy( timeindex, timebuf );
        fclose( fp_ind );

        setuserfile(fname,"msgfile");
        if((fp=fopen(fname,"r"))==NULL)
        return;

        while( fgets( buf, 256, fp ) != NULL )
        {
            if( buf[10] != '#' ) {
                strcpy(msg,buf);
                ptr = strrchr( msg, ';' );
                msgtime = atoi( ptr + 1 );
                if( msgtime > atoi( timeindex ) )
                {
/*
Check if next message is available.
*/
                    while( fgets( buf, 256, fp ) != NULL ) {
                        if( buf[10] != '#' ) {
                                next_msg_flag = 1;
                                break;
                        }
                    }

                    if( ( fp_ind = fopen( fname_ind, "w" ) ) == NULL )
                        return;
                    sprintf( timeindex, "%d", msgtime );
                    fputs( timeindex , fp_ind );
                    fclose( fp_ind );

                    break;
                }
            }
        }
        fclose( fp );
    }
    if( msg[10] == '#' )
        return;
    RMSG=YEA;
    saveline(line, 0);
    move(line,0); clrtoeol(); prints("%s",msg); refresh();
    oflush() ;
   ch=0;
   while(ch!='\n'&&ch!='\r'&&ch!=KEY_ESC)
   {
/*      read(0,&ch,1);*/
        ch=igetkey();
    if(ch=='\n'||ch=='\r')
        break;
    if(ch==Ctrl('R')||ch=='r'||ch=='R')
    {
          struct user_info *uin ;
          char msgbuf[STRLEN];
          int good_id,send_pid;
          char *ptr,usid[STRLEN];

          ptr=strrchr(msg,'[');
          send_pid=atoi(ptr+1);
          if(send_pid>100)
                send_pid-=100;
          ptr=strtok(msg+12," ;
          if(ptr==NULL|| !strcmp(ptr,currentuser.userid))
                good_id=NA;
          else
          {
                strcpy(usid,ptr);
                uin=t_search(usid,send_pid);
                if(uin==NULL)
                        good_id=NA;
                else
                        good_id=YEA;
          }
          if(good_id==YEA)
          {
                  int userpid;
                  userpid=uin->pid;
                  move(line,0);
                  clrtoeol();
                  sprintf(msgbuf,"回訊息給 %s: ",usid);
                  getdata(line,0,msgbuf,buf,51,DOECHO,NULL,YEA);
                  if(buf[0]!='\0')
                  {
                        do_sendmsg(uin,buf,2,userpid);
                        sprintf(msgbuf,"幫你送出訊息了");
                  }else
                        sprintf(msgbuf,"空訊息, 所以不送出.");

          }else
          {
                  sprintf(msgbuf,"找不出發訊息的人");
          }
          move(line,0);
          clrtoeol();
          refresh();
          prints("%s",msgbuf);
          refresh();
          sleep(1);
          break;
    }
   }
    showansi=tmpansi;
    saveline(line, 1); /* restore line */
    move(y,x);
    refresh();
    RMSG=NA;

/*
If we have next message, change RMSG status to YEA and call r_msg() again.
*/
    if( next_msg_flag )
    {
        RMSG=YEA;
        r_msg();
    }
    return;
}

int
friend_login_wall(pageinfo)
struct user_info *pageinfo;
{
        char msg[STRLEN];

        if( !pageinfo->active || !pageinfo->pid )
                return 0;
        if (can_override(pageinfo->userid,currentuser.userid)) {
                if(getuser(pageinfo->userid)<=0)
                        return 0;
                if(!(lookupuser.userdefine&DEF_LOGININFORM))
                        return 0;
                if(!strcmp(pageinfo->userid,currentuser.userid))
                        return 0;
                if( uinfo.invisible == YEA && PERM_SYSOP )
                        return 0;
/*
Mode 4 add by Spock.
*/
                sprintf(msg,"你的好朋友 %s 已經上站囉!",currentuser.userid);
                do_sendmsg(pageinfo,msg,4,pageinfo->pid);
        }
        return 0;
}

--
    Boldly go where no mudder has gone before...     
                                                     
        Spock (roach admin)    蟑螂管理員            
   homepage: http://bbs.csmc.edu.tw/spock/           
                                                     
  From   The Final Frontier   140.128.136.11 4000    

※ 來源:‧中山醫學院BBS -- 絮情小站 bbs.csmc.edu.tw‧[FROM: cc-a23.csmc.edu]
文章選讀←離開[主題上]主題下(k)上篇(j)下篇S/a搜尋 G串列 TAB精華 ↑↓捲 Pg/Space翻