servmsg.c 55 KB

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