home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / LOOK.C < prev    next >
C/C++ Source or Header  |  1994-08-26  |  7KB  |  218 lines

  1. /* Peek into a user's actions on the bbs.
  2.  * This allows you to follow all stuff, and also
  3.  * send messages and initiate chats...
  4.  * (C) 1994, Johan. K. Reinalda, WG7J
  5.  */
  6. #include <ctype.h>
  7. #include "global.h"
  8. #ifdef LOOKSESSION
  9. #include "session.h"
  10. #include "smtp.h"
  11. #include "usock.h"
  12. #include "socket.h"
  13. #include "mailbox.h"
  14.   
  15. static void look_input __ARGS((int unused,void *p1,void *p2));
  16.   
  17. struct look {
  18.     struct session *sp;     /* our look session */
  19.     int user;               /* socket we look at */
  20. };
  21.   
  22. int dolook(int argc, char *argv[],void *p) {
  23.     struct look lk;
  24.     struct usock *up;
  25.     int chat;
  26.     char c,*cp;
  27. #ifdef MAILBOX
  28.     struct mbx *m;
  29. #endif
  30.     char name[MBXNAME+1];
  31.     char buf[MBXLINE];
  32.   
  33.     /* Check if this comes from console - WG7J*/
  34.     if(Curproc->input != Command->input)
  35.         return 0;
  36.   
  37. #ifdef MAILBOX
  38.     /* Find the user ! */
  39.     lk.user = atoi(argv[1]);  /* store socket #, or illegal # if callsign */
  40.     for(m=Mbox;m;m=m->next) {
  41.         if(!stricmp(m->name,argv[1]))
  42.             break;
  43.         if(m->user == lk.user) break;
  44.     }
  45.     if(m) {
  46.         lk.user = m->user;
  47.         strcpy(name,m->name);
  48.     } else {
  49. #endif
  50.         lk.user = atoi(argv[1]);
  51.         sprintf(name,"socket %d",lk.user);
  52. #ifdef MAILBOX
  53.     }
  54. #endif
  55.   
  56.     if((up = itop(lk.user)) == NULLUSOCK){
  57.         tputs("User socket error!\n");
  58.         return 0;
  59.     }
  60.     if(up->look) {
  61.         tprintf("Already looking at %s\n",argv[1]);
  62.         return 0;
  63.     }
  64.     if(lk.user == Curproc->input || lk.user == Curproc->output) {
  65.         tputs("Can not look at myself!\n");
  66.         return 0;
  67.     }
  68.     /* Now everything seems okay ! Get a session */
  69.     if((lk.sp = newsession(name,LOOK,1)) == NULLSESSION) {
  70.         tputs(TooManySessions);
  71.         return 0;
  72.     }
  73.   
  74.     up->look = Curproc;     /* Tell the socket to echo data to this process ! */
  75.     chat = 0;
  76.   
  77.     tprintf("%s session %d looking at %s\n", Sestypes[lk.sp->type],lk.sp->num,argv[1]);
  78.   
  79.     /* Process whatever's typed on the terminal */
  80.     memset(buf,0,MBXLINE);   /* Clear the input buffer */
  81.     while(recvline(Curproc->input,buf,sizeof(buf)-1) >= 0) {
  82.         if(buf[0] == '/') {
  83.             /* process commands */
  84.             switch(tolower(buf[1])) {
  85.                 case 'h':
  86.                 case '?':
  87. #ifdef MAILBOX
  88.                     tprintf("<Cmds>: /c-chat /m-msg /q-quit\n");
  89. #else
  90.                     tprintf("<Cmds>: /q-quit\n");
  91. #endif
  92.                     break;
  93. #ifdef MAILBOX
  94.                 case 'm':   /* Send a message to the user */
  95.                     if(!m)
  96.                     /* Not a mailbox user */
  97.                         break;
  98.                     cp = &buf[2];
  99.                     if(buf[2] == ' ')
  100.                         cp = &buf[3];
  101.                     usprintf(lk.user,"<sysop>: %s",cp);
  102.                     break;
  103.                 case 'c':   /* Initiate chat mode */
  104.                     if(chat || !m)
  105.                     /* Already in 'chat' mode or not a mailbox user */
  106.                         break;
  107.                     usputs(lk.user,"*** SYSOP Initiated CHAT.\n");
  108.                     up->look = NULL;    /* Disable echoing in socket layer */
  109.                 /* Now we need to redirect the network input
  110.                  * from the user's bbs process to the chat process
  111.                  */
  112.                     lk.sp->proc1 = newproc("CHAT Server",1024,look_input,0,
  113.                     (void *)&lk,NULL,0);
  114.                     chat = 1;
  115.                     break;
  116. #endif
  117.                 case 'b':
  118.                 case 'q':   /* quit chat mode, or look mode */
  119. #ifdef MAILBOX
  120.                     if(chat) {
  121.                         killproc(lk.sp->proc1);
  122.                         lk.sp->proc1 = NULLPROC;
  123.                         up->look = Curproc; /* Enable echoing in socket layer */
  124.                         usputs(lk.user,"*** BACK in mailbox\n");
  125.                         resume(up->owner);
  126.                         chat = 0;
  127.                     } else
  128. #endif
  129.                         goto done;
  130.                     break;
  131.             }
  132.         }
  133. #ifdef MAILBOX
  134.         else if(chat)
  135.             usprintf(lk.user,"<sysop>: %s",buf);
  136. #endif
  137.   
  138.         usflush(lk.user);
  139.         usflush(Curproc->output);
  140.         memset(buf,0,MBXLINE);   /* Clear the input buffer */
  141.     }
  142.     done:
  143.     /* A 'close' command was given, or user disconnected.
  144.      * Notify the user, kill the receiver input task and wait for a response
  145.      * from the user before freeing the session.
  146.      */
  147.     cp = sockerr(lk.sp->input);
  148.     tprintf("%s session %u closed: %s\n",
  149.     Sestypes[lk.sp->type],lk.sp->num,
  150.     cp != NULLCHAR ? cp : "EOF");
  151.   
  152.     if((up = itop(lk.user)) != NULLUSOCK)   /* Make sure socket is still there */
  153.         up->look = NULL;
  154. #ifdef MAILBOX
  155.     if(lk.sp->proc1 != NULLPROC) {
  156.         /* kill the receive process */
  157.         killproc(lk.sp->proc1);
  158.         lk.sp->proc1 = NULLPROC;
  159.         if(up) {
  160.             usputs(lk.user,"*** BACK in mailbox\n");
  161.             usflush(lk.user);
  162.             /* restart the mailbox process */
  163.             resume(up->owner);
  164.         }
  165.     }
  166. #endif
  167.     keywait(NULLCHAR,1);
  168.     freesession(lk.sp);
  169.     return 0;
  170. }
  171.   
  172. #ifdef MAILBOX
  173. /* Task that read the user's input socket (that formerly went to the socket
  174.  * process), and sends it to the look session !
  175.  */
  176. void
  177. look_input(unused,p1,p2)
  178. int unused;
  179. void *p1;
  180. void *p2;
  181. {
  182.     struct session *sp;
  183.     struct usock *up;
  184.     int user,c;
  185.   
  186.     sp = ((struct look *)p1)->sp;
  187.     user = ((struct look *)p1)->user;
  188.   
  189.     if((up=itop(user)) == NULLUSOCK) {
  190.         /* Make sure our parent doesn't try to kill us after we exit */
  191.         sp->proc1 = NULLPROC;
  192.         return;
  193.     }
  194.   
  195.     /* Suspend the process that owns the socket */
  196.     suspend(up->owner);
  197.   
  198.     /* Process input on the users socket connection */
  199.     while((c = recvchar(user)) != -1)
  200.         tputc((char)c);
  201.   
  202.     /* Make sure our parent doesn't try to kill us after we exit */
  203.     sp->proc1 = NULLPROC;
  204.   
  205.     /* Alert the parent, in case the chat was terminated by losing the
  206.      * user connection. This in effect will close the look session
  207.      */
  208.     alert(sp->proc,ENOTCONN);
  209.   
  210.     /* Resume the process that owns the socket */
  211.     resume(up->owner);
  212.   
  213. }
  214. #endif /* MAILBOX */
  215.   
  216. #endif /* LOOKSESSION */
  217.   
  218.