]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_opermd5.cpp
WHEEEEE!!!!!
[user/henk/code/inspircd.git] / src / modules / m_opermd5.cpp
index c2b923929f5ec3cca416651287da879c6e159b33..e8fa613ff1b2668eb2ce81368eaba33e21a0e1d6 100644 (file)
@@ -2,7 +2,7 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
+ *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
  *                       E-mail:
  *                <brain@chatspike.net>
  *               <Craig@chatspike.net>
 using namespace std;
 
 #include <stdio.h>
+#include "inspircd_config.h"
+#ifdef HAS_STDINT
+#include <stdint.h>
+#endif
 #include "users.h"
 #include "channels.h"
 #include "modules.h"
 #include "helperfuncs.h"
 
-
 /* The four core functions - F1 is optimized somewhat */
 #define F1(x, y, z) (z ^ (x & (y ^ z)))
 #define F2(x, y, z) F1(z, x, y)
@@ -35,17 +38,21 @@ using namespace std;
 #define MD5STEP(f,w,x,y,z,in,s) \
          (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
 
-typedef unsigned long word32;
+#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;
 
-struct MD5Context {
+class MD5Context : public classbase
+{
+ public:
        word32 buf[4];
        word32 bytes[2];
        word32 in[16];
 };
 
-Server *Srv;
-
 void MD5Init(struct MD5Context *context);
 void MD5Update(struct MD5Context *context, byte const *buf, int len);
 void MD5Final(byte digest[16], struct MD5Context *context);
@@ -57,13 +64,15 @@ void byteSwap(word32 *buf, unsigned words)
 {
        byte *p = (byte *)buf;
 
-       do {
+       do
+       {
                *buf++ = (word32)((unsigned)p[3] << 8 | p[2]) << 16 |
                        ((unsigned)p[1] << 8 | p[0]);
                p += 4;
        } while (--words);
 }
 
+/* XXX - maybe if we had an md5/encryption moduletype? *shrug* */
 void MD5Init(struct MD5Context *ctx)
 {
        ctx->buf[0] = 0x67452301;
@@ -86,7 +95,8 @@ void MD5Update(struct MD5Context *ctx, byte const *buf, int len)
                ctx->bytes[1]++;        /* Carry from low to high */
 
        t = 64 - (t & 0x3f);    /* Space available in ctx->in (at least 1) */
-       if ((unsigned)t > (unsigned)len) {
+       if ((unsigned)t > (unsigned)len)
+       {
                memcpy((byte *)ctx->in + 64 - (unsigned)t, buf, len);
                return;
        }
@@ -98,7 +108,8 @@ void MD5Update(struct MD5Context *ctx, byte const *buf, int len)
        len -= (unsigned)t;
 
        /* Process data in 64-byte chunks */
-       while (len >= 64) {
+       while (len >= 64)
+       {
                memcpy(ctx->in, buf, 64);
                byteSwap(ctx->in, 16);
                MD5Transform(ctx->buf, ctx->in);
@@ -121,7 +132,8 @@ void MD5Final(byte digest[16], struct MD5Context *ctx)
        /* Bytes of padding needed to make 56 bytes (-8..55) */
        count = 56 - 1 - count;
 
-       if (count < 0) {        /* Padding forces an extra block */
+       if (count < 0)
+       {       /* Padding forces an extra block */
                memset(p, 0, count+8);
                byteSwap(ctx->in, 16);
                MD5Transform(ctx->buf, ctx->in);
@@ -240,7 +252,7 @@ void GenHash(const char* src, char* dest)
        int i = 0;
        unsigned char bytes[16];
        char hash[1024];
-       strcpy(hash,"");
+       *hash = 0;
        MyMD5((char*)bytes,(void*)src,strlen(src));
        for (i = 0; i < 16; i++)
        {
@@ -256,29 +268,47 @@ void GenHash(const char* src, char* dest)
        strcpy(dest,hash);
 }
 
-void handle_mkpasswd(char **parameters, int pcnt, userrec *user)
+class cmd_mkpasswd : public command_t
 {
-       char buffer[MAXBUF];
-       GenHash(parameters[0],buffer);
-       WriteServ(user->fd,"NOTICE %s :MD5 hashed password for %s is %s",user->nick,parameters[0],buffer);
-}
+ public:
+       cmd_mkpasswd () : command_t("MKPASSWD", 'o', 1)
+       {
+               this->source = "m_opermd5.so";
+               syntax = "<any-text>";
+       }
+
+       void Handle (const char** parameters, int pcnt, userrec *user)
+       {
+               char buffer[MAXBUF];
+               GenHash(parameters[0],buffer);
+               user->WriteServ("NOTICE %s :MD5 hashed password for %s is %s",user->nick,parameters[0],buffer);
+       }
+};
 
 class ModuleOperMD5 : public Module
 {
+       Server* Srv;
+       cmd_mkpasswd* mycommand;
  public:
 
-       ModuleOperMD5()
+       ModuleOperMD5(Server* Me)
+               : Module::Module(Me)
        {
-               Srv = new Server;
-               Srv->AddCommand("MKPASSWD",handle_mkpasswd,'o',1,"m_opermd5.so");
+               Srv = Me;
+               mycommand = new cmd_mkpasswd();
+               Srv->AddCommand(mycommand);
        }
        
        virtual ~ModuleOperMD5()
        {
-               delete Srv;
        }
 
-       virtual int OnOperCompare(std::string data, std::string input)
+       void Implements(char* List)
+       {
+               List[I_OnOperCompare] = 1;
+       }
+
+       virtual int OnOperCompare(const std::string &data, const std::string &input)
        {
                char buffer[MAXBUF];
                if (data.length() == 32)        // if its 32 chars long, try it as an md5
@@ -297,11 +327,6 @@ class ModuleOperMD5 : public Module
        {
                return Version(1,0,0,1,VF_VENDOR);
        }
-       
-       virtual void OnUserConnect(userrec* user)
-       {
-       }
-
 };
 
 
@@ -316,9 +341,9 @@ class ModuleOperMD5Factory : public ModuleFactory
        {
        }
        
-       virtual Module * CreateModule()
+       virtual Module * CreateModule(Server* Me)
        {
-               return new ModuleOperMD5;
+               return new ModuleOperMD5(Me);
        }
        
 };