servmsg.c 58 KB

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