unrealircd

- supernets unrealircd source & configuration
git clone git://git.acid.vegas/unrealircd.git
Log | Files | Refs | Archive | README | LICENSE

geoip-tag.c (2810B)

      1 /*
      2  *   IRC - Internet Relay Chat, src/modules/geoip-tag.c
      3  *   (C) 2022 westor, Syzop and The UnrealIRCd Team
      4  *
      5  *   See file AUTHORS in IRC package for additional names of
      6  *   the programmers.
      7  *
      8  *   This program is free software; you can redistribute it and/or modify
      9  *   it under the terms of the GNU General Public License as published by
     10  *   the Free Software Foundation; either version 1, or (at your option)
     11  *   any later version.
     12  *
     13  *   This program is distributed in the hope that it will be useful,
     14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  *   GNU General Public License for more details.
     17  *
     18  *   You should have received a copy of the GNU General Public License
     19  *   along with this program; if not, write to the Free Software
     20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     21  */
     22 
     23 #include "unrealircd.h"
     24 
     25 ModuleHeader MOD_HEADER
     26   = {
     27 	"geoip-tag",
     28 	"6.0",
     29 	"geoip message tag",
     30 	"UnrealIRCd Team",
     31 	"unrealircd-6",
     32 	};
     33 
     34 /* Forward declarations */
     35 int geoip_mtag_is_ok(Client *client, const char *name, const char *value);
     36 int geoip_mtag_should_send_to_client(Client *target);
     37 void mtag_add_geoip(Client *client, MessageTag *recv_mtags, MessageTag **mtag_list, const char *signature);
     38 
     39 MOD_INIT()
     40 {
     41 	MessageTagHandlerInfo mtag;
     42 
     43 	MARK_AS_OFFICIAL_MODULE(modinfo);
     44 
     45 	memset(&mtag, 0, sizeof(mtag));
     46 	mtag.name = "unrealircd.org/geoip";
     47 	mtag.is_ok = geoip_mtag_is_ok;
     48 	mtag.should_send_to_client = geoip_mtag_should_send_to_client;
     49 	mtag.flags = MTAG_HANDLER_FLAGS_NO_CAP_NEEDED;
     50 	MessageTagHandlerAdd(modinfo->handle, &mtag);
     51 
     52 	HookAddVoid(modinfo->handle, HOOKTYPE_NEW_MESSAGE, 0, mtag_add_geoip);
     53 
     54 	return MOD_SUCCESS;
     55 }
     56 
     57 MOD_LOAD()
     58 {
     59 	return MOD_SUCCESS;
     60 }
     61 
     62 MOD_UNLOAD()
     63 {
     64 	return MOD_SUCCESS;
     65 }
     66 
     67 /** This function verifies if the client sending
     68  * 'geoip-tag' is permitted to do so and uses a permitted
     69  * syntax.
     70  * We simply allow geoip-tag ONLY from servers and with any syntax.
     71  */
     72 int geoip_mtag_is_ok(Client *client, const char *name, const char *value)
     73 {
     74 	if (IsServer(client))
     75 		return 1;
     76 
     77 	return 0;
     78 }
     79 
     80 void mtag_add_geoip(Client *client, MessageTag *recv_mtags, MessageTag **mtag_list, const char *signature)
     81 {
     82 	MessageTag *m;
     83 	
     84 	GeoIPResult *geoip;
     85 
     86 	if (IsUser(client) && ((geoip = geoip_client(client))))
     87 	{
     88 		MessageTag *m = find_mtag(recv_mtags, "unrealircd.org/geoip");
     89 		if (m)
     90 		{
     91 			m = duplicate_mtag(m);
     92 		} else {
     93 			m = safe_alloc(sizeof(MessageTag));
     94 			safe_strdup(m->name, "unrealircd.org/geoip");
     95 			safe_strdup(m->value, geoip->country_code);
     96 		}
     97 		AddListItem(m, *mtag_list);
     98 	}
     99 }
    100 
    101 /** Outgoing filter for this message tag */
    102 int geoip_mtag_should_send_to_client(Client *target)
    103 {
    104 	if (IsServer(target) || IsOper(target))
    105 		return 1;
    106 
    107 	return 0;
    108 }