Advertisement
Aslai

Console based IRC

Aug 25th, 2011
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 48.26 KB | None | 0 0
  1. /////////////////////////// includes.h \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  2. #pragma once
  3. #include<stdlib.h>
  4. #include<stdio.h>
  5. #include<string.h>
  6. #ifdef _WIN32
  7.     #include<windows.h>
  8.     #include<conio.h>
  9. #else
  10.     #include <ncurses/curses.h>
  11.     #include <sys/types.h>
  12.     #include <sys/socket.h>
  13.     #include <netinet/in.h>
  14.     #include <netdb.h>
  15.     #include <unistd.h>
  16.     #include <termios.h>
  17.     #include <pthread.h>
  18.     #include <errno.h>
  19.     #include <signal.h>
  20. #endif
  21.  
  22.  
  23. /////////////////////////// main.h \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  24. #pragma once
  25. #include "includes.h"
  26. #include "terminaldisplay.h"
  27. #include<vector>
  28. #include<deque>
  29. extern int curroom;
  30. extern char rooms[64][200];
  31. extern int roomtotal;
  32. extern std::vector<char*>users[640];
  33. extern std::deque<int> keyqueue;
  34. extern bool keyqueuewriting;
  35. extern std::deque<char*> msgqueue;
  36. extern bool msgqueuewriting;
  37. void grabkey( void* in );
  38. void grabmsg( void* in );
  39.  
  40. /////////////////////////// main.cpp \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  41. #include "main.h"
  42. #include "network.h"
  43. #include "systemcalls.h"
  44. #include <vector>
  45.  
  46. #define K_UP 18656
  47. #define K_DOWN 20704
  48. #define K_LEFT 19424
  49. #define K_RIGHT 19936
  50. #define K_BACKSPACE 8
  51. #define K_ENTER 13
  52. #define K_ESCAPE 27
  53. #define K_HOME 18400
  54. #define K_END 20448
  55. #define K_DELETE 21472
  56. #define K_INSERT 21216
  57. #define K_F1 15104
  58. #define K_PGUP 18912
  59. #define K_PGDWN 20960
  60. #define K_CTRLPGUP 34528
  61. #define K_CTRLPGDWN 30432
  62. #define K_CTRLHOME 30688
  63. #define K_CTRLEND 30176
  64. #define K_
  65.  
  66.  
  67.  
  68.  
  69.  
  70. bool drawing = false;
  71. void inputloop( void* a );
  72. char globalnick[100] = "";
  73. int curroom = 0;
  74. char rooms[64][200];
  75. std::vector<char*> users[640];
  76. int roomtotal = 0;
  77. SOCKET globalsock;
  78.  
  79. void remroom( int id )
  80. {
  81.     for( int i = 0; i < users[id].size(); i++)
  82.     {
  83.         free( users[id][i] );
  84.     }
  85.     users[id].clear();
  86.     if( id < roomtotal )
  87.         for( int i = id; i < roomtotal; i++)
  88.         {
  89.             users[i] = users[i+1];
  90.             strcpy( rooms[i], rooms[i+1] );
  91.         }
  92.     if( curroom > 0 ) curroom--;
  93.     roomtotal--;
  94.     updaterooms();
  95.  
  96. }
  97.  
  98. int main()
  99. {
  100.     int t;
  101.     /*while( 1 )
  102.     {
  103.         t = getch();
  104.             if( t == 224 || t == 0 )
  105.             {
  106.                 t += getch() << 8;
  107.             }
  108.             printf( "%i\t%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i\n", t,(t&32768)!=0,(t&16384)!=0,(t&8192)!=0,(t&4096)!=0,(t&2048)!=0,(t&1024)!=0,(t&512)!=0,(t&256)!=0,(t&128)!=0,(t&64)!=0,(t&32)!=0,(t&16)!=0,(t&8)!=0,(t&4)!=0,(t&2)!=0, (t&1)!=0);
  109.     }
  110.     //*/
  111.     for( int k = 0; k < 64; k++ ) userpos[k] = 0;
  112.     for( int k = 0; k < 64; k ++)
  113.         for( int i = 0; i < BUFS; buffer[k][i++] = ' ' );
  114.     int i;
  115.     /*
  116.      SMALL_RECT screenpos;
  117.     screenpos.Top = 1;
  118.     screenpos.Left = 1;
  119.     screenpos.Bottom = SCREENHIG;
  120.     screenpos.Left = 800;
  121.     SetConsoleWindowInfo(hStdout, false, &screenpos );*/
  122.  
  123.     WSADATA globalWSAData;
  124.     WSAStartup( MAKEWORD(2, 2), &globalWSAData );
  125.     HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE) ;
  126.     COORD size = { 80, SCREENHIG+2 } ;
  127.     SetConsoleScreenBufferSize( hStdout, size );
  128.  
  129.  
  130.  
  131.     gotoxy( 0, 0 );
  132.     for( i = 0; i < 80*40; i ++ )
  133.     {
  134.         printf( " " );
  135.     }
  136.     gotoxy( 0, 0 );
  137.  
  138.     printf("Enter your nickname: ");
  139.     gets( globalnick );
  140.     char server[100];
  141.     printf("Enter the server address: ");
  142.     gets( server );
  143.     SOCKET sock = connectTCP( resolveAddr( server ), 6667 );
  144.     globalsock = sock;
  145.     sendMsg( sock, "NICK %s", globalnick );
  146.     sendMsg( sock, "USER %s %s %s :%s", globalnick, globalnick, globalnick, globalnick );
  147.  
  148.     callThread( grabkey, 0 );
  149.     callThread( grabmsg, &sock );
  150.     meclear();
  151.  
  152.     int scroll = 0;
  153.     char input[1000] = {0};
  154. //    int t;
  155.     int pos = 0;
  156.     int change = 0;
  157.     int change2 = change;
  158.     int bufprev = bufpos[curroom];
  159.     int moremoove = 0;
  160.  
  161.  
  162.     //inputloop( 0 );
  163.     bool shouldsleep = true;
  164.  
  165.     while(1){
  166.         if( shouldsleep )
  167.             Sleep(50);
  168.         shouldsleep = true;
  169.  
  170.         while( keyqueuewriting ){}
  171.         keyqueuewriting = true;
  172.         while( keyqueue.size() > 0 )
  173.         {
  174.             shouldsleep = false;
  175.             int t = keyqueue.front();
  176.             keyqueue.pop_front();
  177.             switch( t )
  178.             {
  179.                 case K_BACKSPACE:
  180.                     if( pos > 0 ){
  181.                         for( int i = pos-1; input[i] != 0; input[i] = input[++i] );
  182.                         movecursorback( 1 );
  183.                         pos -= 1;
  184.  
  185.                         } break;
  186.                 case K_LEFT: if( pos > 0 ){ movecursorback( 1 ); pos -= 1; } break;
  187.                 case K_RIGHT: if( input[pos] != 0 ) {movecursorforward( 1 ); pos += 1;} break;
  188.                 case K_HOME: { gotoxy(0,SCREENHIG-1); pos = 0; } break;
  189.                 case K_END: { int a = strlen( input ); /*gotoxy( 79,39 );*/ pos = a;} break;
  190.                 case K_PGUP: { if( bufpos[curroom] < (BUFFERLINES-TFIELDH)-4 ) bufpos[curroom] += 5; else bufpos[curroom] = BUFFERLINES-TFIELDH;  drawbuffer();savexy(); gotoxy(0,2); printf( "Line %i/%i    ",bufpos[curroom], BUFFERLINES-TFIELDH ); returntosaved();} break;
  191.                 case K_PGDWN: { if( bufpos[curroom] > 4 ) bufpos[curroom] -= 5; else bufpos[curroom] = 0; drawbuffer(); savexy(); gotoxy(0,2); printf( "Line %i/%i    ",bufpos[curroom], BUFFERLINES-TFIELDH ); returntosaved();} break;
  192.                 case K_CTRLEND: { bufpos[curroom] = BUFFERLINES-TFIELDH;  drawbuffer();savexy(); gotoxy(0,2); printf( "Line %i/%i    ",bufpos[curroom], BUFFERLINES-TFIELDH ); returntosaved();} break;
  193.                 case K_CTRLHOME: { bufpos[curroom] = 0; drawbuffer(); savexy(); gotoxy(0,2); printf( "Line %i/%i    ",bufpos[curroom], BUFFERLINES-TFIELDH ); returntosaved();} break;
  194.                 case K_CTRLPGUP: { userpos[curroom] -= 1; if( userpos[curroom] < 0 ) userpos[curroom] = 0; updateusers(); } break;
  195.                 case K_CTRLPGDWN: { userpos[curroom] += 1; if( userpos[curroom] > (int)(users[curroom].size() - 25) ) userpos[curroom] = users[curroom].size() - 25; updateusers(); } break;
  196.                 case K_F1: { pushstring( "-------------------------- HELP --------------------------");
  197.                              pushstring( "             Aslai's IRC Client version .01B              ");
  198.                              pushstring( "                                                          ");
  199.                              pushstring( " Basic Controls:");
  200.                              pushstring( "    Ctl + Q: Move channel selector backward");
  201.                              pushstring( "    Ctl + W: Move channel selector forward");
  202.                              pushstring( "    Page Up / Down: Scroll through chat history");
  203.                              pushstring( "    Ctl + Home: Scroll to the beginning of the chat log");
  204.                              pushstring( "    Ctl + End: Scroll to the end of the chat log");
  205.                              pushstring( "    Ctl + Page Up / Down: Scroll through user list");
  206.                              pushstring( "    F1: Show this info");
  207.                              pushstring( " ");
  208.                              pushstring( "----------------------------------------------------------");
  209.                              drawbuffer();
  210.                              lastnick[curroom][0] = 0;
  211.                              } break;
  212.                 case 3: exit(3); break;
  213.                 case K_ENTER:
  214.                 {
  215.                     if( input[0] == 0 ) continue;
  216.                     //pushstring( rooms[curroom] );
  217.                     if( (input[0] != '/' || (input[1] == '/' && input[0] == '/') ) && curroom != 0 )
  218.                     {
  219.                         if( input[0] == '/'){
  220.                             pushstringnick( input+1, globalnick );
  221.                             sendMsg( globalsock, "PRIVMSG %s :%s", rooms[curroom], input+1 );
  222.                         }
  223.                         else
  224.                         {
  225.                             pushstringnick( input, globalnick );
  226.                             sendMsg( globalsock, "PRIVMSG %s :%s", rooms[curroom], input );
  227.                         }
  228.                     }
  229.                     else if( input[0] == '/' && input[1] != '/' )
  230.                     {
  231.                         sendMsg( globalsock, "%s", input+1 );
  232.                     }
  233.  
  234.                     pos = 0;
  235.                     input[0] = 0;
  236.  
  237.  
  238.                     //while( drawing == true ){};
  239.                     //draw = false;
  240.                     drawbuffer();
  241.                     gotoxy( 0, SCREENHIG-1 );
  242.                     printf( "                                                                                ");
  243.                     gotoxy( 0, SCREENHIG-1 );
  244.                 } break;
  245.                 case 17: if( curroom > 0 ){ curroom --; newmsg[curroom] = false; drawbuffer(); updaterooms(); updateusers();}break;
  246.                 case 23: if( curroom < roomtotal ){ curroom++; newmsg[curroom] = false; drawbuffer(); updaterooms(); updateusers();}break;
  247.  
  248.                 default: if( t >= 0x20 && t < 0x80 ){
  249.                         int a = strlen( input );
  250.                         if( a > 998 )
  251.                             a = 998;
  252.                         input[999] = 0;
  253.                         for( int i = a+1; i > pos; input[i] = input[--i]);
  254.                         input[ pos ] = t;
  255.                         //savexy();
  256.                         //printf( "%s   ", input + pos );
  257.                         //returntosaved();
  258.                         movecursorforward( 1 );
  259.                         pos++;
  260.                         } break;
  261.             }
  262.             change = bufpos[curroom] - bufprev;
  263.             if( pos > 998 ) pos = 998;
  264.             if( pos - scroll < 1 ) {scroll += (pos-scroll-1); gotoxy( pos-scroll, SCREENHIG-1 );}
  265.             if( pos - scroll > 78 ) {scroll += (pos-scroll-78); gotoxy( pos-scroll, SCREENHIG-1 );}
  266.             if( scroll < 0 ) {scroll = 0; gotoxy( 0, SCREENHIG-1 ); }
  267.             savexy();
  268.             gotoxy( 0, SCREENHIG-1 );
  269.             char y = (input+scroll)[79];
  270.             (input+scroll)[79] = 0;
  271.             printf( "%s   ", input+scroll );
  272.             (input+scroll)[79] = y;
  273.  
  274.             returntosaved();
  275.             /*if( bufpos[curroom] != bufprev )
  276.             {
  277.                 moremoove += bufpos[curroom] - bufprev;
  278.             }
  279.             else
  280.                 moremoove = 0;*/
  281.             change2 = change;
  282.             bufprev = bufpos[curroom];
  283.         }
  284.         keyqueuewriting = false;
  285.  
  286.  
  287.         while( msgqueuewriting ){}
  288.         msgqueuewriting = true;
  289.         while( msgqueue.size() > 0 )
  290.         {
  291.             shouldsleep = false;
  292.             char* recvbuf = msgqueue.front();
  293.             msgqueue.pop_front();
  294.             if( recvbuf == 0 )
  295.             {
  296.                 pushstring("");
  297.                 pushstring("");
  298.  
  299.                 pushstring( "Connection Closed");
  300.                 pushstring("");
  301.                 pushstring("");
  302.                 drawbuffer();
  303.                 Sleep(2000);
  304.                 exit(0);
  305.             }
  306.             if( recvbuf[0] == 0 ) { continue;}
  307.             int g;
  308.             for( g = 0; recvbuf[g] != '\r' && recvbuf[g] != 0; g ++);
  309.                 recvbuf[g] = 0;
  310.             if( recvbuf[0] != ':' )
  311.             {
  312.                 recvbuf[1] = 'O';
  313.                 sendMsg( sock, "%s", recvbuf );
  314.                 continue;
  315.             }
  316.             char* recvbuf2 = recvbuf;//[2000];
  317.             //memcpy( recvbuf2, recvbuf, 2000 );
  318.             char* nick = recvbuf+1;
  319.             char* user = 0, * host = 0, * msg, *argsw[32], *args[32];
  320.             int argc = 0;
  321.             int t = 0;
  322.             bool special = false;
  323.  
  324.             for( int i = 0; recvbuf[i] != 0; i ++, t++ )
  325.             {
  326.  
  327.                 if( recvbuf[i] == '!' )
  328.                 {
  329.                     recvbuf[i] = 0;
  330.                     user = recvbuf+1+i;
  331.                 }
  332.                 if( recvbuf[i] == '@' )
  333.                 {
  334.                     recvbuf[i] = 0;
  335.                     host = recvbuf+1+i;
  336.                 }
  337.                 if( recvbuf[i] == ' ' )
  338.                 {
  339.                     if (host == 0 )
  340.                         special = true;
  341.                     recvbuf[i] = 0;
  342.                     msg = recvbuf+1+i;
  343.                     break;
  344.                 }
  345.             }
  346.             t+=2;
  347.             bool coloned = false;
  348.             for( int i = 0; msg[i] != 0; i ++, t++ ){
  349.                 if( msg[i] == ' ' ){
  350.                     msg[i] = 0;
  351.                     args[0] = msg+1+i;
  352.                     argsw[0] = recvbuf2+t;
  353.                     if( *(msg+1+i) == ':' )
  354.                     {
  355.                         args[0]++;
  356.                         argsw[0]++;
  357.                         argc++;
  358.                         coloned = true;
  359.                         break;
  360.                     }
  361.                     break;
  362.                 }
  363.             }
  364.             t++;
  365.             if( !coloned )
  366.             {
  367.                 for( int i = 0; args[0][i] != 0; i ++, t++ ){
  368.                     if( args[0][i] == ' ' ){
  369.                         argc++;
  370.                         args[argc] = args[0]+1+i;
  371.                         args[0][i] = 0;
  372.                         argsw[argc] = recvbuf2+t;
  373.                         if( *(args[0]+1+i) == ':' )
  374.                         {
  375.                             args[argc]++;
  376.                             argsw[argc]++;
  377.  
  378.                             break;
  379.                         }
  380.  
  381.                     }
  382.                 }
  383.                 argc++;
  384.             }
  385.             /*char aa[2000];
  386.             if( argc >= 3 )
  387.                 sprintf( aa, "*** msg = %s; argc = %i; args[0] = %s; args[1] = %s; args[2] = %s; args[3] = %s; args[4] = %s;", msg, argc,args[0], args[1],args[2],args[3],args[4] );
  388.             else if( argc > 2 )
  389.                 sprintf( aa, "*** msg = %s; argc = %i; args[0] = %s; args[1] = %s; args[2] = %s;", msg, argc,args[0], args[1],args[2] );
  390.             else if( argc > 1 )
  391.                 sprintf( aa, "*** msg = %s; argc = %i; args[0] = %s; args[1] = %s;", msg, argc,args[0], args[1] );
  392.             else if( argc > 0 )
  393.                 sprintf( aa, "*** msg = %s; argc = %i; args[0] = %s;", msg, argc,args[0] );
  394.             else sprintf( aa, "*** msg = %s; argc = %i;", msg, argc );
  395.             pushstring( aa );*/
  396.             if( (strcmp( "042", msg ) == 0 ||
  397.                 strcmp( "375", msg ) == 0 ||
  398.                 strcmp( "372", msg ) == 0 ||
  399.                 strcmp( "376", msg ) == 0 ||
  400.                  //strcmp( "352", msg ) == 0 ||
  401.                 strcmp( "251", msg ) == 0 ||
  402.                 strcmp( "252", msg ) == 0 ||
  403.                 strcmp( "254", msg ) == 0 ||
  404.                 strcmp( "255", msg ) == 0 ||
  405.                 strcmp( "265", msg ) == 0 ||
  406.                 strcmp( "266", msg ) == 0 ||
  407.  
  408.  
  409.                 //strcmp( "PRIVMSG", msg ) == 0 ||
  410.                 strcmp( "NOTICE", msg ) == 0 ) && argc == 2 )
  411.             {
  412.                 pushstringnick( args[1] , nick );
  413.             }
  414.             if( (strcmp( "474", msg ) == 0 && argc == 3 ) )
  415.                pushstringnick( args[2] , nick );
  416.             if( strcmp( "JOIN", msg ) == 0 && strcmp( nick, globalnick ) == 0 && argc == 1 )
  417.             {
  418.                 strcpy( rooms[++roomtotal], args[0] );
  419.                 curroom = roomtotal;
  420.                 //savexy();
  421.                 meclear();
  422.                 //returntosaved();
  423.  
  424.                 //for( int i = 0; i < 70*38; buffer[curroom][i++] = ' ' );
  425.                 drawbuffer();
  426.                 updaterooms();
  427.                 sendMsg( sock, "NAMES %s", args[0] );
  428.             }
  429.             else if( strcmp( "JOIN", msg ) == 0 && argc == 1 )
  430.             {
  431.                 for( i = 1; i <= roomtotal; i ++ )
  432.                 {
  433.                     if( strcmp( rooms[i], args[0] ) == 0 )
  434.                     {
  435.                         char b[1000];
  436.                         sprintf( b, "* %s has joined %s *", nick, args[0] );
  437.                         int bc = curroom;
  438.                                     curroom = i;
  439.                                     pushstring( b );
  440.                                     curroom = bc;
  441.                         char* a = (char*) malloc(200);
  442.                         a[0] = ' ';
  443.                         strcpy( a+1, nick );
  444.                         users[i].push_back( a );
  445.                         updateusers();
  446.                     }
  447.                 }
  448.             }
  449.             if( strcmp( "PART", msg ) == 0 && strcmp( nick, globalnick ) == 0 && argc == 1 )
  450.             {
  451.                 for( i = 1; i <= roomtotal; i ++ )
  452.                 {
  453.                     if( strcmp( rooms[i], args[0] ) == 0 )
  454.                     {
  455.                         remroom(i);
  456.                         if( curroom > roomtotal ) curroom--;
  457.                     }
  458.                 }
  459.  
  460.             }
  461.             else if( strcmp( "PART", msg ) == 0 && (argc == 1 || argc == 2) )
  462.             {
  463.                 for( i = 1; i <= roomtotal; i ++ )
  464.                 {
  465.                     if( strcmp( rooms[i], args[0] ) == 0 )
  466.                     {
  467.                         for( int k = 0; k < users[i].size(); k ++ )
  468.                         {
  469.                             if( strcmp( nick, users[i][k]+1) == 0)
  470.                             {
  471.                                 char a[2000];
  472.                                 sprintf( a, "* %s has left %s %s%s%s*", nick, args[0], argc==2?"(":"",argc==2?args[1]:"",argc==2?") ":"" );
  473.                                 int b = curroom;
  474.                                     curroom = i;
  475.                                     pushstring( a );
  476.                                     curroom = b;
  477.                                 free(users[i][k]);
  478.                                 users[i].erase(users[i].begin()+k);
  479.                                 //users[i].pop_back();
  480.                                 updateusers();
  481.                             }
  482.                         }
  483.                         break;
  484.                     }
  485.                 }
  486.             }
  487.             if( strcmp( "KICK", msg ) == 0 && (argc == 2 || argc == 3) )
  488.             {
  489.                 for( i = 1; i <= roomtotal; i ++ )
  490.                 {
  491.                     if( strcmp( rooms[i], args[0] ) == 0 )
  492.                     {
  493.                         for( int k = 0; k < users[i].size(); k ++ )
  494.                         {
  495.                             if( strcmp( args[1], users[i][k]+1) == 0)
  496.                             {
  497.                                 char a[2000];
  498.                                 sprintf( a, "* %s has kicked %s %s%s%s*", nick, args[1], argc==3?"(":"",argc==3?args[2]:"",argc==3?") ":"" );
  499.                                 int b = curroom;
  500.                                     curroom = i;
  501.                                     pushstring( a );
  502.                                     curroom = b;
  503.                                 if( strcmp( args[1], globalnick ) == 0 )
  504.                                 {
  505.                                     remroom(i);
  506.                                     char aaa[1000];
  507.                                     sprintf( aaa, "* You are no longer in %s *", args[0] );
  508.                                     pushstring(aaa);
  509.                                     break;
  510.                                 }
  511.                                 free(users[i][k]);
  512.                                 users[i].erase(users[i].begin()+k);
  513.                                 //users[i].pop_back();
  514.                                 updateusers();
  515.                             }
  516.                         }
  517.                         break;
  518.                     }
  519.                 }
  520.             }
  521.             if( strcmp( "MODE", msg ) == 0 && (argc > 2) )
  522.             {
  523.                 int modetype = 0;
  524.                 int modetrack = 0;
  525.  
  526.                 for( i = 1; i <= roomtotal; i ++ )
  527.                 {
  528.                     if( strcmp( rooms[i], args[0] ) == 0 )
  529.                     {
  530.                         for( int j = 0; j < argc - 1 && args[1][modetrack] != 0; j ++, modetrack ++ )
  531.                         {
  532.                             while( args[1][modetrack] == '+' || args[1][modetrack] == '-' ){
  533.                                 modetype = 0;
  534.                                 if( args[1][modetrack] == '-' ) modetype = 1;
  535.                                 modetrack++;}
  536.                             //for( int k = 0; k < users[i].size(); k ++ )
  537.                             //{
  538.                                 //if( strcmp( args[j+2], users[i][k]+1) == 0)
  539.                                 //{
  540.                                     char a[2000];
  541.                                     sprintf( a, "* %s sets mode %c%c on %s *", nick, modetype == 0 ? '+' : '-',  args[1][modetrack], args[j+2] );
  542.                                     int b = curroom;
  543.                                     curroom = i;
  544.                                     pushstring( a );
  545.                                     curroom = b;
  546.                                     /*char t = ' ';
  547.                                     switch( args[1][modetrack] ){
  548.                                     case 'v': }
  549.                                     pushstring( a );
  550.                                     curroom = i;
  551.                                     free(users[i][k]);
  552.                                     users[i].erase(users[i].begin()+k);*/
  553.  
  554.                                     //users[i].pop_back();
  555.                                     //updateusers();
  556.                                     sendMsg( sock, "whois %s", args[j+2] );
  557.                                 //}
  558.                             //}
  559.                         }
  560.                         break;
  561.  
  562.                     }
  563.                 }
  564.             }
  565.             if( strcmp( "QUIT", msg ) == 0 && (argc == 1 || argc == 0) )
  566.             {
  567.                 for( i = 1; i <= roomtotal; i ++ )
  568.                 {
  569.                     for( int k = 0; k < users[i].size(); k ++ )
  570.                     {
  571.                         if( strcmp( nick, users[i][k]+1) == 0)
  572.                         {
  573.                             char a[2000];
  574.                             sprintf( a, "* %s has quit %s%s%s*", nick, argc==1?"(":"",argc==1?args[0]:"",argc==1?") ":"" );
  575.                             int aa = curroom;
  576.                             curroom = i;
  577.                             pushstring( a );
  578.                             curroom = aa;
  579.                             free(users[i][k]);
  580.                             users[i].erase(users[i].begin()+k);
  581.                             //users[i].pop_back();
  582.                             updateusers();
  583.                         }
  584.                     }
  585.                 }
  586.             }
  587.  
  588.             if( (strcmp( "319", msg ) == 0 && argc == 3 ) )
  589.             {
  590.                 int i;
  591.                 char* tmp = args[2];
  592.                 for( int k = 0; args[2][k] != 0; k++ )
  593.                 {
  594.                     if( args[2][k] == ' ' )
  595.                     {
  596.                         args[2][k] = 0;
  597.                         for( i = 1; i <= roomtotal; i ++ )
  598.                         {
  599.                             bool c = false;
  600.                             if( tmp[0] != '#' ) {tmp ++; c = true; }
  601.                             if( strcmp( tmp, rooms[i]) == 0 )
  602.                             {
  603.                                 for( int j = 0; j < users[i].size(); j++ )
  604.                                 {
  605.                                     if( strcmp( users[i][j]+1, args[1] ) == 0 )
  606.                                     {
  607.                                         users[i][j][0] = ' ';
  608.                                         if( c )
  609.                                             users[i][j][0] = tmp[-1];
  610.                                     }
  611.                                 }
  612.                             }
  613.                         }
  614.                         tmp = args[2]+k+1;
  615.                     }
  616.                 }
  617.                 updateusers();
  618.             }
  619.  
  620.             if( (strcmp( "353", msg ) == 0 && argc == 4 ) )
  621.             {
  622.                 int i;
  623.                 for( i = 1; i <= roomtotal; i ++ )
  624.                 {
  625.                     if( strcmp( rooms[i], args[2] ) == 0 )
  626.                     {
  627.                         char* tmp = args[3];
  628.                         for( int k = 0; args[3][k-1] != 0 || k == 0; k++ )
  629.                         {
  630.                             if( args[3][k] == ' ' || args[3][k] == 0 )
  631.                             {
  632.                                 args[3][k] = 0;
  633.  
  634.                                 char* a = (char*) malloc( 200 );
  635.                                 if( tmp[0] != '~' && tmp[0] != '@' && tmp[0] != '%' && tmp[0] != '+' && tmp[0] != '&' )
  636.                                 {
  637.                                     a[0] =  ' ';
  638.                                     strcpy( a+1, tmp );
  639.                                 }
  640.                                 else
  641.                                 {
  642.                                     a[0] =  tmp[0];
  643.                                     int g = 0;
  644.                                     while( tmp[g] == '~' || tmp[g] == '@' || tmp[g] == '%' || tmp[g] == '+' || tmp[g] == '&' ) g++;
  645.                                     strcpy( a+1, tmp+g );
  646.                                 }
  647.                                 printf( "%s ", a );
  648.                                 bool broken = false;
  649.                                 for( int p = 0; p < users[i].size(); p++)
  650.                                 {
  651.                                     if( strcmp( users[i][p]+1, a+1 ) == 0 )
  652.                                     {
  653.                                         broken = true;
  654.                                         users[i][p][0] = a[0];
  655.                                         free( a );
  656.                                         break;
  657.                                     }
  658.                                 }
  659.                                 if( !broken )
  660.                                     users[i].push_back( a );
  661.  
  662.                                 //printf( "%s", a );
  663.  
  664.                                 tmp = args[3] + k + 1;
  665.                             }
  666.                         }
  667.                     }
  668.                 }
  669.                 updateusers();
  670.             }
  671.  
  672.             if( strcmp( "NICK", msg ) == 0 &&
  673.                 strcmp( nick, globalnick ) == 0
  674.                && argc == 1 )
  675.                 {
  676.                     strcpy( globalnick, args[0] );
  677.                     char a[1000];
  678.                     sprintf( a, "* You are now known as %s *", globalnick );
  679.                     pushstring( a );
  680.                 }
  681.                 else if( strcmp( "NICK", msg ) == 0 && argc == 1 )
  682.                 {
  683.                     for( int i = 0; i <= roomtotal; i ++ )
  684.                     {
  685.                         for( int j = 0; j < users[i].size(); j ++ )
  686.                         {
  687.                             if( strcmp( users[i][j]+1, nick ) == 0 )
  688.                             {
  689.                                 char a[1000];
  690.                                 sprintf( a, "* %s is now known as %s *", nick, args[0] );
  691.                                 int k = curroom;
  692.                                 curroom = i;
  693.                                 pushstring( a );
  694.                                 curroom = k;
  695.                                 strcpy( users[i][j]+1, args[0] );
  696.                                 break;
  697.                             }
  698.                         }
  699.                     }
  700.                     updateusers();
  701.                 }
  702.  
  703.             if( strcmp( "PRIVMSG", msg ) == 0
  704.                && argc == 2 )
  705.                 {
  706.                     if( args[1][0] == 1 )
  707.                     {
  708.  
  709.                         //sendMsg( sock, "PRIVMSG %s :%cCTCP %c")
  710.                     }
  711.                     if( args[0][0] == '#' ){
  712.                     for( int i = 1; i <= roomtotal; i ++ )
  713.                     {
  714.                         if( strcmp( rooms[i], args[0] ) == 0 )
  715.                         {
  716.                             if( args[1][0] == 1 )
  717.                             {
  718.                                 if( args[1][1] == 'A' && args[1][2] == 'C' && args[1][3] == 'T' && args[1][4] == 'I' && args[1][5] == 'O' && args[1][6] == 'N' && args[1][7] == ' ')
  719.                                 {
  720.                                     char b[1000];
  721.                                     sprintf( b, "* %s %s *", nick, args[1] + 8 );
  722.                                     int a = curroom;
  723.                                     curroom = i;
  724.                                     pushstringnick( b, nick );
  725.                                     curroom = a;
  726.                                 }
  727.                             }
  728.                             else
  729.                             {
  730.                             int a = curroom;
  731.                             curroom = i;
  732.                             pushstringnick( args[1], nick );
  733.                             curroom = a;
  734.                             }
  735.                             if( curroom != i )
  736.                             {
  737.                                 newmsg[i] = true;
  738.                                 updaterooms();
  739.                             }
  740.  
  741.                         }
  742.                     }
  743.                     }
  744.                     else
  745.                     {
  746.                         char a[200];
  747.                         sprintf( a, "%s [PRIVATE]", nick );
  748.                         pushstringnick( args[1], a );
  749.                     }
  750.                 }
  751.  
  752.  
  753.  
  754.             drawbuffer();
  755.             free( recvbuf );
  756.         }
  757.         msgqueuewriting = false;
  758.         //if( recvbuf[0] == ':' )
  759.  
  760.     }
  761.  
  762. }
  763.  
  764. void inputloop( void* a )
  765. {
  766.     int scroll = 0;
  767.     char input[1000] = {0};
  768.     int t;
  769.     int pos = 0;
  770.     int change = 0;
  771.     int change2 = change;
  772.     int bufprev = bufpos[curroom];
  773.     int moremoove = 0;
  774.     while(1){
  775.         gotoxy( 0, SCREENHIG-1 );
  776.         printf( "                                                                                ");
  777.         gotoxy( 0, SCREENHIG-1 );
  778.         bool entered = false;
  779.         while( !entered ){
  780.             t = getch();
  781.             if( t == 224 || t == 0 )
  782.             {
  783.                 t += getch() << 8;
  784.             }
  785.             switch( t )
  786.             {
  787.                 case K_BACKSPACE:
  788.                     if( pos > 0 ){
  789.                         for( int i = pos-1; input[i] != 0; input[i] = input[++i] );
  790.                         movecursorback( 1 );
  791.                         pos -= 1;
  792.  
  793.                         } break;
  794.                 case K_LEFT: if( pos > 0 ){ movecursorback( 1 ); pos -= 1; } break;
  795.                 case K_RIGHT: if( input[pos] != 0 ) {movecursorforward( 1 ); pos += 1;} break;
  796.                 case K_HOME: { gotoxy(0,39); pos = 0; } break;
  797.                 case K_END: { int a = strlen( input ); /*gotoxy( 79,39 );*/ pos = a;} break;
  798.                 case K_PGUP: { if( bufpos[curroom] < (BUFFERLINES-TFIELDH)-4 ) bufpos[curroom] += 5; else bufpos[curroom] = BUFFERLINES-TFIELDH;  drawbuffer();savexy(); gotoxy(0,2); printf( "Line %i/%i    ",bufpos[curroom], BUFFERLINES-TFIELDH ); returntosaved();} break;
  799.                 case K_PGDWN: { if( bufpos[curroom] > 4 ) bufpos[curroom] -= 5; else bufpos[curroom] = 0; drawbuffer(); savexy(); gotoxy(0,2); printf( "Line %i/%i    ",bufpos[curroom], BUFFERLINES-TFIELDH ); returntosaved();} break;
  800.  
  801.                 case 3: exit(3); break;
  802.                 case K_ENTER: entered = true; break;
  803.                 case 17: if( curroom > 0 ){ curroom --; drawbuffer(); updaterooms(); updateusers();}break;
  804.                 case 23: if( curroom < roomtotal ){ curroom++; drawbuffer(); updaterooms(); updateusers();}break;
  805.  
  806.                 default: if( t >= 0x20 && t < 0x80 || t == 7 ){
  807.                         int a = strlen( input );
  808.                         if( a > 998 )
  809.                             a = 998;
  810.                         input[999] = 0;
  811.                         for( int i = a+1; i > pos; input[i] = input[--i]);
  812.                         input[ pos ] = t;
  813.                         //savexy();
  814.                         //printf( "%s   ", input + pos );
  815.                         //returntosaved();
  816.                         movecursorforward( 1 );
  817.                         pos++;
  818.                         } break;
  819.             }
  820.             change = bufpos[curroom] - bufprev;
  821.             if( pos > 998 ) pos = 998;
  822.             if( pos - scroll < 1 ) {scroll += (pos-scroll-1); gotoxy( pos-scroll, 39 );}
  823.             if( pos - scroll > 78 ) {scroll += (pos-scroll-78); gotoxy( pos-scroll, 39 );}
  824.             if( scroll < 0 ) {scroll = 0; gotoxy( 0, 39 ); }
  825.             savexy();
  826.             gotoxy( 0, 39 );
  827.             printf( "%s   ", input+scroll );
  828.             returntosaved();
  829.             /*if( bufpos[curroom] != bufprev )
  830.             {
  831.                 moremoove += bufpos[curroom] - bufprev;
  832.             }
  833.             else
  834.                 moremoove = 0;*/
  835.             change2 = change;
  836.             bufprev = bufpos[curroom];
  837.         }
  838.         //while( draw == true ){};
  839.         //draw = true;
  840.         if( input[0] == 0 ) continue;
  841.         //pushstring( rooms[curroom] );
  842.         if( (input[0] != '/' || (input[1] == '/' && input[0] == '/') ) && curroom != 0 )
  843.         {
  844.             if( input[0] == '/'){
  845.                 pushstringnick( input+1, globalnick );
  846.                 sendMsg( globalsock, "PRIVMSG %s :%s", rooms[curroom], input+1 );
  847.             }
  848.             else
  849.             {
  850.                 pushstringnick( input, globalnick );
  851.                 sendMsg( globalsock, "PRIVMSG %s :%s", rooms[curroom], input );
  852.             }
  853.         }
  854.         else if( input[0] == '/' && input[1] != '/' )
  855.         {
  856.             sendMsg( globalsock, "%s", input+1 );
  857.         }
  858.  
  859.         pos = 0;
  860.         input[0] = 0;
  861.  
  862.  
  863.         //while( drawing == true ){};
  864.         //draw = false;
  865.         drawbuffer();
  866.  
  867.     }
  868. }
  869. bool keyqueuewriting = false;
  870. std::deque<int> keyqueue;
  871. void grabkey( void* in )
  872. {
  873.     int t;
  874.     while( true ){
  875.         t = getch();
  876.         if( t == 224 || t == 0 )
  877.         {
  878.             t += getch() << 8;
  879.         }
  880.         while( keyqueuewriting ){}
  881.         keyqueuewriting = true;
  882.         keyqueue.push_back( t );
  883.         keyqueuewriting = false;
  884.     }
  885. }
  886. std::deque<char*> msgqueue;
  887. bool msgqueuewriting;
  888. void grabmsg( void* in )
  889. {
  890.     SOCKET sock = *( (SOCKET*) in );
  891.     char* recvbuf;
  892.     while( true ){
  893.         recvbuf = (char*) malloc( 1000 );
  894.         if( recvbuf == 0 ) continue;
  895.         char* a = getMessage(sock, recvbuf);
  896.         if( a == 0 ){
  897.             while( msgqueuewriting ){}
  898.             msgqueuewriting = true;
  899.             msgqueue.push_back( 0 );
  900.             msgqueuewriting = false;}
  901.         if( recvbuf[0] == 0 ) { free(recvbuf); continue;}
  902.         while( msgqueuewriting ){}
  903.         msgqueuewriting = true;
  904.         msgqueue.push_back( recvbuf );
  905.         msgqueuewriting = false;
  906.     }
  907. }
  908.  
  909.  
  910. /////////////////////////// network.h \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  911. #pragma once
  912. #include "includes.h"
  913. #ifdef _WIN32
  914.     #include <windows.h>
  915.     #include <winsock2.h>
  916.     #include <ws2tcpip.h>
  917. #else
  918.     #include <sys/types.h>
  919.     #include <sys/socket.h>
  920.     #include <netinet/in.h>
  921.     #include <netdb.h>
  922.     #include <unistd.h>
  923.     #include <termios.h>
  924.     #include <pthread.h>
  925.     #include <errno.h>
  926.     #include <signal.h>
  927.     #define SOCKET int
  928. #endif
  929.  
  930.  
  931. void senda( SOCKET sock, char* str );
  932. unsigned long resolveAddr( char* addr, int ind = -1 );
  933. SOCKET connectTCP( unsigned long ip, short int port );
  934. char* getMessage( SOCKET recvOn, char* buffer );
  935. void sendMsg( SOCKET tosend, const char* format, ... );
  936.  
  937. /////////////////////////// network.cpp \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  938. #include "network.h"
  939. void senda( SOCKET sock, char* str )
  940. {
  941.     send( sock, str, strlen( str ), 0 );
  942. }
  943. unsigned long resolveAddr( char* addr, int ind )
  944. {
  945.     hostent* host = gethostbyname( addr );
  946.     if( host == 0 )
  947.     {
  948.         printf( "Failed to lookup %s.", addr );
  949.         return 0;
  950.     }
  951.     if( ind > host->h_length ) return -1;
  952.     unsigned long     myhost = *((unsigned long*) host->h_addr);
  953.     return myhost;
  954. }
  955.  
  956.  
  957. SOCKET connectTCP( unsigned long ip, short int port )
  958. {
  959.     SOCKET ret;
  960.     ret = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  961.     if( ret < 0 ) return 0;
  962.     sockaddr_in clientService;
  963.     clientService.sin_family = AF_INET;
  964.     clientService.sin_addr.s_addr = ip;
  965.     clientService.sin_port = htons( port );
  966.     int result = connect( ret, (sockaddr*) &clientService, sizeof(clientService));
  967.     if( result < 0 ) return 0;
  968.     return ret;
  969. }
  970.  
  971. char* getMessage( SOCKET recvOn, char* buffer )
  972. {
  973.     char tmp = 0; int pos = 0; int val = 1;
  974.     val = recv( recvOn, &tmp, 1, 0 );
  975.     if( val == 0 ) {
  976.         buffer[0] = 0;
  977.         return (char*)1;
  978.     }
  979.     if( val < 0 ) {
  980.         buffer[0] = 0;
  981.         return 0;
  982.     }
  983.     while( tmp != '\n' && val > 0 ) {
  984.         buffer[pos++] = tmp;
  985.         val = recv( recvOn, &tmp, 1, 0 );
  986.         if( val <= 0 ) {
  987.             buffer[pos] = 0;
  988.             return buffer;
  989.         }
  990.     }
  991.     buffer[pos++] = 0;
  992.     return buffer;
  993. }
  994.  
  995. void sendMsg(  SOCKET tosend, const char* format, ... )
  996. {
  997.     char sendBuf[2000];
  998.     va_list strt;
  999.     va_start( strt, format );
  1000.     vsprintf( sendBuf, format, strt );
  1001.     va_end( strt );
  1002.     senda( tosend, sendBuf );
  1003.     senda( tosend, "\r\n" );
  1004. }
  1005.  
  1006.  
  1007. /////////////////////////// systemcalls.h \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  1008. #pragma once
  1009. #include "includes.h"
  1010. #ifdef _WIN32
  1011.     #include <process.h>
  1012.     unsigned long CURRENT_TIME();
  1013.     uintptr_t callThread( void(*func)(void*), void* args );
  1014. #else
  1015.     int callThread( void(*func)(void*), void* args );
  1016. #endif
  1017.  
  1018. /////////////////////////// systemcalls.cpp \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  1019. #include "systemcalls.h"
  1020. #ifdef _WIN32
  1021.     unsigned long CURRENT_TIME(){
  1022.     SYSTEMTIME t; GetSystemTime(&t); return t.wMilliseconds + t.wSecond * 1000 + t.wMinute * 60000 + t.wHour * 3600000 + t.wDay * 86400000 + t.wMonth * 2678400000;
  1023.     }
  1024.  
  1025.     uintptr_t callThread( void(*func)(void*), void* args )
  1026.     {
  1027.         return _beginthread( func, 0, args );
  1028.     }
  1029. #else
  1030.     int callThread( void(*func)(void*), void* args )
  1031.     {
  1032.  
  1033.         pthread_t iThreadId;
  1034.         return pthread_create(&iThreadId, NULL, (void*(*)(void*))func, args);
  1035.     }
  1036. #endif
  1037.  
  1038. /////////////////////////// terminalcontrol.h \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  1039. #pragma once
  1040. #include "includes.h"
  1041.  
  1042. #ifdef _WIN32
  1043.     extern int cursorPosX;
  1044.     extern int cursorPosY;
  1045.     extern bool saved;
  1046.     #include<wincon.h>
  1047. #endif
  1048. void gotoxy( short x, short y );
  1049. void savexy();
  1050. void returntosaved();
  1051. void invert( );
  1052. void movecursorback( int a );
  1053. void movecursorforward( int a );
  1054.  
  1055. /////////////////////////// terminalcontrol.cpp \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  1056. #include "terminalcontrol.h"
  1057.  
  1058. #ifdef _WIN32
  1059.     int cursorPosX=0;
  1060.     int cursorPosY=0;
  1061.     bool saved = false;
  1062.     void gotoxy( short x, short y )
  1063.     {
  1064.         HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE) ;
  1065.         COORD position = { x, y } ;
  1066.         SetConsoleCursorPosition( hStdout, position ) ;
  1067.     }
  1068.     void savexy()
  1069.     {
  1070.         HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE) ;
  1071.         CONSOLE_SCREEN_BUFFER_INFO buff;
  1072.         GetConsoleScreenBufferInfo( hStdout, &buff );
  1073.         cursorPosX=buff.dwCursorPosition.X;
  1074.         cursorPosY=buff.dwCursorPosition.Y;
  1075.         saved = true;
  1076.     }
  1077.     void returntosaved()
  1078.     {
  1079.         HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE) ;
  1080.         COORD position = { cursorPosX, cursorPosY } ;
  1081.         SetConsoleCursorPosition( hStdout, position ) ;
  1082.         saved = false;
  1083.     }
  1084.     void movecursorback( int a )
  1085.     {
  1086.         HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE) ;
  1087.         CONSOLE_SCREEN_BUFFER_INFO buff;
  1088.         GetConsoleScreenBufferInfo( hStdout, &buff );
  1089.         gotoxy( max( buff.dwCursorPosition.X - a, 0 ), buff.dwCursorPosition.Y );
  1090.     }
  1091.     void movecursorforward( int a )
  1092.     {
  1093.         HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE) ;
  1094.         CONSOLE_SCREEN_BUFFER_INFO buff;
  1095.         GetConsoleScreenBufferInfo( hStdout, &buff );
  1096.         gotoxy( max( buff.dwCursorPosition.X + a, 0 ), buff.dwCursorPosition.Y );
  1097.     }
  1098.     /*void invert( )
  1099.     {
  1100.         HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE) ;
  1101.         SetConsoleTextAttribute( hStdout,  );
  1102.     }*/
  1103.  
  1104. #else
  1105.     void gotoxy( short x, short y )
  1106.     {
  1107.         //printf( "\027[%i;%iH", x, y );
  1108.         move(y, x);
  1109.         refresh();
  1110.     }
  1111.     void savexy()
  1112.     {
  1113.         printf( "\027[s" );
  1114.     }
  1115.     void returntosaved()
  1116.     {
  1117.         printf( "\027[8" );
  1118.     }
  1119.     /*void invert( )
  1120.     {
  1121.         printf( "\x1B[7m" );
  1122.     }*/
  1123.     void movecursorback( int a )
  1124.     {
  1125.         printf( "\027[%iD", a );
  1126.     }
  1127.     void movecursorforward( int a )
  1128.     {
  1129.         printf( "\027[%iC", a );
  1130.     }
  1131. #endif
  1132.  
  1133. /////////////////////////// terminaldisplay.h \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  1134. #pragma once
  1135. #include "includes.h"
  1136. #include "terminalcontrol.h"
  1137. #include "main.h"
  1138. #define SCREENHIG 48
  1139. #define TFIELDW 60
  1140. #define TFIELDH (SCREENHIG-4)
  1141. #define BUFFERLINES 500
  1142. #define BUFS (TFIELDW*BUFFERLINES)
  1143.  
  1144. extern char buffer[64][BUFS];
  1145. extern bool draw;
  1146. extern int bufpos[64];
  1147. extern int userpos[64];
  1148. extern bool newmsg[64];
  1149. extern char lastnick[64][100];
  1150. void meclear( );
  1151.  
  1152. void pushstring( const char* str );
  1153. void pushstringnick( const char* str, const char* nick );
  1154. void drawbuffer( );
  1155. void updaterooms();
  1156. void updateusers();
  1157.  
  1158. /////////////////////////// terminaldisplay.cpp \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  1159. #include "terminaldisplay.h"
  1160.  
  1161. char buffer[64][BUFS];
  1162. char lastnick[64][100]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  1163. int bufpos[64]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  1164. int userpos[64]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  1165. bool newmsg[64]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  1166. bool draw = false;
  1167. int scroll = 0;
  1168.  
  1169.  
  1170. void meclear( )
  1171. {
  1172.     //savexy();
  1173.     int i;
  1174.     //for( i = 0; i < 70*38; buffer[curroom][i++] = ' ' );
  1175.     gotoxy( 0, 0 );
  1176.     for( i = 0; i < 80*SCREENHIG; i ++ )
  1177.     {
  1178.         fputc( ' ', stdout );
  1179.     }
  1180.     for( i = 1; i < SCREENHIG -2; i++ )
  1181.     {
  1182.         gotoxy( TFIELDW+1, i );
  1183.         fputc( 0xBA, stdout );
  1184.     }
  1185.     gotoxy( 0, 1 );
  1186.     for( int i = 0; i < 80; i ++ ) fputc( 0xCD, stdout );
  1187.     gotoxy( TFIELDW+1, 1 );
  1188.     fputc( 0xCB, stdout );
  1189.  
  1190.     gotoxy( 0, SCREENHIG - 2);
  1191.     for( int i = 0; i < 80; i ++ ) fputc( 0xCD, stdout );
  1192.     gotoxy( TFIELDW+1, SCREENHIG-2 );
  1193.     fputc( 0xCA, stdout );
  1194.  
  1195.     updaterooms();
  1196.     gotoxy(0, SCREENHIG-1);
  1197.     //returntosaved();
  1198. }
  1199.  
  1200. void updateusers()
  1201. {
  1202.     savexy();
  1203.     bool listsorted = (users[curroom].size() <= 1);
  1204.     int seeker1 = 0;
  1205.     int seeker2 = 1;
  1206.     char alpha[] = "~&@%+ ";
  1207.     char buf1[1000];
  1208.     char buf2[1000];
  1209.     char buf3[1000];
  1210.  
  1211.     /*while( !listsorted )
  1212.     {
  1213.         bool swap = false;
  1214.         strcpy( buf1, users[curroom][seeker1] );
  1215.         strcpy( buf2, users[curroom][seeker2] );
  1216.         int pos1=1000, pos2=1000;
  1217.         for( int i = 0; alpha[i] != 0; i ++ ){if(buf1[0] == alpha[i])pos1 = i; if(buf2[0] == alpha[i]) pos2 = i;}
  1218.         if( pos1 > pos2 )
  1219.         {
  1220.             strcpy( buf3, users[curroom][seeker1] );
  1221.             strcpy( users[curroom][seeker1], users[curroom][seeker2] );
  1222.             strcpy( users[curroom][seeker2], buf3 );
  1223.             seeker1 = 0; seeker2 = 1;
  1224.             continue;
  1225.         }
  1226.         if( pos1 == pos2 ){
  1227.         strupr( buf1 );
  1228.         strupr( buf2 );
  1229.         int i;
  1230.         for( i = 1; buf1[i] != 0 && buf2[i] != 0; i ++ )
  1231.         {
  1232.             if( buf1[i] < buf2[i] ) break;
  1233.             if( buf1[i] > buf2[i] )
  1234.             {
  1235.                 strcpy( buf3, users[curroom][seeker1] );
  1236.                 strcpy( users[curroom][seeker1], users[curroom][seeker2] );
  1237.                 strcpy( users[curroom][seeker2], buf3 );
  1238.                 swap = true;
  1239.                 //seeker1 = 0; seeker2 = 1;
  1240.                 break;
  1241.             }
  1242.         }
  1243.         }
  1244.         //if( swap ) continue;
  1245.         /*if( buf2[i] == 0 && buf1[i] != 0 && !swap )
  1246.         {
  1247.             strcpy( buf3, users[curroom][seeker1] );
  1248.             strcpy( users[curroom][seeker1], users[curroom][seeker2] );
  1249.             strcpy( users[curroom][seeker2], buf3 );
  1250.             seeker1 = 0; seeker2 = 1;
  1251.         }*//*
  1252.         seeker2++;
  1253.         if( seeker2 >= users[curroom].size() )
  1254.         {
  1255.             seeker1++;
  1256.             seeker2 = seeker1+1;
  1257.         }
  1258.         if( seeker2 >= users[curroom].size() )
  1259.         {
  1260.             listsorted = true;
  1261.         }
  1262.     }*/
  1263.     for( int i = 0; i < TFIELDH; i++ )
  1264.     {
  1265.         gotoxy( TFIELDW+2, i+2 );
  1266.         printf( "                  " );
  1267.     }
  1268.     if( userpos[curroom] < 0 ) userpos[curroom] = 0;
  1269.     for( int i = 0; i < (users[curroom].size()-userpos[curroom]) && i < TFIELDH; i++ )
  1270.     {
  1271.         gotoxy( TFIELDW+2, i+2 );
  1272.         printf( "                  " );
  1273.         gotoxy( TFIELDW+2, i+2 );
  1274.         //printf( "i=%i vs %i", i , (int)(users[curroom].size())-userpos[curroom] );
  1275.         if( i >= (int)(users[curroom].size())-userpos[curroom] ) break;
  1276.         if( users[curroom][i+userpos[curroom]] == 0 ) continue;
  1277.         printf( "%s", users[curroom][i+userpos[curroom]] );
  1278.     }
  1279.     returntosaved();
  1280. }
  1281.  
  1282. void pushstring( const char* str )
  1283. {
  1284.     //while( draw ){}
  1285.     //draw = true;
  1286.     int len = strlen( str );
  1287.     int offset = ((len/TFIELDW+1)*TFIELDW<BUFS?(len/TFIELDW+1)*TFIELDW:BUFS );
  1288.     int i, k;
  1289.     for( i = 0; i < BUFS-offset; buffer[curroom][i]=buffer[curroom][(i++)+offset] );
  1290.     for( k = 0; str[k] != 0 && i < BUFS; ){ if(str[k++]>=0x20) buffer[curroom][i++] = str[k-1]; }
  1291.     for( ; i < BUFS; buffer[curroom][i++] = ' ' );
  1292.     //printf( "\n\nSTRING: %s\n\n ", buffer[curroom] );
  1293.     //draw = false;
  1294. }
  1295.  
  1296. void pushstringnick( const char* str, const char* nickky )
  1297. {
  1298.     if( str[0] == 0 ) return;
  1299.     if( lastnick[curroom] == 0 || strcmp( nickky, lastnick[curroom] ) != 0 )
  1300.     {
  1301.         char b[200];
  1302.         //printf( nick );
  1303.         sprintf( b, "%s - ", nickky );
  1304.         //printf( nickky );
  1305.         pushstring( b );
  1306.         strcpy( lastnick[curroom], nickky );
  1307.     }
  1308.     char a[2000];
  1309.     sprintf( a, "  %s", str );
  1310.     pushstring( a );
  1311. }
  1312.  
  1313.  
  1314. void drawbuffer( )
  1315. {
  1316.     //while( draw ){}
  1317.     //draw = true;
  1318.     savexy();
  1319.  
  1320.     //while( draw ){}
  1321.     //draw = true;
  1322.     int i, j, t = BUFS-TFIELDW*(TFIELDH+bufpos[curroom]);
  1323.     for( j = 0; j < TFIELDH; j ++)
  1324.     {
  1325.         gotoxy( 0, 2+j );
  1326.         char a = buffer[curroom][t+TFIELDW*(j+1)];
  1327.         buffer[curroom][t+TFIELDW*(j+1)] = 0;
  1328.         printf( "%s", buffer[curroom]+(t+TFIELDW*j) );
  1329.         buffer[curroom][t+TFIELDW*(j+1)] = a;
  1330.         /*for( i = 0; i < TFIELDW; i ++ )
  1331.         {
  1332.             if( buffer[curroom][t] >= 0x20 )
  1333.             fputc( buffer[curroom][t], stdout );
  1334.             t++;
  1335.         }*/
  1336.     }
  1337.     returntosaved();
  1338.     //draw = false;
  1339.     //draw = false;
  1340.  
  1341. }
  1342.  
  1343. void updaterooms()
  1344. {
  1345.  
  1346.     /*int poses[64];
  1347.     poses[0] = 0;
  1348.     printf( " Server|");
  1349.     int pos = 7;
  1350.     for( int i = 1; i < roomtotal; i ++ )
  1351.     {
  1352.         poses[i] = pos+1;
  1353.         pos += strlen( rooms[i] ) + 2;
  1354.     }*/
  1355.     char a[4000] = {0};
  1356.     char* b = a;
  1357.     b+=sprintf( b, 0==curroom?">":" ");
  1358.     b+=sprintf( b, "%s", "Server" );
  1359.     b+=sprintf( b, 0==curroom?"< - ":"  - ");
  1360.     for( int i = 1; i <= roomtotal; i ++ )
  1361.     {
  1362.         b+=sprintf( b, i==curroom?">":(newmsg[i]?"~":" "));
  1363.         b+=sprintf( b, "%s", rooms[i]);
  1364.         b+=sprintf( b, i==curroom?"< - ":(newmsg[i]?"~ - ":"  - "));
  1365.         if( i == curroom && (int)(b - a)-scroll > 80 )
  1366.         {
  1367.             scroll = (int)(b - a)-74;
  1368.         }
  1369.         if( i == curroom && (int)(b - a)-scroll < 0 )
  1370.         {
  1371.             scroll = (int)(b - a)-74 > 0?(int)(b - a)-74:0;
  1372.         }
  1373.  
  1374.     }
  1375.     b+=sprintf( b, "                                                                                ");
  1376.     savexy();
  1377.     gotoxy(0, 0);
  1378.     a[scroll + 80] = 0;
  1379.     printf( "%s", a+scroll );
  1380.     returntosaved();
  1381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement