servmsg.c 55 KB

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