servmsg.c 54 KB

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