Advertisement
Guest User

Untitled

a guest
May 12th, 2017
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.48 KB | None | 0 0
  1. /*
  2.  
  3.     cpIRC - C++ class based IRC protocol wrapper
  4.  
  5.     Copyright (C) 2003 Iain Sheppard
  6.  
  7.  
  8.  
  9.     This library is free software; you can redistribute it and/or
  10.  
  11.     modify it under the terms of the GNU Lesser General Public
  12.  
  13.     License as published by the Free Software Foundation; either
  14.  
  15.     version 2.1 of the License, or (at your option) any later version.
  16.  
  17.  
  18.  
  19.     This library is distributed in the hope that it will be useful,
  20.  
  21.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.  
  23.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24.  
  25.     Lesser General Public License for more details.
  26.  
  27.  
  28.  
  29.     You should have received a copy of the GNU Lesser General Public
  30.  
  31.     License along with this library; if not, write to the Free Software
  32.  
  33.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  34.  
  35.  
  36.  
  37.     Contacting the author:
  38.  
  39.     ~~~~~~~~~~~~~~~~~~~~~~
  40.  
  41.  
  42.  
  43.     email:  iainsheppard@yahoo.co.uk
  44.  
  45.     IRC:    #magpie @ irc.quakenet.org
  46.  
  47. */
  48.  
  49.  
  50.  
  51. #include "IRC.h"
  52.  
  53. #ifdef WIN32
  54.  
  55. #include <windows.h>
  56.  
  57. #else
  58.  
  59. #include <stdlib.h>
  60.  
  61. #include <unistd.h>
  62.  
  63. #include <errno.h>
  64.  
  65. #include <string.h>
  66.  
  67. #include <netdb.h>
  68.  
  69. #include <sys/types.h>
  70.  
  71. #include <netinet/in.h>
  72.  
  73. #include <sys/socket.h>
  74.  
  75. #define closesocket(s) close(s)
  76.  
  77. #define SOCKET_ERROR -1
  78.  
  79. #define INVALID_SOCKET -1
  80.  
  81. #endif
  82.  
  83.  
  84.  
  85. IRC::IRC()
  86.  
  87. {
  88.  
  89.     hooks=0;
  90.  
  91.     chan_users=0;
  92.  
  93.     connected=false;
  94.  
  95.     sentnick=false;
  96.  
  97.     sentpass=false;
  98.  
  99.     sentuser=false;
  100.  
  101.     cur_nick=0;
  102.  
  103. }
  104.  
  105.  
  106.  
  107. IRC::~IRC()
  108.  
  109. {
  110.  
  111.     if (hooks)
  112.  
  113.         delete_irc_command_hook(hooks);
  114.  
  115. }
  116.  
  117.  
  118.  
  119. void IRC::insert_irc_command_hook(irc_command_hook* hook, char* cmd_name, int (*function_ptr)(char*, irc_reply_data*, void*))
  120.  
  121. {
  122.  
  123.     if (hook->function)
  124.  
  125.     {
  126.  
  127.         if (!hook->next)
  128.  
  129.         {
  130.  
  131.             hook->next=new irc_command_hook;
  132.  
  133.             hook->next->function=0;
  134.  
  135.             hook->next->irc_command=0;
  136.  
  137.             hook->next->next=0;
  138.  
  139.         }
  140.  
  141.         insert_irc_command_hook(hook->next, cmd_name, function_ptr);
  142.  
  143.     }
  144.  
  145.     else
  146.  
  147.     {
  148.  
  149.         hook->function=function_ptr;
  150.  
  151.         hook->irc_command=new char[strlen(cmd_name)+1];
  152.  
  153.         strcpy(hook->irc_command, cmd_name);
  154.  
  155.     }
  156.  
  157. }
  158.  
  159.  
  160.  
  161. void IRC::hook_irc_command(char* cmd_name, int (*function_ptr)(char*, irc_reply_data*, void*))
  162.  
  163. {
  164.  
  165.     if (!hooks)
  166.  
  167.     {
  168.  
  169.         hooks=new irc_command_hook;
  170.  
  171.         hooks->function=0;
  172.  
  173.         hooks->irc_command=0;
  174.  
  175.         hooks->next=0;
  176.  
  177.         insert_irc_command_hook(hooks, cmd_name, function_ptr);
  178.  
  179.     }
  180.  
  181.     else
  182.  
  183.     {
  184.  
  185.         insert_irc_command_hook(hooks, cmd_name, function_ptr);
  186.  
  187.     }
  188.  
  189. }
  190.  
  191.  
  192.  
  193. void IRC::delete_irc_command_hook(irc_command_hook* cmd_hook)
  194.  
  195. {
  196.  
  197.     if (cmd_hook->next)
  198.  
  199.         delete_irc_command_hook(cmd_hook->next);
  200.  
  201.     if (cmd_hook->irc_command)
  202.  
  203.         delete cmd_hook->irc_command;
  204.  
  205.     delete cmd_hook;
  206.  
  207. }
  208.  
  209.  
  210.  
  211. int IRC::start(char* server, int port, char* nick, char* user, char* name, char* pass)
  212.  
  213. {
  214.  
  215.     #ifdef WIN32
  216.  
  217.     HOSTENT* resolv;
  218.  
  219.     #else
  220.  
  221.     hostent* resolv;
  222.  
  223.     #endif
  224.  
  225.     sockaddr_in rem;
  226.  
  227.  
  228.  
  229.     if (connected)
  230.  
  231.         return 1;
  232.  
  233.  
  234.  
  235.     irc_socket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  236.  
  237.     if (irc_socket==INVALID_SOCKET)
  238.  
  239.     {
  240.  
  241.         return 1;
  242.  
  243.     }
  244.  
  245.     resolv=gethostbyname(server);
  246.  
  247.     if (!resolv)
  248.  
  249.     {
  250.  
  251.         closesocket(irc_socket);
  252.  
  253.         return 1;
  254.  
  255.     }
  256.  
  257.     memcpy(&rem.sin_addr, resolv->h_addr, 4);
  258.  
  259.     rem.sin_family=AF_INET;
  260.  
  261.     rem.sin_port=htons(port);
  262.  
  263.  
  264.  
  265.     if (connect(irc_socket, (const sockaddr*)&rem, sizeof(rem))==SOCKET_ERROR)
  266.  
  267.     {
  268.  
  269.         #ifdef WIN32
  270.  
  271.         printf("Failed to connect: %d\n", WSAGetLastError());
  272.  
  273.         #endif
  274.  
  275.         closesocket(irc_socket);
  276.  
  277.         return 1;
  278.  
  279.     }
  280.  
  281.  
  282.  
  283.     dataout=fdopen(irc_socket, "w");
  284.  
  285.     //datain=fdopen(irc_socket, "r");
  286.  
  287.    
  288.  
  289.     if (!dataout /*|| !datain*/)
  290.  
  291.     {
  292.  
  293.         printf("Failed to open streams!\n");
  294.  
  295.         closesocket(irc_socket);
  296.  
  297.         return 1;
  298.  
  299.     }
  300.  
  301.    
  302.  
  303.     connected=true;
  304.  
  305.    
  306.  
  307.     cur_nick=new char[strlen(nick)+1];
  308.  
  309.     strcpy(cur_nick, nick);
  310.  
  311.  
  312.  
  313.     fprintf(dataout, "PASS %s\r\n", pass);
  314.  
  315.     fprintf(dataout, "NICK %s\r\n", nick);
  316.  
  317.     fprintf(dataout, "USER %s * 0 :%s\r\n", user, name);
  318.  
  319.     fflush(dataout);       
  320.  
  321.  
  322.  
  323.     return 0;
  324.  
  325. }
  326.  
  327.  
  328.  
  329. void IRC::disconnect()
  330.  
  331. {
  332.  
  333.     if (connected)
  334.  
  335.     {
  336.  
  337.         fclose(dataout);
  338.  
  339.         printf("Disconnected from server.\n");
  340.  
  341.         connected=false;
  342.  
  343.         quit("Leaving");
  344.  
  345.         #ifdef WIN32
  346.  
  347.         shutdown(irc_socket, 2);
  348.  
  349.         #endif
  350.  
  351.         closesocket(irc_socket);
  352.  
  353.     }
  354.  
  355. }
  356.  
  357.  
  358.  
  359. int IRC::quit(char* quit_message)
  360.  
  361. {
  362.  
  363.     if (connected)
  364.  
  365.     {
  366.  
  367.         if (quit_message)
  368.  
  369.             fprintf(dataout, "QUIT %s\r\n", quit_message);
  370.  
  371.         else
  372.  
  373.             fprintf(dataout, "QUIT\r\n");
  374.  
  375.         if (fflush(dataout))
  376.  
  377.             return 1;
  378.  
  379.     }
  380.  
  381.     return 0;
  382.  
  383. }
  384.  
  385.  
  386.  
  387. int IRC::message_loop()
  388.  
  389. {
  390.  
  391.     char buffer[1024];
  392.  
  393.     int ret_len;
  394.  
  395.  
  396.  
  397.     if (!connected)
  398.  
  399.     {
  400.  
  401.         printf("Not connected!\n");
  402.  
  403.         return 1;
  404.  
  405.     }
  406.  
  407.  
  408.  
  409.     while (1)
  410.  
  411.     {
  412.  
  413.         ret_len=recv(irc_socket, buffer, 1023, 0);
  414.  
  415.         if (ret_len==SOCKET_ERROR || !ret_len)
  416.  
  417.         {
  418.  
  419.             return 1;
  420.  
  421.         }
  422.  
  423.         buffer[ret_len]='\0';
  424.  
  425.         split_to_replies(buffer);
  426.  
  427.     }
  428.  
  429.  
  430.  
  431.     return 0;
  432.  
  433. }
  434.  
  435.  
  436.  
  437. void IRC::split_to_replies(char* data)
  438.  
  439. {
  440.  
  441.     char* p;
  442.  
  443.  
  444.  
  445.     while (p=strstr(data, "\r\n"))
  446.  
  447.     {
  448.  
  449.         *p='\0';
  450.  
  451.         parse_irc_reply(data);
  452.  
  453.         data=p+2;
  454.  
  455.     }
  456.  
  457. }
  458.  
  459.  
  460.  
  461. int IRC::is_op(char* channel, char* nick)
  462.  
  463. {
  464.  
  465.     channel_user* cup;
  466.  
  467.  
  468.  
  469.     cup=chan_users;
  470.  
  471.    
  472.  
  473.     while (cup)
  474.  
  475.     {
  476.  
  477.         if (!strcmp(cup->channel, channel) && !strcmp(cup->nick, nick))
  478.  
  479.         {
  480.  
  481.             return cup->flags&IRC_USER_OP;
  482.  
  483.         }
  484.  
  485.         cup=cup->next;
  486.  
  487.     }
  488.  
  489.  
  490.  
  491.     return 0;
  492.  
  493. }
  494.  
  495.  
  496.  
  497. int IRC::is_voice(char* channel, char* nick)
  498.  
  499. {
  500.  
  501.     channel_user* cup;
  502.  
  503.  
  504.  
  505.     cup=chan_users;
  506.  
  507.    
  508.  
  509.     while (cup)
  510.  
  511.     {
  512.  
  513.         if (!strcmp(cup->channel, channel) && !strcmp(cup->nick, nick))
  514.  
  515.         {
  516.  
  517.             return cup->flags&IRC_USER_VOICE;
  518.  
  519.         }
  520.  
  521.         cup=cup->next;
  522.  
  523.     }
  524.  
  525.  
  526.  
  527.     return 0;
  528.  
  529. }
  530.  
  531.  
  532.  
  533. void IRC::parse_irc_reply(char* data)
  534.  
  535. {
  536.  
  537.     char* hostd;
  538.  
  539.     char* cmd;
  540.  
  541.     char* params;
  542.  
  543.     char buffer[514];
  544.  
  545.     irc_reply_data hostd_tmp;
  546.  
  547.     channel_user* cup;
  548.  
  549.     char* p;
  550.  
  551.     char* chan_temp;
  552.  
  553.  
  554.  
  555.     hostd_tmp.target=0;
  556.  
  557.  
  558.  
  559.     printf("%s\n", data);
  560.  
  561.  
  562.  
  563.     if (data[0]==':')
  564.  
  565.     {
  566.  
  567.         hostd=&data[1];
  568.  
  569.         cmd=strchr(hostd, ' ');
  570.  
  571.         if (!cmd)
  572.  
  573.             return;
  574.  
  575.         *cmd='\0';
  576.  
  577.         cmd++;
  578.  
  579.         params=strchr(cmd, ' ');
  580.  
  581.         if (params)
  582.  
  583.         {
  584.  
  585.             *params='\0';
  586.  
  587.             params++;
  588.  
  589.         }
  590.  
  591.         hostd_tmp.nick=hostd;
  592.  
  593.         hostd_tmp.ident=strchr(hostd, '!');
  594.  
  595.         if (hostd_tmp.ident)
  596.  
  597.         {
  598.  
  599.             *hostd_tmp.ident='\0';
  600.  
  601.             hostd_tmp.ident++;
  602.  
  603.             hostd_tmp.host=strchr(hostd_tmp.ident, '@');
  604.  
  605.             if (hostd_tmp.host)
  606.  
  607.             {
  608.  
  609.                 *hostd_tmp.host='\0';
  610.  
  611.                 hostd_tmp.host++;
  612.  
  613.             }
  614.  
  615.         }
  616.  
  617.  
  618.  
  619.         if (!strcmp(cmd, "JOIN"))
  620.  
  621.         {
  622.  
  623.             cup=chan_users;
  624.  
  625.             if (cup)
  626.  
  627.             {
  628.  
  629.                 while (cup->nick)
  630.  
  631.                 {
  632.  
  633.                     if (!cup->next)
  634.  
  635.                     {
  636.  
  637.                         cup->next=new channel_user;
  638.  
  639.                         cup->next->channel=0;
  640.  
  641.                         cup->next->flags=0;
  642.  
  643.                         cup->next->next=0;
  644.  
  645.                         cup->next->nick=0;
  646.  
  647.                     }
  648.  
  649.                     cup=cup->next;
  650.  
  651.                 }
  652.  
  653.                 cup->channel=new char[strlen(params)+1];
  654.  
  655.                 strcpy(cup->channel, params);
  656.  
  657.                 cup->nick=new char[strlen(hostd_tmp.nick)+1];
  658.  
  659.                 strcpy(cup->nick, hostd_tmp.nick);
  660.  
  661.             }
  662.  
  663.         }
  664.  
  665.         else if (!strcmp(cmd, "PART"))
  666.  
  667.         {
  668.  
  669.             channel_user* d;
  670.  
  671.             channel_user* prev;
  672.  
  673.  
  674.  
  675.             d=0;
  676.  
  677.             prev=0;
  678.  
  679.             cup=chan_users;
  680.  
  681.             while (cup)
  682.  
  683.             {
  684.  
  685.                 if (!strcmp(cup->channel, params) && !strcmp(cup->nick, hostd_tmp.nick))
  686.  
  687.                 {
  688.  
  689.                     d=cup;
  690.  
  691.                     break;
  692.  
  693.                 }
  694.  
  695.                 else
  696.  
  697.                 {
  698.  
  699.                     prev=cup;
  700.  
  701.                 }
  702.  
  703.                 cup=cup->next;
  704.  
  705.             }
  706.  
  707.             if (d)
  708.  
  709.             {
  710.  
  711.                 if (d==chan_users)
  712.  
  713.                 {
  714.  
  715.                     chan_users=d->next;
  716.  
  717.                     if (d->channel)
  718.  
  719.                         delete [] d->channel;
  720.  
  721.                     if (d->nick)
  722.  
  723.                         delete [] d->nick;
  724.  
  725.                     delete d;
  726.  
  727.                 }
  728.  
  729.                 else
  730.  
  731.                 {
  732.  
  733.                     if (prev)
  734.  
  735.                     {
  736.  
  737.                         prev->next=d->next;
  738.  
  739.                     }
  740.  
  741.                     chan_users=d->next;
  742.  
  743.                     if (d->channel)
  744.  
  745.                         delete [] d->channel;
  746.  
  747.                     if (d->nick)
  748.  
  749.                         delete [] d->nick;
  750.  
  751.                     delete d;
  752.  
  753.                 }
  754.  
  755.             }
  756.  
  757.         }
  758.  
  759.         else if (!strcmp(cmd, "QUIT"))
  760.  
  761.         {
  762.  
  763.             channel_user* d;
  764.  
  765.             channel_user* prev;
  766.  
  767.  
  768.  
  769.             d=0;
  770.  
  771.             prev=0;
  772.  
  773.             cup=chan_users;
  774.  
  775.             while (cup)
  776.  
  777.             {
  778.  
  779.                 if (!strcmp(cup->nick, hostd_tmp.nick))
  780.  
  781.                 {
  782.  
  783.                     d=cup;
  784.  
  785.                     if (d==chan_users)
  786.  
  787.                     {
  788.  
  789.                         chan_users=d->next;
  790.  
  791.                         if (d->channel)
  792.  
  793.                             delete [] d->channel;
  794.  
  795.                         if (d->nick)
  796.  
  797.                             delete [] d->nick;
  798.  
  799.                         delete d;
  800.  
  801.                     }
  802.  
  803.                     else
  804.  
  805.                     {
  806.  
  807.                         if (prev)
  808.  
  809.                         {
  810.  
  811.                             prev->next=d->next;
  812.  
  813.                         }
  814.  
  815.                         if (d->channel)
  816.  
  817.                             delete [] d->channel;
  818.  
  819.                         if (d->nick)
  820.  
  821.                             delete [] d->nick;
  822.  
  823.                         delete d;
  824.  
  825.                     }
  826.  
  827.                     break;
  828.  
  829.                 }
  830.  
  831.                 else
  832.  
  833.                 {
  834.  
  835.                     prev=cup;
  836.  
  837.                 }
  838.  
  839.                 cup=cup->next;
  840.  
  841.             }
  842.  
  843.         }
  844.  
  845.         else if (!strcmp(cmd, "MODE"))
  846.  
  847.         {
  848.  
  849.             char* chan;
  850.  
  851.             char* changevars;
  852.  
  853.             channel_user* cup;
  854.  
  855.             channel_user* d;
  856.  
  857.             char* tmp;
  858.  
  859.             int i;
  860.  
  861.             bool plus;
  862.  
  863.  
  864.  
  865.             chan=params;
  866.  
  867.             params=strchr(chan, ' ');
  868.  
  869.             *params='\0';
  870.  
  871.             params++;
  872.  
  873.             changevars=params;
  874.  
  875.             params=strchr(changevars, ' ');
  876.  
  877.             if (!params)
  878.  
  879.             {
  880.  
  881.                 return;
  882.  
  883.             }
  884.  
  885.             if (chan[0]!='#')
  886.  
  887.             {
  888.  
  889.                 return;
  890.  
  891.             }
  892.  
  893.             *params='\0';
  894.  
  895.             params++;
  896.  
  897.        
  898.  
  899.             plus=false;
  900.  
  901.             for (i=0; i<(signed)strlen(changevars); i++)
  902.  
  903.             {
  904.  
  905.                 switch (changevars[i])
  906.  
  907.                 {
  908.  
  909.                 case '+':
  910.  
  911.                     plus=true;
  912.  
  913.                     break;
  914.  
  915.                 case '-':
  916.  
  917.                     plus=false;
  918.  
  919.                     break;
  920.  
  921.                 case 'o':
  922.  
  923.                     tmp=strchr(params, ' ');
  924.  
  925.                     if (tmp)
  926.  
  927.                     {
  928.  
  929.                         *tmp='\0';
  930.  
  931.                         tmp++;
  932.  
  933.                     }
  934.  
  935.                     tmp=params;
  936.  
  937.                     if (plus)
  938.  
  939.                     {
  940.  
  941.                         // user has been opped (chan, params)
  942.  
  943.                         cup=chan_users;
  944.  
  945.                         d=0;
  946.  
  947.                         while (cup)
  948.  
  949.                         {
  950.  
  951.                             if (cup->next && cup->channel)
  952.  
  953.                             {
  954.  
  955.                                 if (!strcmp(cup->channel, chan) && !strcmp(cup->nick, tmp))
  956.  
  957.                                 {
  958.  
  959.                                     d=cup;
  960.  
  961.                                     break;
  962.  
  963.                                 }
  964.  
  965.                             }
  966.  
  967.                             cup=cup->next;
  968.  
  969.                         }
  970.  
  971.                         if (d)
  972.  
  973.                         {
  974.  
  975.                             d->flags=d->flags|IRC_USER_OP;
  976.  
  977.                         }
  978.  
  979.                     }
  980.  
  981.                     else
  982.  
  983.                     {
  984.  
  985.                         // user has been deopped (chan, params)
  986.  
  987.                         cup=chan_users;
  988.  
  989.                         d=0;
  990.  
  991.                         while (cup)
  992.  
  993.                         {
  994.  
  995.                             if (!strcmp(cup->channel, chan) && !strcmp(cup->nick, tmp))
  996.  
  997.                             {
  998.  
  999.                                 d=cup;
  1000.  
  1001.                                 break;
  1002.  
  1003.                             }
  1004.  
  1005.                             cup=cup->next;
  1006.  
  1007.                         }
  1008.  
  1009.                         if (d)
  1010.  
  1011.                         {
  1012.  
  1013.                             d->flags=d->flags^IRC_USER_OP;
  1014.  
  1015.                         }
  1016.  
  1017.                     }
  1018.  
  1019.                     params=tmp;
  1020.  
  1021.                     break;
  1022.  
  1023.                 case 'v':
  1024.  
  1025.                     tmp=strchr(params, ' ');
  1026.  
  1027.                     if (tmp)
  1028.  
  1029.                     {
  1030.  
  1031.                         *tmp='\0';
  1032.  
  1033.                         tmp++;
  1034.  
  1035.                     }
  1036.  
  1037.                     if (plus)
  1038.  
  1039.                     {
  1040.  
  1041.                         // user has been voiced
  1042.  
  1043.                         cup=chan_users;
  1044.  
  1045.                         d=0;
  1046.  
  1047.                         while (cup)
  1048.  
  1049.                         {
  1050.  
  1051.                             if (!strcmp(cup->channel, params) && !strcmp(cup->nick, hostd_tmp.nick))
  1052.  
  1053.                             {
  1054.  
  1055.                                 d=cup;
  1056.  
  1057.                                 break;
  1058.  
  1059.                             }
  1060.  
  1061.                             cup=cup->next;
  1062.  
  1063.                         }
  1064.  
  1065.                         if (d)
  1066.  
  1067.                         {
  1068.  
  1069.                             d->flags=d->flags|IRC_USER_VOICE;
  1070.  
  1071.                         }
  1072.  
  1073.                     }
  1074.  
  1075.                     else
  1076.  
  1077.                     {
  1078.  
  1079.                         // user has been devoiced
  1080.  
  1081.                         cup=chan_users;
  1082.  
  1083.                         d=0;
  1084.  
  1085.                         while (cup)
  1086.  
  1087.                         {
  1088.  
  1089.                             if (!strcmp(cup->channel, params) && !strcmp(cup->nick, hostd_tmp.nick))
  1090.  
  1091.                             {
  1092.  
  1093.                                 d=cup;
  1094.  
  1095.                                 break;
  1096.  
  1097.                             }
  1098.  
  1099.                             cup=cup->next;
  1100.  
  1101.                         }
  1102.  
  1103.                         if (d)
  1104.  
  1105.                         {
  1106.  
  1107.                             d->flags=d->flags^IRC_USER_VOICE;
  1108.  
  1109.                         }
  1110.  
  1111.                     }
  1112.  
  1113.                     params=tmp;
  1114.  
  1115.                     break;
  1116.  
  1117.                 default:
  1118.  
  1119.                     return;
  1120.  
  1121.                     break;
  1122.  
  1123.                 }
  1124.  
  1125.                 // ------------ END OF MODE ---------------
  1126.  
  1127.             }
  1128.  
  1129.         }
  1130.  
  1131.         else if (!strcmp(cmd, "353"))
  1132.  
  1133.         {
  1134.  
  1135.             // receiving channel names list
  1136.  
  1137.             if (!chan_users)
  1138.  
  1139.             {
  1140.  
  1141.                 chan_users=new channel_user;
  1142.  
  1143.                 chan_users->next=0;
  1144.  
  1145.                 chan_users->nick=0;
  1146.  
  1147.                 chan_users->flags=0;
  1148.  
  1149.                 chan_users->channel=0;
  1150.  
  1151.             }
  1152.  
  1153.             cup=chan_users;
  1154.  
  1155.             chan_temp=strchr(params, '#');
  1156.  
  1157.             if (chan_temp)
  1158.  
  1159.             {
  1160.  
  1161.                 //chan_temp+=3;
  1162.  
  1163.                 p=strstr(chan_temp, " :");
  1164.  
  1165.                 if (p)
  1166.  
  1167.                 {
  1168.  
  1169.                     *p='\0';
  1170.  
  1171.                     p+=2;
  1172.  
  1173.                     while (strchr(p, ' '))
  1174.  
  1175.                     {
  1176.  
  1177.                         char* tmp;
  1178.  
  1179.  
  1180.  
  1181.                         tmp=strchr(p, ' ');
  1182.  
  1183.                         *tmp='\0';
  1184.  
  1185.                         tmp++;
  1186.  
  1187.                         while (cup->nick)
  1188.  
  1189.                         {
  1190.  
  1191.                             if (!cup->next)
  1192.  
  1193.                             {
  1194.  
  1195.                                 cup->next=new channel_user;
  1196.  
  1197.                                 cup->next->channel=0;
  1198.  
  1199.                                 cup->next->flags=0;
  1200.  
  1201.                                 cup->next->next=0;
  1202.  
  1203.                                 cup->next->nick=0;
  1204.  
  1205.                             }
  1206.  
  1207.                             cup=cup->next;
  1208.  
  1209.                         }
  1210.  
  1211.                         if (p[0]=='@')
  1212.  
  1213.                         {
  1214.  
  1215.                             cup->flags=cup->flags|IRC_USER_OP;
  1216.  
  1217.                             p++;
  1218.  
  1219.                         }
  1220.  
  1221.                         else if (p[0]=='+')
  1222.  
  1223.                         {
  1224.  
  1225.                             cup->flags=cup->flags|IRC_USER_VOICE;
  1226.  
  1227.                             p++;
  1228.  
  1229.                         }
  1230.  
  1231.                         cup->nick=new char[strlen(p)+1];
  1232.  
  1233.                         strcpy(cup->nick, p);
  1234.  
  1235.                         cup->channel=new char[strlen(chan_temp)+1];
  1236.  
  1237.                         strcpy(cup->channel, chan_temp);
  1238.  
  1239.                         p=tmp;
  1240.  
  1241.                     }
  1242.  
  1243.                     while (cup->nick)
  1244.  
  1245.                     {
  1246.  
  1247.                         if (!cup->next)
  1248.  
  1249.                         {
  1250.  
  1251.                             cup->next=new channel_user;
  1252.  
  1253.                             cup->next->channel=0;
  1254.  
  1255.                             cup->next->flags=0;
  1256.  
  1257.                             cup->next->next=0;
  1258.  
  1259.                             cup->next->nick=0;
  1260.  
  1261.                         }
  1262.  
  1263.                         cup=cup->next;
  1264.  
  1265.                     }
  1266.  
  1267.                     if (p[0]=='@')
  1268.  
  1269.                     {
  1270.  
  1271.                         cup->flags=cup->flags|IRC_USER_OP;
  1272.  
  1273.                         p++;
  1274.  
  1275.                     }
  1276.  
  1277.                     else if (p[0]=='+')
  1278.  
  1279.                     {
  1280.  
  1281.                         cup->flags=cup->flags|IRC_USER_VOICE;
  1282.  
  1283.                         p++;
  1284.  
  1285.                     }
  1286.  
  1287.                     cup->nick=new char[strlen(p)+1];
  1288.  
  1289.                     strcpy(cup->nick, p);
  1290.  
  1291.                     cup->channel=new char[strlen(chan_temp)+1];
  1292.  
  1293.                     strcpy(cup->channel, chan_temp);
  1294.  
  1295.                 }
  1296.  
  1297.             }
  1298.  
  1299.         }
  1300.  
  1301.         else if (!strcmp(cmd, "NOTICE"))
  1302.  
  1303.         {
  1304.  
  1305.             hostd_tmp.target=params;
  1306.  
  1307.             params=strchr(hostd_tmp.target, ' ');
  1308.  
  1309.             if (params)
  1310.  
  1311.                 *params='\0';
  1312.  
  1313.             params++;
  1314.  
  1315.             #ifdef __IRC_DEBUG__
  1316.  
  1317.             printf("%s >-%s- %s\n", hostd_tmp.nick, hostd_tmp.target, &params[1]);
  1318.  
  1319.             #endif
  1320.  
  1321.         }
  1322.  
  1323.         else if (!strcmp(cmd, "PRIVMSG"))
  1324.  
  1325.         {
  1326.  
  1327.             hostd_tmp.target=params;
  1328.  
  1329.             params=strchr(hostd_tmp.target, ' ');
  1330.  
  1331.             if (!params)
  1332.  
  1333.                 return;
  1334.  
  1335.             *(params++)='\0';
  1336.  
  1337.             #ifdef __IRC_DEBUG__
  1338.  
  1339.             printf("%s: <%s> %s\n", hostd_tmp.target, hostd_tmp.nick, &params[1]);
  1340.  
  1341.             #endif
  1342.  
  1343.         }
  1344.  
  1345.         else if (!strcmp(cmd, "NICK"))
  1346.  
  1347.         {
  1348.  
  1349.             if (!strcmp(hostd_tmp.nick, cur_nick))
  1350.  
  1351.             {
  1352.  
  1353.                 delete [] cur_nick;
  1354.  
  1355.                 cur_nick=new char[strlen(params)+1];
  1356.  
  1357.                 strcpy(cur_nick, params);
  1358.  
  1359.             }
  1360.  
  1361.         }
  1362.  
  1363.         /* else if (!strcmp(cmd, ""))
  1364.  
  1365.         {
  1366.  
  1367.             #ifdef __IRC_DEBUG__
  1368.  
  1369.             #endif
  1370.  
  1371.         } */
  1372.  
  1373.         call_hook(cmd, params, &hostd_tmp);
  1374.  
  1375.     }
  1376.  
  1377.     else
  1378.  
  1379.     {
  1380.  
  1381.         cmd=data;
  1382.  
  1383.         data=strchr(cmd, ' ');
  1384.  
  1385.         if (!data)
  1386.  
  1387.             return;
  1388.  
  1389.         *data='\0';
  1390.  
  1391.         params=data+1;
  1392.  
  1393.  
  1394.  
  1395.         if (!strcmp(cmd, "PING"))
  1396.  
  1397.         {
  1398.  
  1399.             if (!params)
  1400.  
  1401.                 return;
  1402.  
  1403.             fprintf(dataout, "PONG %s\r\n", &params[1]);
  1404.  
  1405.             #ifdef __IRC_DEBUG__
  1406.  
  1407.             printf("Ping received, pong sent.\n");
  1408.  
  1409.             #endif
  1410.  
  1411.             fflush(dataout);
  1412.  
  1413.         }
  1414.  
  1415.         else
  1416.  
  1417.         {
  1418.  
  1419.             hostd_tmp.host=0;
  1420.  
  1421.             hostd_tmp.ident=0;
  1422.  
  1423.             hostd_tmp.nick=0;
  1424.  
  1425.             hostd_tmp.target=0;
  1426.  
  1427.             call_hook(cmd, params, &hostd_tmp);
  1428.  
  1429.         }
  1430.  
  1431.     }
  1432.  
  1433. }
  1434.  
  1435.  
  1436.  
  1437. void IRC::call_hook(char* irc_command, char* params, irc_reply_data* hostd)
  1438.  
  1439. {
  1440.  
  1441.     irc_command_hook* p;
  1442.  
  1443.  
  1444.  
  1445.     if (!hooks)
  1446.  
  1447.         return;
  1448.  
  1449.  
  1450.  
  1451.     p=hooks;
  1452.  
  1453.     while (p)
  1454.  
  1455.     {
  1456.  
  1457.         if (!strcmp(p->irc_command, irc_command))
  1458.  
  1459.         {
  1460.  
  1461.             (*(p->function))(params, hostd, this);
  1462.  
  1463.             p=0;
  1464.  
  1465.         }
  1466.  
  1467.         else
  1468.  
  1469.         {
  1470.  
  1471.             p=p->next;
  1472.  
  1473.         }
  1474.  
  1475.     }
  1476.  
  1477. }
  1478.  
  1479.  
  1480.  
  1481. int IRC::notice(char* target, char* message)
  1482.  
  1483. {
  1484.  
  1485.     if (!connected)
  1486.  
  1487.         return 1;
  1488.  
  1489.     fprintf(dataout, "NOTICE %s :%s\r\n", target, message);
  1490.  
  1491.     return fflush(dataout);
  1492.  
  1493. }
  1494.  
  1495.  
  1496.  
  1497. int IRC::notice(char* fmt, ...)
  1498.  
  1499. {
  1500.  
  1501.     va_list argp;
  1502.  
  1503.     char* target;
  1504.  
  1505.    
  1506.  
  1507.     if (!connected)
  1508.  
  1509.         return 1;
  1510.  
  1511.     va_start(argp, fmt);
  1512.  
  1513.     fprintf(dataout, "NOTICE %s :", fmt);
  1514.  
  1515.     vfprintf(dataout, va_arg(argp, char*), argp);
  1516.  
  1517.     va_end(argp);
  1518.  
  1519.     fprintf(dataout, "\r\n");
  1520.  
  1521.     return fflush(dataout);
  1522.  
  1523. }
  1524.  
  1525.  
  1526.  
  1527. int IRC::privmsg(char* target, char* message)
  1528.  
  1529. {
  1530.  
  1531.     if (!connected)
  1532.  
  1533.         return 1;
  1534.  
  1535.     fprintf(dataout, "PRIVMSG %s :%s\r\n", target, message);
  1536.  
  1537.     return fflush(dataout);
  1538.  
  1539. }
  1540.  
  1541.  
  1542.  
  1543. int IRC::privmsg(char* fmt, ...)
  1544.  
  1545. {
  1546.  
  1547.     va_list argp;
  1548.  
  1549.     char* target;
  1550.  
  1551.    
  1552.  
  1553.     if (!connected)
  1554.  
  1555.         return 1;
  1556.  
  1557.     va_start(argp, fmt);
  1558.  
  1559.     fprintf(dataout, "PRIVMSG %s :", fmt);
  1560.  
  1561.     vfprintf(dataout, va_arg(argp, char*), argp);
  1562.  
  1563.     va_end(argp);
  1564.  
  1565.     fprintf(dataout, "\r\n");
  1566.  
  1567.     return fflush(dataout);
  1568.  
  1569. }
  1570.  
  1571.  
  1572.  
  1573.  
  1574.  
  1575. int IRC::join(char* channel)
  1576.  
  1577. {
  1578.  
  1579.     if (!connected)
  1580.  
  1581.         return 1;
  1582.  
  1583.     fprintf(dataout, "JOIN %s\r\n", channel);
  1584.  
  1585.     return fflush(dataout);
  1586.  
  1587. }
  1588.  
  1589.  
  1590.  
  1591. int IRC::part(char* channel)
  1592.  
  1593. {
  1594.  
  1595.     if (!connected)
  1596.  
  1597.         return 1;
  1598.  
  1599.     fprintf(dataout, "PART %s\r\n", channel);
  1600.  
  1601.     return fflush(dataout);
  1602.  
  1603. }
  1604.  
  1605.  
  1606.  
  1607. int IRC::kick(char* channel, char* nick)
  1608.  
  1609. {
  1610.  
  1611.     if (!connected)
  1612.  
  1613.         return 1;
  1614.  
  1615.     fprintf(dataout, "KICK %s %s\r\n", channel, nick);
  1616.  
  1617.     return fflush(dataout);
  1618.  
  1619. }
  1620.  
  1621.  
  1622.  
  1623. int IRC::raw(char* data)
  1624.  
  1625. {
  1626.  
  1627.     if (!connected)
  1628.  
  1629.         return 1;
  1630.  
  1631.     fprintf(dataout, "%s\r\n", data);
  1632.  
  1633.     return fflush(dataout);
  1634.  
  1635. }
  1636.  
  1637.  
  1638.  
  1639. int IRC::kick(char* channel, char* nick, char* message)
  1640.  
  1641. {
  1642.  
  1643.     if (!connected)
  1644.  
  1645.         return 1;
  1646.  
  1647.     fprintf(dataout, "KICK %s %s :%s\r\n", channel, nick, message);
  1648.  
  1649.     return fflush(dataout);
  1650.  
  1651. }
  1652.  
  1653.  
  1654.  
  1655. int IRC::mode(char* channel, char* modes, char* targets)
  1656.  
  1657. {
  1658.  
  1659.     if (!connected)
  1660.  
  1661.         return 1;
  1662.  
  1663.     if (!targets)
  1664.  
  1665.         fprintf(dataout, "MODE %s %s\r\n", channel, modes);
  1666.  
  1667.     else
  1668.  
  1669.         fprintf(dataout, "MODE %s %s %s\r\n", channel, modes, targets);
  1670.  
  1671.     return fflush(dataout);
  1672.  
  1673. }
  1674.  
  1675.  
  1676.  
  1677. int IRC::mode(char* modes)
  1678.  
  1679. {
  1680.  
  1681.     if (!connected)
  1682.  
  1683.         return 1;
  1684.  
  1685.     mode(cur_nick, modes, 0);
  1686.  
  1687.     return 0;
  1688.  
  1689. }
  1690.  
  1691.  
  1692.  
  1693. int IRC::nick(char* newnick)
  1694.  
  1695. {
  1696.  
  1697.     if (!connected)
  1698.  
  1699.         return 1;
  1700.  
  1701.     fprintf(dataout, "NICK %s\r\n", newnick);
  1702.  
  1703.     return fflush(dataout);
  1704.  
  1705. }
  1706.  
  1707.  
  1708.  
  1709. char* IRC::current_nick()
  1710.  
  1711. {
  1712.  
  1713.     return cur_nick;
  1714.  
  1715. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement