servmsg.c 38 KB

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