servmsg.c 63 KB

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