servmsg.c 61 KB

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