servmsg.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * servmsg.c -- part of server.mod
  22. *
  23. */
  24. #include <netinet/tcp.h>
  25. char cursrvname[120] = "";
  26. char curnetwork[120] = "";
  27. static time_t last_ctcp = (time_t) 0L;
  28. static int count_ctcp = 0;
  29. char altnick_char = 0;
  30. unsigned int rolls = 0;
  31. #define ROLL_RIGHT
  32. #undef ROLL_LEFT
  33. static void rotate_nick(char *nick, char *orignick)
  34. {
  35. size_t len = strlen(nick);
  36. // Cap the len calcs at the max NICKLEN for this server
  37. if (len > nick_len) {
  38. len = nick_len;
  39. nick[len] = 0;
  40. }
  41. int use_chr = 1;
  42. #ifdef DEBUG
  43. sdprintf("rotate_nick(%s, %s) rolls: %d altnick_char: %c", nick, orignick, rolls, altnick_char);
  44. #endif
  45. /* First run? */
  46. if (altnick_char == 0 && !rolls && altchars[0]) {
  47. altnick_char = altchars[0];
  48. /* the nick is already as long as it can be. */
  49. if (len == (unsigned) nick_len) {
  50. /* make the last char the current altnick_char */
  51. nick[len - 1] = altnick_char;
  52. } else {
  53. /* tack it on to the end */
  54. nick[len] = altnick_char;
  55. nick[len + 1] = 0;
  56. }
  57. } else {
  58. char *p = NULL;
  59. if ((p = strchr(altchars, altnick_char)))
  60. p++;
  61. /* if we haven't been rolling, use the ALTCHARS
  62. */
  63. if (!rolls && p && *p) {
  64. altnick_char = *p;
  65. /* if we've already rolled, just keep rolling until we've rolled completely
  66. * after that, just generate random chars
  67. */
  68. } else if (rolls < len) {
  69. /* fun BX style rolling, WEEEE */
  70. char tmp = 0;
  71. size_t i = 0;
  72. altnick_char = 0;
  73. use_chr = 0;
  74. if (rolls == 0) {
  75. strlcpy(nick, orignick, sizeof(botname));
  76. len = strlen(nick);
  77. }
  78. #ifdef ROLL_RIGHT
  79. tmp = nick[len - 1];
  80. #endif /* ROLL_RIGHT */
  81. #ifdef ROLL_LEFT
  82. tmp = nick[0];
  83. #endif /* ROLL_LEFT */
  84. if (strchr(BADHANDCHARS, tmp))
  85. tmp = '_';
  86. rolls++;
  87. #ifdef ROLL_RIGHT
  88. for (i = (len - 1); i > 0; i--)
  89. nick[i] = nick[i - 1];
  90. nick[0] = tmp;
  91. #endif /* ROLL_RIGHT */
  92. #ifdef ROLL_LEFT
  93. for (i = 0; i < (len - 1); i++)
  94. nick[i] = nick[i + 1];
  95. nick[len - 1] = tmp;
  96. #endif /* ROLL_LEFT */
  97. nick[len] = 0;
  98. } else {
  99. /* we've tried ALTCHARS, and rolled the nick, just use random chars now */
  100. altnick_char = 'a' + randint(26);
  101. }
  102. if (use_chr && altnick_char) {
  103. nick[len - 1] = altnick_char;
  104. nick[len] = 0;
  105. }
  106. }
  107. }
  108. //This is only called on failed nick on connect
  109. static int gotfake433(char *nick)
  110. {
  111. //Failed to get jupenick on connect, try normal nick
  112. if (altnick_char == 0 && jupenick[0] && !rfc_casecmp(botname, jupenick)) {
  113. strlcpy(botname, origbotname, sizeof(botname));
  114. } else //Rotate on failed normal nick
  115. rotate_nick(botname, origbotname);
  116. putlog(LOG_SERV, "*", "NICK IN USE: '%s' Trying '%s'", nick, botname);
  117. dprintf(DP_SERVER, "NICK %s\n", botname);
  118. return 0;
  119. }
  120. /* Check for tcl-bound msg command, return 1 if found
  121. *
  122. * msg: proc-name <nick> <user@host> <handle> <args...>
  123. */
  124. static void check_bind_msg(const char *cmd, char *nick, char *uhost, struct userrec *u, char *args)
  125. {
  126. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  127. int x;
  128. get_user_flagrec(u, &fr, NULL);
  129. x = check_bind(BT_msg, cmd, &fr, nick, uhost, u, args);
  130. if (x & BIND_RET_LOG)
  131. putlog(LOG_CMDS, "*", "(%s!%s) !%s! %s %s", nick, uhost, u ? u->handle : "*" , cmd, args);
  132. else if (x == 0)
  133. putlog(LOG_MSGS, "*", "[%s!%s] %s %s", nick, uhost, cmd, args);
  134. }
  135. /* Return 1 if processed.
  136. */
  137. static int check_bind_raw(char *from, char *code, char *msg)
  138. {
  139. char *p1 = NULL, *p2 = NULL, *myfrom = NULL, *mymsg = NULL;
  140. int ret = 0;
  141. myfrom = p1 = strdup(from);
  142. mymsg = p2 = strdup(msg);
  143. ret = check_bind(BT_raw, code, NULL, myfrom, mymsg);
  144. free(p1);
  145. free(p2);
  146. return ret;
  147. }
  148. int check_bind_ctcpr(char *nick, char *uhost, struct userrec *u,
  149. char *dest, char *keyword, char *args,
  150. bind_table_t *table)
  151. {
  152. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  153. get_user_flagrec(u, &fr, NULL);
  154. return check_bind(table, keyword, &fr, nick, uhost, u, dest, keyword, args);
  155. }
  156. bool match_my_nick(char *nick)
  157. {
  158. return (!rfc_casecmp(nick, botname));
  159. }
  160. void rehash_monitor_list() {
  161. if (!use_monitor) return;
  162. dprintf(DP_SERVER, "MONITOR C\n");
  163. if (jupenick[0])
  164. dprintf(DP_SERVER, "MONITOR + %s,%s\n", jupenick, origbotname);
  165. else
  166. dprintf(DP_SERVER, "MONITOR + %s", origbotname);
  167. }
  168. void rehash_server(const char *servname, const char *nick)
  169. {
  170. strlcpy(cursrvname, servname, sizeof(cursrvname));
  171. if (servidx >= 0)
  172. curservport = dcc[servidx].port;
  173. if (nick && nick[0]) {
  174. strlcpy(botname, nick, sizeof(botname));
  175. dprintf(DP_MODE, "WHOIS %s\n", botname); /* get user@host */
  176. dprintf(DP_MODE, "USERHOST %s\n", botname); /* get user@ip */
  177. dprintf(DP_SERVER, "MODE %s %s\n", botname, var_get_str_by_name("usermode"));
  178. rehash_monitor_list();
  179. }
  180. }
  181. void
  182. join_chans()
  183. {
  184. for (register struct chanset_t *chan = chanset; chan; chan = chan->next) {
  185. if (shouldjoin(chan))
  186. force_join_chan(chan, DP_SERVER);
  187. }
  188. }
  189. /* 001: welcome to IRC (use it to fix the server name)
  190. */
  191. static int got001(char *from, char *msg)
  192. {
  193. fixcolon(msg);
  194. server_online = now;
  195. waiting_for_awake = 0;
  196. rehash_server(from, msg);
  197. /* Ok...param #1 of 001 = what server thinks my nick is */
  198. #ifdef no
  199. if (strcasecmp(from, dcc[servidx].host)) {
  200. struct server_list *x = serverlist;
  201. if (x == NULL)
  202. return 0; /* Uh, no server list */
  203. putlog(LOG_MISC, "*", "(%s claims to be %s; updating server list)", dcc[servidx].host, from);
  204. for (int i = curserv; i > 0 && x != NULL; i--)
  205. x = x->next;
  206. if (x == NULL) {
  207. putlog(LOG_MISC, "*", "Invalid server list!");
  208. return 0;
  209. }
  210. }
  211. #endif
  212. return 0;
  213. }
  214. /* <server> 004 <to> <servername> <version> <user modes> <channel modes> */
  215. static int
  216. got004(char *from, char *msg)
  217. {
  218. char *tmp = NULL;
  219. newsplit(&msg); /* nick */
  220. newsplit(&msg); /* server */
  221. //Cache the results for later parsing if needed (after a restart)
  222. //Sending 'VERSION' does not work on all servers, plus this speeds
  223. //up a restart by removing the need to wait for the information
  224. if (!replaying_cache)
  225. dprintf(DP_CACHE, ":%s 004 . . %s", from, msg);
  226. tmp = newsplit(&msg);
  227. bool connect_burst = 0;
  228. /* cookies won't work on ircu or Unreal or snircd */
  229. if (strstr(tmp, "u2.") || strstr(tmp, "Unreal") || strstr(tmp, "snircd")) {
  230. putlog(LOG_DEBUG, "*", "Disabling cookies as they are not supported on %s", cursrvname);
  231. cookies_disabled = true;
  232. } else if (strstr(tmp, "hybrid") || strstr(tmp, "ratbox") || strstr(tmp, "Charybdis") || strstr(tmp, "ircd-seven")) {
  233. connect_burst = 1;
  234. if (!strstr(tmp, "hybrid"))
  235. use_flood_count = 1;
  236. }
  237. if (!replaying_cache && connect_burst) {
  238. connect_bursting = now;
  239. msgburst = SERVER_CONNECT_BURST_RATE;
  240. msgrate = 200;
  241. flood_time.sec = last_time.sec = now - 100;
  242. flood_time.usec = last_time.usec = 0;
  243. putlog(LOG_DEBUG, "*", "Server allows connect bursting, bursting for %d seconds", SERVER_CONNECT_BURST_TIME);
  244. }
  245. if (!replaying_cache)
  246. join_chans();
  247. return 0;
  248. }
  249. /* <server> 005 <to> <option> <option> <... option> :are supported by this server */
  250. static int
  251. got005(char *from, char *msg)
  252. {
  253. char *tmp = NULL, *p, *p2;
  254. newsplit(&msg); /* nick */
  255. //Cache the results for later parsing if needed (after a restart)
  256. //Sending 'VERSION' does not work on all servers, plus this speeds
  257. //up a restart by removing the need to wait for the information
  258. if (!replaying_cache)
  259. dprintf(DP_CACHE, ":%s 005 . %s", from, msg);
  260. while ((tmp = newsplit(&msg))[0]) {
  261. p = NULL;
  262. if ((p = strchr(tmp, '=')))
  263. *p++ = 0;
  264. if (!strcasecmp(tmp, ":are"))
  265. break;
  266. else if (!strcasecmp(tmp, "MODES")) {
  267. modesperline = atoi(p);
  268. if (modesperline > MODES_PER_LINE_MAX)
  269. modesperline = MODES_PER_LINE_MAX;
  270. } else if (!strcasecmp(tmp, "NICKLEN"))
  271. nick_len = atoi(p);
  272. else if (!strcasecmp(tmp, "MONITOR")) {
  273. bool need_to_rehash_monitor = 0;
  274. if (!use_monitor && !replaying_cache) // Don't rehash if restarting. No need.
  275. need_to_rehash_monitor = 1;
  276. use_monitor = 1;
  277. if (need_to_rehash_monitor)
  278. rehash_monitor_list();
  279. }
  280. else if (!strcasecmp(tmp, "NETWORK")) {
  281. strlcpy(curnetwork, p, 120);
  282. if (!strcasecmp(tmp, "IRCnet")) {
  283. simple_snprintf(stackablecmds, sizeof(stackablecmds), "INVITE AWAY VERSION NICK");
  284. simple_snprintf(stackable2cmds, sizeof(stackable2cmds), "USERHOST ISON");
  285. use_fastdeq = 3;
  286. } else if (!strcasecmp(tmp, "DALnet")) {
  287. simple_snprintf(stackablecmds, sizeof(stackablecmds), "PRIVMSG NOTICE PART WHOIS WHOWAS USERHOST ISON WATCH DCCALLOW");
  288. simple_snprintf(stackable2cmds, sizeof(stackable2cmds), "USERHOST ISON WATCH");
  289. use_fastdeq = 2;
  290. } else if (!strcasecmp(tmp, "UnderNet")) {
  291. simple_snprintf(stackablecmds, sizeof(stackablecmds), "PRIVMSG NOTICE TOPIC PART WHOIS USERHOST USERIP ISON");
  292. simple_snprintf(stackable2cmds, sizeof(stackable2cmds), "USERHOST USERIP ISON");
  293. use_fastdeq = 2;
  294. } else {
  295. stackablecmds[0] = 0;
  296. simple_snprintf(stackable2cmds, sizeof(stackable2cmds), "USERHOST ISON");
  297. use_fastdeq = 0;
  298. }
  299. } else if (!strcasecmp(tmp, "PENALTY"))
  300. use_penalties = 1;
  301. else if (!strcasecmp(tmp, "WHOX"))
  302. use_354 = 1;
  303. else if (!strcasecmp(tmp, "DEAF")) {
  304. deaf_char = p ? p[0] : 'D';
  305. if (use_deaf && !in_deaf) {
  306. dprintf(DP_SERVER, "MODE %s +%c\n", botname, deaf_char);
  307. in_deaf = 1;
  308. }
  309. } else if (!strcasecmp(tmp, "CALLERID")) {
  310. callerid_char = p ? p[0] : 'g';
  311. if (use_callerid && !in_callerid) {
  312. dprintf(DP_SERVER, "MODE %s +%c\n", botname, callerid_char);
  313. in_callerid = 1;
  314. }
  315. } else if (!strcasecmp(tmp, "EXCEPTS"))
  316. use_exempts = 1;
  317. else if (!strcasecmp(tmp, "CPRIVMSG"))
  318. have_cprivmsg = 1;
  319. else if (!strcasecmp(tmp, "CNOTICE"))
  320. have_cnotice = 1;
  321. else if (!strcasecmp(tmp, "INVEX"))
  322. use_invites = 1;
  323. else if (!strcasecmp(tmp, "MAXBANS")) {
  324. max_bans = atoi(p);
  325. max_modes = max_bans;
  326. max_exempts = max_bans;
  327. max_invites = max_bans;
  328. }
  329. else if (!strcasecmp(tmp, "MAXLIST")) {
  330. p2 = NULL;
  331. if ((p2 = strchr(p, ':'))) {
  332. *p2++ = 0;
  333. max_modes = atoi(p2);
  334. if (strchr(p, 'e'))
  335. max_exempts = max_modes;
  336. if (strchr(p, 'b'))
  337. max_bans = max_modes;
  338. if (strchr(p, 'I'))
  339. max_invites = max_modes;
  340. }
  341. }
  342. else if (!strcasecmp(tmp, "CASEMAPPING")) {
  343. /* we are default set to rfc1459, so only switch if NOT rfc1459 */
  344. if (strcasecmp(p, "rfc1459")) {
  345. rfc_casecmp = strcasecmp;
  346. rfc_toupper = toupper;
  347. }
  348. }
  349. }
  350. return 0;
  351. }
  352. /* Got 442: not on channel
  353. */
  354. static int got442(char *from, char *msg)
  355. {
  356. char *chname = NULL;
  357. struct chanset_t *chan = NULL;
  358. int i;
  359. struct server_list *x = NULL;
  360. for (x = serverlist, i = 0; x; x = x->next, i++)
  361. if (i == curserv) {
  362. if (strcasecmp(from, x->name))
  363. return 0;
  364. break;
  365. }
  366. newsplit(&msg);
  367. chname = newsplit(&msg);
  368. chan = findchan(chname);
  369. if (chan && shouldjoin(chan) && !channel_joining(chan)) {
  370. putlog(LOG_MISC, chname, "Server says I'm not on channel: %s", chname);
  371. force_join_chan(chan);
  372. }
  373. return 0;
  374. }
  375. /* Close the current server connection.
  376. */
  377. void nuke_server(const char *reason)
  378. {
  379. if (serv >= 0 && servidx >= 0) {
  380. if (reason)
  381. dprintf(-serv, "QUIT :%s\n", reason);
  382. sleep(1);
  383. disconnect_server(servidx, DO_LOST);
  384. }
  385. }
  386. char ctcp_reply[1024] = "";
  387. static int lastmsgs[FLOOD_GLOBAL_MAX];
  388. static char lastmsghost[FLOOD_GLOBAL_MAX][128];
  389. static time_t lastmsgtime[FLOOD_GLOBAL_MAX];
  390. static int dronemsgs;
  391. static time_t dronemsgtime;
  392. static interval_t flood_callerid_time = 60;
  393. rate_t flood_callerid = { 6, 2 };
  394. void unset_callerid(int data)
  395. {
  396. if (in_callerid) {
  397. dprintf(DP_MODE, "MODE %s :-%c\n", botname, callerid_char);
  398. in_callerid = 0;
  399. }
  400. }
  401. /* Do on NICK, PRIVMSG, NOTICE and JOIN.
  402. */
  403. static bool detect_flood(char *floodnick, char *floodhost, char *from, int which)
  404. {
  405. struct userrec *u = get_user_by_host(from);
  406. int atr = u ? u->flags : 0;
  407. if ((u && u->bot) || (atr & USER_NOFLOOD))
  408. return 0;
  409. char *p = NULL, ftype[10] = "", h[1024] = "";
  410. int thr = 0;
  411. time_t lapse = 0;
  412. /* Determine how many are necessary to make a flood */
  413. switch (which) {
  414. case FLOOD_PRIVMSG:
  415. case FLOOD_NOTICE:
  416. thr = flood_msg.count;
  417. lapse = flood_msg.time;
  418. strlcpy(ftype, "msg", 4);
  419. break;
  420. case FLOOD_CTCP:
  421. thr = flood_ctcp.count;
  422. lapse = flood_ctcp.time;
  423. strlcpy(ftype, "ctcp", 5);
  424. break;
  425. }
  426. if ((thr == 0) || (lapse == 0))
  427. return 0; /* No flood protection */
  428. /* Okay, make sure i'm not flood-checking myself */
  429. if (match_my_nick(floodnick))
  430. return 0;
  431. if (!strcasecmp(floodhost, botuserhost))
  432. return 0; /* My user@host (?) */
  433. if (dronemsgtime < now - flood_callerid.time) { //expired, reset counter
  434. dronemsgs = 0;
  435. // dronemsgtime = now;
  436. }
  437. dronemsgs++;
  438. dronemsgtime = now;
  439. if (!in_callerid && dronemsgs >= flood_callerid.count && callerid_char) { //flood from dronenet, let's attempt to set +g
  440. egg_timeval_t howlong;
  441. in_callerid = 1;
  442. dronemsgs = 0;
  443. dronemsgtime = 0;
  444. dprintf(DP_MODE_NEXT, "MODE %s :+%c\n", botname, callerid_char);
  445. howlong.sec = flood_callerid_time;
  446. howlong.usec = 0;
  447. timer_create(&howlong, "Unset CALLERID", (Function) unset_callerid);
  448. putlog(LOG_MISC, "*", "Drone flood detected! Setting CALLERID for %d seconds.", flood_callerid_time);
  449. return 1; //ignore the current msg
  450. }
  451. p = strchr(floodhost, '@');
  452. if (p) {
  453. p++;
  454. if (strcasecmp(lastmsghost[which], p)) { /* New */
  455. strlcpy(lastmsghost[which], p, 128);
  456. lastmsgtime[which] = now;
  457. lastmsgs[which] = 0;
  458. return 0;
  459. }
  460. } else
  461. return 0; /* Uh... whatever. */
  462. if (lastmsgtime[which] < now - lapse) {
  463. /* Flood timer expired, reset it */
  464. lastmsgtime[which] = now;
  465. lastmsgs[which] = 0;
  466. return 0;
  467. }
  468. lastmsgs[which]++;
  469. if (lastmsgs[which] >= thr) { /* FLOOD */
  470. /* Reset counters */
  471. lastmsgs[which] = 0;
  472. lastmsgtime[which] = 0;
  473. lastmsghost[which][0] = 0;
  474. u = get_user_by_host(from);
  475. /* Private msg */
  476. simple_snprintf(h, sizeof(h), "*!*@%s", p);
  477. putlog(LOG_MISC, "*", "Flood from @%s! Placing on ignore!", p);
  478. addignore(h, conf.bot->nick, (which == FLOOD_CTCP) ? "CTCP flood" : "MSG/NOTICE flood", now + (60 * ignore_time));
  479. return 1;
  480. }
  481. return 0;
  482. }
  483. /* Check for more than 8 control characters in a line.
  484. * This could indicate: beep flood CTCP avalanche.
  485. */
  486. bool detect_avalanche(char *msg)
  487. {
  488. int count = 0;
  489. for (unsigned char *p = (unsigned char *) msg; (*p) && (count < 8); p++)
  490. if ((*p == 7) || (*p == 1))
  491. count++;
  492. if (count >= 8)
  493. return 1;
  494. else
  495. return 0;
  496. }
  497. /* Got a private message.
  498. */
  499. static int gotmsg(char *from, char *msg)
  500. {
  501. if (msg[0] && ((strchr(CHANMETA, *msg) != NULL) ||
  502. (*msg == '@'))) /* Notice to a channel, not handled here */
  503. return 0;
  504. char *to = NULL, buf[UHOSTLEN] = "", *nick = NULL, ctcpbuf[512] = "", *uhost = buf,
  505. *ctcp = NULL, *p = NULL, *p1 = NULL, *code = NULL;
  506. struct userrec *u = NULL;
  507. int ctcp_count = 0;
  508. bool ignoring = match_ignore(from);
  509. to = newsplit(&msg);
  510. fixcolon(msg);
  511. /* Only check if flood-ctcp is active */
  512. strlcpy(uhost, from, UHOSTLEN);
  513. nick = splitnick(&uhost);
  514. if (flood_ctcp.count && detect_avalanche(msg)) {
  515. if (!ignoring) {
  516. putlog(LOG_MODES, "*", "Avalanche from %s - ignoring", from);
  517. p = strchr(uhost, '@');
  518. if (p != NULL)
  519. p++;
  520. else
  521. p = uhost;
  522. simple_snprintf(ctcpbuf, sizeof(ctcpbuf), "*!*@%s", p);
  523. addignore(ctcpbuf, conf.bot->nick, "ctcp avalanche", now + (60 * ignore_time));
  524. ignoring = 1;
  525. }
  526. }
  527. /* Check for CTCP: */
  528. ctcp_reply[0] = 0;
  529. p = strchr(msg, 1);
  530. while ((p != NULL) && (*p)) {
  531. p++;
  532. p1 = p;
  533. while ((*p != 1) && (*p != 0))
  534. p++;
  535. if (*p == 1) {
  536. *p = 0;
  537. strlcpy(ctcpbuf, p1, sizeof(ctcpbuf));
  538. ctcp = ctcpbuf;
  539. /* remove the ctcp in msg */
  540. memmove(p1 - 1, p + 1, strlen(p + 1) + 1);
  541. if (!ignoring)
  542. detect_flood(nick, uhost, from, strncmp(ctcp, "ACTION ", 7) ? FLOOD_CTCP : FLOOD_PRIVMSG);
  543. /* Respond to the first answer_ctcp */
  544. p = strchr(msg, 1);
  545. if (ctcp_count < answer_ctcp) {
  546. ctcp_count++;
  547. if (ctcp[0] != ' ') {
  548. code = newsplit(&ctcp);
  549. if ((to[0] == '$') || strchr(to, '.')) {
  550. if (!ignoring) {
  551. /* Don't interpret */
  552. putlog(LOG_PUBLIC, to, "CTCP %s from %s (%s) to %s: %s", code, nick, uhost, to, ctcp);
  553. }
  554. } else {
  555. u = get_user_by_host(from);
  556. if (!ignoring || trigger_on_ignore) {
  557. if (check_bind_ctcp(nick, uhost, u, to, code, ctcp) == BIND_RET_LOG && !ignoring) {
  558. if (!strcmp(code, "DCC")) {
  559. /* If it gets this far unhandled, it means that
  560. * the user is totally unknown.
  561. */
  562. code = newsplit(&ctcp);
  563. if (!strcmp(code, "CHAT")) {
  564. if (!ischanhub())
  565. putlog(LOG_MISC, "*", "%s: %s", "Refused DCC chat (I'm not a chathub (+c))", from);
  566. else
  567. putlog(LOG_MISC, "*", "%s: %s", "Refused DCC chat (no access)", from);
  568. } else {
  569. putlog(LOG_MISC, "*", "Refused DCC %s: %s", code, from);
  570. }
  571. } else if (!strcmp(code, "CHAT")) {
  572. if (!ischanhub())
  573. putlog(LOG_MISC, "*", "%s: %s", "Refused DCC chat (I'm not a chathub (+c))", from);
  574. else
  575. putlog(LOG_MISC, "*", "%s: %s", "Refused DCC chat (no access)", from);
  576. }
  577. }
  578. if (!strcmp(code, "ACTION")) {
  579. putlog(LOG_MSGS, "*", "* %s (%s): %s", nick, uhost, ctcp);
  580. } else {
  581. putlog(LOG_MSGS, "*", "CTCP %s: from %s (%s): %s", code, nick, uhost, ctcp);
  582. } /* I love a good close cascade ;) */
  583. }
  584. }
  585. }
  586. }
  587. }
  588. }
  589. /* Send out possible ctcp responses */
  590. if (ctcp_reply[0]) {
  591. if (ctcp_mode != 2) {
  592. notice(nick, ctcp_reply, DP_HELP);
  593. } else {
  594. if (now - last_ctcp > flood_ctcp.time) {
  595. notice(nick, ctcp_reply, DP_HELP);
  596. count_ctcp = 1;
  597. } else if (count_ctcp < flood_ctcp.count) {
  598. notice(nick, ctcp_reply, DP_HELP);
  599. count_ctcp++;
  600. }
  601. last_ctcp = now;
  602. }
  603. }
  604. if (msg[0]) {
  605. if ((to[0] == '$') || (strchr(to, '.') != NULL)) {
  606. /* Msg from oper */
  607. if (!ignoring) {
  608. detect_flood(nick, uhost, from, FLOOD_PRIVMSG);
  609. /* Do not interpret as command */
  610. putlog(LOG_MSGS | LOG_SERV, "*", "[%s!%s to %s] %s",nick, uhost, to, msg);
  611. }
  612. } else {
  613. char *my_code = NULL;
  614. Auth *auth = NULL;
  615. if (auth_prefix[0])
  616. auth = Auth::Find(uhost);
  617. if (!auth && !ignoring)
  618. detect_flood(nick, uhost, from, FLOOD_PRIVMSG);
  619. my_code = newsplit(&msg);
  620. rmspace(msg);
  621. /* is it a cmd? */
  622. if (auth_prefix[0] && my_code && my_code[0] && my_code[1] && auth && auth->Authed() && my_code[0] == auth_prefix[0]) {
  623. my_code++; //eliminate the prefix
  624. auth->atime = now;
  625. if (!check_bind_authc(my_code, auth, NULL, msg))
  626. putlog(LOG_MSGS, "*", "[%s] %c%s %s", from, auth_prefix[0], my_code, msg);
  627. } else if (!ignoring && (my_code[0] != auth_prefix[0] || !my_code[1] || !auth || !auth->Authed())) {
  628. struct userrec *my_u = get_user_by_host(from);
  629. bool doit = 1;
  630. if (!strcasecmp(my_code, "op") || !strcasecmp(my_code, "pass") || !strcasecmp(my_code, "invite")
  631. || !strcasecmp(my_code, "ident") || !strcasecmp(my_code, "release")
  632. || !strcasecmp(my_code, msgop) || !strcasecmp(my_code, msgpass) || !strcasecmp(my_code, msgrelease)
  633. || !strcasecmp(my_code, msginvite) || !strcasecmp(my_code, msgident)) {
  634. const char *buf2 = NULL;
  635. doit = 0;
  636. if (!strcasecmp(my_code, msgop))
  637. buf2 = "op";
  638. else if (!strcasecmp(my_code, msgpass))
  639. buf2 = "pass";
  640. else if (!strcasecmp(my_code, msginvite))
  641. buf2 = "invite";
  642. else if (!strcasecmp(my_code, msgident))
  643. buf2 = "ident";
  644. else if (!strcasecmp(my_code, msgrelease))
  645. buf2 = "release";
  646. if (buf2)
  647. check_bind_msg(buf2, nick, uhost, my_u, msg);
  648. else
  649. putlog(LOG_MSGS, "*", "(%s!%s) attempted to use invalid msg cmd '%s'", nick, uhost, my_code);
  650. }
  651. if (doit)
  652. check_bind_msg(my_code, nick, uhost, my_u, msg);
  653. }
  654. }
  655. }
  656. return 0;
  657. }
  658. /* Got a private notice.
  659. */
  660. static int gotnotice(char *from, char *msg)
  661. {
  662. if (msg[0] && ((strchr(CHANMETA, *msg) != NULL) ||
  663. (*msg == '@'))) /* Notice to a channel, not handled here */
  664. return 0;
  665. char *to = NULL, *nick = NULL, ctcpbuf[512] = "", *p = NULL, *p1 = NULL, buf[512] = "",
  666. *uhost = buf, *ctcp = NULL, *ctcpmsg = NULL, *ptr = NULL;
  667. struct userrec *u = NULL;
  668. bool ignoring = match_ignore(from);
  669. to = newsplit(&msg);
  670. fixcolon(msg);
  671. strlcpy(uhost, from, UHOSTLEN);
  672. nick = splitnick(&uhost);
  673. if (flood_ctcp.count && detect_avalanche(msg)) {
  674. /* Discard -- kick user if it was to the channel */
  675. if (!ignoring)
  676. putlog(LOG_MODES, "*", "Avalanche from %s", from);
  677. return 0;
  678. }
  679. /* Check for CTCP: */
  680. ctcpmsg = ptr = strdup(msg);
  681. p = strchr(ctcpmsg, 1);
  682. while ((p != NULL) && (*p)) {
  683. p++;
  684. p1 = p;
  685. while ((*p != 1) && (*p != 0))
  686. p++;
  687. if (*p == 1) {
  688. *p = 0;
  689. strlcpy(ctcpbuf, p1, sizeof(ctcpbuf));
  690. ctcp = ctcpbuf;
  691. /* remove the ctcp in msg */
  692. memmove(p1 - 1, p + 1, strlen(p + 1) + 1);
  693. if (!ignoring)
  694. detect_flood(nick, uhost, from, FLOOD_CTCP);
  695. p = strchr(ctcpmsg, 1);
  696. if (ctcp[0] != ' ') {
  697. char *code = newsplit(&ctcp);
  698. if ((to[0] == '$') || strchr(to, '.')) {
  699. if (!ignoring)
  700. putlog(LOG_PUBLIC, "*",
  701. "CTCP reply %s: %s from %s (%s) to %s", code, ctcp, nick, uhost, to);
  702. } else {
  703. u = get_user_by_host(from);
  704. if (!ignoring || trigger_on_ignore) {
  705. check_bind_ctcr(nick, uhost, u, to, code, ctcp);
  706. if (!ignoring)
  707. /* Who cares? */
  708. putlog(LOG_MSGS, "*", "CTCP reply %s: %s from %s (%s) to %s", code, ctcp, nick, uhost, to);
  709. }
  710. }
  711. }
  712. }
  713. }
  714. free(ptr);
  715. if (msg[0]) {
  716. if (((to[0] == '$') || strchr(to, '.')) && !ignoring) {
  717. detect_flood(nick, uhost, from, FLOOD_NOTICE);
  718. putlog(LOG_MSGS | LOG_SERV, "*", "-%s (%s) to %s- %s", nick, uhost, to, msg);
  719. } else {
  720. /* Server notice? */
  721. if ((nick[0] == 0) || (uhost[0] == 0)) {
  722. if (!server_online &&
  723. strstr(msg, "*** You are exempt from flood"))
  724. floodless = 1;
  725. /* Hidden `250' connection count message from server */
  726. if (strncmp(msg, "Highest connection count:", 25))
  727. putlog(LOG_SERV, "*", "-NOTICE- %s", msg);
  728. } else if (!ignoring) {
  729. detect_flood(nick, uhost, from, FLOOD_NOTICE);
  730. u = get_user_by_host(from);
  731. putlog(LOG_MSGS, "*", "-%s (%s)- %s", nick, uhost, msg);
  732. }
  733. }
  734. }
  735. return 0;
  736. }
  737. /* WALLOPS: oper's nuisance
  738. */
  739. static int gotwall(char *from, char *msg)
  740. {
  741. fixcolon(msg);
  742. putlog(LOG_WALL, "*", "!%s! %s", from, msg);
  743. return 0;
  744. }
  745. void server_send_ison()
  746. {
  747. // Only check if we're not on jupenick, or there is no jupenick and we're not on the preferred nick
  748. bool have_jupenick = jupenick[0] ? match_my_nick(jupenick) : 0;
  749. if (!have_jupenick) {
  750. /* See if my nickname is in use and if if my nick is right. */
  751. if (jupenick[0] && !have_jupenick) {
  752. if (use_monitor) // If our nick is juped due to split, we will NOT get a RPL_MONOFFLINE when it is available. So check periodically.
  753. dprintf(DP_SERVER, "MONITOR S\n");
  754. else
  755. dprintf(DP_SERVER, "ISON %s %s\n", origbotname, jupenick);
  756. } else if (!match_my_nick(origbotname)) {
  757. if (use_monitor) // If our nick is juped due to split, we will NOT get a RPL_MONOFFLINE when it is available. So check periodically.
  758. dprintf(DP_SERVER, "MONITOR S\n");
  759. else
  760. dprintf(DP_SERVER, "ISON %s\n", origbotname);
  761. }
  762. }
  763. }
  764. #ifdef not_used
  765. /* Called once a minute... but if we're the only one on the
  766. * channel, we only wanna send out "lusers" once every 5 mins.
  767. */
  768. static void minutely_checks()
  769. {
  770. /* Only check if we have already successfully logged in. */
  771. if (server_online) {
  772. static int count = 4;
  773. bool ok = 0;
  774. for (register struct chanset_t *chan = chanset; chan; chan = chan->next) {
  775. if (channel_active(chan) && chan->channel.members == 1) {
  776. ok = 1;
  777. break;
  778. }
  779. }
  780. if (!ok)
  781. return;
  782. count++;
  783. if (count >= 5) {
  784. dprintf(DP_SERVER, "LUSERS\n");
  785. count = 0;
  786. }
  787. }
  788. }
  789. #endif
  790. /* Pong from server.
  791. */
  792. static int gotpong(char *from, char *msg)
  793. {
  794. newsplit(&msg);
  795. fixcolon(msg); /* Scrap server name */
  796. server_lag = now - my_atoul(msg);
  797. if (server_lag > 99999) {
  798. /* IRCnet lagmeter support by drummer */
  799. server_lag = now - lastpingtime;
  800. }
  801. return 0;
  802. }
  803. static int nick_which(const char* nick, bool& is_jupe, bool& is_orig) {
  804. if (jupenick[0] && !rfc_casecmp(nick, jupenick)) {
  805. is_jupe = 1;
  806. // If some stupid reason they have the same jupenick/nick, make sure to mark it as on
  807. if (!rfc_casecmp(nick, origbotname))
  808. is_orig = 1;
  809. return 1;
  810. } else if (!rfc_casecmp(nick, origbotname)) {
  811. is_orig = 1;
  812. }
  813. return 0;
  814. }
  815. static void nick_available(bool is_jupe, bool is_orig) {
  816. if (jupenick[0] && is_jupe && !match_my_nick((jupenick))) {
  817. /* Ensure we aren't processing a QUIT/NICK and a MONITOR, or just some screw up */
  818. if (!tried_jupenick || ((now - tried_jupenick) > 2)) {
  819. tried_jupenick = now;
  820. dprintf(DP_MODE_NEXT, "NICK %s\n", jupenick);
  821. if (!jnick_juped)
  822. putlog(LOG_MISC, "*", "Switching back to jupenick '%s'", jupenick);
  823. }
  824. // Don't switch to the nick if already on jupenick
  825. } else if (is_orig && !match_my_nick(origbotname) && (!jupenick[0] || !match_my_nick(jupenick))) {
  826. if (!tried_nick || ((now - tried_nick) > 2)) {
  827. altnick_char = rolls = 0;
  828. tried_nick = now;
  829. dprintf(DP_MODE_NEXT, "NICK %s\n", origbotname);
  830. if (!nick_juped)
  831. putlog(LOG_MISC, "*", "Switching back to nick '%s'", origbotname);
  832. }
  833. }
  834. }
  835. void nicks_available(char* buf, char delim, bool buf_contains_available) {
  836. if (!buf[0] || !keepnick) return;
  837. bool is_jupe = 0, is_orig = 0;
  838. char *nick = NULL;
  839. if (delim) {
  840. while ((nick = newsplit(&buf, delim))[0]) {
  841. if (nick_which(nick, is_jupe, is_orig))
  842. break;
  843. }
  844. } else {
  845. nick = buf;
  846. nick_which(nick, is_jupe, is_orig);
  847. }
  848. if (buf_contains_available == 0) {
  849. is_jupe = !is_jupe;
  850. is_orig = !is_orig;
  851. }
  852. nick_available(is_jupe, is_orig);
  853. }
  854. void release_nick(const char* nick) {
  855. // Only do this if currently on a jupenick
  856. if (jupenick[0] && ((!nick && match_my_nick(jupenick)) || (nick && !rfc_casecmp(jupenick, nick)))) {
  857. keepnick = 0;
  858. release_time = now;
  859. if (match_my_nick(jupenick)) {
  860. altnick_char = rolls = 0;
  861. tried_nick = now;
  862. dprintf(DP_MODE, "NICK %s\n", origbotname);
  863. putlog(LOG_MISC, "*", "Releasing jupenick '%s' and switching back to nick '%s'", jupenick, origbotname);
  864. }
  865. } else if (!nick)
  866. putlog(LOG_CMDS, "*", "Not releasing nickname. (Not currently on a jupenick)");
  867. }
  868. /* This is a reply to MONITOR: Online
  869. * RPL_MONONLINE
  870. * :<server> 730 <nick|*> :nick!user@host[,nick!user@host]*
  871. */
  872. static void got730(char* from, char* msg)
  873. {
  874. char *tmp = newsplit(&msg);
  875. fixcolon(msg);
  876. if (tmp[0] && (!strcmp(tmp, "*") || match_my_nick(tmp))) {
  877. // If any of the nicks online match my jupenick or nick, unset nick_juped and jnick_juped
  878. // This will prevent the bot from thinking its nick is juped if someone else got it after a netsplit
  879. char *nick = NULL;
  880. while ((nick = newsplit(&msg, ','))[0]) {
  881. bool is_jupe = 0, is_orig = 0;
  882. nick_which(nick, is_jupe, is_orig);
  883. if (is_jupe && jnick_juped) jnick_juped = 0;
  884. else if (is_orig && nick_juped) nick_juped = 0;
  885. }
  886. }
  887. }
  888. /* This is a reply to MONITOR: Offline
  889. * RPL_MONOFFLINE
  890. * :<server> 731 <nick|*> :nick[,nick1]*
  891. */
  892. static void got731(char* from, char* msg)
  893. {
  894. char *tmp = newsplit(&msg);
  895. fixcolon(msg);
  896. //msg now contains the nick(s) available
  897. if (tmp[0] && (!strcmp(tmp, "*") || match_my_nick(tmp)))
  898. nicks_available(msg, ',');
  899. }
  900. /* This is a reply on ISON :<orig> [<jupenick>]
  901. */
  902. static void got303(char *from, char *msg)
  903. {
  904. char *tmp = newsplit(&msg);
  905. fixcolon(msg);
  906. if (tmp[0] && match_my_nick(tmp))
  907. nicks_available(msg, ' ', 0);
  908. }
  909. /* 432 : Bad nickname (RESV)
  910. */
  911. static int got432(char *from, char *msg)
  912. {
  913. char *erroneus = NULL;
  914. newsplit(&msg);
  915. erroneus = newsplit(&msg);
  916. bool is_jnick = 0, was_juped = 0;
  917. if (jupenick[0] && !strcmp(erroneus, jupenick)) {
  918. was_juped = jnick_juped;
  919. is_jnick = 1;
  920. jnick_juped = 1;
  921. } else {
  922. was_juped = nick_juped;
  923. nick_juped = 1;
  924. }
  925. if (server_online) {
  926. if (!was_juped)
  927. putlog(LOG_MISC, "*", "%sNICK IS INVALID: '%s' (keeping '%s').", is_jnick ? "JUPE" : "", erroneus, botname);
  928. } else {
  929. putlog(LOG_MISC, "*", "Server says my %snick '%s' is invalid.", is_jnick ? "jupe" : "", botname);
  930. if (jupenick[0] && !strcmp(botname, jupenick))
  931. strlcpy(botname, origbotname, sizeof(botname));
  932. else
  933. rotate_nick(botname, origbotname);
  934. dprintf(DP_MODE, "NICK %s\n", botname);
  935. return 0;
  936. }
  937. return 0;
  938. }
  939. /* 433 : Nickname in use
  940. * Change nicks till we're acceptable or we give up
  941. */
  942. static char rnick[NICKLEN] = "";
  943. static int got433(char *from, char *msg)
  944. {
  945. char *tmp = newsplit(&msg);
  946. /* We are online and have a nickname, we'll keep it */
  947. //Also make sure we're not juping the jupenick if we shouldn't be.
  948. //Prefer to be on the 'nick'(origbotname) at all times
  949. if (server_online) {
  950. tmp = newsplit(&msg);
  951. if (tried_jupenick)
  952. jnick_juped = 0;
  953. else if (!rfc_casecmp(tmp, origbotname))
  954. nick_juped = 0;
  955. tried_nick = 0;
  956. sdprintf("433: botname: %s tmp: %s rolls: %d altnick_char: %c tried_jupenick: %li", botname, tmp, rolls, altnick_char, (long)tried_jupenick);
  957. // Tried jupenick, but already on origbotname (or a rolled nick), stay on it.
  958. if (tried_jupenick && (match_my_nick(origbotname) || rolls || altnick_char != 0)) {
  959. putlog(LOG_MISC, "*", "%sNICK IN USE: '%s' (keeping '%s').", tried_jupenick ? "JUPE" : "", tmp, botname);
  960. } else { //Else need to find a new nick
  961. // Failed to get jupenick, not on origbotname now, try for origbotname and rotate from there
  962. if (tried_jupenick) {
  963. strlcpy(rnick, origbotname, sizeof(rnick));
  964. tried_jupenick = 0;
  965. } else {
  966. // Was a failed attempt at a rotated nick, keep rotating on origbotname
  967. strlcpy(rnick, tmp, NICKLEN);
  968. rotate_nick(rnick, origbotname);
  969. }
  970. // Make sure not trying to set the nick we're already on
  971. if (!match_my_nick(rnick)) {
  972. putlog(LOG_MISC, "*", "NICK IN USE: '%s' Trying '%s'", tmp, rnick);
  973. dprintf(DP_SERVER, "NICK %s\n", rnick);
  974. } else
  975. putlog(LOG_MISC, "*", "NICK IN USE: '%s' (keeping '%s')", tmp, botname);
  976. }
  977. } else {
  978. //Hack for different responses:
  979. //[@] efnet.demon.co.uk 433 * lolwut :Nickname is already in use.
  980. //[@] irc.efnet.no 433 lolwut :Nickname is already in use.
  981. if (tmp[0] == '*')
  982. tmp = newsplit(&msg);
  983. gotfake433(tmp);
  984. }
  985. return 0;
  986. }
  987. /* 437 : Channel/Nickname juped (IRCnet) (Also temp jupe during splits on efnet)
  988. * :ca.us.irc.xzibition.com 437 test1 bryan :Nick/channel is temporarily unavailable
  989. */
  990. static int got437(char *from, char *msg)
  991. {
  992. char *s = NULL;
  993. struct chanset_t *chan = NULL;
  994. newsplit(&msg);
  995. s = newsplit(&msg);
  996. if (s[0] && (strchr(CHANMETA, s[0]) != NULL)) {
  997. chan = findchan(s);
  998. if (chan) {
  999. chan->ircnet_status &= ~(CHAN_JOINING);
  1000. if (chan->ircnet_status & CHAN_ACTIVE) {
  1001. putlog(LOG_MISC, "*", "Can't change nickname on %s. Is my nickname banned?", s);
  1002. } else {
  1003. if (!channel_juped(chan)) {
  1004. putlog(LOG_MISC, "*", "Channel %s is juped. :(", s);
  1005. chan->ircnet_status |= CHAN_JUPED;
  1006. }
  1007. }
  1008. }
  1009. } else if (server_online) {
  1010. if (!rfc_casecmp(s, origbotname)) {
  1011. if (!nick_juped)
  1012. putlog(LOG_MISC, "*", "NICK IS TEMPORARILY UNAVAILABLE: '%s' (keeping '%s').", s, botname);
  1013. nick_juped = 2;
  1014. } else if (jupenick[0] && !rfc_casecmp(s, jupenick)) {
  1015. if (!jnick_juped)
  1016. putlog(LOG_MISC, "*", "JUPENICK IS TEMPORARILY UNAVAILABLE: '%s' (keeping '%s').", s, botname);
  1017. jnick_juped = 2;
  1018. }
  1019. } else {
  1020. putlog(LOG_MISC, "*", "%s: %s", "Nickname has been juped", s);
  1021. gotfake433(s);
  1022. }
  1023. return 0;
  1024. }
  1025. /* 438 : Nick change too fast
  1026. */
  1027. static int got438(char *from, char *msg)
  1028. {
  1029. newsplit(&msg);
  1030. newsplit(&msg);
  1031. fixcolon(msg);
  1032. putlog(LOG_MISC, "*", "%s", msg);
  1033. return 0;
  1034. }
  1035. static int got451(char *from, char *msg)
  1036. {
  1037. /* Usually if we get this then we really messed up somewhere
  1038. * or this is a non-standard server, so we log it and kill the socket
  1039. * hoping the next server will work :) -poptix
  1040. */
  1041. /* Um, this does occur on a lagged anti-spoof server connection if the
  1042. * (minutely) sending of joins occurs before the bot does its ping reply.
  1043. * Probably should do something about it some time - beldin
  1044. */
  1045. putlog(LOG_SERV, "*", "%s says I'm not registered, trying next one.", from);
  1046. nuke_server("The server says we are not registered yet..");
  1047. return 0;
  1048. }
  1049. /* Got error notice
  1050. */
  1051. static int goterror(char *from, char *msg)
  1052. {
  1053. if (msg[0] == ':')
  1054. msg++;
  1055. putlog(LOG_SERV, "*", "-ERROR from server- %s", msg);
  1056. putlog(LOG_SERV, "*", "Disconnecting from %s.", dcc[servidx].host);
  1057. nuke_server("Bah, stupid error messages.");
  1058. return 1;
  1059. }
  1060. /* Got nick change.
  1061. */
  1062. static int gotnick(char *from, char *msg)
  1063. {
  1064. char *nick = NULL, *buf = NULL, *buf_ptr = NULL;
  1065. struct userrec *u = NULL;
  1066. //Done to prevent gotnick in irc.mod getting a mangled from
  1067. buf = buf_ptr = strdup(from);
  1068. //Cache user
  1069. u = get_user_by_host(buf);
  1070. nick = splitnick(&buf);
  1071. fixcolon(msg);
  1072. if (match_my_nick(nick)) {
  1073. /* Regained nick! */
  1074. strlcpy(botname, msg, sizeof(botname));
  1075. tried_jupenick = 0;
  1076. tried_nick = 0;
  1077. if (jupenick[0] && !rfc_casecmp(msg, jupenick)) {
  1078. altnick_char = rolls = 0;
  1079. putlog(LOG_SERV | LOG_MISC, "*", "Regained jupenick '%s'.", msg);
  1080. jnick_juped = 0;
  1081. nick_juped = 0; // Unset this, no reason for it now.
  1082. } else if (!strcmp(msg, origbotname)) {
  1083. altnick_char = rolls = 0;
  1084. putlog(LOG_SERV | LOG_MISC, "*", "Regained nickname '%s'.", msg);
  1085. nick_juped = 0;
  1086. } else if ((keepnick || release_time) && strcmp(nick, msg)) {
  1087. // Was this an attempt at a nick thru rotation?
  1088. if (!strcmp(rnick, msg)) {
  1089. putlog(LOG_MISC, "*", "Nickname changed to '%s'", msg);
  1090. } else {
  1091. putlog(LOG_SERV | LOG_MISC, "*", "Nickname changed to '%s'???", msg);
  1092. nicks_available(nick);
  1093. }
  1094. } else
  1095. putlog(LOG_SERV | LOG_MISC, "*", "Nickname changed to '%s'???", msg);
  1096. } else if ((keepnick) && (rfc_casecmp(nick, msg))) { //Ignore case changes
  1097. nicks_available(nick);
  1098. }
  1099. free(buf_ptr);
  1100. return 0;
  1101. }
  1102. static int gotmode(char *from, char *msg)
  1103. {
  1104. char *ch = NULL, *buf = NULL, *buf_ptr = NULL;
  1105. buf_ptr = buf = strdup(msg);
  1106. ch = newsplit(&buf);
  1107. /* Usermode changes? */
  1108. if (strchr(CHANMETA, ch[0]) == NULL) {
  1109. if (match_my_nick(ch) && !strcmp(curnetwork, "IRCnet")) {
  1110. // Umode +r is restricted on IRCnet, can only chat.
  1111. fixcolon(buf);
  1112. if ((buf[0] == '+') && strchr(buf, 'r')) {
  1113. putlog(LOG_SERV, "*", "%s has me i-lined (jumping)", dcc[servidx].host);
  1114. nuke_server("i-lines suck");
  1115. }
  1116. }
  1117. }
  1118. free(buf_ptr);
  1119. return 0;
  1120. }
  1121. static void end_burstmode();
  1122. static void disconnect_server(int idx, int dolost)
  1123. {
  1124. if ((serv != dcc[idx].sock) && serv >= 0)
  1125. killsock(serv);
  1126. if (dcc[idx].sock >= 0)
  1127. killsock(dcc[idx].sock);
  1128. dcc[idx].sock = -1;
  1129. serv = -1;
  1130. servidx = -1;
  1131. server_online = 0;
  1132. use_monitor = 0;
  1133. cookies_disabled = false;
  1134. floodless = 0;
  1135. botuserhost[0] = 0;
  1136. botuserip[0] = 0;
  1137. use_penalties = 0;
  1138. use_354 = 0;
  1139. deaf_char = 0;
  1140. in_deaf = 0;
  1141. callerid_char = 0;
  1142. in_callerid = 0;
  1143. use_exempts = 0;
  1144. use_invites = 0;
  1145. have_cprivmsg = 0;
  1146. have_cnotice = 0;
  1147. use_flood_count = 0;
  1148. modesperline = 0;
  1149. if (dolost) {
  1150. Auth::DeleteAll();
  1151. trying_server = 0;
  1152. lostdcc(idx);
  1153. }
  1154. end_burstmode();
  1155. reset_chans = 0;
  1156. keepnick = 1;
  1157. /* Invalidate the cmd_swhois cache callback data */
  1158. for (int i = 0; i < dcc_total; i++) {
  1159. if (dcc[i].type && dcc[i].whois[0]) {
  1160. dcc[i].whois[0] = 0;
  1161. dcc[i].whowas = 0;
  1162. }
  1163. }
  1164. if (!segfaulted) //Avoid if crashed, too many free()/malloc() in here
  1165. for (struct chanset_t *chan = chanset; chan; chan = chan->next)
  1166. clear_channel(chan, 1);
  1167. }
  1168. static void eof_server(int idx)
  1169. {
  1170. putlog(LOG_SERV, "*", "Disconnected from %s (EOF)", dcc[idx].host);
  1171. disconnect_server(idx, DO_LOST);
  1172. }
  1173. static void display_server(int idx, char *buf, size_t bufsiz)
  1174. {
  1175. simple_snprintf(buf, bufsiz, "%s (lag: %d)", trying_server ? "conn" : "serv", server_lag);
  1176. }
  1177. static void connect_server(void);
  1178. static void kill_server(int idx, void *x)
  1179. {
  1180. disconnect_server(idx, NO_LOST); /* eof_server will lostdcc() it. */
  1181. /* A new server connection will be automatically initiated in
  1182. about 2 seconds. */
  1183. }
  1184. static void timeout_server(int idx)
  1185. {
  1186. putlog(LOG_SERV, "*", "Timeout: connect to %s", dcc[idx].host);
  1187. disconnect_server(idx, DO_LOST);
  1188. }
  1189. static void server_activity(int, char *, int);
  1190. struct dcc_table SERVER_SOCKET =
  1191. {
  1192. "SERVER",
  1193. 0,
  1194. eof_server,
  1195. server_activity,
  1196. NULL,
  1197. timeout_server,
  1198. display_server,
  1199. kill_server,
  1200. NULL,
  1201. NULL
  1202. };
  1203. static void server_activity(int idx, char *msg, int len)
  1204. {
  1205. char *from = NULL, *code = NULL;
  1206. if (unlikely(trying_server)) {
  1207. strlcpy(dcc[idx].nick, "(server)", sizeof(dcc[idx].nick));
  1208. putlog(LOG_SERV, "*", "Connected to %s", dcc[idx].host);
  1209. trying_server = 0;
  1210. /*
  1211. servidx = idx;
  1212. serv = dcc[idx].sosck;
  1213. */
  1214. SERVER_SOCKET.timeout_val = 0;
  1215. // Setup timer for conecting
  1216. waiting_for_awake = 1;
  1217. lastpingtime = now - (stoned_timeout - 30); //30 seconds to reach 001
  1218. } else if (server_online) // Only set once 001 has been received
  1219. waiting_for_awake = 0;
  1220. if (msg[0] == ':') {
  1221. msg++;
  1222. from = newsplit(&msg);
  1223. } else
  1224. from = "";
  1225. code = newsplit(&msg);
  1226. if (!strcmp(code, "PRIVMSG") || !strcmp(code, "NOTICE")) {
  1227. if (!match_ignore(from))
  1228. putlog(LOG_RAW, "@", "[@] %s %s %s", from, code, msg);
  1229. } else
  1230. putlog(LOG_RAW, "@", "[@] %s %s %s", from, code, msg);
  1231. /* This has GOT to go into the raw binding table, * merely because this
  1232. * is less effecient.
  1233. */
  1234. check_bind_raw(from, code, msg);
  1235. }
  1236. static int gotping(char *from, char *msg)
  1237. {
  1238. fixcolon(msg);
  1239. dprintf(DP_MODE, "PONG :%s\n", msg);
  1240. return 0;
  1241. }
  1242. static int gotkick(char *from, char *msg)
  1243. {
  1244. if (!match_my_nick(from))
  1245. /* Not my kick, I don't need to bother about it. */
  1246. return 0;
  1247. if (use_penalties) {
  1248. last_time.sec += 2;
  1249. if (debug_output)
  1250. putlog(LOG_SRVOUT, "*", "adding 2secs penalty (successful kick)");
  1251. }
  1252. return 0;
  1253. }
  1254. /* Another sec penalty if bot did a whois on another server.
  1255. */
  1256. static int got318_369(char *, char *, int);
  1257. static int whoispenalty(char *from, char *msg)
  1258. {
  1259. struct server_list *x = serverlist;
  1260. if (x && use_penalties) {
  1261. int i = 0, ii = 0;
  1262. for (; x; x = x->next) {
  1263. if (i == curserv) {
  1264. if (strcmp(x->name, from))
  1265. ii = 1;
  1266. }
  1267. i++;
  1268. }
  1269. if (ii) {
  1270. last_time.sec += 1;
  1271. if (debug_output)
  1272. putlog(LOG_SRVOUT, "*", "adding 1sec penalty (remote whois)");
  1273. }
  1274. }
  1275. got318_369(from, msg, 0);
  1276. return 0;
  1277. }
  1278. static void irc_whois(char *, const char*, const char *, ...) __attribute__((format(printf, 3, 4)));
  1279. static void
  1280. irc_whois(char *nick, const char* prefix, const char *format, ...)
  1281. {
  1282. char va_out[2001] = "";
  1283. va_list va;
  1284. va_start(va, format);
  1285. egg_vsnprintf(va_out, sizeof(va_out) - 1, format, va);
  1286. va_end(va);
  1287. for (int idx = 0; idx < dcc_total; idx++) {
  1288. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(nick, dcc[idx].whois)) {
  1289. if (prefix)
  1290. dumplots(idx, prefix, va_out);
  1291. else
  1292. dprintf(idx, "%s\n", va_out);
  1293. break;
  1294. }
  1295. }
  1296. }
  1297. void check_hostmask()
  1298. {
  1299. if (!server_online || !botuserhost[0])
  1300. return;
  1301. char s[UHOSTLEN + 2] = "";
  1302. simple_snprintf(s, sizeof(s), "*!%s", botuserhost); /* just add actual user@ident, regardless of ~ */
  1303. /* dont add the host if it conflicts with another in the userlist */
  1304. struct userrec *u = NULL;
  1305. if ((u = host_conflicts(s))) {
  1306. if (u != conf.bot->u) {
  1307. putlog(LOG_WARN, "*", "My automatic hostmask '%s' would conflict with user: '%s'. (Not adding)", s, u->handle);
  1308. } else
  1309. sdprintf("Already have hostmask '%s' added for myself", s);
  1310. return;
  1311. }
  1312. addhost_by_handle(conf.bot->nick, s);
  1313. putlog(LOG_GETIN, "*", "Updated my hostmask: %s", s);
  1314. }
  1315. /* 311 $me nick username address * :realname */
  1316. static int got311(char *from, char *msg)
  1317. {
  1318. char *nick = NULL, *username = NULL, *address = NULL, uhost[UHOSTLEN + 1];
  1319. struct userrec *u = NULL;
  1320. newsplit(&msg);
  1321. nick = newsplit(&msg);
  1322. username = newsplit(&msg);
  1323. address = newsplit(&msg);
  1324. newsplit(&msg);
  1325. fixcolon(msg);
  1326. if (match_my_nick(nick)) {
  1327. simple_snprintf(botuserhost, sizeof botuserhost, "%s@%s", username, address);
  1328. check_hostmask();
  1329. }
  1330. irc_whois(nick, NULL, "$b%s$b [%s@%s]", nick, username, address);
  1331. // FIXME: This should incorporate userip and member/client lookups - and should ACT on discovering a user.
  1332. // FIXME: This should also use whois_actually to cache userip
  1333. simple_snprintf(uhost, sizeof uhost, "%s!%s@%s", nick, username, address);
  1334. if ((u = get_user_by_host(uhost))) {
  1335. int idx = 0;
  1336. for (idx = 0; idx < dcc_total; idx++) {
  1337. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(nick, dcc[idx].whois)) {
  1338. if (whois_access(dcc[idx].user, u)) {
  1339. irc_whois(nick, " username : ", "$u%s$u", u->handle);
  1340. }
  1341. break;
  1342. }
  1343. }
  1344. }
  1345. irc_whois(nick, " ircname : ", "%s", msg);
  1346. return 0;
  1347. }
  1348. static char *
  1349. hide_chans(const char *nick, struct userrec *u, char *_channels, bool publicOnly)
  1350. {
  1351. char *channels = strdup(_channels), *channels_p = channels;
  1352. char *chname = NULL;
  1353. size_t len = strlen(channels) + 100 + 1;
  1354. struct chanset_t *chan = NULL;
  1355. struct flag_record fr = { FR_CHAN | FR_GLOBAL, 0, 0, 0 };
  1356. char *chans = (char *) my_calloc(1, len), *p = NULL;
  1357. while ((chname = newsplit(&channels))[0]) {
  1358. /* skip any modes in front of #chan */
  1359. if (!(p = strchr(chname, '#')))
  1360. if (!(p = strchr(chname, '&')))
  1361. if (!(p = strchr(chname, '!')))
  1362. continue;
  1363. chan = findchan(p);
  1364. if (chan && !publicOnly)
  1365. get_user_flagrec(u, &fr, chan->dname, chan);
  1366. if (!chan ||
  1367. (!publicOnly && (
  1368. getnick(u->handle, chan)[0] ||
  1369. !(channel_hidden(chan)) ||
  1370. chk_op(fr, chan)
  1371. )) ||
  1372. (publicOnly && !(channel_hidden(chan)))
  1373. ) {
  1374. if (chans[0])
  1375. strlcat(chans, " ", len);
  1376. strlcat(chans, chname, len);
  1377. }
  1378. }
  1379. free(channels_p);
  1380. return chans;
  1381. }
  1382. /* 319 $me nick :channels */
  1383. static int got319(char *from, char *msg)
  1384. {
  1385. char *nick = NULL;
  1386. newsplit(&msg);
  1387. nick = newsplit(&msg);
  1388. fixcolon(msg);
  1389. for (int idx = 0; idx < dcc_total; idx++) {
  1390. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(nick, dcc[idx].whois)) {
  1391. char *chans = NULL;
  1392. /* Show every channel the user has access to view */
  1393. if ((chans = hide_chans(nick, dcc[idx].user, msg, 0))) {
  1394. irc_whois(nick, " channels : ", "%s", chans);
  1395. free(chans);
  1396. }
  1397. chans = NULL;
  1398. /* Show all of the 'public' channels */
  1399. if ((chans = hide_chans(nick, dcc[idx].user, msg, 1))) {
  1400. irc_whois(nick, " public : ", "%s", chans);
  1401. free(chans);
  1402. }
  1403. break;
  1404. }
  1405. }
  1406. return 0;
  1407. }
  1408. /* 312 $me nick server :text */
  1409. static int got312(char *from, char *msg)
  1410. {
  1411. char *nick = NULL, *server = NULL;
  1412. newsplit(&msg);
  1413. nick = newsplit(&msg);
  1414. server = newsplit(&msg);
  1415. fixcolon(msg);
  1416. irc_whois(nick, " server : ", "%s [%s]", server, msg);
  1417. return 0;
  1418. }
  1419. /* 301 $me nick :away msg */
  1420. static int got301(char *from, char *msg)
  1421. {
  1422. char *nick = NULL;
  1423. newsplit(&msg);
  1424. nick = newsplit(&msg);
  1425. fixcolon(msg);
  1426. irc_whois(nick, " away : ", "%s", msg);
  1427. return 0;
  1428. }
  1429. /* got 302: userhost
  1430. * <server> 302 <to> :<nick??user@host>
  1431. */
  1432. static int got302(char *from, char *msg)
  1433. {
  1434. char *p = NULL, *nick = NULL, *uhost = NULL;
  1435. newsplit(&msg);
  1436. fixcolon(msg);
  1437. p = strchr(msg, '=');
  1438. if (!p)
  1439. p = strchr(msg, '*');
  1440. if (!p)
  1441. return 0;
  1442. *p = 0;
  1443. nick = msg;
  1444. p += 2; /* skip =|* plus the next char */
  1445. uhost = p;
  1446. if ((p = strchr(uhost, ' ')))
  1447. *p = 0;
  1448. if (match_my_nick(nick)) {
  1449. strlcpy(botuserip, uhost, UHOSTLEN);
  1450. sdprintf("botuserip: %s", botuserip);
  1451. return 0;
  1452. }
  1453. return 0;
  1454. }
  1455. /* 313 $me nick :server text */
  1456. static int got313(char *from, char *msg)
  1457. {
  1458. char *nick = NULL;
  1459. newsplit(&msg);
  1460. nick = newsplit(&msg);
  1461. fixcolon(msg);
  1462. irc_whois(nick, " : ", "$b%s$b", msg);
  1463. return 0;
  1464. }
  1465. /* 317 $me nick idle signon :idle-eng signon-eng */
  1466. static int got317(char *from, char *msg)
  1467. {
  1468. char *nick = NULL, date[50] = "";
  1469. time_t idle, signon;
  1470. int mydays, myhours, mymins, mysecs;
  1471. newsplit(&msg);
  1472. nick = newsplit(&msg);
  1473. idle = atol(newsplit(&msg));
  1474. signon = atol(newsplit(&msg));
  1475. fixcolon(msg);
  1476. strftime(date, sizeof date, "%c %Z", gmtime(&signon));
  1477. mydays = idle / 86400;
  1478. idle = idle % 86400;
  1479. myhours = idle / 3600;
  1480. idle = idle % 3600;
  1481. mymins = idle / 60;
  1482. idle = idle % 60;
  1483. mysecs = idle;
  1484. irc_whois(nick, " idle : " , "%d days %d hours %d mins %d secs [signon: %s]", mydays, myhours, mymins, mysecs, date);
  1485. return 0;
  1486. }
  1487. static int got369(char *from, char *msg)
  1488. {
  1489. return got318_369(from, msg, 1);
  1490. }
  1491. /* 318/319 $me nick :End of /? */
  1492. static int got318_369(char *from, char *msg, int whowas)
  1493. {
  1494. char *nick = NULL;
  1495. newsplit(&msg);
  1496. nick = newsplit(&msg);
  1497. fixcolon(msg);
  1498. irc_whois(nick, NULL, "%s", msg);
  1499. for (int idx = 0; idx < dcc_total; idx++) {
  1500. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(dcc[idx].whois, nick) &&
  1501. ((!whowas && !dcc[idx].whowas) || (whowas && dcc[idx].whowas))) {
  1502. dcc[idx].whois[0] = 0;
  1503. dcc[idx].whowas = 0;
  1504. }
  1505. }
  1506. return 0;
  1507. }
  1508. /* 401 $me nick :text */
  1509. static int got401(char *from, char *msg)
  1510. {
  1511. char *nick = NULL;
  1512. newsplit(&msg);
  1513. nick = newsplit(&msg);
  1514. fixcolon(msg);
  1515. irc_whois(nick, NULL, "%s", msg);
  1516. for (int idx = 0; idx < dcc_total; idx++)
  1517. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(dcc[idx].whois, nick))
  1518. dcc[idx].whowas = 1;
  1519. dprintf(DP_SERVER, "WHOWAS %s %s\n", nick, from);
  1520. return 0;
  1521. }
  1522. /* 406 $me nick :text */
  1523. static int got406(char *from, char *msg)
  1524. {
  1525. char *nick = NULL;
  1526. newsplit(&msg);
  1527. nick = newsplit(&msg);
  1528. fixcolon(msg);
  1529. irc_whois(nick, NULL, "%s", msg);
  1530. return 0;
  1531. }
  1532. /* 465 info_ :You are banned from this server- Temporary K-line 1 min. - Testing kline notices (2008/4/3 09.51) */
  1533. static int got465(char *from, char *msg)
  1534. {
  1535. newsplit(&msg); /* nick */
  1536. fixcolon(msg);
  1537. putlog(LOG_SERV, "*", "I am klined: %s", msg);
  1538. putlog(LOG_SERV, "*", "Disconnecting from %s.", dcc[servidx].host);
  1539. nuke_server("I am klined!");
  1540. return 1;
  1541. }
  1542. /* 718 $me nick user@host :msg
  1543. * for receiving a msg while +g
  1544. * csircd: :irc.nac.net 718 bryand_ bryand_ bryan bryan@shatow.net :is messaging you, but you have CALLERID enabled (umode +g)
  1545. * ratbox: :irc.servercentral.net 718 bryand_ bryan bryan@shatow.net :is messaging you, and you have umode +g.
  1546. * hybrid: :irc.swepipe.se 718 jjdjff bryan[bryan@shatow.net] :is messaging you, and you are umode +g.
  1547. */
  1548. static int got718(char *from, char *msg)
  1549. {
  1550. char *nick = NULL, *uhost = NULL;
  1551. newsplit(&msg);
  1552. nick = newsplit(&msg);
  1553. if (match_my_nick(nick)) // CSIRCD is stupid.
  1554. nick = newsplit(&msg);
  1555. else if (msg[0] == ':') { // Hybrid is stupid too.
  1556. char *p = strchr(nick, '['), *p2 = NULL;
  1557. if (p) {
  1558. *p++ = 0;
  1559. if ((p2 = strchr(p, ']'))) {
  1560. uhost = p;
  1561. *p2 = 0;
  1562. }
  1563. }
  1564. }
  1565. if (!uhost)
  1566. uhost = newsplit(&msg);
  1567. fixcolon(msg);
  1568. if (ischanhub()) {
  1569. char s[UHOSTLEN + 2] = "";
  1570. struct userrec *u = NULL;
  1571. simple_snprintf(s, sizeof(s), "%s!%s", nick, uhost);
  1572. u = get_user_by_host(s);
  1573. if (u) {
  1574. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  1575. get_user_flagrec(u, &fr, NULL);
  1576. if (glob_op(fr) || chan_op(fr) || glob_voice(fr) || chan_voice(fr)) {
  1577. putlog(LOG_WALL, "*", "(CALLERID) !%s! (%s!%s) %s (Accepting user)", u->handle, nick, uhost, msg);
  1578. dprintf(DP_HELP, "ACCEPT %s\n", nick);
  1579. dprintf(DP_HELP, "PRIVMSG %s :You have been accepted. Please send your message again.\n", nick);
  1580. } else {
  1581. putlog(LOG_WALL, "*", "(CALLERID) !%s! (%s!%s) %s (User is not +o or +v)", u->handle, nick, uhost, msg);
  1582. }
  1583. } else {
  1584. putlog(LOG_WALL, "*", "(CALLERID) !*! (%s!%s) %s (User unknown)", nick, uhost, msg);
  1585. }
  1586. } else {
  1587. putlog(LOG_WALL, "*", "(CALLERID) (%s!%s) %s (I'm not a chathub (+c))", nick, uhost, msg);
  1588. }
  1589. return 0;
  1590. }
  1591. static cmd_t my_raw_binds[] =
  1592. {
  1593. {"PRIVMSG", "", (Function) gotmsg, NULL, LEAF},
  1594. {"NOTICE", "", (Function) gotnotice, NULL, LEAF},
  1595. {"MODE", "", (Function) gotmode, NULL, LEAF},
  1596. {"PING", "", (Function) gotping, NULL, LEAF},
  1597. {"PONG", "", (Function) gotpong, NULL, LEAF},
  1598. {"WALLOPS", "", (Function) gotwall, NULL, LEAF},
  1599. {"001", "", (Function) got001, NULL, LEAF},
  1600. {"004", "", (Function) got004, NULL, LEAF},
  1601. {"005", "", (Function) got005, NULL, LEAF},
  1602. {"302", "", (Function) got302, NULL, LEAF},
  1603. {"303", "", (Function) got303, NULL, LEAF},
  1604. {"432", "", (Function) got432, NULL, LEAF},
  1605. {"433", "", (Function) got433, NULL, LEAF},
  1606. {"437", "", (Function) got437, NULL, LEAF},
  1607. {"438", "", (Function) got438, NULL, LEAF},
  1608. {"451", "", (Function) got451, NULL, LEAF},
  1609. {"442", "", (Function) got442, NULL, LEAF},
  1610. {"NICK", "", (Function) gotnick, NULL, LEAF},
  1611. {"ERROR", "", (Function) goterror, NULL, LEAF},
  1612. /* ircu2.10.10 has a bug when a client is throttled ERROR is sent wrong */
  1613. {"ERROR:", "", (Function) goterror, NULL, LEAF},
  1614. {"KICK", "", (Function) gotkick, NULL, LEAF},
  1615. /* WHOIS RAWS */
  1616. {"311", "", (Function) got311, NULL, LEAF}, /* ident host * :realname */
  1617. {"314", "", (Function) got311, NULL, LEAF}, /* "" -WHOWAS */
  1618. {"319", "", (Function) got319, NULL, LEAF}, /* :#channels */
  1619. {"312", "", (Function) got312, NULL, LEAF}, /* server :gecos */
  1620. {"301", "", (Function) got301, NULL, LEAF}, /* :away msg */
  1621. {"313", "", (Function) got313, NULL, LEAF}, /* :ircop */
  1622. {"317", "", (Function) got317, NULL, LEAF}, /* idle, signon :idle-eng, signon-eng */
  1623. {"401", "", (Function) got401, NULL, LEAF},
  1624. {"406", "", (Function) got406, NULL, LEAF},
  1625. {"465", "", (Function) got465, NULL, LEAF}, /* RPL_YOUREBANNEDCREEP */
  1626. {"318", "", (Function) whoispenalty, NULL, LEAF}, /* :End of /WHOIS */
  1627. {"369", "", (Function) got369, NULL, LEAF}, /* :End of /WHOWAS */
  1628. {"718", "", (Function) got718, NULL, LEAF},
  1629. {"730", "", (Function) got730, NULL, LEAF}, /* RPL_MONONLINE */
  1630. {"731", "", (Function) got731, NULL, LEAF}, /* RPL_MONOFFLINE */
  1631. {NULL, NULL, NULL, NULL, 0}
  1632. };
  1633. static void server_dns_callback(int, void *, const char *, bd::Array<bd::String>);
  1634. /* Hook up to a server
  1635. */
  1636. static void connect_server(void)
  1637. {
  1638. char pass[121] = "", botserver[UHOSTLEN] = "";
  1639. int newidx;
  1640. port_t botserverport = 0;
  1641. waiting_for_awake = 0;
  1642. /* trying_server = now; */
  1643. empty_msgq();
  1644. if (newserverport) { /* cmd_jump was used; connect specified server */
  1645. curserv = -1; /* Reset server list */
  1646. strlcpy(botserver, newserver, sizeof(botserver));
  1647. botserverport = newserverport;
  1648. strlcpy(pass, newserverpass, sizeof(pass));
  1649. newserver[0] = newserverport = newserverpass[0] = 0;
  1650. }
  1651. if (!cycle_time) {
  1652. struct chanset_t *chan = NULL;
  1653. struct server_list *x = serverlist;
  1654. if (!x)
  1655. return;
  1656. trying_server = now;
  1657. newidx = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  1658. if (newidx < 0) {
  1659. putlog(LOG_SERV, "*", "NO MORE DCC CONNECTIONS -- Can't create server connection.");
  1660. trying_server = 0;
  1661. return;
  1662. }
  1663. next_server(&curserv, botserver, &botserverport, pass);
  1664. putlog(LOG_SERV, "*", "Trying server %s:%d", botserver, botserverport);
  1665. dcc[newidx].port = botserverport;
  1666. strlcpy(dcc[newidx].nick, "(server)", sizeof(dcc[newidx].nick));
  1667. strlcpy(dcc[newidx].host, botserver, sizeof(dcc[newidx].host));
  1668. botuserhost[0] = 0;
  1669. nick_juped = 0;
  1670. jnick_juped = 0;
  1671. tried_jupenick = 0;
  1672. tried_nick = 0;
  1673. altnick_char = rolls = 0;
  1674. use_monitor = 0;
  1675. include_lk = 1;
  1676. for (chan = chanset; chan; chan = chan->next)
  1677. chan->ircnet_status &= ~CHAN_JUPED;
  1678. dcc[newidx].timeval = now;
  1679. dcc[newidx].sock = -1;
  1680. dcc[newidx].u.dns->cbuf = strdup(pass);
  1681. cycle_time = 15; /* wait 15 seconds before attempting next server connect */
  1682. /* I'm resolving... don't start another server connect request */
  1683. resolvserv = 1;
  1684. /* Resolve the hostname. */
  1685. int dns_id = egg_dns_lookup(botserver, 20, server_dns_callback, (void *) (long) newidx, conf.bot->net.v6 ? DNS_LOOKUP_AAAA : DNS_LOOKUP_A);
  1686. if (dns_id >= 0)
  1687. dcc[newidx].dns_id = dns_id;
  1688. /* wait for async reply */
  1689. }
  1690. }
  1691. static void server_dns_callback(int id, void *client_data, const char *host, bd::Array<bd::String> ips)
  1692. {
  1693. //64bit hacks
  1694. long data = (long) client_data;
  1695. int idx = (int) data;
  1696. Context;
  1697. resolvserv = 0;
  1698. if (!valid_dns_id(idx, id))
  1699. return;
  1700. if (!ips.size()) {
  1701. putlog(LOG_SERV, "*", "Failed connect to %s (DNS lookup failed)", host);
  1702. trying_server = 0;
  1703. lostdcc(idx);
  1704. return;
  1705. }
  1706. my_addr_t addr;
  1707. char *ip = NULL;
  1708. const char* dns_type = NULL;
  1709. bd::String ip_from_dns;
  1710. #ifdef USE_IPV6
  1711. /* If IPv6 is wanted, ensure we are connecting to an IPv6 server, otherwise skip it */
  1712. if (conf.bot->net.v6) {
  1713. ip_from_dns = dns_find_ip(ips, AF_INET6);
  1714. if (!ip_from_dns.length()) {
  1715. dns_type = "IPv6";
  1716. goto fatal_dns;
  1717. }
  1718. } else
  1719. #endif /* USE_IPV6 */
  1720. {
  1721. /* If IPv4 is wanted, don't connect to IPv6! */
  1722. ip_from_dns = dns_find_ip(ips, AF_INET);
  1723. if (!ip_from_dns.length()) {
  1724. dns_type = "IPv4";
  1725. goto fatal_dns;
  1726. }
  1727. }
  1728. ip = ip_from_dns.dup();
  1729. get_addr(ip, &addr);
  1730. if (addr.family == AF_INET)
  1731. dcc[idx].addr = htonl(addr.u.addr.s_addr);
  1732. strlcpy(serverpass, (char *) dcc[idx].u.dns->cbuf, sizeof(serverpass));
  1733. changeover_dcc(idx, &SERVER_SOCKET, 0);
  1734. //No proxy, use identd, 2 = spoof ident
  1735. serv = open_telnet(ip, dcc[idx].port, 0, 2);
  1736. if (serv < 0) {
  1737. putlog(LOG_SERV, "*", "Failed connect to %s (%s)", dcc[idx].host, strerror(errno));
  1738. trying_server = 0;
  1739. lostdcc(idx);
  1740. } else {
  1741. int i = 1;
  1742. /* set these now so if we fail disconnect_server() can cleanup right. */
  1743. dcc[idx].sock = serv;
  1744. servidx = idx;
  1745. sdprintf("Connecting to '%s' (serv: %d, servidx: %d)", dcc[idx].host, serv, servidx);
  1746. setsockopt(serv, 6, TCP_NODELAY, &i, sizeof(int));
  1747. /* Queue standard login */
  1748. dcc[idx].timeval = now;
  1749. SERVER_SOCKET.timeout_val = &server_timeout;
  1750. /* Another server may have truncated it, so use the original */
  1751. if (jupenick[0])
  1752. strlcpy(botname, jupenick, sizeof(botname));
  1753. else
  1754. strlcpy(botname, origbotname, sizeof(botname));
  1755. /* Start alternate nicks from the beginning */
  1756. altnick_char = rolls = 0;
  1757. /* reset counter so first ctcp is dumped for tcms */
  1758. first_ctcp_check = 0;
  1759. // Just connecting, set last queue time to the past.
  1760. flood_time.sec = last_time.sec = now - 100;
  1761. flood_time.usec = last_time.usec = 0;
  1762. end_burstmode();
  1763. use_flood_count = 0;
  1764. real_msgburst = msgburst;
  1765. real_msgrate = msgrate;
  1766. if (serverpass[0])
  1767. dprintf(DP_MODE, "PASS %s\n", serverpass);
  1768. dprintf(DP_MODE, "NICK %s\n", botname);
  1769. dprintf(DP_MODE, "USER %s localhost %s :%s\n", botuser, dcc[idx].host, replace_vars(botrealname));
  1770. /* Wait for async result now */
  1771. }
  1772. delete[] ip;
  1773. return;
  1774. fatal_dns:
  1775. putlog(LOG_SERV, "*", "Failed connect to %s (Could not DNS as %s)", host, dns_type);
  1776. trying_server = 0;
  1777. lostdcc(idx);
  1778. return;
  1779. }