| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770 |
- /*
- * Copyright (C) 1997 Robey Pointer
- * Copyright (C) 1999 - 2002 Eggheads Development Team
- * Copyright (C) 2002 - 2014 Bryan Drewery
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- /*
- * irc.c -- part of irc.mod
- * support for channels within the bot
- *
- */
- #include "src/common.h"
- #define MAKING_IRC
- #include "irc.h"
- #include "src/adns.h"
- #include "src/match.h"
- #include "src/settings.h"
- #include "src/base64.h"
- #include "src/tandem.h"
- #include "src/net.h"
- #include "src/botnet.h"
- #include "src/botmsg.h"
- #include "src/main.h"
- #include "src/response.h"
- #include "src/set.h"
- #include "src/userrec.h"
- #include "src/misc.h"
- #include "src/rfc1459.h"
- #include "src/socket.h"
- #include "src/adns.h"
- #include "src/chanprog.h"
- #include "src/auth.h"
- #include "src/userrec.h"
- #include "src/binds.h"
- #include "src/userent.h"
- #include "src/egg_timer.h"
- #include "src/mod/share.mod/share.h"
- #include "src/mod/server.mod/server.h"
- #include "src/mod/channels.mod/channels.h"
- #include "src/mod/ctcp.mod/ctcp.h"
- #include <bdlib/src/String.h>
- #include <bdlib/src/HashTable.h>
- #include <bdlib/src/Queue.h>
- #include <bdlib/src/base64.h>
- #include <stdarg.h>
- #include <math.h>
- #define PRIO_DEOP 1
- #define PRIO_KICK 2
- #ifdef CACHE
- static cache_t *irccache = NULL;
- #endif /* CACHE */
- #define do_eI (((now - chan->channel.last_eI) > 30) ? 1 : 0)
- static time_t wait_split = 900; /* Time to wait for user to return from
- * net-split. */
- int max_bans; /* Modified by net-type 1-4 */
- int max_exempts;
- int max_invites;
- int max_modes; /* Modified by net-type 1-4 */
- static bool bounce_bans = 0;
- static bool bounce_exempts = 0;
- static bool bounce_invites = 0;
- static bool bounce_modes = 0;
- unsigned int modesperline; /* Number of modes per line to send. */
- static size_t mode_buf_len = 200; /* Maximum bytes to send in 1 mode. */
- bool use_354 = 0; /* Use ircu's short 354 /who
- * responses. */
- static bool kick_fun = 0;
- static bool ban_fun = 1;
- static bool prevent_mixing = 1; /* To prevent mixing old/new modes */
- bool include_lk = 1; /* For correct calculation
- * in real_add_mode. */
- bd::HashTable<bd::String, unsigned long> bot_counters;
- unsigned long my_cookie_counter = 0;
- static bd::Queue<bd::String> chained_who;
- static int chained_who_idx;
- static int
- voice_ok(memberlist *m, struct chanset_t *chan)
- {
- if (m->flags & EVOICE)
- return 0;
- if (m->userhost[0] && !chan->voice_non_ident && m->userhost[0] == '~')
- return 0;
- return 1;
- }
- #include "chan.cc"
- #include "mode.cc"
- #include "cmdsirc.cc"
- #include "msgcmds.cc"
- static int
- detect_offense(memberlist* m, struct chanset_t *chan, char *msg)
- {
- if (!chan || !msg
- || !(chan->capslimit || chan->colorlimit)
- || chan_sentkick(m)) //sanity check
- return 0;
- member_getuser(m, 0);
- struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
- get_user_flagrec(m->user, &fr, chan->dname, chan);
- if (glob_bot(fr) ||
- chk_noflood(fr) ||
- (m && chan->flood_exempt_mode == CHAN_FLAG_OP && chan_hasop(m)) ||
- (m && chan->flood_exempt_mode == CHAN_FLAG_VOICE && (chan_hasvoice(m) || chan_hasop(m))))
- return 0;
- int color_count = 0, hit_check = 0, hit_count = 0;
- double caps_percentage = 0, caps_count = 0;
- const double caps_limit = chan->capslimit ? double(chan->capslimit) / 100.0 : double(0);
- // Need to know how long the message is, and want to ignore spaces, so avoid a strlen() and just loop to count
- size_t tot = 0;
- if (caps_limit) {
- char *msg_check = msg;
- while (msg_check && *msg_check) {
- if (!egg_isspace(*msg_check) && *msg_check != 3 && *msg_check != 2) {
- ++tot;
- }
- ++msg_check;
- }
- if (tot >= 30) {
- hit_check = tot/5; //check in-between for hits to save waste of cpu
- } else if (!tot && !chan->colorlimit) {
- // Nothing to do, bail out
- return 0;
- }
- }
- while (msg && *msg) {
- // Skip spaces
- if (egg_isspace(*msg)) {
- ++msg;
- continue;
- }
- if (egg_isupper(*msg)) {
- ++caps_count;
- } else if (*msg == 3 || *msg == 2) {
- ++color_count;
- }
- if (hit_check && !(hit_count % hit_check)) {
- if (caps_limit) {
- caps_percentage = (caps_count)/(double(tot));
- if (caps_percentage >= caps_limit) {
- break;
- }
- }
- if (chan->colorlimit && color_count >= chan->colorlimit) {
- break;
- }
- ++hit_count;
- }
- ++msg;
- }
- if (chan->capslimit && caps_count && tot >= 6) {
- caps_percentage = (caps_count)/(double(tot));
- if (caps_percentage >= caps_limit) {
- const char *response = punish_flooder(chan, m);
- putlog(LOG_MODES, chan->name, "Caps flood (%d%%) from %s -- %s", int(caps_percentage * 100), m->nick, response);
- return 1;
- }
- } else if (chan->colorlimit && color_count) {
- if (color_count >= chan->colorlimit) {
- const char *response = punish_flooder(chan, m);
- putlog(LOG_MODES, chan->name, "Color flood (%d) from %s -- %s", color_count, m->nick, response);
- return 1;
- }
- }
- return 0;
- }
- void set_devoice(struct chanset_t *chan, memberlist* m) {
- if (!(m->flags & EVOICE)) {
- putlog(LOG_DEBUG, "@", "Giving EVOICE flag to: %s (%s)", m->nick, chan->dname);
- m->flags |= EVOICE;
- }
- }
- const char* punish_flooder(struct chanset_t* chan, memberlist* m, const char *reason) {
- if (channel_voice(chan) && chan->voice_moderate) {
- if (!chan_sentdevoice(m)) {
- add_mode(chan, '-', 'v', m->nick);
- m->flags |= SENTDEVOICE;
- set_devoice(chan, m);
- return "devoicing";
- } else {
- return "devoiced";
- }
- } else {
- if (!chan_sentkick(m)) {
- dprintf(DP_SERVER, "KICK %s %s :%s%s\n", chan->name, m->nick, kickprefix, reason ? reason : response(RES_FLOOD));
- m->flags |= SENTKICK;
- return "kicking";
- } else {
- return "kicked";
- }
- }
- return "ignoring";
- }
- void unlock_chan(struct chanset_t *chan)
- {
- if (chan->channel.drone_set_mode) {
- char buf[3] = "", *p = buf;
- if ((chan->channel.drone_set_mode & CHANINV) && !(chan->mode_pls_prot & CHANINV))
- *p++ = 'i';
- if ((chan->channel.drone_set_mode & CHANMODER) && !((chan->mode_pls_prot & CHANMODER) || (channel_voice(chan) && chan->voice_moderate)))
- *p++ = 'm';
- *p = 0;
- dprintf(DP_MODE, "MODE %s :-%s\n", chan->name[0] ? chan->name : chan->dname, buf);
- }
- chan->channel.drone_set_mode = 0;
- }
- void lockdown_chan(struct chanset_t* chan, flood_reason_t reason, const char* flood_type) {
- if (!me_op(chan)) {
- return;
- }
- egg_timeval_t howlong;
- chan->channel.drone_set_mode = 0;
- char buf[3] = "", *p = buf;
- if (!(chan->channel.mode & CHANINV) && !(chan->mode_mns_prot & CHANINV)) {
- chan->channel.drone_set_mode |= CHANINV;
- *p++ = 'i';
- }
- if (!(chan->channel.mode & CHANMODER) && !(chan->mode_mns_prot & CHANMODER)) {
- chan->channel.drone_set_mode |= CHANMODER;
- *p++ = 'm';
- }
- *p = 0;
- if (chan->channel.drone_set_mode && buf[0]) {
- chan->channel.drone_joins = 0;
- chan->channel.drone_jointime = 0;
- dprintf(DP_MODE_NEXT, "MODE %s +%s\n", chan->name[0] ? chan->name : chan->dname, buf);
- howlong.sec = chan->flood_lock_time;
- howlong.usec = 0;
- timer_create_complex(&howlong, "unlock", (Function) unlock_chan, (void *) chan, 0);
- if (reason == FLOOD_MASSJOIN) {
- putlog(LOG_MISC, "*", "Join flood detected in %s! Locking for %d seconds.", chan->dname, chan->flood_lock_time);
- } else if (reason == FLOOD_BANLIST) {
- putlog(LOG_MISC, "*", "Banlist full in %s! Locking for %d seconds.", chan->dname, chan->flood_lock_time);
- } else if (reason == FLOOD_MASS_FLOOD) {
- putlog(LOG_MISC, "*", "Mass flood (%s) detected in %s! Locking for %d seconds.", flood_type, chan->dname, chan->flood_lock_time);
- }
- }
- }
- void notice_invite(struct chanset_t *chan, char *handle, char *nick, char *uhost, bool op) {
- char fhandle[21] = "";
- const char *ops = " (auto-op)";
- if (handle)
- simple_snprintf(fhandle, sizeof(fhandle), "\002%s\002 ", handle);
- putlog(LOG_MISC, "*", "Invited %s%s(%s%s%s) to %s.", handle ? handle : "", handle ? " " : "", nick, uhost ? "!" : "", uhost ? uhost : "", chan->dname);
- bd::String msg;
- msg = bd::String::printf("\001ACTION has invited %s(%s%s%s) to %s.%s\001",
- fhandle, nick, uhost ? "!" : "", uhost ? uhost : "", chan->dname, op ? ops : "");
- privmsg(chan->name, msg, DP_MODE);
- }
- #ifdef CACHE
- static cache_t *cache_new(char *nick)
- {
- cache_t *cache = (cache_t *) my_calloc(1, sizeof(cache_t));
- cache->next = NULL;
- strlcpy(cache->nick, nick, sizeof(cache->nick));
- cache->uhost[0] = 0;
- cache->handle[0] = 0;
- // cache->user = NULL;
- cache->timeval = now;
- cache->cchan = NULL;
- list_append((struct list_type **) &irccache, (struct list_type *) cache);
- return cache;
- }
- static cache_chan_t *cache_chan_add(cache_t *cache, char *chname)
- {
- cache_chan_t *cchan = (cache_chan_t *) my_calloc(1, sizeof(cache_chan_t));
-
- cchan->next = NULL;
- strlcpy(cchan->dname, chname, sizeof(cchan->dname));
- cchan->ban = 0;
- cchan->invite = 0;
- cchan->invited = 0;
- list_append((struct list_type **) &(cache->cchan), (struct list_type *) cchan);
- return cchan;
- }
- static void cache_chan_find(cache_t *cache, cache_chan_t *cchan, char *nick, char *chname)
- {
- if (!cache)
- cache = cache_find(nick);
- if (cache) {
- for (cchan = cache->cchan; cchan && cchan->dname[0]; cchan = cchan->next) {
- if (!rfc_casecmp(cchan->dname, chname)) {
- return;
- }
- }
- }
- return;
- }
- static void cache_chan_del(char *nick, char *chname) {
- cache_t *cache = NULL;
- cache_chan_t *cchan = NULL;
- cache_chan_find(cache, cchan, nick, chname);
- if (cchan) {
- list_delete((struct list_type **) &cache->cchan, (struct list_type *) cchan);
- free(cache);
- }
- }
- static cache_t *cache_find(char *nick)
- {
- cache_t *cache = NULL;
- for (cache = irccache; cache && cache->nick[0]; cache = cache->next)
- if (!rfc_casecmp(cache->nick, nick))
- break;
- return cache;
- }
- static void cache_del(char *nick, cache_t *cache)
- {
- if (!cache)
- cache = cache_find(nick);
-
- if (cache) {
- list_delete((struct list_type **) &irccache, (struct list_type *) cache);
- free(cache);
- }
- }
- static void cache_debug(void)
- {
- cache_t *cache = NULL;
- cache_chan_t *cchan = NULL;
- for (cache = irccache; cache && cache->nick[0]; cache = cache->next) {
- dprintf(DP_MODE, "PRIVMSG #wraith :%s!%s (%s)\n", cache->nick, cache->uhost, cache->handle);
- for (cchan = cache->cchan; cchan && cchan->dname[0]; cchan = cchan->next)
- dprintf(DP_MODE, "PRIVMSG #wraith :%s %d %d %d\n", cchan->dname, cchan->ban, cchan->invite, cchan->invited);
- }
- }
- #endif /* CACHE */
- static void cache_invite(struct chanset_t *chan, char *nick, char *host, char *handle, bool op, bool bot)
- {
- #ifdef CACHE
- cache_t *cache = NULL;
- if ((cache = cache_find(nick)) == NULL)
- cache = cache_new(nick);
- cache->bot = bot;
- /* if we find they have a host but it doesnt match the new host, wipe it */
- if (host && cache->uhost[0] && strcasecmp(cache->uhost, host))
- cache->uhost[0] = 0;
- if (host && !cache->uhost[0])
- strlcpy(cache->uhost, host, sizeof(cache->uhost));
- /* if we find they have a handle but it doesnt match the new handle, wipe it */
- if (handle && cache->handle[0] && strcasecmp(cache->handle, handle))
- cache->handle[0] = 0;
- if (handle && !cache->handle[0])
- strlcpy(cache->handle, handle, sizeof(cache->handle));
- cache_chan_t *cchan = cache_chan_add(cache, chan->dname);
- cchan->op = op;
-
- if (!host) {
- cchan->invite = 1;
- dprintf(DP_MODE, "USERHOST %s\n", nick);
- return;
- }
- /* if we have a uhost already, it's safe to invite them */
- cchan->invited = 1;
- cchan->invite = 0;
- #endif /* CACHE */
- dprintf(DP_SERVER, "INVITE %s %s\n", nick, chan->name);
- }
- static inline const char * cookie_hash(const char* chname, const memberlist* opper, const memberlist* opped, const char* ts, const char* randstring, const char* key) {
- char tohash[201] = "";
- const char salt2[] = SALT2;
- simple_snprintf(tohash, sizeof(tohash), STR("%c%s%c%c%c\n%c%c%s%s%s"),
- salt2[0],
- ts,
- randstring[0], randstring[1], randstring[2], randstring[3],
- salt2[15],
- opped->nick,
- opped->userhost,
- key);
- #ifdef DEBUG
- sdprintf("chname: %s ts: %s randstring: %c%c%c%c", chname, ts, randstring[0], randstring[1], randstring[2], randstring[3]);
- sdprintf("tohash: %s", tohash);
- #endif
- const char* md5 = MD5(tohash);
- OPENSSL_cleanse(tohash, sizeof(tohash));
- return md5;
- }
- static inline void cookie_key(char *key, size_t key_len, const char* randstring, const memberlist *opper, const char *chname) {
- const char salt1[] = SALT1;
- const char salt2[] = SALT2;
- simple_snprintf2(key, key_len, STR("%c%c%c%^s%c%c%c%c%c%c%^s%c%c%c%c%c%c%c%s"),
- randstring[0],
- salt1[5],
- randstring[3],
- opper->user->handle,
- randstring[2],
- salt1[4],
- salt1[0],
- salt1[1],
- salt1[3],
- salt1[6],
- chname,
- salt1[10],
- randstring[1],
- salt2[15],
- salt2[13],
- salt1[10],
- salt2[3],
- salt2[1],
- opper->userhost
- );
- }
- #define HASH_INDEX1(_x) (8 + (_x))
- #define HASH_INDEX2(_x) (16 + (_x))
- #define HASH_INDEX3(_x) (18 + (_x))
- void makecookie(char *out, size_t len, const char *chname, const memberlist* opper, const memberlist* m1, const memberlist* m2, const memberlist* m3) {
- char randstring[5] = "", ts[11] = "";
- make_rand_str(randstring, 4);
- /* &ts[4] is now last 6 digits of time */
- simple_snprintf(ts, sizeof(ts), "%010li", (long) (now + timesync));
- char cookie_clear[101] = "";
- //Increase my counter
- ++my_cookie_counter;
- if (my_cookie_counter > (unsigned long)(-500))
- my_cookie_counter = 0;
- simple_snprintf2(cookie_clear, sizeof(cookie_clear), STR("%s%s%D"), randstring, &ts[3], my_cookie_counter);
- char key[150] = "";
- cookie_key(key, sizeof(key), randstring, opper, chname);
- const char* hash1 = cookie_hash(chname, opper, m1, &ts[4], randstring, key);
- const char* hash2 = m2 ? cookie_hash(chname, opper, m2, &ts[4], randstring, key) : NULL;
- const char* hash3 = m3 ? cookie_hash(chname, opper, m3, &ts[4], randstring, key) : NULL;
- bd::String cookie = encrypt_string(MD5(key), bd::String(cookie_clear));
- cookie = bd::base64Encode(cookie);
- #ifdef DEBUG
- sdprintf("key: %s", key);
- sdprintf("cookie_clear: %s", cookie_clear);
- sdprintf("hash1: %s", hash1);
- if (hash2) sdprintf("hash2: %s", hash2);
- if (hash3) sdprintf("hash3: %s", hash3);
- #endif
- if (m3)
- simple_snprintf(out, len + 1, STR("%c%c%c%c%c%c%c%c%c!%s@%s"),
- hash1[HASH_INDEX1(0)],
- hash1[HASH_INDEX2(0)],
- hash1[HASH_INDEX3(0)],
- hash2[HASH_INDEX1(1)],
- hash2[HASH_INDEX2(1)],
- hash2[HASH_INDEX3(1)],
- hash3[HASH_INDEX1(2)],
- hash3[HASH_INDEX2(2)],
- hash3[HASH_INDEX3(2)],
- randstring,
- cookie.c_str());
- else if (m2)
- simple_snprintf(out, len + 1, STR("%c%c%c%c%c%c!%s@%s"),
- hash1[HASH_INDEX1(0)],
- hash1[HASH_INDEX2(0)],
- hash1[HASH_INDEX3(0)],
- hash2[HASH_INDEX1(1)],
- hash2[HASH_INDEX2(1)],
- hash2[HASH_INDEX3(1)],
- randstring,
- cookie.c_str());
- else
- simple_snprintf(out, len + 1, STR("%c%c%c!%s@%s"),
- hash1[HASH_INDEX1(0)],
- hash1[HASH_INDEX2(0)],
- hash1[HASH_INDEX3(0)],
- randstring,
- cookie.c_str());
- #ifdef DEBUG
- sdprintf("cookie: %s", out);
- #endif
- }
- // Clear counter for bot
- void counter_clear(const char* botnick) {
- bot_counters[botnick] = 0;
- }
- static inline int checkcookie(const char *chname, const memberlist* opper, const memberlist* opped, const char *cookie, int indexHint) {
- #define HOST(_x) (6 + (_x) + ((hashes << 1) + hashes)) /* x + (hashes * 3) */
- #define SALT(_x) (1 + (_x) + ((hashes << 1) + hashes)) /* x + (hashes * 3) */
- /* How many hashes are in the cookie? */
- const size_t hashes = cookie[3] == '!' ? 1 : (cookie[6] == '!' ? 2 : 3);
- char key[150] = "";
- const char *randstring = &cookie[SALT(0)];
- cookie_key(key, sizeof(key), randstring, opper, chname);
- bd::String ciphertext = bd::base64Decode((char*) &cookie[HOST(0)]);
- bd::String cleartext = decrypt_string(MD5(key), ciphertext);
- char ts[8] = "";
- strlcpy(ts, cleartext.c_str() + 4, sizeof(ts));
- unsigned long counter = base64_to_int(cleartext.c_str() + 4 + 7);
- //Lookup counter for the opper
- unsigned long last_counter = 0;
- // Don't check counter for my own ops as it may have incremented in the queue before seeing the last last counter
- bd::String handle;
- if (conf.bot->u != opper->user) {
- handle = opper->user->handle;
- if (bot_counters.contains(handle))
- last_counter = bot_counters[handle];
- else
- last_counter = 0;
- }
- #ifdef DEBUG
- sdprintf("key: %s", key);
- sdprintf("plaintext from cookie: %s", cleartext.c_str());
- sdprintf("ts from cookie: %s", ts);
- if (indexHint == 0) {
- sdprintf("last counter from %s: %lu", opper->user->handle, last_counter);
- sdprintf("counter from cookie: %lu", counter);
- }
- #endif
- const time_t optime = atol(ts);
- if ((((now + timesync) % 10000000) - optime) > 3900)
- return BC_SLACK;
- //Only check on the first cookie
- if (indexHint == 0 && conf.bot->u != opper->user) {
- if (counter <= last_counter)
- return BC_COUNTER;
- // graceful overflow
- if (counter > (unsigned long)(-1000))
- counter = 0;
- //Update counter for the opper
- bot_counters[handle] = counter;
- }
- const char *hash = cookie_hash(chname, opper, opped, &ts[1], randstring, key);
- #ifdef DEBUG
- sdprintf("hash: %s", hash);
- #endif
- /* Compare the expected hash to each of the given hashes */
- /* indexHint, Which position of the +ooo are we? (1) could be either index (1) or (2).. but not (0). */
- /* See if any of the cookies match the hash we want */
- for (size_t i = indexHint; i < hashes; ++i) {
- const char *cookie_index = cookie + ((i << 1) + i); /* i * 3 */
- if ((hash[HASH_INDEX1(i)] == cookie_index[0] &&
- hash[HASH_INDEX2(i)] == cookie_index[1] &&
- hash[HASH_INDEX3(i)] == cookie_index[2])) {
- return 0;
- }
- }
- /* None matched -> failure */
- return BC_HASH;
- #undef HOST
- #undef SALT
- }
- /*
- opreq = o #chan nick
- inreq = i #chan nick uhost
- inreq keyreply = K #chan key
- */
- void
- getin_request(char *botnick, char *code, char *par)
- {
- if (unlikely(!server_online))
- return;
- if (unlikely(!par[0] || !par))
- return;
- char *what = newsplit(&par), *chname = newsplit(&par);
- if (unlikely(!chname[0] || !chname))
- return;
- char *tmp = newsplit(&par); /* nick */
- if (unlikely(!tmp[0]))
- return;
- char nick[NICKLEN] = "";
- strlcpy(nick, tmp, sizeof(nick));
- const char *type = what[0] == 'o' ? "op" : "in", *desc = what[0] == 'o' ? "on" : "for";
- struct chanset_t *chan = findchan_by_dname(chname);
- if (unlikely(!chan)) {
- putlog(LOG_GETIN, "*", "%sreq from %s/%s %s %s which is not a valid channel!", type, botnick, nick, desc, chname);
- return;
- }
- struct userrec *u = get_user_by_handle(userlist, botnick);
- if (unlikely(!u)) {
- putlog(LOG_GETIN, "*", "%sreq from %s/%s %s %s - No user called %s in userlist", type, botnick, nick, desc,
- chan->dname, botnick);
- return;
- }
- if (connect_bursting) {
- putlog(LOG_GETIN, "*", "%sreq from %s/%s %s %s - I'm in connect burst mode.", type, botnick, nick, desc, chan->dname);
- return;
- }
- if (server_lag > lag_threshold) {
- putlog(LOG_GETIN, "*", "%sreq from %s/%s %s %s - I'm too lagged", type, botnick, nick, desc, chan->dname);
- return;
- }
- if (what[0] != 'K') {
- if (!(channel_pending(chan) || channel_active(chan))) {
- return;
- }
- }
- memberlist* mem = ismember(chan, nick);
- if (mem && chan_issplit(mem)) {
- putlog(LOG_GETIN, "*", "%sreq from %s/%s %s %s - %s is split", type, botnick, nick, desc, chan->dname, nick);
- return;
- }
- if (what[0] == 'K') {
- if (unlikely(!shouldjoin(chan))) {
- putlog(LOG_GETIN, "*", "Got key for %s from %s - I shouldn't be on that chan?!?", chan->dname, botnick);
- } else {
- if (!(channel_pending(chan) || channel_active(chan))) {
- my_setkey(chan, nick);
- putlog(LOG_GETIN, "*", "Got key for %s from %s (%s) - Joining", chan->dname, botnick, chan->channel.key);
- join_chan(chan);
- } else {
- putlog(LOG_GETIN, "*", "Got key for %s from %s - I'm already in the channel", chan->dname, botnick);
- }
- }
- return;
- }
- char uhost[UHOSTLEN] = "";
- struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
- tmp = newsplit(&par); /* userhost */
- if (tmp[0])
- strlcpy(uhost, tmp, sizeof(uhost));
- if (what[0] == 'o') {
- if (!me_op(chan)) {
- putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - I haven't got ops", botnick, nick, chan->dname);
- return;
- }
- if (!mem) {
- putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - %s isn't on %s", botnick, nick, chan->dname, nick, chan->dname);
- return;
- }
- if (chan_hasop(mem)) {
- putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - %s already has ops", botnick, nick, chan->dname, nick);
- return;
- }
- if (chan_sentop(mem)) {
- putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - Already sent a +o", botnick, nick, chan->dname);
- return;
- }
- member_getuser(mem);
- if (mem->user != u) {
- putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - user mismatch: (%s)/(%s)", botnick, nick, chan->dname, u ? u->handle : "*", mem->user ? mem->user->handle : "*");
- return;
- }
- get_user_flagrec(u, &fr, chan->dname, chan);
- if (!chk_op(fr, chan)) {
- putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - %s doesnt have +o for chan.", botnick, nick, chan->dname, botnick);
- return;
- }
- if (unlikely(glob_kick(fr) || chan_kick(fr))) {
- putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - %s is currently being autokicked.", botnick, nick, chan->dname, botnick);
- return;
- }
- // Does the remote bot have the same number of clients in its channel as me? And a shared member?
- const char *bot_network = tmp;
- int members = atoi(tmp);
- char *shared_nick = par[0] ? newsplit(&par) : NULL;
- memberlist* shared_member = shared_nick ? ismember(chan, shared_nick) : NULL;
- char *shared_host = par[0] ? newsplit(&par) : NULL;
- if (!shared_nick || !shared_member || !shared_host || (strcasecmp(curnetwork, bot_network) && ((chan->channel.members - chan->channel.splitmembers) != members)) || strcmp(shared_host, shared_member->userhost)) {
- putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - Bot seems to be on a different network '%s' / me: '%s'", botnick, nick, chan->dname, bot_network, curnetwork);
- return;
- }
- if (chan->channel.no_op) {
- if (chan->channel.no_op > now) /* dont op until this time has passed */
- return;
- else
- chan->channel.no_op = 0;
- }
- do_op(nick, chan, 0, 1);
- putlog(LOG_GETIN, "*", "opreq from %s/%s on %s - Opped", botnick, nick, chan->dname);
- } else if (what[0] == 'i') {
- // Should I respond to this request?
- // If there's 18 eligible bots in the channel, and in-bots is 2, I have a 2/18 chance of replying.
- int eligible_bots = 0;
- for (memberlist* m = chan->channel.member; m && m->nick[0]; m = m->next) {
- if (chan_hasop(m)) {
- member_getuser(m, 0);
- if (m->user && m->user->bot) {
- ++eligible_bots;
- }
- }
- }
- if (!eligible_bots) {
- return;
- }
- if (!((randint(eligible_bots) + 1) <= static_cast<unsigned int>(in_bots))) {
- // Not my turn
- return;
- }
- if (mem) {
- putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - %s is already on %s", botnick, nick, chan->dname, nick, chan->dname);
- return;
- }
- get_user_flagrec(u, &fr, chan->dname, chan);
- if (unlikely(!chk_op(fr, chan) || chan_kick(fr) || glob_kick(fr))) {
- putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - %s doesn't have acces for chan.", botnick, nick, chan->dname, botnick);
- return;
- }
- if (chan->channel.mode & CHANKEY) {
- char *key = chan->channel.key[0] ? chan->channel.key : chan->key_prot;
- size_t siz = strlen(chan->dname) + strlen(key ? key : 0) + 6 + 1;
- tmp = (char *) my_calloc(1, siz);
- simple_snprintf(tmp, siz, "gi K %s %s", chan->dname, key ? key : "");
- putbot(botnick, tmp);
- putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - Sent key (%s)", botnick, nick, chan->dname, key ? key : "");
- free(tmp);
- }
- if (!me_op(chan)) {
- putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - I haven't got ops", botnick, nick, chan->dname);
- return;
- }
- bool sendi = 0;
- if (chan->channel.maxmembers) {
- if (raise_limit(chan, 5)) {
- putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - Raised limit", botnick, nick, chan->dname);
- }
- }
- char uip[UHOSTLEN] = "";
- tmp = newsplit(&par); /* userip */
- if (tmp[0])
- simple_snprintf(uip, sizeof uip, "%s!%s", nick, tmp);
- struct maskrec **mr = NULL, *tmr = NULL;
- /* Check internal global bans */
- mr = &global_bans;
- while (*mr) {
- if (unlikely(wild_match((*mr)->mask, uhost) ||
- wild_match((*mr)->mask, uip) ||
- match_cidr((*mr)->mask, uip))) {
- if (!noshare)
- shareout("-m b %s\n", (*mr)->mask);
- putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - Removed permanent global ban %s", botnick, nick,
- chan->dname, (*mr)->mask);
- free((*mr)->mask);
- free((*mr)->desc);
- free((*mr)->user);
- tmr = *mr;
- *mr = (*mr)->next;
- free(tmr);
- } else {
- mr = &((*mr)->next);
- }
- }
- /* Check internal channel bans */
- mr = &chan->bans;
- while (*mr) {
- if (unlikely(wild_match((*mr)->mask, uhost) ||
- wild_match((*mr)->mask, uip) ||
- match_cidr((*mr)->mask, uip))) {
- if (!noshare)
- shareout("-mc b %s %s\n", chan->dname, (*mr)->mask);
- putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - Removed permanent channel ban %s", botnick, nick,
- chan->dname, (*mr)->mask);
- free((*mr)->mask);
- free((*mr)->desc);
- free((*mr)->user);
- tmr = *mr;
- *mr = (*mr)->next;
- free(tmr);
- } else {
- mr = &((*mr)->next);
- }
- }
- /* Check bans active in channel */
- for (struct maskstruct *b = chan->channel.ban; b->mask[0]; b = b->next) {
- if (unlikely(wild_match(b->mask, uhost) ||
- wild_match(b->mask, uip) ||
- match_cidr(b->mask, uip))) {
- add_mode(chan, '-', 'b', b->mask);
- putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - Removed active ban %s", botnick, nick, chan->dname,
- b->mask);
- sendi = 1;
- }
- }
- if (chan->channel.mode & CHANINV) {
- sendi = 1;
- putlog(LOG_GETIN, "*", "inreq from %s/%s for %s - Invited", botnick, nick, chan->dname);
- }
- if (sendi)
- dprintf(DP_MODE, "INVITE %s %s\n", nick, chan->name[0] ? chan->name : chan->dname);
- }
- }
- static void
- request_op(struct chanset_t *chan)
- {
- if (!chan || (chan && (channel_pending(chan) || !shouldjoin(chan) || !channel_active(chan) || me_op(chan)))) {
- chan->channel.do_opreq = 0;
- return;
- }
- if (chan->channel.no_op) {
- if (chan->channel.no_op > now) /* dont op until this time has passed */
- return;
- else
- chan->channel.no_op = 0;
- }
- /* check server lag */
- if (server_lag > lag_threshold) {
- putlog(LOG_GETIN, "*", "Not asking for ops on %s - I'm too lagged", chan->dname);
- chan->channel.no_op = now + 10;
- return;
- }
- /* Check if my hostmask is recognized (every 10th time) */
- static int check_hostmask_cnt = 10;
- if (check_hostmask_cnt++ == 10) {
- check_hostmask();
- check_hostmask_cnt = 0;
- }
- struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_BOT, 0, 0, 0 };
- get_user_flagrec(conf.bot->u, &fr, chan->dname, chan);
- if (!chk_op(fr, chan) || glob_kick(fr) || chan_kick(fr)) {
- putlog(LOG_GETIN, "*", "Not requesting op for %s - I do not have access to that channel.", chan->dname);
- chan->channel.no_op = now + 10;
- return;
- }
- int i = 0, my_exp = 0, first = 100;
- time_t n = now;
- /* max OPREQ_COUNT requests per OPREQ_SECONDS sec */
- while (i < 5) {
- if (n - chan->opreqtime[i] > op_requests.time) {
- if (first > i)
- first = i;
- my_exp++;
- chan->opreqtime[i] = 0;
- }
- i++;
- }
- if ((5 - my_exp) >= op_requests.count) {
- putlog(LOG_GETIN, "*", "Delaying opreq for %s - Maximum of %d:%d reached", chan->dname, op_requests.count,
- op_requests.time);
- chan->channel.no_op = now + op_requests.time + 1;
- return;
- }
- int cnt = op_bots, i2 = 0;
- memberlist *ml = NULL, *botops[MAX_BOTS];
- char s[UHOSTLEN] = "";
- for (i = 0, ml = chan->channel.member; (i < MAX_BOTS) && ml && ml->nick[0]; ml = ml->next) {
- if (!chan_hasop(ml) || chan_issplit(ml))
- continue;
- member_getuser(ml, 1);
- if (!ml->user || !ml->user->bot)
- continue;
- /* Is the bot linked? */
- if (findbot(ml->user->handle))
- botops[i++] = ml;
- }
- if (!i) {
- if (channel_active(chan) && !channel_pending(chan)) {
- chan->channel.no_op = now + op_requests.time;
- putlog(LOG_GETIN, "*", "No one to ask for ops on %s - Delaying requests for %d seconds.", chan->dname, op_requests.time);
- }
- return;
- }
- char l[1024] = "";
- size_t len = 0;
- // Pick a random member to use as verification
- memberlist* shared_member = NULL;
- for (int z = 0; z < 5; ++z) {
- int shared_member_cnt = z < 4 ? randint(chan->channel.members) : 0;
- int shared_idx = 0;
- for (shared_member = chan->channel.member; shared_member && shared_member->nick[0]; shared_member = shared_member->next) {
- if (shared_member->split) continue;
- if (shared_idx >= shared_member_cnt)
- break;
- ++shared_idx;
- }
- if (shared_member) break;
- }
- if (!shared_member) {
- chan->channel.no_op = now + op_requests.time;
- putlog(LOG_GETIN, "*", "Too many split clients on %s - Delaying requests for %d seconds.", chan->dname, op_requests.time);
- return;
- }
- /* first scan for bots on my server, ask first found for ops */
- simple_snprintf(s, sizeof(s), "gi o %s %s %s %s %s", chan->dname, botname, curnetwork, shared_member->nick, shared_member->userhost);
- /* look for bots 0-1 hops away */
- for (i2 = 0; i2 < i; i2++) {
- if (botops[i2]->hops < 2) {
- putbot(botops[i2]->user->handle, s);
- chan->opreqtime[first] = n;
- if (l[0])
- len += strlcpy(l + len, ", ", sizeof(l) - len);
- len += strlcpy(l + len, botops[i2]->user->handle, sizeof(l) - len);
- len += strlcpy(l + len, "/", sizeof(l) - len);
- len += strlcpy(l + len, botops[i2]->nick, sizeof(l) - len);
- botops[i2] = NULL;
- --cnt;
- break;
- }
- //FIXME: Also prefer our localhub or child bots
- }
- /* Pick random op and ask for ops */
- while (cnt) {
- i2 = randint(i);
- if (botops[i2]) {
- putbot(botops[i2]->user->handle, s);
- chan->opreqtime[first] = n;
- if (l[0])
- len += strlcpy(l + len, ", ", sizeof(l) - len);
- len += strlcpy(l + len, botops[i2]->user->handle, sizeof(l) - len);
- len += strlcpy(l + len, "/", sizeof(l) - len);
- len += strlcpy(l + len, botops[i2]->nick, sizeof(l) - len);
- --cnt;
- botops[i2] = NULL;
- } else {
- if (i < op_bots)
- cnt--;
- }
- }
- l[len] = 0;
- putlog(LOG_GETIN, "*", "Requested ops on %s from %s", chan->dname, l);
- }
- static void
- request_in(struct chanset_t *chan)
- {
- /* Lag situation */
- if (!shouldjoin(chan))
- return;
- /* Check if my hostmask is recognized (every 10th time) */
- static int check_hostmask_cnt = 10;
- if (check_hostmask_cnt++ == 10) {
- check_hostmask();
- check_hostmask_cnt = 0;
- }
- struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_BOT, 0, 0, 0 };
- get_user_flagrec(conf.bot->u, &fr, chan->dname, chan);
- if (!chk_op(fr, chan) || glob_kick(fr) || chan_kick(fr)) {
- putlog(LOG_GETIN, "*", "Not requesting help to join %s - I do not have access to that channel.", chan->dname);
- return;
- }
- int foundBots = 0;
- // char* botops[MAX_BOTS];
- for (tand_t* bot = tandbot; bot && (foundBots < MAX_BOTS); bot = bot->next) {
- if (bot->hub || !bot->u)
- continue;
- get_user_flagrec(bot->u, &fr, chan->dname, chan);
- if (bot_shouldjoin(bot->u, &fr, chan) && chk_op(fr, chan)) {
- ++foundBots;
- // botops[foundBots++] = bot->bot;
- }
- }
- if (!foundBots) {
- putlog(LOG_GETIN, "*", "No bots available, can't request help to join %s", chan->dname);
- return;
- }
- bd::String request(bd::String::printf("gi i %s %s %s!%s %s", chan->dname, botname, botname, botuserhost, botuserip));
- putallbots(request.c_str());
- putlog(LOG_GETIN, "*", "Requested help to join %s", chan->dname);
- }
- /* Set the key.
- */
- void
- my_setkey(struct chanset_t *chan, char *k)
- {
- free(chan->channel.key);
- chan->channel.key = k ? strdup(k) : (char *) my_calloc(1, 1);
- }
- /* Adds a ban, exempt or invite mask to the list
- * m should be chan->channel.(exempt|invite|ban)
- */
- static bool
- new_mask(masklist *m, char *s, char *who)
- {
- for (; m && m->mask[0] && rfc_casecmp(m->mask, s); m = m->next) ;
- if (m->mask[0])
- return 1; /* Already existent mask */
- m->next = (masklist *) my_calloc(1, sizeof(masklist));
- m->next->next = NULL;
- m->next->mask = (char *) my_calloc(1, 1);
- free(m->mask);
- m->mask = strdup(s);
- m->who = strdup(who);
- m->timer = now;
- return 0;
- }
- /* Removes a nick from the channel member list (returns 1 if successful)
- */
- static bool
- killmember(struct chanset_t *chan, char *nick, bool cacheMember)
- {
- memberlist *x = NULL, *old = NULL;
- for (x = chan->channel.member; x && x->nick[0]; old = x, x = x->next)
- if (!rfc_casecmp(x->nick, nick))
- break;
- if (unlikely(!x || !x->nick[0])) {
- if (!channel_pending(chan))
- putlog(LOG_MISC, "*", "(!) killmember(%s, %s) -> nonexistent", chan->dname, nick);
- return 0;
- }
- if (old)
- old->next = x->next;
- else
- chan->channel.member = x->next;
- if (cacheMember) {
- x->last = now;
- // Don't delete here, will delete when it expires from the cache.
- (*chan->channel.cached_members)[x->userhost] = x;
- } else {
- chan->channel.cached_members->remove(x->userhost);
- delete_member(x);
- }
- --chan->channel.members;
- /* The following two errors should NEVER happen. We will try to correct
- * them though, to keep the bot from crashing.
- */
- if (unlikely(chan->channel.members < 0)) {
- chan->channel.members = 0;
- chan->channel.splitmembers = 0;
- for (x = chan->channel.member; x && x->nick[0]; x = x->next) {
- chan->channel.members++;
- if (x->split)
- ++(chan->channel.splitmembers);
- }
- putlog(LOG_MISC, "*", "(!) actually I know of %d members.", chan->channel.members);
- }
- if (unlikely(!chan->channel.member)) {
- chan->channel.member = (memberlist *) my_calloc(1, sizeof(memberlist));
- chan->channel.member->nick[0] = 0;
- chan->channel.member->next = NULL;
- }
- return 1;
- }
- /**
- * Update the member with cached information from a parted/quitted member
- */
- static void member_update_from_cache(struct chanset_t* chan, memberlist *m) {
- using std::swap;
- // Are they in the cache?
- const bd::String userhost(m->userhost);
- if (chan->channel.cached_members->contains(userhost)) {
- memberlist *cached_member = (*chan->channel.cached_members)[userhost];
- // Update important flood tracking information
- swap(m->floodtime, cached_member->floodtime);
- swap(m->floodnum, cached_member->floodnum);
- // Update EVOICE flag
- if (cached_member->flags & EVOICE) {
- m->flags |= EVOICE;
- }
- // No longer need the cached member
- delete_member(cached_member);
- chan->channel.cached_members->remove(userhost);
- }
- }
- /* Check whether I'm voice. Returns boolean 1 or 0.
- */
- bool
- me_voice(const struct chanset_t *chan)
- {
- memberlist *mx = ismember(chan, botname);
- if (!mx)
- return 0;
- if (chan_hasvoice(mx))
- return 1;
- else
- return 0;
- }
- /* Check if there are any ops on the channel. Returns boolean 1 or 0.
- */
- static bool
- any_ops(struct chanset_t *chan)
- {
- memberlist *x = NULL;
- for (x = chan->channel.member; x && x->nick[0]; x = x->next)
- if (chan_hasop(x))
- break;
- if (!x || !x->nick[0])
- return 0;
- return 1;
- }
- static void get_channel_masks(struct chanset_t* chan) {
- bd::String tocheck(size_t(4));
- if (!(chan->ircnet_status & CHAN_ASKEDBANS)) {
- chan->ircnet_status |= CHAN_ASKEDBANS;
- tocheck += 'b';
- }
- if (do_eI) {
- chan->channel.last_eI = now;
- if (!(chan->ircnet_status & CHAN_ASKED_EXEMPTS) && use_exempts == 1) {
- chan->ircnet_status |= CHAN_ASKED_EXEMPTS;
- tocheck += 'e';
- }
- if (!(chan->ircnet_status & CHAN_ASKED_INVITES) && use_invites == 1) {
- chan->ircnet_status |= CHAN_ASKED_INVITES;
- tocheck += 'I';
- }
- }
- if (tocheck.length())
- dprintf(DP_MODE, "MODE %s %s\n", chan->name, tocheck.c_str());
- }
- /* Reset the channel information.
- */
- void
- reset_chan_info(struct chanset_t *chan)
- {
- if (!chan) return;
- if (!chan->name[0])
- strlcpy(chan->name, chan->dname, sizeof(chan->name));
- /* Don't reset the channel if we're already resetting it */
- if (!shouldjoin(chan)) {
- sdprintf("Resetting %s but I shouldn't be there, parting...", chan->dname);
- dprintf(DP_MODE, "PART %s\n", chan->name);
- return;
- }
- if (!channel_pending(chan)) {
- bool opped = me_op(chan) ? 1 : 0;
- my_setkey(chan, NULL);
- clear_channel(chan, 1);
- chan->ircnet_status |= CHAN_PEND;
- chan->ircnet_status &= ~(CHAN_ACTIVE | CHAN_ASKEDMODES | CHAN_JOINING);
- /* don't bother checking bans if it's +take */
- if (!channel_take(chan)) {
- if (opped) {
- get_channel_masks(chan);
- }
- }
- /* These 2 need to get out asap, so into the mode queue */
- dprintf(DP_MODE, "MODE %s\n", chan->name);
- send_chan_who(DP_MODE, chan, 1);
- /* clear_channel nuked the data...so */
- dprintf(DP_HELP, "TOPIC %s\n", chan->name);//Topic is very low priority
- }
- }
- static void send_chan_who(int queue, struct chanset_t *chan, bool chain) {
- if (chain) {
- if (!chained_who.contains(chan->name))
- chained_who.enqueue(chan->name);
- chained_who_idx = queue;
- if (chained_who.size() > 1)
- return;
- }
- if (use_354) /* Added benefit of getting numeric IP! :) */
- dprintf(queue, "WHO %s %%c%%h%%n%%u%%f%%r%%d%%i\n", chan->name);
- else
- dprintf(queue, "WHO %s\n", chan->name);
- }
- void force_join_chan(struct chanset_t* chan, int idx) {
- chan->ircnet_status = 0;
- join_chan(chan, idx);
- }
- void join_chan(struct chanset_t* chan, int idx) {
- if (shouldjoin(chan) && !(chan->ircnet_status & (CHAN_ACTIVE | CHAN_PEND | CHAN_JOINING))) {
- dprintf(idx, "JOIN %s %s\n",
- (chan->name[0]) ? chan->name : chan->dname,
- chan->channel.key[0] ? chan->channel.key : chan->key_prot);
- clear_channel(chan, 1);
- chan->ircnet_status |= CHAN_JOINING;
- }
- }
- /* If i'm the only person on the channel, and i'm not op'd,
- * might as well leave and rejoin. If i'm NOT the only person
- * on the channel, but i'm still not op'd, demand ops.
- */
- static void
- check_lonely_channel(struct chanset_t *chan)
- {
- if (channel_pending(chan) || !channel_active(chan) || me_op(chan) ||
- !shouldjoin(chan) || (chan->channel.mode & CHANANON))
- return;
- static int whined = 0;
- if ((chan->channel.members - chan->channel.splitmembers) == 1 && channel_cycle(chan) && !channel_stop_cycle(chan)) {
- if (chan->name[0] != '+') { /* Its pointless to cycle + chans for ops */
- putlog(LOG_MISC, "*", "Trying to cycle %s to regain ops.", chan->dname);
- dprintf(DP_MODE, "PART %s\n", chan->name);
- // Will auto rejoin once the bot PARTs
- whined = 0;
- }
- } else if (any_ops(chan)) {
- whined = 0;
- } else {
- /* Other people here, but none are ops. If there are other bots make
- * them LEAVE!
- */
- if (!whined) {
- /* + is opless. Complaining about no ops when without special
- * help(services), we cant get them - Raist
- */
- if (chan->name[0] != '+')
- putlog(LOG_MISC, "*", "%s is active but has no ops :(", chan->dname);
- whined = 1;
- }
- #ifdef disabled
- memberlist *m = NULL;
- bool ok = 1;
- for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
- member_getuser(m, 0);
- if (!m->is_me && (!m->user || !m->user->bot)) {
- ok = 0;
- break;
- }
- }
- if (ok && channel_cycle(chan)) {
- /* ALL bots! make them LEAVE!!! */
- /*
- for (m = chan->channel.member; m && m->nick[0]; m = m->next)
- if (!m->is_me)
- dprintf(DP_SERVER, "PRIVMSG %s :go %s\n", m->nick, chan->dname);
- */
- }
- #endif
- }
- }
- static void
- warn_pls_take(struct chanset_t *chan)
- {
- if (channel_take(chan) && me_op(chan))
- putlog(LOG_WARN, "*", "%s is set +take, and I'm already opped! +take is insecure, try +bitch instead",
- chan->dname);
- }
- /* FIXME: max sendq will occur. */
- static void
- check_servers(struct chanset_t *chan)
- {
- for (memberlist *m = chan->channel.member; m && m->nick[0]; m = m->next) {
- if (!m->is_me && chan_hasop(m) && (m->hops == -1)) {
- putlog(LOG_DEBUG, "*", "Updating WHO for '%s' because '%s' is missing data.", chan->dname, m->nick);
- send_chan_who(DP_HELP, chan);
- break; /* lets just do one chan at a time to save from flooding */
- }
- }
- }
- static void do_protect(struct chanset_t* chan, const char* reason) {
- // Don't bother with these if already botbitch, already processed it, or it's a hacked bot and +botbitch won't help.
- if (!channel_botbitch(chan)) {
- if (chan->protect_backup) {
- putlog(LOG_MISC, "*", "%s detected in %s: Setting +botbitch/+backup to protect the channel.", reason, chan->dname);
- do_chanset(NULL, chan, "+botbitch +bitch +backup", DO_LOCAL | DO_NET);
- } else {
- putlog(LOG_MISC, "*", "%s detected in %s: Setting +botbitch to protect the channel.", reason, chan->dname);
- do_chanset(NULL, chan, "+botbitch +bitch", DO_LOCAL | DO_NET);
- }
- // enforce_closed(chan);
- // dprintf(DP_MODE, "TOPIC %s :Auto-closed - channel fight\n", chan->name);
- // enforce_bitch(chan);
- reversing = 1; // Reverse any modes which triggered this.
- }
- }
- static void
- check_netfight(struct chanset_t *chan)
- {
- if (channel_protect(chan) && fight_threshold) {
- if ((chan->channel.fighting) && (chan->channel.fighting > fight_threshold) && !chan_bitch(chan)) {
- do_protect(chan, "Channel fight");
- }
- }
- chan->channel.fighting = 0; /* we put this here because we need to clear it once per min */
- }
- bool
- raise_limit(struct chanset_t *chan, int default_limitraise)
- {
- if (!chan || !me_op(chan))
- return false;
- /* Don't bother setting limit if the user has set a protect -l */
- if (chan->mode_mns_prot & CHANLIMIT)
- return false;
- const int limitraise = (chan->limitraise ? ((chan->limitraise % 2 == 0) ? chan->limitraise : (chan->limitraise + 1)) : default_limitraise);
- if (limitraise) {
- const int nl = (chan->channel.members - chan->channel.splitmembers) + limitraise; /* new limit */
- const int i = limitraise >> 2; /* DIV 4 */
- /* if the newlimit will be in the range made by these vars, dont change. */
- const int ul = nl + i; /* upper limit */
- const int ll = nl - i; /* lower limit */
- if ((chan->channel.maxmembers >= ll) && (chan->channel.maxmembers <= ul)) {
- return false; /* the current limit is in the range, so leave it. */
- }
- if (nl != chan->channel.maxmembers) {
- char s[6] = "";
- simple_snprintf(s, sizeof(s), "%d", nl);
- add_mode(chan, '+', 'l', s);
- return true;
- }
- }
- return false;
- }
- void check_shouldjoin(struct chanset_t* chan)
- {
- if ((channel_active(chan) || channel_pending(chan)) && !shouldjoin(chan)) {
- sdprintf("Active/Pending in %s but I shouldn't be there, parting...", chan->dname);
- dprintf(DP_SERVER, "PART %s\n", chan->name[0] ? chan->name : chan->dname);
- } else if (shouldjoin(chan)) {
- join_chan(chan);
- }
- }
- static void
- check_expired_chanstuff(struct chanset_t *chan)
- {
- memberlist *m = NULL;
- check_shouldjoin(chan);
- if (channel_active(chan) && shouldjoin(chan)) {
- masklist *b = NULL, *e = NULL;
- memberlist *n = NULL;
- struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
- if (me_op(chan)) {
- if (channel_dynamicbans(chan) && chan->ban_time)
- for (b = chan->channel.ban; b->mask[0]; b = b->next)
- if (now - b->timer > 60 * chan->ban_time &&
- !u_sticky_mask(chan->bans, b->mask) &&
- !u_sticky_mask(global_bans, b->mask) && expired_mask(chan, b->who)) {
- putlog(LOG_MODES, chan->dname, "(%s) Channel ban on %s expired.", chan->dname, b->mask);
- add_mode(chan, '-', 'b', b->mask);
- b->timer = now;
- }
- if (use_exempts && channel_dynamicexempts(chan) && chan->exempt_time)
- for (e = chan->channel.exempt; e->mask[0]; e = e->next)
- if (now - e->timer > 60 * chan->exempt_time &&
- !u_sticky_mask(chan->exempts, e->mask) &&
- !u_sticky_mask(global_exempts, e->mask) && expired_mask(chan, e->who)) {
- /* Check to see if it matches a ban */
- int match = 0;
- for (b = chan->channel.ban; b->mask[0]; b = b->next)
- if (wild_match(b->mask, e->mask) || wild_match(e->mask, b->mask)) {
- match = 1;
- break;
- }
- /* Leave this extra logging in for now. Can be removed later
- * Jason
- */
- if (match) {
- putlog(LOG_MODES, chan->dname,
- "(%s) Channel exemption %s NOT expired. Exempt still set!", chan->dname, e->mask);
- } else {
- putlog(LOG_MODES, chan->dname, "(%s) Channel exemption on %s expired.", chan->dname, e->mask);
- add_mode(chan, '-', 'e', e->mask);
- }
- e->timer = now;
- }
- if (use_invites && channel_dynamicinvites(chan) && chan->invite_time && !(chan->channel.mode & CHANINV)) {
- for (b = chan->channel.invite; b->mask[0]; b = b->next) {
- if (now - b->timer > 60 * chan->invite_time &&
- !u_sticky_mask(chan->invites, b->mask) &&
- !u_sticky_mask(global_invites, b->mask) && expired_mask(chan, b->who)) {
- putlog(LOG_MODES, chan->dname, "(%s) Channel invitation on %s expired.", chan->dname, b->mask);
- add_mode(chan, '-', 'I', b->mask);
- b->timer = now;
- }
- }
- }
- } /* me_op */
- size_t splitmembers = 0, bot_ops = 0;
- const bool im_opped = me_op(chan);
- for (m = chan->channel.member; m && m->nick[0]; m = n) {
- n = m->next;
- // Update split members
- if (m->split) {
- ++splitmembers;
- if (now - m->split > wait_split) {
- putlog(LOG_JOIN, chan->dname, "%s (%s) got lost in the net-split.", m->nick, m->userhost);
- --(chan->channel.splitmembers);
- killmember(chan, m->nick, false);
- continue;
- }
- }
- //This bot is set +r, so resolve.
- if (!m->userip[0] && doresolv(chan)) {
- char host[UHOSTLEN] = "", *p = NULL;
- p = strchr(m->userhost, '@');
- if (p) {
- ++p;
- strlcpy(host, p, strlen(m->userhost) - (p - host));
- resolve_to_member(chan, m->nick, host);
- }
- }
- if (im_opped) {
- if (dovoice(chan) && !loading) { /* autovoice of +v users if bot is +y */
- if (!chan_hasop(m) && !chan_hasvoice(m) && !chan_sentvoice(m)) {
- member_getuser(m, 1);
- if (m->user) {
- get_user_flagrec(m->user, &fr, chan->dname, chan);
- if (!glob_bot(fr)) {
- if (!(m->flags & EVOICE) &&
- (
- /* +voice: Voice all clients who are not flag:+q. If the chan is +voicebitch, only op flag:+v clients */
- (channel_voice(chan) && !chk_devoice(fr) && (!channel_voicebitch(chan) || (channel_voicebitch(chan) && chk_voice(fr, chan)))) ||
- /* Or, if the channel is -voice but they still qualify to be voiced */
- (!channel_voice(chan) && !privchan(fr, chan, PRIV_VOICE) && chk_voice(fr, chan))
- )
- ) {
- add_mode(chan, '+', 'v', m->nick);
- }
- }
- } else if (!m->user && channel_voice(chan) && !channel_voicebitch(chan) && voice_ok(m, chan)) {
- add_mode(chan, '+', 'v', m->nick);
- }
- }
- }
- }
- if (m->user && m->user->bot) {
- ++bot_ops;
- }
- m = n;
- }
- // Update minutely
- chan->channel.splitmembers = splitmembers;
- check_lonely_channel(chan);
- if (bot_ops && !im_opped) {
- request_op(chan);
- }
- if (role == 3) {
- recheck_channel_modes(chan);
- }
- }
- // Clear out expired cached members
- if (chan->channel.cached_members && chan->channel.cached_members->size()) {
- bd::Array<bd::String> member_uhosts(chan->channel.cached_members->keys());
- for (size_t i = 0; i < member_uhosts.length(); ++i) {
- const bd::String uhost(member_uhosts[i]);
- m = (*chan->channel.cached_members)[uhost];
- // Delete the expired member
- if (now - m->last > wait_split) {
- delete_member(m);
- chan->channel.cached_members->remove(uhost);
- }
- }
- }
- }
- void
- irc_minutely()
- {
- for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
- warn_pls_take(chan);
- if (server_online) {
- if (!channel_pending(chan)) {
- check_netfight(chan);
- check_servers(chan);
- }
- check_expired_chanstuff(chan);
- }
- }
- }
- int check_bind_authc(char *cmd, Auth *a, char *chname, char *par)
- {
- struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
- int x = 0;
- get_user_flagrec(a->user, &fr, chname);
- if (a->GetIdx(chname)) {
- x = check_auth_dcc(a, cmd, par);
- }
- LOGC(cmd);
- if (x & BIND_RET_BREAK)
- return (1);
- return (0);
- }
- /* Flush the modes for EVERY channel.
- */
- void
- flush_modes()
- {
- if (!modesperline) /* Haven't received 005 yet :) */
- return;
- memberlist *m = NULL;
- Context;
- for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
- if (!me_op(chan)) continue;
- for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
- if (m->delay && m->delay <= now) {
- m->delay = 0L;
- m->flags &= ~FULL_DELAY;
- if (chan_sentop(m)) {
- m->flags &= ~SENTOP;
- do_op(m->nick, chan, 0, 0);
- }
- if (chan_sentvoice(m)) {
- m->flags &= ~SENTVOICE;
- add_mode(chan, '+', 'v', m->nick);
- }
- }
- }
- flush_mode(chan, NORMAL);
- }
- }
- void
- irc_report(int idx, int details)
- {
- struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
- char ch[1024] = "", q[160] = "";
- const char *p = NULL;
- int k = 10;
- size_t len;
- strlcpy(q, "Channels: ", sizeof(q));
- for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
- if (idx != DP_STDOUT)
- get_user_flagrec(dcc[idx].user, &fr, chan->dname, chan);
- if (!privchan(fr, chan, PRIV_OP) && (idx == DP_STDOUT || glob_master(fr) || chan_master(fr))) {
- p = NULL;
- if (shouldjoin(chan)) {
- if (chan->ircnet_status & CHAN_JUPED)
- p = "juped";
- else if (channel_joining(chan))
- p = "joining";
- else if (!(chan->ircnet_status & CHAN_ACTIVE))
- p = "trying";
- else if (chan->ircnet_status & CHAN_PEND)
- p = "pending";
- else if ((chan->dname[0] != '+') && !me_op(chan))
- p = "want ops!";
- }
- len = simple_snprintf(ch, sizeof(ch), "%s%s%s%s, ", chan->dname, p ? "(" : "", p ? p : "", p ? ")" : "");
- if ((k + len) > 70) {
- dprintf(idx, " %s\n", q);
- strlcpy(q, " ", sizeof(q));
- k = 10;
- }
- k += my_strcpy(q + k, ch);
- }
- }
- if (k > 10) {
- q[k - 2] = 0;
- dprintf(idx, " %s\n", q);
- }
- }
- static void bot_release_nick (char *botnick, char *code, char *par) {
- release_nick(par);
- }
- static cmd_t irc_bot[] = {
- {"gi", "", (Function) getin_request, NULL, LEAF},
- {"mr", "", (Function) mass_request, NULL, LEAF},
- {"rn", "", (Function) bot_release_nick, NULL, LEAF},
- {NULL, NULL, NULL, NULL, 0}
- };
- void
- irc_init()
- {
- timer_create_secs(60, "irc_minutely", (Function) irc_minutely);
- /* Add our commands to the imported tables. */
- add_builtins("dcc", irc_dcc);
- add_builtins("bot", irc_bot);
- add_builtins("raw", irc_raw);
- add_builtins("msg", C_msg);
- }
- /* vim: set sts=2 sw=2 ts=8 et: */
|