servmsg.c 61 KB

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