servmsg.c 60 KB

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