]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_md5.cpp
Add support for blocking tag messages with the deaf mode.
[user/henk/code/inspircd.git] / src / modules / m_md5.cpp
index 2771ba495468afeab069fb21f9fa99e5e6d893db..0de69c99a1ca61d1164da86c29736c892cdd3d07 100644 (file)
@@ -1,24 +1,31 @@
-/*       +------------------------------------+
- *       | Inspire Internet Relay Chat Daemon |
- *       +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
  *
- *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
- * See: http://wiki.inspircd.org/Credits
+ *   Copyright (C) 2014 Daniel Vassdal <shutter@canternet.org>
+ *   Copyright (C) 2013, 2015, 2017 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
+ *   Copyright (C) 2012 Garrett Holmstrom <gholms@fedoraproject.org>
+ *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
+ *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
+ *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2006, 2010 Craig Edwards <brain@inspircd.org>
  *
- * This program is free but copyrighted software; see
- *            the file COPYING for details.
+ * This file is part of InspIRCd.  InspIRCd 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, version 2.
  *
- * ---------------------------------------------------
+ * 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, see <http://www.gnu.org/licenses/>.
  */
 
-/* $ModDesc: Allows for MD5 encrypted oper passwords */
-/* $ModDep: m_hash.h */
 
 #include "inspircd.h"
-#ifdef HAS_STDINT
-#include <stdint.h>
-#endif
-#include "m_hash.h"
+#include "modules/hash.h"
 
 /* The four core functions - F1 is optimized somewhat */
 #define F1(x, y, z) (z ^ (x & (y ^ z)))
 #define MD5STEP(f,w,x,y,z,in,s) \
        (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
 
-#ifndef HAS_STDINT
-typedef unsigned int uint32_t;
-#endif
-
 typedef uint32_t word32; /* NOT unsigned long. We don't support 16 bit platforms, anyway. */
 typedef unsigned char byte;
 
 /** An MD5 context, used by m_opermd5
  */
-class MD5Context : public classbase
+class MD5Context
 {
  public:
        word32 buf[4];
@@ -47,7 +50,7 @@ class MD5Context : public classbase
        word32 in[16];
 };
 
-class ModuleMD5 : public Module
+class MD5Provider : public HashProvider
 {
        void byteSwap(word32 *buf, unsigned words)
        {
@@ -61,23 +64,13 @@ class ModuleMD5 : public Module
                } while (--words);
        }
 
-       void MD5Init(MD5Context *ctx, unsigned int* ikey = NULL)
+       void MD5Init(MD5Context *ctx)
        {
                /* These are the defaults for md5 */
-               if (!ikey)
-               {
-                       ctx->buf[0] = 0x67452301;
-                       ctx->buf[1] = 0xefcdab89;
-                       ctx->buf[2] = 0x98badcfe;
-                       ctx->buf[3] = 0x10325476;
-               }
-               else
-               {
-                       ctx->buf[0] = ikey[0];
-                       ctx->buf[1] = ikey[1];
-                       ctx->buf[2] = ikey[2];
-                       ctx->buf[3] = ikey[3];
-               }
+               ctx->buf[0] = 0x67452301;
+               ctx->buf[1] = 0xefcdab89;
+               ctx->buf[2] = 0x98badcfe;
+               ctx->buf[3] = 0x10325476;
 
                ctx->bytes[0] = 0;
                ctx->bytes[1] = 0;
@@ -149,12 +142,12 @@ class ModuleMD5 : public Module
 
                byteSwap(ctx->buf, 4);
                memcpy(digest, ctx->buf, 16);
-               memset(ctx, 0, sizeof(ctx));
+               memset(ctx, 0, sizeof(*ctx));
        }
 
        void MD5Transform(word32 buf[4], word32 const in[16])
        {
-               register word32 a, b, c, d;
+               word32 a, b, c, d;
 
                a = buf[0];
                b = buf[1];
@@ -236,81 +229,36 @@ class ModuleMD5 : public Module
        }
 
 
-       void MyMD5(void *dest, void *orig, int len, unsigned int* ikey)
+       void MyMD5(void *dest, void *orig, int len)
        {
                MD5Context context;
-               MD5Init(&context, ikey);
+               MD5Init(&context);
                MD5Update(&context, (const unsigned char*)orig, len);
                MD5Final((unsigned char*)dest, &context);
        }
 
-
-       void GenHash(const char* src, char* dest, const char* xtab, unsigned int* ikey, size_t srclen)
-       {
-               unsigned char bytes[16];
-
-               MyMD5((char*)bytes, (void*)src, srclen, ikey);
-
-               for (int i = 0; i < 16; i++)
-               {
-                       *dest++ = xtab[bytes[i] / 16];
-                       *dest++ = xtab[bytes[i] % 16];
-               }
-               *dest++ = 0;
-       }
-
-       unsigned int *key;
-       char* chars;
-
  public:
-
-       ModuleMD5(InspIRCd* Me)
-               : Module(Me), key(NULL), chars(NULL)
+       std::string GenerateRaw(const std::string& data) CXX11_OVERRIDE
        {
-               ServerInstance->Modules->PublishInterface("HashRequest", this);
-               Implementation eventlist[] = { I_OnRequest };
-               ServerInstance->Modules->Attach(eventlist, this, 1);
-       }
-
-       virtual ~ModuleMD5()
-       {
-               ServerInstance->Modules->UnpublishInterface("HashRequest", this);
+               char res[16];
+               MyMD5(res, (void*)data.data(), data.length());
+               return std::string(res, 16);
        }
 
+       MD5Provider(Module* parent) : HashProvider(parent, "md5", 16, 64) {}
+};
 
-       virtual const char* OnRequest(Request* request)
+class ModuleMD5 : public Module
+{
+       MD5Provider md5;
+ public:
+       ModuleMD5() : md5(this)
        {
-               HashRequest* MD5 = (HashRequest*)request;
-
-               if (strcmp("KEY", request->GetId()) == 0)
-               {
-                       this->key = (unsigned int*)MD5->GetKeyData();
-               }
-               else if (strcmp("HEX", request->GetId()) == 0)
-               {
-                       this->chars = (char*)MD5->GetOutputs();
-               }
-               else if (strcmp("SUM", request->GetId()) == 0)
-               {
-                       static char data[MAXBUF];
-                       GenHash(MD5->GetHashData().data(), data, chars ? chars : "0123456789abcdef", key, MD5->GetHashData().length());
-                       return data;
-               }
-               else if (strcmp("NAME", request->GetId()) == 0)
-               {
-                       return "md5";
-               }
-               else if (strcmp("RESET", request->GetId()) == 0)
-               {
-                       this->chars = NULL;
-                       this->key = NULL;
-               }
-               return NULL;
        }
 
-       virtual Version GetVersion()
+       Version GetVersion() CXX11_OVERRIDE
        {
-               return Version("Allows for MD5 encrypted oper passwords",VF_VENDOR|VF_SERVICEPROVIDER,API_VERSION);
+               return Version("Allows other modules to generate MD5 hashes.",VF_VENDOR);
        }
 };