added ModeBuffer to tools.c
authorpk910 <philipp@zoelle1.de>
Thu, 25 Aug 2011 03:11:21 +0000 (05:11 +0200)
committerpk910 <philipp@zoelle1.de>
Thu, 25 Aug 2011 03:11:21 +0000 (05:11 +0200)
main.h
tools.c
tools.h

diff --git a/main.h b/main.h
index 92861853f784cadb9cd055e488f952f93fe585bc..e355d2375fad94349cae3030d24bde2bb93d2584 100644 (file)
--- a/main.h
+++ b/main.h
@@ -44,6 +44,7 @@
 #define TRIGGERLEN      50
 #define MAXNUMPARAMS    200 /* maximum number of parameters in one line */
 #define MAXLANGUAGES    5
+#define MAXMODES        6
 
 //valid nick chars
 #define VALID_NICK_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890{|}~[\\]^_`"
diff --git a/tools.c b/tools.c
index 55b69200544bbe105fed85b366ccd01d97b9b8ab..3ae71d978bd9b145f0e3c1e9588d11e3d4bb351f 100644 (file)
--- a/tools.c
+++ b/tools.c
@@ -303,6 +303,70 @@ int strToTime(struct UserNode *user, char *str) {
     return total_time;
 }
 
+struct ModeBuffer* initModeBuffer(struct ClientSocket *client, struct ChanNode *chan) {
+    struct ModeBuffer *modeBuf = malloc(sizeof(*modeBuf));
+    if(!modeBuf) {
+        perror("malloc() failed");
+        return;
+    }
+    modeBuf->client = client;
+    modeBuf->chan = chan;
+    modeBuf->addCount = 0;
+    modeBuf->delCount = 0;
+    return modeBuf;
+}
+
+void modeBufferSet(struct ModeBuffer *modeBuf, int add, char mode, char *param) {
+    if(add) {
+        modeBuf->addModes[modeBuf->addCount] = mode;
+        modeBuf->addModesParams[modeBuf->addCount] = (param ? strdup(param) : NULL);
+        modeBuf->addCount++;
+        modeBuf->addModes[modeBuf->addCount] = '\0';
+    } else {
+        modeBuf->delModes[modeBuf->delCount] = mode;
+        modeBuf->delModesParams[modeBuf->delCount] = (param ? strdup(param) : NULL);
+        modeBuf->delCount++;
+        modeBuf->delModes[modeBuf->delCount] = '\0';
+    }
+    if(modeBuf->addCount + modeBuf->delCount == MAXMODES)
+        flushModeBuffer(modeBuf);
+}
+
+void flushModeBuffer(struct ModeBuffer *modeBuf) {
+    char modeStr[MAXMODES+3];
+    int modePos = 0;
+    char paramStr[MAXLEN];
+    int paramPos = 0;
+    int i;
+    if(modeBuf->addCount) {
+        modeStr[modePos++] = '+';
+        for(i = 0; i < modeBuf->addCount; i++) {
+            modeStr[modePos++] = modeBuf->addModes[i];
+            if(modeBuf->addModesParams[i]) {
+                paramPos += sprintf(paramStr + paramPos, " %s", modeBuf->addModesParams[i]);
+            }
+        }
+        modeBuf->addCount = 0;
+    }
+    if(modeBuf->delCount) {
+        modeStr[modePos++] = '-';
+        for(i = 0; i < modeBuf->delCount; i++) {
+            modeStr[modePos++] = modeBuf->delModes[i];
+            if(modeBuf->delModesParams[i]) {
+                paramPos += sprintf(paramStr + paramPos, " %s", modeBuf->delModesParams[i]);
+            }
+        }
+        modeBuf->delCount = 0;
+    }
+    modeStr[modePos++] = '\0';
+    putsock(modeBuf->client, "MODE %s %s%s", modeBuf->chan->name, modeStr, paramStr);
+}
+
+void freeModeBuffer(struct ModeBuffer *modeBuf) {
+    if(modeBuf->addCount + modeBuf->delCount)
+        flushModeBuffer(modeBuf);
+    free(modeBuf);
+}
 
 void init_tools() {
     register_default_language_table(msgtab);
diff --git a/tools.h b/tools.h
index 60a590e5f178c3a71da39e22c50b220ec781f1ce..b47a6bb320b91593635f61120c3a21026dfffd78 100644 (file)
--- a/tools.h
+++ b/tools.h
@@ -6,7 +6,9 @@
 #define TABLE_FLAG_USE_POINTER 0x01
 #define TABLE_FLAG_COL_BOLD    0x02
 
+struct ClientSocket;
 struct UserNode;
+struct ChanNode
 
 struct Table {
     char ***contents;
@@ -21,6 +23,17 @@ struct Table {
     char **table_lines; //we just store this to free it in table_free
 };
 
+struct ModeBuffer {
+    struct ClientSocket *client;
+    struct ChanNode *chan;
+    char addModes[MAXMODES];
+    char delModes[MAXMODES];
+    char *addModesParams[MAXMODES];
+    char *delModesParams[MAXMODES];
+    char addCount;
+    char delCount;
+};
+
 int match(const char *mask, const char *name);
 
 struct Table *table_init(int width, int length, int flags);
@@ -32,6 +45,16 @@ void table_free(struct Table *table);
 char* timeToStr(struct UserNode *user, int seconds, int items, char *buf);
 int strToTime(struct UserNode *user, char *str);
 
+struct ModeBuffer* initModeBuffer(struct ClientSocket *client, struct ChanNode *chan);
+#define modeBufferSimpleMode(MODEBUF,ADD,MODE) modeBufferSet(MODEBUF, ADD, MODE, NULL) 
+#define modeBufferOp(MODEBUF,USER) modeBufferSet(MODEBUF, 1, 'o', USER)
+#define modeBufferDeop(MODEBUF,USER) modeBufferSet(MODEBUF, 0, 'o', USER) 
+#define modeBufferVoice(MODEBUF,USER) modeBufferSet(MODEBUF, 1, 'v', USER)
+#define modeBufferDevoice(MODEBUF,USER) modeBufferSet(MODEBUF, 0, 'v', USER) 
+void modeBufferSet(struct ModeBuffer *modeBuf, int add, char mode, char *param);
+void flushModeBuffer(struct ModeBuffer *modeBuf);
+void freeModeBuffer(struct ModeBuffer *modeBuf);
+
 void init_tools();
 
 #endif
\ No newline at end of file