handle_bot.c
上传用户:tany51
上传日期:2013-06-12
资源大小:1397k
文件大小:11k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /*
  2.  * Copyright (C) 1999,2000,2001  Ross Combs (rocombs@cs.nmsu.edu)
  3.  * Copyright (C) 1999  Rob Crittenden (rcrit@greyoak.com)
  4.  *
  5.  * This program is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU General Public License
  7.  * as published by the Free Software Foundation; either version 2
  8.  * of the License, or (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  */
  19. #include "common/setup_before.h"
  20. #ifdef HAVE_STDDEF_H
  21. # include <stddef.h>
  22. #else
  23. # ifndef NULL
  24. #  define NULL ((void *)0)
  25. # endif
  26. #endif
  27. #ifdef STDC_HEADERS
  28. # include <stdlib.h>
  29. #endif
  30. #ifdef HAVE_STRING_H
  31. # include <string.h>
  32. #else
  33. # ifdef HAVE_STRINGS_H
  34. #  include <strings.h>
  35. # endif
  36. # ifdef HAVE_MEMORY_H
  37. #  include <memory.h>
  38. # endif
  39. #endif
  40. #include "compat/strdup.h"
  41. #include <ctype.h>
  42. #include "common/packet.h"
  43. #include "common/bot_protocol.h"
  44. #include "common/tag.h"
  45. #include "message.h"
  46. #include "common/eventlog.h"
  47. #include "command.h"
  48. #include "account.h"
  49. #include "connection.h"
  50. #include "channel.h"
  51. #include "common/queue.h"
  52. #include "common/bnethash.h"
  53. #include "common/bnethashconv.h"
  54. #include "common/bn_type.h"
  55. #include "common/field_sizes.h"
  56. #include "common/list.h"
  57. #include "handle_bot.h"
  58. #include "common/setup_after.h"
  59. extern int handle_bot_packet(t_connection * c, t_packet const * const packet)
  60. {
  61.     t_packet * rpacket;
  62.     
  63.     if (!c)
  64.     {
  65. eventlog(eventlog_level_error,"handle_bot_packet","[%d] got NULL connection",conn_get_socket(c));
  66. return -1;
  67.     }
  68.     if (!packet)
  69.     {
  70. eventlog(eventlog_level_error,"handle_bot_packet","[%d] got NULL packet",conn_get_socket(c));
  71. return -1;
  72.     }
  73.     if (packet_get_class(packet)!=packet_class_raw)
  74.     {
  75.         eventlog(eventlog_level_error,"handle_bot_packet","[%d] got bad packet (class %d)",conn_get_socket(c),(int)packet_get_class(packet));
  76.         return -1;
  77.     }
  78.     
  79.     {
  80. char const * const linestr=packet_get_str_const(packet,0,MAX_MESSAGE_LEN);
  81. if (packet_get_size(packet)<2) /* empty line */
  82.     return 0;
  83. if (!linestr)
  84. {
  85.     eventlog(eventlog_level_warn,"handle_bot_packet","[%d] line too long",conn_get_socket(c));
  86.     return 0;
  87. }
  88. switch (conn_get_state(c))
  89. {
  90. case conn_state_connected:
  91.     conn_add_flags(c,MF_PLUG);
  92.     conn_set_clienttag(c,CLIENTTAG_BNCHATBOT);
  93.     
  94.     {
  95. char const * temp=linestr;
  96. if (temp[0]=='04') /* FIXME: no echo, ignore for now (we always do no echo) */
  97.     temp = &temp[1];
  98. if (temp[0]=='') /* empty line */
  99. {
  100.     conn_set_state(c,conn_state_bot_username); /* don't look for ^D or reset tag and flags */
  101.     break;
  102. }
  103. conn_set_state(c,conn_state_bot_password);
  104. if (conn_set_botuser(c,temp)<0)
  105.     eventlog(eventlog_level_error,"handle_bot_packet","[%d] could not set username to "%s"",conn_get_socket(c),temp);
  106. {
  107.     char const * const msg="rnPassword: ";
  108.     
  109.     if (!(rpacket = packet_create(packet_class_raw)))
  110.     {
  111. eventlog(eventlog_level_error,"handle_bot_packet","[%d] could not create rpacket",conn_get_socket(c));
  112. break;
  113.     }
  114. #if 1 /* don't echo */
  115.     packet_append_ntstring(rpacket,conn_get_botuser(c));
  116. #endif
  117.     packet_append_ntstring(rpacket,msg);
  118.     conn_push_outqueue(c,rpacket);
  119.     packet_del_ref(rpacket);
  120. }
  121.     }
  122.     break;
  123.     
  124. case conn_state_bot_username:
  125.     conn_set_state(c,conn_state_bot_password);
  126.     
  127.     if (conn_set_botuser(c,linestr)<0)
  128. eventlog(eventlog_level_error,"handle_bot_packet","[%d] could not set username to "%s"",conn_get_socket(c),linestr);
  129.     
  130.     {
  131. char const * const temp="rnPassword: ";
  132.     
  133. if (!(rpacket = packet_create(packet_class_raw)))
  134. {
  135.     eventlog(eventlog_level_error,"handle_bot_packet","[%d] could not create rpacket",conn_get_socket(c));
  136.     break;
  137. }
  138. #if 1 /* don't echo */
  139. packet_append_ntstring(rpacket,linestr);
  140. #endif
  141. packet_append_ntstring(rpacket,temp);
  142. conn_push_outqueue(c,rpacket);
  143. packet_del_ref(rpacket);
  144.     }
  145.     break;
  146.     
  147. case conn_state_bot_password:
  148.     {
  149. char const * const tempa="rnLogin failed.rnrnUsername: ";
  150. char const * const tempb="rnAccount has no bot access.rnrnUsername: ";
  151. char const * const botuser=conn_get_botuser(c);
  152. t_account *        account;
  153. char const *       oldstrhash1;
  154. t_hash             trypasshash1;
  155. t_hash             oldpasshash1;
  156. char const *       tname;
  157. char *             testpass;
  158. if (!botuser) /* error earlier in login */
  159. {
  160.     /* no log message... */
  161.     conn_set_state(c,conn_state_bot_username);
  162.     
  163.     if (!(rpacket = packet_create(packet_class_raw)))
  164.     {
  165. eventlog(eventlog_level_error,"handle_bot_packet","[%d] could not create rpacket",conn_get_socket(c));
  166. break;
  167.     }
  168.     
  169.     packet_append_ntstring(rpacket,tempa);
  170.     conn_push_outqueue(c,rpacket);
  171.     packet_del_ref(rpacket);
  172.     break;
  173. }
  174. if (connlist_find_connection_by_accountname(botuser))
  175. {
  176.     eventlog(eventlog_level_info,"handle_bot_packet","[%d] bot login for "%s" refused (already logged in)",conn_get_socket(c),botuser);
  177.     conn_set_state(c,conn_state_bot_username);
  178.     
  179.     if (!(rpacket = packet_create(packet_class_raw)))
  180.     {
  181. eventlog(eventlog_level_error,"handle_bot_packet","[%d] could not create rpacket",conn_get_socket(c));
  182. break;
  183.     }
  184.     
  185.     packet_append_ntstring(rpacket,tempa);
  186.     conn_push_outqueue(c,rpacket);
  187.     packet_del_ref(rpacket);
  188.     break;
  189. }
  190. if (!(account = accountlist_find_account(botuser)))
  191. {
  192. eventlog(eventlog_level_info,"handle_bot_packet","[%d] bot login for "%s" refused (bad account)",conn_get_socket(c),botuser);
  193. conn_set_state(c,conn_state_bot_username);
  194. if (!(rpacket = packet_create(packet_class_raw)))
  195. {
  196.     eventlog(eventlog_level_error,"handle_bot_packet","[%d] could not create rpacket",conn_get_socket(c));
  197.     break;
  198. }
  199. packet_append_ntstring(rpacket,tempa);
  200. conn_push_outqueue(c,rpacket);
  201. packet_del_ref(rpacket);
  202.     break;
  203. }
  204. if ((oldstrhash1 = account_get_pass(account)))
  205. {
  206.     if (hash_set_str(&oldpasshash1,oldstrhash1)<0)
  207.     {
  208. account_unget_pass(oldstrhash1);
  209. eventlog(eventlog_level_info,"handle_bot_packet","[%d] bot login for "%s" refused (corrupted passhash1?)",conn_get_socket(c),(tname = account_get_name(account)));
  210. account_unget_name(tname);
  211. conn_set_state(c,conn_state_bot_username);
  212. if (!(rpacket = packet_create(packet_class_raw)))
  213. {
  214.     eventlog(eventlog_level_error,"handle_bot_packet","[%d] could not create rpacket",conn_get_socket(c));
  215.     break;
  216. }
  217. packet_append_ntstring(rpacket,tempa);
  218. conn_push_outqueue(c,rpacket);
  219. packet_del_ref(rpacket);
  220. break;
  221.     }
  222.     account_unget_pass(oldstrhash1);
  223.     
  224.                     if (!(testpass = strdup(linestr)))
  225.                         break;
  226.     {
  227. unsigned int i;
  228. for (i=0; i<strlen(testpass); i++)
  229.     if (isupper((int)testpass[i]))
  230. testpass[i] = tolower((int)testpass[i]);
  231.     }
  232.     if (bnet_hash(&trypasshash1,strlen(testpass),testpass)<0) /* FIXME: force to lowercase */
  233.     {
  234. eventlog(eventlog_level_info,"handle_bot_packet","[%d] bot login for "%s" refused (unable to hash password)",conn_get_socket(c),(tname = account_get_name(account)));
  235. account_unget_name(tname);
  236. conn_set_state(c,conn_state_bot_username);
  237. free((void *)testpass);
  238. if (!(rpacket = packet_create(packet_class_raw)))
  239. {
  240.     eventlog(eventlog_level_error,"handle_bot_packet","[%d] could not create rpacket",conn_get_socket(c));
  241.     break;
  242. }
  243. packet_append_ntstring(rpacket,tempa);
  244. conn_push_outqueue(c,rpacket);
  245. packet_del_ref(rpacket);
  246. break;
  247.     }
  248.     free((void *)testpass);
  249.     if (hash_eq(trypasshash1,oldpasshash1)!=1)
  250.     {
  251. eventlog(eventlog_level_info,"handle_bot_packet","[%d] bot login for "%s" refused (wrong password)",conn_get_socket(c),(tname = account_get_name(account)));
  252. account_unget_name(tname);
  253. conn_set_state(c,conn_state_bot_username);
  254. if (!(rpacket = packet_create(packet_class_raw)))
  255. {
  256.     eventlog(eventlog_level_error,"handle_bot_packet","[%d] could not create rpacket",conn_get_socket(c));
  257.     break;
  258. }
  259. packet_append_ntstring(rpacket,tempa);
  260. conn_push_outqueue(c,rpacket);
  261. packet_del_ref(rpacket);
  262. break;
  263.     }
  264.     
  265.     
  266.     if (account_get_auth_botlogin(account)!=1) /* default to false */
  267.     {
  268. eventlog(eventlog_level_info,"handle_bot_packet","[%d] bot login for "%s" refused (no bot access)",conn_get_socket(c),(tname = account_get_name(account)));
  269. account_unget_name(tname);
  270. conn_set_state(c,conn_state_bot_username);
  271. if (!(rpacket = packet_create(packet_class_raw)))
  272. {
  273.     eventlog(eventlog_level_error,"handle_bot_packet","[%d] could not create rpacket",conn_get_socket(c));
  274.     break;
  275. }
  276. packet_append_ntstring(rpacket,tempb);
  277. conn_push_outqueue(c,rpacket);
  278. packet_del_ref(rpacket);
  279. break;
  280.     }
  281.     else if (account_get_auth_lock(account)==1) /* default to false */
  282.     {
  283. eventlog(eventlog_level_info,"handle_bot_packet","[%d] bot login for "%s" refused (this account is locked)",conn_get_socket(c),(tname = account_get_name(account)));
  284. account_unget_name(tname);
  285. conn_set_state(c,conn_state_bot_username);
  286. if (!(rpacket = packet_create(packet_class_raw)))
  287. {
  288.     eventlog(eventlog_level_error,"handle_bot_packet","[%d] could not create rpacket",conn_get_socket(c));
  289.     break;
  290. }
  291. packet_append_ntstring(rpacket,tempb);
  292. conn_push_outqueue(c,rpacket);
  293. packet_del_ref(rpacket);
  294. break;
  295.     }
  296.     
  297.     eventlog(eventlog_level_info,"handle_bot_packet","[%d] "%s" bot logged in (correct password)",conn_get_socket(c),(tname = account_get_name(account)));
  298.     account_unget_name(tname);
  299. }
  300. else
  301. {
  302.     eventlog(eventlog_level_info,"handle_bot_packet","[%d] "%s" bot logged in (no password)",conn_get_socket(c),(tname = account_get_name(account)));
  303.     account_unget_name(tname);
  304. }
  305.     if (!(rpacket = packet_create(packet_class_raw))) /* if we got this far, let them log in even if this fails */
  306. eventlog(eventlog_level_error,"handle_bot_packet","[%d] could not create rpacket",conn_get_socket(c));
  307.     else
  308.     {
  309. packet_append_ntstring(rpacket,"rn");
  310. conn_push_outqueue(c,rpacket);
  311. packet_del_ref(rpacket);
  312.     }
  313.     conn_set_account(c,account);
  314.     
  315.     message_send_text(c,message_type_uniqueid,c,(tname = account_get_name(account)));
  316.     account_unget_name(tname);
  317.          
  318.     if (conn_set_channel(c,CHANNEL_NAME_CHAT)<0)
  319. conn_set_channel(c,CHANNEL_NAME_BANNED); /* should not fail */
  320.     }
  321.     break;
  322.     
  323. case conn_state_loggedin:
  324.     {
  325. t_channel const * channel;
  326. conn_set_idletime(c);
  327. if ((channel = conn_get_channel(c)))
  328.     channel_message_log(channel,c,1,linestr);
  329. /* we don't log game commands currently */
  330. if (linestr[0]=='/')
  331.     handle_command(c,linestr);
  332. else
  333.     if (channel && !conn_quota_exceeded(c,linestr))
  334. channel_message_send(channel,message_type_talk,c,linestr);
  335.     /* else discard */
  336.     }
  337.     break;
  338.     
  339. default:
  340.     eventlog(eventlog_level_error,"handle_bot_packet","[%d] unknown bot connection state %d",conn_get_socket(c),(int)conn_get_state(c));
  341. }
  342.     }
  343.     
  344.     return 0;
  345. }