0

I've made a ts3 plugin this is code:

int ts3plugin_onTextMessageEvent(uint64 serverConnectionHandlerID, anyID targetMode, anyID toID, anyID fromID, const char* fromName, const char* fromUniqueIdentifier, const char* message, int ffIgnored) {
    if(ffIgnored) {
        return 0; /* Client will ignore the message anyways, so return value here doesn't matter */
    }

    /* Example code: Autoreply to sender */
    /* Disabled because quite annoying, but should give you some ideas what is possible here */
    /* Careful, when two clients use this, they will get banned quickly... */
    anyID myID;
    if(ts3Functions.getClientID(serverConnectionHandlerID, &myID) != ERROR_ok) {
        ts3Functions.logMessage("Error querying own client id", LogLevel_ERROR, "Plugin", serverConnectionHandlerID);
        return 0;
    }


    printf("start plugin music");
    HANDLE hStdout;

    TCHAR msgBuf[255];
    msgBuf[0] = "a";
    size_t cchStringSize;
    DWORD dwChars;

    // Make sure there is a console to receive output results. 

    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    if(hStdout == INVALID_HANDLE_VALUE)
        return 1;
    if(targetMode == TextMessageTarget_CHANNEL) {
        if(fromID != myID && serverConnectionHandlerID != 1) {  /* Don't reply when source is own client */
            char *str3 = (char *)malloc(1 + strlen(message) + 1 + strlen(fromUniqueIdentifier));
            strcpy(str3, message);
            strcat(str3, " ");
            strcat(str3, fromUniqueIdentifier);
            if(checkMessage(str3, serverConnectionHandlerID, toID, fromID)) {
                printf("true");
            }
        }
    }

    return 0;  /* 0 = handle normally, 1 = client will ignore the text message */
}

// Sample custom data structure for threads to use.
// This is passed by void pointer so it can be any data type
// that can be passed using a single void pointer (LPVOID).
typedef struct MyData {
    uint64 serverConnectionHandlerID;
    anyID toID;
    anyID fromID;
} MYDATA, *PMYDATA;

BOOL checkMessage(const char *message, uint64 serverConnectionHandlerID, anyID toID, anyID fromID) {
    const char *init = "#pj ";

    for(int i = 0; i < strlen(init); i++) {
        if(message[i] != init[i])
            return FALSE;
    }

    writeToFile(message, "file.txt");

    PMYDATA pData;
    DWORD   dwThreadId;
    HANDLE  hThread;

    pData = (PMYDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MYDATA));

    if(pData == NULL) {
        // If the array allocation fails, the system is out of memory
        // so there is no point in trying to print an error message.
        // Just terminate execution.
        ExitProcess(2);
    }

    pData->serverConnectionHandlerID = serverConnectionHandlerID;
    pData->toID = toID;
    pData->fromID = fromID;

    hThread = CreateThread(
        NULL,                   // default security attributes
        0,                      // use default stack size  
        MyThreadFunction,       // thread function name
        pData,          // argument to thread function 
        0,                      // use default creation flags 
        &dwThreadId);   // returns the thread identifier

    //WaitForMultipleObjects(MAX_THREADS, hThreadArray, TRUE, INFINITE);

    return FALSE;
}

void writeToFile(char *comm, char *file) {
    FILE *fp;
    char *str1 = "some/path/";
    char *str3 = (char *)malloc(1 + strlen(str1) + strlen(file));
    strcpy(str3, str1);
    strcat(str3, file);
    fp = fopen(str3, "w");
    fprintf(fp, "%s", comm);
    fclose(fp);
    free(str3);
}

DWORD WINAPI MyThreadFunction(LPVOID lpParam) {
    PMYDATA pDataArray;

    // Cast the parameter to the correct data type.
    // The pointer is known to be valid because 
    // it was checked for NULL before the thread was created.

    pDataArray = (PMYDATA)lpParam;
    FILE *fp;
    long lSize;
    const char *buffer;

    fp = fopen("some/path/file2.txt", "r");
    fseek(fp, 0L, SEEK_END);
    lSize = ftell(fp);
    if(lSize > 0) {
        const char *priv = "$";
        int priv_length = 1;
        rewind(fp);

        buffer = calloc(1, lSize + 1);
        fread(buffer, lSize, 1, fp);
        fclose(fp);

        fp = fopen("some/path/file2.txt", "w");
        fclose(fp);

        if(buffer[0] == priv[0]) {
            const char *buffer2;
            buffer2 = calloc(1, lSize);
            strncpy(buffer2, buffer + 1, lSize - 1);
            ts3Functions.requestSendPrivateTextMsg(pDataArray->serverConnectionHandlerID, buffer2, pDataArray->fromID, NULL);
            free(buffer2);
        } else
            ts3Functions.requestSendChannelTextMsg(pDataArray->serverConnectionHandlerID, buffer, pDataArray->toID, NULL);

        free(buffer);
    }
    Sleep(1000);
    MyThreadFunction(lpParam);
}

The problem is that (when i have turn on my plugin) after few minutes of running ts3 it stops working. It must be something wrong with creating thread, because before i've added code from here Creating Threads everything was fine, but now i need checking if file was changed.

Konrad
  • 25
  • 1
  • 8

0 Answers0