servmsg.cc 63 KB

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