servmsg.cc 63 KB

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