servmsg.c 40 KB

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