servmsg.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. /*
  2. * servmsg.c -- part of server.mod
  3. *
  4. */
  5. #include <netinet/tcp.h>
  6. char cursrvname[120] = "";
  7. char curnetwork[120] = "";
  8. static time_t last_ctcp = (time_t) 0L;
  9. static int count_ctcp = 0;
  10. static char altnick_char = 0;
  11. static unsigned int rolls = 0;
  12. #define ALTCHARS "-_\\`^[]"
  13. #define ROLL_RIGHT
  14. #undef ROLL_LEFT
  15. static int gotfake433(char *from)
  16. {
  17. size_t len = strlen(botname);
  18. int use_chr = 1;
  19. /* First run? */
  20. if (altnick_char == 0 && !rolls) {
  21. altnick_char = ALTCHARS[0];
  22. /* the nick is already as long as it can be. */
  23. if (len == (unsigned) nick_len) {
  24. /* make the last char the current altnick_char */
  25. botname[len - 1] = altnick_char;
  26. } else {
  27. /* tack it on to the end */
  28. botname[len] = altnick_char;
  29. botname[len + 1] = 0;
  30. }
  31. } else {
  32. char *p = NULL;
  33. if ((p = strchr(ALTCHARS, altnick_char)))
  34. p++;
  35. /* if we haven't been rolling, use the ALTCHARS
  36. */
  37. if (!rolls && p && *p) {
  38. altnick_char = *p;
  39. /* if we've already rolled, just keep rolling until we've rolled completely
  40. * after that, just generate random chars
  41. */
  42. } else if (rolls < len) {
  43. /* fun BX style rolling, WEEEE */
  44. char tmp = 0;
  45. size_t i = 0;
  46. altnick_char = 0;
  47. use_chr = 0;
  48. if (rolls == 0) {
  49. strcpy(botname, origbotname);
  50. len = strlen(botname);
  51. }
  52. #ifdef ROLL_RIGHT
  53. tmp = botname[len - 1];
  54. #endif /* ROLL_RIGHT */
  55. #ifdef ROLL_LEFT
  56. tmp = botname[0];
  57. #endif /* ROLL_LEFT */
  58. if (strchr(BADHANDCHARS, tmp))
  59. tmp = '_';
  60. rolls++;
  61. #ifdef ROLL_RIGHT
  62. for (i = (len - 1); i > 0; i--)
  63. botname[i] = botname[i - 1];
  64. botname[0] = tmp;
  65. #endif /* ROLL_RIGHT */
  66. #ifdef ROLL_LEFT
  67. for (i = 0; i < (len - 1); i++)
  68. botname[i] = botname[i + 1];
  69. botname[len - 1] = tmp;
  70. #endif /* ROLL_LEFT */
  71. botname[len] = 0;
  72. } else {
  73. /* we've tried ALTCHARS, and rolled the nick, just use random chars now */
  74. altnick_char = 'a' + randint(26);
  75. }
  76. if (use_chr && altnick_char) {
  77. botname[len - 1] = altnick_char;
  78. botname[len] = 0;
  79. }
  80. }
  81. putlog(LOG_SERV, "*", "NICK IN USE: Trying '%s'", botname);
  82. dprintf(DP_SERVER, "NICK %s\n", botname);
  83. return 0;
  84. }
  85. /* Check for tcl-bound msg command, return 1 if found
  86. *
  87. * msg: proc-name <nick> <user@host> <handle> <args...>
  88. */
  89. static void check_bind_msg(const char *cmd, char *nick, char *uhost, struct userrec *u, char *args)
  90. {
  91. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  92. int x;
  93. get_user_flagrec(u, &fr, NULL);
  94. x = check_bind(BT_msg, cmd, &fr, nick, uhost, u, args);
  95. if (x & BIND_RET_LOG)
  96. putlog(LOG_CMDS, "*", "(%s!%s) !%s! %s %s", nick, uhost, u ? u->handle : "*" , cmd, args);
  97. else if (x == 0)
  98. putlog(LOG_MSGS, "*", "[%s!%s] %s %s", nick, uhost, cmd, args);
  99. }
  100. /* Return 1 if processed.
  101. */
  102. static int check_bind_raw(char *from, char *code, char *msg)
  103. {
  104. char *p1 = NULL, *p2 = NULL, *myfrom = NULL, *mymsg = NULL;
  105. int ret = 0;
  106. myfrom = p1 = strdup(from);
  107. mymsg = p2 = strdup(msg);
  108. ret = check_bind(BT_raw, code, NULL, myfrom, mymsg);
  109. free(p1);
  110. free(p2);
  111. return ret;
  112. }
  113. int check_bind_ctcpr(char *nick, char *uhost, struct userrec *u,
  114. char *dest, char *keyword, char *args,
  115. bind_table_t *table)
  116. {
  117. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  118. get_user_flagrec(u, &fr, NULL);
  119. return check_bind(table, keyword, &fr, nick, uhost, u, dest, keyword, args);
  120. }
  121. bool match_my_nick(char *nick)
  122. {
  123. return (!rfc_casecmp(nick, botname));
  124. }
  125. void rehash_server(const char *servname, const char *nick)
  126. {
  127. server_online = now;
  128. altnick_char = 0;
  129. strlcpy(cursrvname, servname, sizeof(cursrvname));
  130. if (servidx >= 0)
  131. curservport = dcc[servidx].port;
  132. if (nick && nick[0]) {
  133. strlcpy(botname, nick, NICKLEN);
  134. dprintf(DP_SERVER, "WHOIS %s\n", botname); /* get user@host */
  135. dprintf(DP_SERVER, "USERHOST %s\n", botname); /* get user@ip */
  136. dprintf(DP_SERVER, "MODE %s +iws\n", botname);
  137. }
  138. }
  139. void
  140. join_chans()
  141. {
  142. for (register struct chanset_t *chan = chanset; chan; chan = chan->next) {
  143. chan->status &= ~(CHAN_ACTIVE | CHAN_PEND | CHAN_JOINING);
  144. if (shouldjoin(chan)) {
  145. dprintf(DP_SERVER, "JOIN %s %s\n", (chan->name[0]) ? chan->name : chan->dname,
  146. chan->channel.key[0] ? chan->channel.key : chan->key_prot);
  147. chan->status |= CHAN_JOINING;
  148. }
  149. }
  150. }
  151. /* 001: welcome to IRC (use it to fix the server name)
  152. */
  153. static int got001(char *from, char *msg)
  154. {
  155. fixcolon(msg);
  156. rehash_server(from, msg);
  157. /* Ok...param #1 of 001 = what server thinks my nick is */
  158. join_chans();
  159. #ifdef no
  160. if (egg_strcasecmp(from, dcc[servidx].host)) {
  161. struct server_list *x = serverlist;
  162. if (x == NULL)
  163. return 0; /* Uh, no server list */
  164. putlog(LOG_MISC, "*", "(%s claims to be %s; updating server list)", dcc[servidx].host, from);
  165. for (int i = curserv; i > 0 && x != NULL; i--)
  166. x = x->next;
  167. if (x == NULL) {
  168. putlog(LOG_MISC, "*", "Invalid server list!");
  169. return 0;
  170. }
  171. }
  172. #endif
  173. return 0;
  174. }
  175. /* <server> 005 <to> <option> <option> <... option> :are supported by this server */
  176. static int
  177. got005(char *from, char *msg)
  178. {
  179. char *tmp = NULL, *p, *p2;
  180. newsplit(&msg); /* nick */
  181. while ((tmp = newsplit(&msg))[0]) {
  182. p = NULL;
  183. if ((p = strchr(tmp, '=')))
  184. *p++ = 0;
  185. if (!egg_strcasecmp(tmp, ":are"))
  186. break;
  187. else if (!egg_strcasecmp(tmp, "MODES")) {
  188. modesperline = atoi(p);
  189. if (modesperline > MODES_PER_LINE_MAX)
  190. modesperline = MODES_PER_LINE_MAX;
  191. } else if (!egg_strcasecmp(tmp, "NICKLEN"))
  192. nick_len = atoi(p);
  193. else if (!egg_strcasecmp(tmp, "NETWORK"))
  194. strlcpy(curnetwork, p, 120);
  195. else if (!egg_strcasecmp(tmp, "PENALTY"))
  196. use_penalties = 1;
  197. else if (!egg_strcasecmp(tmp, "WHOX"))
  198. use_354 = 1;
  199. else if (!egg_strcasecmp(tmp, "EXCEPTS"))
  200. use_exempts = 1;
  201. else if (!egg_strcasecmp(tmp, "INVEX"))
  202. use_invites = 1;
  203. else if (!egg_strcasecmp(tmp, "MAXBANS")) {
  204. max_bans = atoi(p);
  205. max_modes = max_bans;
  206. max_exempts = max_bans;
  207. max_invites = max_bans;
  208. }
  209. else if (!egg_strcasecmp(tmp, "MAXLIST")) {
  210. p2 = NULL;
  211. if ((p2 = strchr(p, ':'))) {
  212. *p2++ = 0;
  213. max_modes = atoi(p2);
  214. if (strchr(p, 'e'))
  215. max_exempts = max_modes;
  216. if (strchr(p, 'b'))
  217. max_bans = max_modes;
  218. if (strchr(p, 'I'))
  219. max_invites = max_modes;
  220. }
  221. }
  222. else if (!egg_strcasecmp(tmp, "CASEMAPPING")) {
  223. /* we are default set to rfc1459, so only switch if NOT rfc1459 */
  224. if (egg_strcasecmp(p, "rfc1459")) {
  225. rfc_casecmp = egg_strcasecmp;
  226. rfc_toupper = toupper;
  227. }
  228. }
  229. }
  230. return 0;
  231. }
  232. /* Got 442: not on channel
  233. */
  234. static int got442(char *from, char *msg)
  235. {
  236. char *chname = NULL;
  237. struct chanset_t *chan = NULL;
  238. int i;
  239. struct server_list *x = NULL;
  240. for (x = serverlist, i = 0; x; x = x->next, i++)
  241. if (i == curserv) {
  242. if (egg_strcasecmp(from, x->name))
  243. return 0;
  244. break;
  245. }
  246. newsplit(&msg);
  247. chname = newsplit(&msg);
  248. chan = findchan(chname);
  249. if (chan)
  250. if (shouldjoin(chan) && !channel_joining(chan)) {
  251. putlog(LOG_MISC, chname, "Server says I'm not on channel: %s", chname);
  252. clear_channel(chan, 1);
  253. chan->status &= ~CHAN_ACTIVE;
  254. chan->status |= CHAN_JOINING;
  255. dprintf(DP_MODE, "JOIN %s %s\n", chan->name,
  256. chan->channel.key[0] ? chan->channel.key : chan->key_prot);
  257. }
  258. return 0;
  259. }
  260. /* Close the current server connection.
  261. */
  262. void nuke_server(const char *reason)
  263. {
  264. if (serv >= 0 && servidx >= 0) {
  265. if (reason)
  266. dprintf(DP_DUMP, "QUIT :%s\n", reason);
  267. sleep(1);
  268. disconnect_server(servidx, DO_LOST);
  269. }
  270. }
  271. char ctcp_reply[1024] = "";
  272. static int lastmsgs[FLOOD_GLOBAL_MAX];
  273. static char lastmsghost[FLOOD_GLOBAL_MAX][128];
  274. static time_t lastmsgtime[FLOOD_GLOBAL_MAX];
  275. static int dronemsgs;
  276. static time_t dronemsgtime;
  277. static bool set_pls_g;
  278. static time_t flood_g_time = 60;
  279. rate_t flood_g = { 6, 2 };
  280. void unset_g(int data)
  281. {
  282. dprintf(DP_MODE, "MODE %s :-g\n", botname);
  283. set_pls_g = 0;
  284. }
  285. /* Do on NICK, PRIVMSG, NOTICE and JOIN.
  286. */
  287. static bool detect_flood(char *floodnick, char *floodhost, char *from, int which)
  288. {
  289. struct userrec *u = get_user_by_host(from);
  290. int atr = u ? u->flags : 0;
  291. if ((u && u->bot) || (atr & USER_NOFLOOD))
  292. return 0;
  293. char *p = NULL, ftype[10] = "", h[1024] = "";
  294. int thr = 0;
  295. time_t lapse = 0;
  296. /* Determine how many are necessary to make a flood */
  297. switch (which) {
  298. case FLOOD_PRIVMSG:
  299. case FLOOD_NOTICE:
  300. thr = flood_msg.count;
  301. lapse = flood_msg.time;
  302. strcpy(ftype, "msg");
  303. break;
  304. case FLOOD_CTCP:
  305. thr = flood_ctcp.count;
  306. lapse = flood_ctcp.time;
  307. strcpy(ftype, "ctcp");
  308. break;
  309. }
  310. if ((thr == 0) || (lapse == 0))
  311. return 0; /* No flood protection */
  312. /* Okay, make sure i'm not flood-checking myself */
  313. if (match_my_nick(floodnick))
  314. return 0;
  315. if (!egg_strcasecmp(floodhost, botuserhost))
  316. return 0; /* My user@host (?) */
  317. //FIXME: hack for +g
  318. if (dronemsgtime < now - flood_g.time) { //expired, reset counter
  319. dronemsgs = 0;
  320. // dronemsgtime = now;
  321. }
  322. dronemsgs++;
  323. dronemsgtime = now;
  324. if (!set_pls_g && dronemsgs >= flood_g.count) { //flood from dronenet, let's attempt to set +g
  325. egg_timeval_t howlong;
  326. set_pls_g = 1;
  327. dronemsgs = 0;
  328. dronemsgtime = 0;
  329. dprintf(DP_DUMP, "MODE %s :+g\n", botname);
  330. howlong.sec = flood_g_time;
  331. howlong.usec = 0;
  332. timer_create(&howlong, "Unset umode +g", (Function) unset_g);
  333. putlog(LOG_MISC, "*", "Drone flood detected! Setting +g for %li seconds.", flood_g_time);
  334. return 1; //ignore the current msg
  335. }
  336. p = strchr(floodhost, '@');
  337. if (p) {
  338. p++;
  339. if (egg_strcasecmp(lastmsghost[which], p)) { /* New */
  340. strcpy(lastmsghost[which], p);
  341. lastmsgtime[which] = now;
  342. lastmsgs[which] = 0;
  343. return 0;
  344. }
  345. } else
  346. return 0; /* Uh... whatever. */
  347. if (lastmsgtime[which] < now - lapse) {
  348. /* Flood timer expired, reset it */
  349. lastmsgtime[which] = now;
  350. lastmsgs[which] = 0;
  351. return 0;
  352. }
  353. lastmsgs[which]++;
  354. if (lastmsgs[which] >= thr) { /* FLOOD */
  355. /* Reset counters */
  356. lastmsgs[which] = 0;
  357. lastmsgtime[which] = 0;
  358. lastmsghost[which][0] = 0;
  359. u = get_user_by_host(from);
  360. /* Private msg */
  361. simple_sprintf(h, "*!*@%s", p);
  362. putlog(LOG_MISC, "*", "Flood from @%s! Placing on ignore!", p);
  363. addignore(h, conf.bot->nick, (which == FLOOD_CTCP) ? "CTCP flood" : "MSG/NOTICE flood", now + (60 * ignore_time));
  364. return 1;
  365. }
  366. return 0;
  367. }
  368. /* Check for more than 8 control characters in a line.
  369. * This could indicate: beep flood CTCP avalanche.
  370. */
  371. bool detect_avalanche(char *msg)
  372. {
  373. int count = 0;
  374. for (unsigned char *p = (unsigned char *) msg; (*p) && (count < 8); p++)
  375. if ((*p == 7) || (*p == 1))
  376. count++;
  377. if (count >= 8)
  378. return 1;
  379. else
  380. return 0;
  381. }
  382. /* Got a private message.
  383. */
  384. static int gotmsg(char *from, char *msg)
  385. {
  386. if (msg[0] && ((strchr(CHANMETA, *msg) != NULL) ||
  387. (*msg == '@'))) /* Notice to a channel, not handled here */
  388. return 0;
  389. char *to = NULL, buf[UHOSTLEN] = "", *nick = NULL, ctcpbuf[512] = "", *uhost = buf,
  390. *ctcp = NULL, *p = NULL, *p1 = NULL, *code = NULL;
  391. struct userrec *u = NULL;
  392. int ctcp_count = 0;
  393. bool ignoring = match_ignore(from);
  394. to = newsplit(&msg);
  395. fixcolon(msg);
  396. /* Only check if flood-ctcp is active */
  397. strcpy(uhost, from);
  398. nick = splitnick(&uhost);
  399. if (flood_ctcp.count && detect_avalanche(msg)) {
  400. if (!ignoring) {
  401. putlog(LOG_MODES, "*", "Avalanche from %s - ignoring", from);
  402. p = strchr(uhost, '@');
  403. if (p != NULL)
  404. p++;
  405. else
  406. p = uhost;
  407. simple_sprintf(ctcpbuf, "*!*@%s", p);
  408. addignore(ctcpbuf, conf.bot->nick, "ctcp avalanche", now + (60 * ignore_time));
  409. ignoring++;
  410. }
  411. }
  412. /* Check for CTCP: */
  413. ctcp_reply[0] = 0;
  414. p = strchr(msg, 1);
  415. while ((p != NULL) && (*p)) {
  416. p++;
  417. p1 = p;
  418. while ((*p != 1) && (*p != 0))
  419. p++;
  420. if (*p == 1) {
  421. *p = 0;
  422. ctcp = strcpy(ctcpbuf, p1);
  423. strcpy(p1 - 1, p + 1);
  424. if (!ignoring)
  425. detect_flood(nick, uhost, from, strncmp(ctcp, "ACTION ", 7) ? FLOOD_CTCP : FLOOD_PRIVMSG);
  426. /* Respond to the first answer_ctcp */
  427. p = strchr(msg, 1);
  428. if (ctcp_count < answer_ctcp) {
  429. ctcp_count++;
  430. if (ctcp[0] != ' ') {
  431. code = newsplit(&ctcp);
  432. if ((to[0] == '$') || strchr(to, '.')) {
  433. if (!ignoring) {
  434. /* Don't interpret */
  435. putlog(LOG_PUBLIC, to, "CTCP %s: %s from %s (%s) to %s", code, ctcp, nick, uhost, to);
  436. }
  437. } else {
  438. u = get_user_by_host(from);
  439. if (!ignoring || trigger_on_ignore) {
  440. if (check_bind_ctcp(nick, uhost, u, to, code, ctcp) == BIND_RET_LOG && !ignoring) {
  441. if (!strcmp(code, "DCC")) {
  442. /* If it gets this far unhandled, it means that
  443. * the user is totally unknown.
  444. */
  445. code = newsplit(&ctcp);
  446. if (!strcmp(code, "CHAT")) {
  447. if (!quiet_reject) {
  448. if (u)
  449. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, "I'm not accepting call at the moment.");
  450. else
  451. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, DCC_NOSTRANGERS);
  452. }
  453. if (!ischanhub())
  454. putlog(LOG_MISC, "*", "%s: %s", DCC_REFUSEDNC, from);
  455. else
  456. putlog(LOG_MISC, "*", "%s: %s", DCC_REFUSED, from);
  457. } else {
  458. putlog(LOG_MISC, "*", "Refused DCC %s: %s", code, from);
  459. }
  460. } else if (!strcmp(code, "CHAT")) {
  461. if (!quiet_reject) {
  462. if (u)
  463. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, "I'm not accepting call at the moment.");
  464. else
  465. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, DCC_NOSTRANGERS);
  466. }
  467. if (!ischanhub())
  468. putlog(LOG_MISC, "*", "%s: %s", DCC_REFUSEDNC, from);
  469. else
  470. putlog(LOG_MISC, "*", "%s: %s", DCC_REFUSED, from);
  471. }
  472. }
  473. if (!strcmp(code, "ACTION")) {
  474. putlog(LOG_MSGS, "*", "* %s (%s): %s", nick, uhost, ctcp);
  475. } else {
  476. putlog(LOG_MSGS, "*", "CTCP %s: %s from %s (%s)", code, ctcp, nick, uhost);
  477. } /* I love a good close cascade ;) */
  478. }
  479. }
  480. }
  481. }
  482. }
  483. }
  484. /* Send out possible ctcp responses */
  485. if (ctcp_reply[0]) {
  486. if (ctcp_mode != 2) {
  487. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
  488. } else {
  489. if (now - last_ctcp > flood_ctcp.time) {
  490. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
  491. count_ctcp = 1;
  492. } else if (count_ctcp < flood_ctcp.count) {
  493. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
  494. count_ctcp++;
  495. }
  496. last_ctcp = now;
  497. }
  498. }
  499. if (msg[0]) {
  500. if ((to[0] == '$') || (strchr(to, '.') != NULL)) {
  501. /* Msg from oper */
  502. if (!ignoring) {
  503. detect_flood(nick, uhost, from, FLOOD_PRIVMSG);
  504. /* Do not interpret as command */
  505. putlog(LOG_MSGS | LOG_SERV, "*", "[%s!%s to %s] %s",nick, uhost, to, msg);
  506. }
  507. } else {
  508. char *my_code = NULL;
  509. Auth *auth = NULL;
  510. if (auth_prefix[0])
  511. auth = Auth::Find(uhost);
  512. if (!auth)
  513. detect_flood(nick, uhost, from, FLOOD_PRIVMSG);
  514. my_code = newsplit(&msg);
  515. rmspace(msg);
  516. /* is it a cmd? */
  517. if (auth_prefix[0] && my_code && my_code[0] && my_code[1] && auth && auth->Authed() && my_code[0] == auth_prefix[0]) {
  518. my_code++; //eliminate the prefix
  519. auth->atime = now;
  520. if (!check_bind_authc(my_code, auth, NULL, msg))
  521. putlog(LOG_MSGS, "*", "[%s] %c%s %s", from, auth_prefix[0], my_code, msg);
  522. } else if (!ignoring && (my_code[0] != auth_prefix[0] || !my_code[1] || !auth || !auth->Authed())) {
  523. struct userrec *my_u = get_user_by_host(from);
  524. bool doit = 1;
  525. if (!egg_strcasecmp(my_code, "op") || !egg_strcasecmp(my_code, "pass") || !egg_strcasecmp(my_code, "invite")
  526. || !egg_strcasecmp(my_code, "ident")
  527. || !egg_strcasecmp(my_code, msgop) || !egg_strcasecmp(my_code, msgpass)
  528. || !egg_strcasecmp(my_code, msginvite) || !egg_strcasecmp(my_code, msgident)) {
  529. const char *buf2 = NULL;
  530. doit = 0;
  531. if (!egg_strcasecmp(my_code, msgop))
  532. buf2 = "op";
  533. else if (!egg_strcasecmp(my_code, msgpass))
  534. buf2 = "pass";
  535. else if (!egg_strcasecmp(my_code, msginvite))
  536. buf2 = "invite";
  537. else if (!egg_strcasecmp(my_code, msgident))
  538. buf2 = "ident";
  539. if (buf2)
  540. check_bind_msg(buf2, nick, uhost, my_u, msg);
  541. else
  542. putlog(LOG_MSGS, "*", "(%s!%s) attempted to use invalid msg cmd '%s'", nick, uhost, my_code);
  543. }
  544. if (doit)
  545. check_bind_msg(my_code, nick, uhost, my_u, msg);
  546. }
  547. }
  548. }
  549. return 0;
  550. }
  551. /* Got a private notice.
  552. */
  553. static int gotnotice(char *from, char *msg)
  554. {
  555. if (msg[0] && ((strchr(CHANMETA, *msg) != NULL) ||
  556. (*msg == '@'))) /* Notice to a channel, not handled here */
  557. return 0;
  558. char *to = NULL, *nick = NULL, ctcpbuf[512] = "", *p = NULL, *p1 = NULL, buf[512] = "",
  559. *uhost = buf, *ctcp = NULL, *ctcpmsg = NULL, *ptr = NULL;
  560. struct userrec *u = NULL;
  561. bool ignoring = match_ignore(from);
  562. to = newsplit(&msg);
  563. fixcolon(msg);
  564. strcpy(uhost, from);
  565. nick = splitnick(&uhost);
  566. if (flood_ctcp.count && detect_avalanche(msg)) {
  567. /* Discard -- kick user if it was to the channel */
  568. if (!ignoring)
  569. putlog(LOG_MODES, "*", "Avalanche from %s", from);
  570. return 0;
  571. }
  572. /* Check for CTCP: */
  573. ctcpmsg = ptr = strdup(msg);
  574. p = strchr(ctcpmsg, 1);
  575. while ((p != NULL) && (*p)) {
  576. p++;
  577. p1 = p;
  578. while ((*p != 1) && (*p != 0))
  579. p++;
  580. if (*p == 1) {
  581. *p = 0;
  582. ctcp = strcpy(ctcpbuf, p1);
  583. strcpy(p1 - 1, p + 1);
  584. if (!ignoring)
  585. detect_flood(nick, uhost, from, FLOOD_CTCP);
  586. p = strchr(ctcpmsg, 1);
  587. if (ctcp[0] != ' ') {
  588. char *code = newsplit(&ctcp);
  589. if ((to[0] == '$') || strchr(to, '.')) {
  590. if (!ignoring)
  591. putlog(LOG_PUBLIC, "*",
  592. "CTCP reply %s: %s from %s (%s) to %s", code, ctcp, nick, uhost, to);
  593. } else {
  594. u = get_user_by_host(from);
  595. if (!ignoring || trigger_on_ignore) {
  596. check_bind_ctcr(nick, uhost, u, to, code, ctcp);
  597. if (!ignoring)
  598. /* Who cares? */
  599. putlog(LOG_MSGS, "*", "CTCP reply %s: %s from %s (%s) to %s", code, ctcp, nick, uhost, to);
  600. }
  601. }
  602. }
  603. }
  604. }
  605. free(ptr);
  606. if (msg[0]) {
  607. if (((to[0] == '$') || strchr(to, '.')) && !ignoring) {
  608. detect_flood(nick, uhost, from, FLOOD_NOTICE);
  609. putlog(LOG_MSGS | LOG_SERV, "*", "-%s (%s) to %s- %s", nick, uhost, to, msg);
  610. } else {
  611. /* Server notice? */
  612. if ((nick[0] == 0) || (uhost[0] == 0)) {
  613. if (!server_online &&
  614. !strcmp(msg, "*** You are exempt from flood limits."))
  615. floodless = 1;
  616. /* Hidden `250' connection count message from server */
  617. if (strncmp(msg, "Highest connection count:", 25))
  618. putlog(LOG_SERV, "*", "-NOTICE- %s", msg);
  619. } else {
  620. detect_flood(nick, uhost, from, FLOOD_NOTICE);
  621. u = get_user_by_host(from);
  622. if (!ignoring)
  623. putlog(LOG_MSGS, "*", "-%s (%s)- %s", nick, uhost, msg);
  624. }
  625. }
  626. }
  627. return 0;
  628. }
  629. /* WALLOPS: oper's nuisance
  630. */
  631. static int gotwall(char *from, char *msg)
  632. {
  633. fixcolon(msg);
  634. putlog(LOG_WALL, "*", "!%s! %s", from, msg);
  635. return 0;
  636. }
  637. void server_send_ison()
  638. {
  639. if (server_online && keepnick) {
  640. /* NOTE: now that botname can but upto NICKLEN bytes long,
  641. * check that it's not just a truncation of the full nick.
  642. */
  643. if (strncmp(botname, origbotname, strlen(botname))) {
  644. /* See if my nickname is in use and if if my nick is right. */
  645. dprintf(DP_SERVER, "ISON :%s %s\n", botname, origbotname);
  646. }
  647. }
  648. }
  649. /* Called once a minute... but if we're the only one on the
  650. * channel, we only wanna send out "lusers" once every 5 mins.
  651. */
  652. static void minutely_checks()
  653. {
  654. /* Only check if we have already successfully logged in. */
  655. if (server_online) {
  656. static int count = 4;
  657. bool ok = 0;
  658. for (register struct chanset_t *chan = chanset; chan; chan = chan->next) {
  659. if (channel_active(chan) && chan->channel.members == 1) {
  660. ok = 1;
  661. break;
  662. }
  663. }
  664. if (!ok)
  665. return;
  666. count++;
  667. if (count >= 5) {
  668. dprintf(DP_SERVER, "LUSERS\n");
  669. count = 0;
  670. }
  671. }
  672. }
  673. /* Pong from server.
  674. */
  675. static int gotpong(char *from, char *msg)
  676. {
  677. newsplit(&msg);
  678. fixcolon(msg); /* Scrap server name */
  679. server_lag = now - my_atoul(msg);
  680. if (server_lag > 99999) {
  681. /* IRCnet lagmeter support by drummer */
  682. server_lag = now - lastpingtime;
  683. }
  684. return 0;
  685. }
  686. /* This is a reply on ISON :<current> <orig> [<alt>]
  687. */
  688. static void got303(char *from, char *msg)
  689. {
  690. if (!keepnick || !strncmp(botname, origbotname, strlen(botname)))
  691. return;
  692. char *tmp = NULL;
  693. newsplit(&msg);
  694. fixcolon(msg);
  695. tmp = newsplit(&msg);
  696. if (tmp[0] && !rfc_casecmp(botname, tmp)) {
  697. bool ison_orig = 0;
  698. while ((tmp = newsplit(&msg))[0]) { /* no, it's NOT == */
  699. if (!rfc_casecmp(tmp, origbotname))
  700. ison_orig = 1;
  701. }
  702. if (!ison_orig) {
  703. if (!nick_juped)
  704. putlog(LOG_MISC, "*", "Switching back to nick %s", origbotname);
  705. dprintf(DP_SERVER, "NICK %s\n", origbotname);
  706. }
  707. }
  708. }
  709. /* 432 : Bad nickname
  710. */
  711. static int got432(char *from, char *msg)
  712. {
  713. char *erroneus = NULL;
  714. newsplit(&msg);
  715. erroneus = newsplit(&msg);
  716. if (server_online)
  717. putlog(LOG_MISC, "*", "NICK IS INVALID: %s (keeping '%s').", erroneus,
  718. botname);
  719. else {
  720. putlog(LOG_MISC, "*", "Server says my nickname is invalid.");
  721. if (!keepnick) {
  722. makepass(erroneus);
  723. erroneus[NICKMAX] = 0;
  724. dprintf(DP_MODE, "NICK %s\n", erroneus);
  725. }
  726. return 0;
  727. }
  728. return 0;
  729. }
  730. /* 433 : Nickname in use
  731. * Change nicks till we're acceptable or we give up
  732. */
  733. static int got433(char *from, char *msg)
  734. {
  735. /* We are online and have a nickname, we'll keep it */
  736. if (server_online) {
  737. char *tmp = NULL;
  738. newsplit(&msg);
  739. tmp = newsplit(&msg);
  740. putlog(LOG_MISC, "*", "NICK IN USE: %s (keeping '%s').", tmp, botname);
  741. nick_juped = 0;
  742. return 0;
  743. }
  744. gotfake433(from);
  745. return 0;
  746. }
  747. /* 437 : Channel/Nickname juped (IRCnet)
  748. */
  749. static int got437(char *from, char *msg)
  750. {
  751. char *s = NULL;
  752. struct chanset_t *chan = NULL;
  753. newsplit(&msg);
  754. s = newsplit(&msg);
  755. if (s[0] && (strchr(CHANMETA, s[0]) != NULL)) {
  756. chan = findchan(s);
  757. if (chan) {
  758. chan->status &= ~(CHAN_JOINING);
  759. if (chan->status & CHAN_ACTIVE) {
  760. putlog(LOG_MISC, "*", IRC_CANTCHANGENICK, s);
  761. } else {
  762. if (!channel_juped(chan)) {
  763. putlog(LOG_MISC, "*", "Channel %s is juped. :(", s);
  764. chan->status |= CHAN_JUPED;
  765. }
  766. }
  767. }
  768. } else if (server_online) {
  769. if (!nick_juped)
  770. putlog(LOG_MISC, "*", "NICK IS JUPED: %s (keeping '%s').", s, botname);
  771. if (!rfc_casecmp(s, origbotname))
  772. nick_juped = 1;
  773. } else {
  774. putlog(LOG_MISC, "*", "%s: %s", "Nickname has been juped", s);
  775. gotfake433(from);
  776. }
  777. return 0;
  778. }
  779. /* 438 : Nick change too fast
  780. */
  781. static int got438(char *from, char *msg)
  782. {
  783. newsplit(&msg);
  784. newsplit(&msg);
  785. fixcolon(msg);
  786. putlog(LOG_MISC, "*", "%s", msg);
  787. return 0;
  788. }
  789. static int got451(char *from, char *msg)
  790. {
  791. /* Usually if we get this then we really messed up somewhere
  792. * or this is a non-standard server, so we log it and kill the socket
  793. * hoping the next server will work :) -poptix
  794. */
  795. /* Um, this does occur on a lagged anti-spoof server connection if the
  796. * (minutely) sending of joins occurs before the bot does its ping reply.
  797. * Probably should do something about it some time - beldin
  798. */
  799. putlog(LOG_MISC, "*", "%s says I'm not registered, trying next one.", from);
  800. nuke_server("The server says we are not registered yet..");
  801. return 0;
  802. }
  803. /* Got error notice
  804. */
  805. static int goterror(char *from, char *msg)
  806. {
  807. if (msg[0] == ':')
  808. msg++;
  809. putlog(LOG_SERV, "*", "-ERROR from server- %s", msg);
  810. putlog(LOG_SERV, "*", "Disconnecting from server.");
  811. nuke_server("Bah, stupid error messages.");
  812. return 1;
  813. }
  814. /* Got nick change.
  815. */
  816. static int gotnick(char *from, char *msg)
  817. {
  818. char *nick = NULL, *buf = NULL, *buf_ptr = NULL;
  819. struct userrec *u = NULL;
  820. buf = buf_ptr = strdup(from);
  821. u = get_user_by_host(buf);
  822. nick = splitnick(&buf);
  823. fixcolon(msg);
  824. if (match_my_nick(nick)) {
  825. /* Regained nick! */
  826. strlcpy(botname, msg, NICKLEN);
  827. altnick_char = 0;
  828. if (!strcmp(msg, origbotname)) {
  829. putlog(LOG_SERV | LOG_MISC, "*", "Regained nickname '%s'.", msg);
  830. nick_juped = 0;
  831. } else if (keepnick && strcmp(nick, msg)) {
  832. putlog(LOG_SERV | LOG_MISC, "*", "Nickname changed to '%s'???", msg);
  833. if (!rfc_casecmp(nick, origbotname)) {
  834. putlog(LOG_MISC, "*", "Switching back to nick %s", origbotname);
  835. dprintf(DP_SERVER, "NICK %s\n", origbotname);
  836. }
  837. } else
  838. putlog(LOG_SERV | LOG_MISC, "*", "Nickname changed to '%s'???", msg);
  839. } else if ((keepnick) && (rfc_casecmp(nick, msg))) {
  840. /* Only do the below if there was actual nick change, case doesn't count */
  841. if (!rfc_casecmp(nick, origbotname)) {
  842. putlog(LOG_MISC, "*", "Switching back to nick %s", origbotname);
  843. dprintf(DP_SERVER, "NICK %s\n", origbotname);
  844. }
  845. }
  846. free(buf_ptr);
  847. return 0;
  848. }
  849. static int gotmode(char *from, char *msg)
  850. {
  851. char *ch = NULL, *buf = NULL, *buf_ptr = NULL;
  852. buf_ptr = buf = strdup(msg);
  853. ch = newsplit(&buf);
  854. /* Usermode changes? */
  855. if (strchr(CHANMETA, ch[0]) == NULL) {
  856. if (match_my_nick(ch) && check_mode_r) {
  857. /* umode +r? - D0H dalnet uses it to mean something different */
  858. fixcolon(buf);
  859. if ((buf[0] == '+') && strchr(buf, 'r')) {
  860. putlog(LOG_MISC | LOG_JOIN, "*", "%s has me i-lined (jumping)", dcc[servidx].host);
  861. nuke_server("i-lines suck");
  862. }
  863. }
  864. }
  865. free(buf_ptr);
  866. return 0;
  867. }
  868. static void disconnect_server(int idx, int dolost)
  869. {
  870. if ((serv != dcc[idx].sock) && serv >= 0)
  871. killsock(serv);
  872. if (dcc[idx].sock >= 0)
  873. killsock(dcc[idx].sock);
  874. dcc[idx].sock = -1;
  875. serv = -1;
  876. servidx = -1;
  877. server_online = 0;
  878. floodless = 0;
  879. botuserhost[0] = 0;
  880. botuserip[0] = 0;
  881. if (dolost) {
  882. Auth::DeleteAll();
  883. trying_server = 0;
  884. lostdcc(idx);
  885. }
  886. }
  887. static void eof_server(int idx)
  888. {
  889. putlog(LOG_SERV, "*", "Disconnected from %s", dcc[idx].host);
  890. disconnect_server(idx, DO_LOST);
  891. }
  892. static void display_server(int idx, char *buf)
  893. {
  894. simple_sprintf(buf, "%s (lag: %d)", trying_server ? "conn" : "serv", server_lag);
  895. }
  896. static void connect_server(void);
  897. static void kill_server(int idx, void *x)
  898. {
  899. disconnect_server(idx, NO_LOST); /* eof_server will lostdcc() it. */
  900. if (!segfaulted) // don't bother if we've segfaulted, too many memory calls in this loop
  901. for (struct chanset_t *chan = chanset; chan; chan = chan->next)
  902. clear_channel(chan, 1);
  903. /* A new server connection will be automatically initiated in
  904. about 2 seconds. */
  905. }
  906. static void timeout_server(int idx)
  907. {
  908. putlog(LOG_SERV, "*", "Timeout: connect to %s", dcc[idx].host);
  909. disconnect_server(idx, DO_LOST);
  910. }
  911. static void server_activity(int, char *, int);
  912. struct dcc_table SERVER_SOCKET =
  913. {
  914. "SERVER",
  915. 0,
  916. eof_server,
  917. server_activity,
  918. NULL,
  919. timeout_server,
  920. display_server,
  921. kill_server,
  922. NULL,
  923. NULL
  924. };
  925. static void server_activity(int idx, char *msg, int len)
  926. {
  927. char *from = NULL, *code = NULL;
  928. if (trying_server) {
  929. strcpy(dcc[idx].nick, "(server)");
  930. putlog(LOG_SERV, "*", "Connected to %s", dcc[idx].host);
  931. trying_server = 0;
  932. /*
  933. servidx = idx;
  934. serv = dcc[idx].sosck;
  935. */
  936. SERVER_SOCKET.timeout_val = 0;
  937. }
  938. waiting_for_awake = 0;
  939. if (msg[0] == ':') {
  940. msg++;
  941. from = newsplit(&msg);
  942. } else
  943. from = "";
  944. code = newsplit(&msg);
  945. if (!strcmp(code, "PRIVMSG") || !strcmp(code, "NOTICE")) {
  946. if (!match_ignore(from))
  947. putlog(LOG_RAW, "@", "[@] %s %s %s", from, code, msg);
  948. } else
  949. putlog(LOG_RAW, "@", "[@] %s %s %s", from, code, msg);
  950. /* This has GOT to go into the raw binding table, * merely because this
  951. * is less effecient.
  952. */
  953. check_bind_raw(from, code, msg);
  954. }
  955. static int gotping(char *from, char *msg)
  956. {
  957. fixcolon(msg);
  958. dprintf(DP_MODE, "PONG :%s\n", msg);
  959. return 0;
  960. }
  961. static int gotkick(char *from, char *msg)
  962. {
  963. if (!match_my_nick(from))
  964. /* Not my kick, I don't need to bother about it. */
  965. return 0;
  966. if (use_penalties) {
  967. last_time += 2;
  968. if (debug_output)
  969. putlog(LOG_SRVOUT, "*", "adding 2secs penalty (successful kick)");
  970. }
  971. return 0;
  972. }
  973. /* Another sec penalty if bot did a whois on another server.
  974. */
  975. static int got318_369(char *, char *, int);
  976. static int whoispenalty(char *from, char *msg)
  977. {
  978. struct server_list *x = serverlist;
  979. if (x && use_penalties) {
  980. int i = 0, ii = 0;
  981. for (; x; x = x->next) {
  982. if (i == curserv) {
  983. if (strcmp(x->name, from))
  984. ii = 1;
  985. }
  986. i++;
  987. }
  988. if (ii) {
  989. last_time += 1;
  990. if (debug_output)
  991. putlog(LOG_SRVOUT, "*", "adding 1sec penalty (remote whois)");
  992. }
  993. }
  994. got318_369(from, msg, 0);
  995. return 0;
  996. }
  997. static void irc_whois(char *, const char *, ...) __attribute__((format(printf, 2, 3)));
  998. static void
  999. irc_whois(char *nick, const char *format, ...)
  1000. {
  1001. char va_out[2001] = "";
  1002. va_list va;
  1003. va_start(va, format);
  1004. egg_vsnprintf(va_out, sizeof(va_out) - 1, format, va);
  1005. va_end(va);
  1006. for (int idx = 0; idx < dcc_total; idx++)
  1007. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(nick, dcc[idx].whois))
  1008. dprintf(idx, "%s\n", va_out);
  1009. }
  1010. void check_hostmask()
  1011. {
  1012. if (!server_online || !botuserhost[0])
  1013. return;
  1014. char s[UHOSTLEN + 2] = "";
  1015. simple_sprintf(s, "*!%s", botuserhost); /* just add actual user@ident, regardless of ~ */
  1016. /* dont add the host if it conflicts with another in the userlist */
  1017. struct userrec *u = NULL;
  1018. if ((u = host_conflicts(s))) {
  1019. if (u != conf.bot->u) {
  1020. putlog(LOG_WARN, "*", "My automatic hostmask '%s' would conflict with user: '%s'. (Not adding)", s, u->handle);
  1021. sdprintf("I am %s, they are: %s, (%X vs %X)", conf.bot->u->handle, u->handle, conf.bot->u, u);
  1022. } else
  1023. sdprintf("Already have hostmask '%s' added for myself", s);
  1024. return;
  1025. }
  1026. addhost_by_handle(conf.bot->nick, s);
  1027. putlog(LOG_GETIN, "*", "Updated my hostmask: %s", s);
  1028. }
  1029. /* 311 $me nick username address * :realname */
  1030. static int got311(char *from, char *msg)
  1031. {
  1032. char *nick = NULL, *username = NULL, *address = NULL, uhost[UHOSTLEN + 1];
  1033. struct userrec *u = NULL;
  1034. newsplit(&msg);
  1035. nick = newsplit(&msg);
  1036. username = newsplit(&msg);
  1037. address = newsplit(&msg);
  1038. newsplit(&msg);
  1039. fixcolon(msg);
  1040. if (match_my_nick(nick)) {
  1041. simple_snprintf(botuserhost, sizeof botuserhost, "%s@%s", username, address);
  1042. check_hostmask();
  1043. }
  1044. irc_whois(nick, "$b%s$b [%s@%s]", nick, username, address);
  1045. simple_snprintf(uhost, sizeof uhost, "%s!%s@%s", nick, username, address);
  1046. if ((u = get_user_by_host(uhost)))
  1047. irc_whois(nick, " username : $u%s$u", u->handle);
  1048. irc_whois(nick, " ircname : %s", msg);
  1049. return 0;
  1050. }
  1051. /* 319 $me nick :channels */
  1052. static int got319(char *from, char *msg)
  1053. {
  1054. char *nick = NULL;
  1055. newsplit(&msg);
  1056. nick = newsplit(&msg);
  1057. fixcolon(msg);
  1058. irc_whois(nick, " channels : %s", msg);
  1059. return 0;
  1060. }
  1061. /* 312 $me nick server :text */
  1062. static int got312(char *from, char *msg)
  1063. {
  1064. char *nick = NULL, *server = NULL;
  1065. newsplit(&msg);
  1066. nick = newsplit(&msg);
  1067. server = newsplit(&msg);
  1068. fixcolon(msg);
  1069. irc_whois(nick, " server : %s [%s]", server, msg);
  1070. return 0;
  1071. }
  1072. /* 301 $me nick :away msg */
  1073. static int got301(char *from, char *msg)
  1074. {
  1075. char *nick = NULL;
  1076. newsplit(&msg);
  1077. nick = newsplit(&msg);
  1078. fixcolon(msg);
  1079. irc_whois(nick, " away : %s", msg);
  1080. return 0;
  1081. }
  1082. /* 313 $me nick :server text */
  1083. static int got313(char *from, char *msg)
  1084. {
  1085. char *nick = NULL;
  1086. newsplit(&msg);
  1087. nick = newsplit(&msg);
  1088. fixcolon(msg);
  1089. irc_whois(nick, " : $b%s$b", msg);
  1090. return 0;
  1091. }
  1092. /* 317 $me nick idle signon :idle-eng signon-eng */
  1093. static int got317(char *from, char *msg)
  1094. {
  1095. char *nick = NULL, date[50] = "";
  1096. time_t idle, signon;
  1097. int mydays, myhours, mymins, mysecs;
  1098. newsplit(&msg);
  1099. nick = newsplit(&msg);
  1100. idle = atol(newsplit(&msg));
  1101. signon = atol(newsplit(&msg));
  1102. fixcolon(msg);
  1103. egg_strftime(date, sizeof date, "%c %Z", gmtime(&signon));
  1104. mydays = idle / 86400;
  1105. idle = idle % 86400;
  1106. myhours = idle / 3600;
  1107. idle = idle % 3600;
  1108. mymins = idle / 60;
  1109. idle = idle % 60;
  1110. mysecs = idle;
  1111. irc_whois(nick, " idle : %d days %d hours %d mins %d secs [signon: %s]", mydays, myhours, mymins, mysecs, date);
  1112. return 0;
  1113. }
  1114. static int got369(char *from, char *msg)
  1115. {
  1116. return got318_369(from, msg, 1);
  1117. }
  1118. /* 318/319 $me nick :End of /? */
  1119. static int got318_369(char *from, char *msg, int whowas)
  1120. {
  1121. char *nick = NULL;
  1122. newsplit(&msg);
  1123. nick = newsplit(&msg);
  1124. fixcolon(msg);
  1125. irc_whois(nick, "%s", msg);
  1126. for (int idx = 0; idx < dcc_total; idx++) {
  1127. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(dcc[idx].whois, nick) &&
  1128. ((!whowas && !dcc[idx].whowas) || (whowas && dcc[idx].whowas))) {
  1129. dcc[idx].whois[0] = 0;
  1130. dcc[idx].whowas = 0;
  1131. }
  1132. }
  1133. return 0;
  1134. }
  1135. /* 401 $me nick :text */
  1136. static int got401(char *from, char *msg)
  1137. {
  1138. char *nick = NULL;
  1139. newsplit(&msg);
  1140. nick = newsplit(&msg);
  1141. fixcolon(msg);
  1142. irc_whois(nick, "%s", msg);
  1143. for (int idx = 0; idx < dcc_total; idx++)
  1144. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(dcc[idx].whois, nick))
  1145. dcc[idx].whowas = 1;
  1146. dprintf(DP_SERVER, "WHOWAS %s %s\n", nick, from);
  1147. return 0;
  1148. }
  1149. /* 406 $me nick :text */
  1150. static int got406(char *from, char *msg)
  1151. {
  1152. char *nick = NULL;
  1153. newsplit(&msg);
  1154. nick = newsplit(&msg);
  1155. fixcolon(msg);
  1156. irc_whois(nick, "%s", msg);
  1157. return 0;
  1158. }
  1159. /* 718 $me nick user@host :msg
  1160. * for receiving a msg while +g
  1161. */
  1162. static int got718(char *from, char *msg)
  1163. {
  1164. char *nick = NULL, *uhost = NULL;
  1165. newsplit(&msg);
  1166. nick = newsplit(&msg);
  1167. uhost = newsplit(&msg);
  1168. fixcolon(msg);
  1169. putlog(LOG_WALL, "*", "(+g) !%s!%s! %s", nick, uhost, msg);
  1170. return 0;
  1171. }
  1172. static cmd_t my_raw_binds[] =
  1173. {
  1174. {"PRIVMSG", "", (Function) gotmsg, NULL, LEAF},
  1175. {"NOTICE", "", (Function) gotnotice, NULL, LEAF},
  1176. {"MODE", "", (Function) gotmode, NULL, LEAF},
  1177. {"PING", "", (Function) gotping, NULL, LEAF},
  1178. {"PONG", "", (Function) gotpong, NULL, LEAF},
  1179. {"WALLOPS", "", (Function) gotwall, NULL, LEAF},
  1180. {"001", "", (Function) got001, NULL, LEAF},
  1181. {"005", "", (Function) got005, NULL, LEAF},
  1182. {"303", "", (Function) got303, NULL, LEAF},
  1183. {"432", "", (Function) got432, NULL, LEAF},
  1184. {"433", "", (Function) got433, NULL, LEAF},
  1185. {"437", "", (Function) got437, NULL, LEAF},
  1186. {"438", "", (Function) got438, NULL, LEAF},
  1187. {"451", "", (Function) got451, NULL, LEAF},
  1188. {"442", "", (Function) got442, NULL, LEAF},
  1189. {"NICK", "", (Function) gotnick, NULL, LEAF},
  1190. {"ERROR", "", (Function) goterror, NULL, LEAF},
  1191. /* ircu2.10.10 has a bug when a client is throttled ERROR is sent wrong */
  1192. {"ERROR:", "", (Function) goterror, NULL, LEAF},
  1193. {"KICK", "", (Function) gotkick, NULL, LEAF},
  1194. /* WHOIS RAWS */
  1195. {"311", "", (Function) got311, NULL, LEAF}, /* ident host * :realname */
  1196. {"314", "", (Function) got311, NULL, LEAF}, /* "" -WHOWAS */
  1197. {"319", "", (Function) got319, NULL, LEAF}, /* :#channels */
  1198. {"312", "", (Function) got312, NULL, LEAF}, /* server :gecos */
  1199. {"301", "", (Function) got301, NULL, LEAF}, /* :away msg */
  1200. {"313", "", (Function) got313, NULL, LEAF}, /* :ircop */
  1201. {"317", "", (Function) got317, NULL, LEAF}, /* idle, signon :idle-eng, signon-eng */
  1202. {"401", "", (Function) got401, NULL, LEAF},
  1203. {"406", "", (Function) got406, NULL, LEAF},
  1204. {"318", "", (Function) whoispenalty, NULL, LEAF}, /* :End of /WHOIS */
  1205. {"369", "", (Function) got369, NULL, LEAF}, /* :End of /WHOWAS */
  1206. {"718", "", (Function) got718, NULL, LEAF},
  1207. {NULL, NULL, NULL, NULL, 0}
  1208. };
  1209. static void server_dns_callback(int, void *, const char *, char **);
  1210. /* Hook up to a server
  1211. */
  1212. static void connect_server(void)
  1213. {
  1214. char pass[121] = "", botserver[UHOSTLEN] = "";
  1215. int newidx;
  1216. port_t botserverport = 0;
  1217. waiting_for_awake = 0;
  1218. /* trying_server = now; */
  1219. empty_msgq();
  1220. if (newserverport) { /* cmd_jump was used; connect specified server */
  1221. curserv = -1; /* Reset server list */
  1222. strcpy(botserver, newserver);
  1223. botserverport = newserverport;
  1224. strcpy(pass, newserverpass);
  1225. newserver[0] = newserverport = newserverpass[0] = 0;
  1226. }
  1227. if (!cycle_time) {
  1228. struct chanset_t *chan = NULL;
  1229. struct server_list *x = serverlist;
  1230. if (!x)
  1231. return;
  1232. trying_server = now;
  1233. newidx = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  1234. if (newidx < 0) {
  1235. putlog(LOG_SERV, "*", "NO MORE DCC CONNECTIONS -- Can't create server connection.");
  1236. trying_server = 0;
  1237. return;
  1238. }
  1239. next_server(&curserv, botserver, &botserverport, pass);
  1240. putlog(LOG_SERV, "*", "Trying server %s:%d", botserver, botserverport);
  1241. dcc[newidx].port = botserverport;
  1242. strcpy(dcc[newidx].nick, "(server)");
  1243. strlcpy(dcc[newidx].host, botserver, UHOSTLEN);
  1244. botuserhost[0] = 0;
  1245. nick_juped = 0;
  1246. for (chan = chanset; chan; chan = chan->next)
  1247. chan->status &= ~CHAN_JUPED;
  1248. dcc[newidx].timeval = now;
  1249. dcc[newidx].sock = -1;
  1250. dcc[newidx].u.dns->cbuf = strdup(pass);
  1251. cycle_time = 15; /* wait 15 seconds before attempting next server connect */
  1252. /* I'm resolving... don't start another server connect request */
  1253. resolvserv = 1;
  1254. /* Resolve the hostname. */
  1255. int dns_id = egg_dns_lookup(botserver, 20, server_dns_callback, (void *) newidx);
  1256. if (dns_id >= 0)
  1257. dcc[newidx].dns_id = dns_id;
  1258. /* wait for async reply */
  1259. }
  1260. }
  1261. static void server_dns_callback(int id, void *client_data, const char *host, char **ips)
  1262. {
  1263. int idx = (int) client_data;
  1264. resolvserv = 0;
  1265. if (!valid_dns_id(idx, id))
  1266. return;
  1267. if (!ips) {
  1268. putlog(LOG_SERV, "*", "Failed connect to %s (DNS lookup failed)", host);
  1269. trying_server = 0;
  1270. lostdcc(idx);
  1271. return;
  1272. }
  1273. my_addr_t addr;
  1274. char *ip = NULL;
  1275. /* FIXME: this is a temporary hack to stop bots from connecting over ipv4 when they should be on ipv6
  1276. * eventually will handle this in open_telnet(ips);
  1277. */
  1278. #ifdef USE_IPV6
  1279. if (conf.bot->net.v6) {
  1280. int i = 0;
  1281. for (i = 0; ips[i]; i++) {
  1282. if (is_dotted_ip(ips[i]) == AF_INET6) {
  1283. ip = ips[i];
  1284. break;
  1285. }
  1286. }
  1287. if (!ip) {
  1288. putlog(LOG_SERV, "*", "Failed connect to %s (Could not DNS as IPV6)", host);
  1289. trying_server = 0;
  1290. lostdcc(idx);
  1291. return;
  1292. }
  1293. } else
  1294. #endif /* USE_IPV6 */
  1295. ip = ips[0];
  1296. get_addr(ip, &addr);
  1297. if (addr.family == AF_INET)
  1298. dcc[idx].addr = htonl(addr.u.addr.s_addr);
  1299. strcpy(serverpass, (char *) dcc[idx].u.dns->cbuf);
  1300. changeover_dcc(idx, &SERVER_SOCKET, 0);
  1301. identd_open();
  1302. serv = open_telnet(ip, dcc[idx].port, 0);
  1303. if (serv < 0) {
  1304. putlog(LOG_SERV, "*", "Failed connect to %s (%s)", dcc[idx].host, strerror(errno));
  1305. trying_server = 0;
  1306. lostdcc(idx);
  1307. } else {
  1308. int i = 1;
  1309. /* set these now so if we fail disconnect_server() can cleanup right. */
  1310. dcc[idx].sock = serv;
  1311. servidx = idx;
  1312. sdprintf("Connecting to '%s' (serv: %d, servidx: %d)", dcc[idx].host, serv, servidx);
  1313. setsockopt(serv, 6, TCP_NODELAY, &i, sizeof(int));
  1314. /* Queue standard login */
  1315. dcc[idx].timeval = now;
  1316. SERVER_SOCKET.timeout_val = &server_timeout;
  1317. /* Another server may have truncated it, so use the original */
  1318. strcpy(botname, origbotname);
  1319. /* Start alternate nicks from the beginning */
  1320. altnick_char = 0;
  1321. /* reset counter so first ctcp is dumped for tcms */
  1322. first_ctcp_check = 0;
  1323. if (serverpass[0])
  1324. dprintf(DP_MODE, "PASS %s\n", serverpass);
  1325. dprintf(DP_MODE, "NICK %s\n", botname);
  1326. dprintf(DP_MODE, "USER %s localhost %s :%s\n", botuser, dcc[idx].host, botrealname);
  1327. /* Wait for async result now */
  1328. }
  1329. }