servmsg.c 61 KB

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