Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************************************************
- * mIRC MySQL interface.cpp
- * Copyright (C) 2000-2003 Michael Reynolds
- * Modified by Bas 'soczol' Verhoeven
- *
- * See file readme.txt for additional names of the programmers.
- *
- * This file is part of mIRC MySQL.
- *
- * mIRC MySQL 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; either version 2 of the License, or
- * (at your option) any later version.
- *
- * mIRC MySQL 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 mIRC MySQL; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
- #include "mircmysql.h"
- /************************************************************************
- * Non MySQL functions *
- ************************************************************************/
- mIRC(Echo)
- {
- char *data = _strdup(mIRCdata);
- if (data == NULL || strlen(data) == 0)
- {
- // 030 - Null data.
- strcpy(mIRCdata, "0 030 Echo");
- }
- else
- {
- snprintf(mIRCdata, BUFSIZE, "You supplied: %s", data);
- }
- free(data);
- return Status::ReturnData;
- }
- mIRC(Reverse)
- {
- char *data = _strdup(mIRCdata);
- strncpy(mIRCdata, _strrev(data), BUFSIZE);
- free(data);
- return Status::ReturnData;
- }
- mIRC(DLLInfo)
- {
- #if !defined(STATUS) || STATUS==ALPHA || STATUS==BETA || STATUS==RC
- snprintf(mIRCdata,BUFSIZE,"mIRC MySQL v%d.%d.%.2d(%s) for libmysql v%s by wshs, modified by soczol. [\37\2NO SUPPORT FOR THIS VERSION\2\37\17]",VERSIONmajor,VERSIONminor,VERSIONpatch,bench,mysql_get_client_info());
- #else
- snprintf(mIRCdata,BUFSIZE,"mIRC MySQL v%d.%d.%.2d(%s) for libmysql v%s by wshs.",VERSIONmajor,VERSIONminor,VERSIONpath,bench,mysql_get_client_info());
- #endif
- return Status::ReturnData;
- }
- mIRC(ToBase64)
- {
- //char *data=_strdup(mIRCdata);
- //unsigned long i=0;
- //if (data==NULL || *data=='\0')
- // mIRCdata="0 030 ToBase64";
- //else {
- // i=strtoul(data,NULL,10);
- // snprintf(mIRCdata,BUFSIZE,"1 026 ToBase64 %s",int_to_base64(i));
- //}
- //free(data);
- //return 3;
- strcpy(mIRCdata, "Not supported");
- return Status::ReturnData;
- }
- mIRC(ToBase10)
- {
- //char *data=_strdup(mIRCdata);
- //unsigned long i=0;
- //if (data==NULL || *data=='\0')
- // mIRCdata="0 030 ToBase10";
- //else {
- // i=base64_to_int(data);
- // snprintf(mIRCdata,BUFSIZE,"1 026 ToBase10 %lu",i);
- //}
- //free(data);
- //return 3;
- strcpy(mIRCdata, "Not supported");
- return Status::ReturnData;
- }
- LABELS *GetLabel(char *labelname, char *function, char **mIRCdata)
- {
- LABELS *ret = NULL;
- if (labelname == NULL || strlen(labelname) == 0)
- {
- // 001 - No label specified.
- snprintf(*mIRCdata, BUFSIZE, "0 001 %s", function);
- }
- else if (strlen(labelname) > LBLMAX)
- {
- // 002 - Label too long.
- snprintf(*mIRCdata, BUFSIZE, "0 002 %s %s", function, labelname);
- }
- else if ((ret = findlabel(labelname)) == NULL)
- {
- // 012 - Label not in use.
- snprintf(*mIRCdata, BUFSIZE, "0 012 %s %s", function, labelname);
- }
- return ret;
- }
- /************************************************************************
- * MySQL functions *
- ************************************************************************/
- // Yuck, me no touchie..
- mIRC(Connect) {
- char *data=_strdup(mIRCdata),*lbl=NULL,*input_word_1=NULL,*sql_host=NULL,*sql_db=NULL,*sql_user=NULL,*sql_pass=NULL,*timeout=NULL;
- const char *sqlerror;
- lbl=strtok(data," "),input_word_1=strtok(NULL," ");
- int sqlerrno=0,sql_port=MYSQL_PORT,ssl=0,compress=0,flags=0;
- bool error=false;
- LABELS *templabel=NULL;
- if (check_version(1)) {
- mIRCdata="";
- error=true;
- }
- else if (lbl==NULL || *lbl=='\0') {
- mIRCdata="0 001 Connect";
- error=true;
- }
- else if (strlen(lbl) > LBLMAX) {
- snprintf(mIRCdata,BUFSIZE,"0 002 Connect %s",lbl);
- error=true;
- }
- else if (atoi(lbl) != 0) {
- snprintf(mIRCdata,BUFSIZE,"0 003 Connect %s",lbl);
- error=true;
- }
- else if (findlabel(lbl) != NULL) {
- snprintf(mIRCdata,BUFSIZE,"0 004 Connect %s",lbl);
- error=true;
- }
- else {
- if (input_word_1 && *input_word_1 && (*input_word_1 == '-')) {
- while (*input_word_1++ && !error) {
- switch (*input_word_1) {
- case 'h':
- sql_host=strtok(NULL," ");
- if (sql_host==NULL || *sql_host=='\0') {
- mIRCdata="0 005 Connect";
- error=true;
- }
- break;
- case 'p':
- sql_port=atoi(strtok(NULL," "));
- if (!sql_port) {
- mIRCdata="0 006 Connect";
- error=true;
- }
- break;
- case 'u':
- sql_user=strtok(NULL," ");
- if (sql_user==NULL || *sql_user=='\0') {
- mIRCdata="0 007 Connect";
- error=true;
- }
- break;
- case 'w':
- sql_pass=strtok(NULL," ");
- if (sql_pass==NULL || *sql_pass=='\0') {
- mIRCdata="0 008 Connect";
- error=true;
- }
- break;
- case 'd':
- sql_db=strtok(NULL," ");
- if (sql_db==NULL || *sql_db=='\0') {
- mIRCdata="0 019 Connect";
- error=true;
- }
- break;
- case 't':
- timeout=strtok(NULL," ");
- if (atoi(timeout)==0) {
- mIRCdata="0 032 Connect";
- error=true;
- }
- break;
- case 's':
- ssl=1;
- flags|=CLIENT_SSL;
- break;
- case 'c':
- compress=1;
- flags|=CLIENT_COMPRESS;
- break;
- default:
- if (*input_word_1 && (*input_word_1 > 0) && (*input_word_1 != 32)) {
- snprintf(mIRCdata,BUFSIZE,"0 009 Connect %c",*input_word_1);
- error=true;
- }
- break;
- }
- }
- }
- }
- if (!error) {
- templabel=makelabel(lbl);
- mysql_init(&templabel->mysql);
- if (mysql_real_connect(&templabel->mysql,sql_host,sql_user,sql_pass,sql_db,sql_port,NULL,ssl|compress)) {
- if (timeout)
- mysql_options(&templabel->mysql,MYSQL_OPT_CONNECT_TIMEOUT,timeout);
- snprintf(mIRCdata,BUFSIZE,"1 010 Connect %s",lbl);
- }
- else {
- sqlerror=mysql_error(&templabel->mysql);
- sqlerrno=mysql_errno(&templabel->mysql);
- snprintf(mIRCdata,BUFSIZE,"0 011 Connect %d %s",sqlerrno,sqlerror);
- }
- }
- free(data);
- return Status::ReturnData;
- }
- mIRC(Close)
- {
- char *data = _strdup(mIRCdata);
- LABELS *label;
- // MySQL.dll,Close,<LABEL>
- // Closes the database connection.
- if ((label = GetLabel(data, "Close", &mIRCdata)) == NULL)
- {
- free(data);
- return Status::ReturnData;
- }
- removelabel(label);
- // 013 - Successful close.
- snprintf(mIRCdata, BUFSIZE, "1 013 Close %s", data);
- free(data);
- return Status::ReturnData;
- }
- mIRC(Labels)
- {
- char *data = _strdup(mIRCdata);
- int num = atoi(data);
- LABELS *label;
- // MySQL.dll,Labels,[n]
- // If n is specified, and a positive number, the label matching that number will be returned.
- // If n is 0, or not specified, the total number of labels will be returned. If n is greater
- // than the number of labels, an error will be returned.
- if (num == 0)
- {
- snprintf(mIRCdata, BUFSIZE, "1 016 Labels %d", labelcount());
- }
- else if (num < 0)
- {
- strcpy(mIRCdata, "0 015 Labels");
- }
- else if ((unsigned long)num > labelcount())
- {
- strcpy(mIRCdata, "0 014 Labels");
- }
- else
- {
- label = findlabel_by_digit(num);
- snprintf(mIRCdata, BUFSIZE, "1 016 Labels %s", label->label);
- }
- free(data);
- return Status::ReturnData;
- }
- mIRC(Rows)
- {
- char *data = _strdup(mIRCdata);
- char *labelname = data;
- char buffer[(BUFSIZE - 20)] = "";
- LABELS *label = NULL;
- // Usage: MySQL.dll,Rows,<LABEL>
- // Will return the number of rows returned by Query.
- if ((label = GetLabel(data, "Rows", &mIRCdata)) == NULL)
- {
- free(data);
- return Status::ReturnData;
- }
- if (label->result == NULL)
- {
- // 028 - No result available.
- snprintf(mIRCdata, BUFSIZE, "0 028 Rows %s", labelname);
- free(data);
- return Status::ReturnData;
- }
- // 018 - Successful row count.
- snprintf(mIRCdata, BUFSIZE, "1 018 Rows %s %d", labelname, mysql_num_rows(label->result));
- free(data);
- return Status::ReturnData;
- }
- mIRC(RowsAffected)
- {
- char *data = _strdup(mIRCdata);
- char *labelname = data;
- char buffer[(BUFSIZE - 20)] = "";
- LABELS *label = NULL;
- // Usage: MySQL.dll,RowsAffected,<LABEL>
- // Will return the number of rows affected by a DELETE, UPDATE, or INSERT statement. If the
- // query was a SELECT statement, RowsAffected will return the same result as Rows.
- if ((label = GetLabel(data, "RowsAffected", &mIRCdata)) == NULL)
- {
- free(data);
- return Status::ReturnData;
- }
- if (label->result == NULL)
- {
- // 028 - No result available.
- snprintf(mIRCdata, BUFSIZE, "0 028 RowsAffected %s", labelname);
- free(data);
- return Status::ReturnData;
- }
- // 018 - Successful row count.
- snprintf(mIRCdata, BUFSIZE, "1 018 RowsAffected %s %d", labelname, mysql_affected_rows(&label->mysql));
- free(data);
- return Status::ReturnData;
- }
- mIRC(Query)
- {
- char *data = _strdup(mIRCdata);
- char *tok, *temp, *labelname, *dbname = NULL, *query = NULL;
- LABELS *label = NULL;
- // Usage: MySQL.dll,Query,<LABEL> -d [DB] <QUERY>
- // LABEL is the label you used when calling the Connect command. Use a standard MySQL query for
- // QUERY. If the -d flag is specified, then the default database will be changed to DB.
- labelname = strtok_s(data, " ", &tok);
- temp = strtok_s(NULL, "", &tok);
- if ((label = GetLabel(labelname, "Query", &mIRCdata)) == NULL)
- {
- free(data);
- return Status::ReturnData;
- }
- if (temp == NULL || strlen(temp) == 0)
- {
- // 030 - Null data.
- snprintf(mIRCdata, BUFSIZE, "0 030 Query %s", labelname);
- free(data);
- return Status::ReturnData;
- }
- query = temp;
- // Check for '-d <database>'
- if (temp[0] == '-')
- {
- char *flags = strtok_s(temp, " ", &tok);
- if (strlen(flags) == 1 || flags[1] != 'd')
- {
- // 009 - Invalid option flag.
- snprintf(mIRCdata, BUFSIZE, "0 009 Query %s %c", labelname, (strlen(flags) == 1 ? '?' : flags[1]));
- free(data);
- return Status::ReturnData;
- }
- if ((dbname = strtok_s(NULL, " ", &tok)) == NULL)
- {
- // 019 - No database specified.
- snprintf(mIRCdata, BUFSIZE, "0 019 Query %s", labelname);
- free(data);
- return Status::ReturnData;
- }
- query = strtok_s(NULL, "", &tok);
- }
- if (query == NULL)
- {
- // 030 - Null data.
- snprintf(mIRCdata, BUFSIZE, "0 030 Query %s", labelname);
- free(data);
- return Status::ReturnData;
- }
- if (label->result != NULL)
- {
- mysql_free_result(label->result);
- label->result = NULL;
- }
- if (mysql_ping(&label->mysql) != 0)
- {
- // 034 - Server connection dropped.
- snprintf(mIRCdata, BUFSIZE, "0 034 Query %s", labelname);
- free(data);
- return Status::ReturnData;
- }
- if (dbname != NULL && mysql_select_db(&label->mysql, dbname) != 0)
- {
- // 020 - Unable to set DB.
- snprintf(mIRCdata, BUFSIZE, "0 020 Query %s %s", labelname, dbname);
- free(data);
- return Status::ReturnData;
- }
- if (mysql_real_query(&label->mysql, query, strlen(query)) != 0)
- {
- // 021 - Unable to send Query.
- snprintf(mIRCdata, BUFSIZE, "0 021 Query %s %s", labelname, query);
- free(data);
- return Status::ReturnData;
- }
- label->result = mysql_store_result(&label->mysql);
- // 022 - Successful query.
- snprintf(mIRCdata, BUFSIZE, "1 022 Query %s %d", labelname, (label->result == NULL ? 0 : 1));
- free(data);
- return Status::ReturnData;
- }
- mIRC(Fields)
- {
- char *data = _strdup(mIRCdata);
- char *labelname = data;
- char buffer[(BUFSIZE - 20)] = "";
- int numfields;
- LABELS *label = NULL;
- MYSQL_FIELD *fields;
- // Usage: MySQL.dll,Fields,<LABEL>
- // Will return a comma delimited list of the field names returned by a Query.
- if ((label = GetLabel(data, "Fields", &mIRCdata)) == NULL)
- {
- free(data);
- return Status::ReturnData;
- }
- if (label->result == NULL)
- {
- // 028 - No result available.
- snprintf(mIRCdata, BUFSIZE, "0 028 Fields %s", labelname);
- free(data);
- return Status::ReturnData;
- }
- numfields = mysql_num_fields(label->result);
- if ((fields = mysql_fetch_fields(label->result)) == NULL)
- {
- // 031 - Null result.
- snprintf(mIRCdata, BUFSIZE, "0 031 Fields %s", labelname);
- free(data);
- return Status::ReturnData;
- }
- for (int i = 0; i < numfields; i++)
- {
- char *value = (fields[i].name == NULL ? "NULL" : fields[i].name);
- if (strlen(buffer) + strlen(value) + 1 >= sizeof(buffer))
- {
- // 035 - Result too large.
- // TODO: Add to error file
- snprintf(mIRCdata, BUFSIZE, "0 035 Fields %s", labelname);
- free(data);
- return Status::ReturnData;
- }
- if (strlen(buffer) > 0) strcat(buffer, ",");
- strcat(buffer, value);
- }
- // 023 - Successful fields lookup.
- snprintf(mIRCdata, BUFSIZE, "1 023 Fields %s %d %s", labelname, numfields, buffer);
- free(data);
- return Status::ReturnData;
- }
- mIRC(FetchRow)
- {
- char *data = _strdup(mIRCdata);
- char *tok, *labelname, *rownum, *seperator;
- char buffer[(BUFSIZE - 20)] = "";
- int row, numfields;
- LABELS *label = NULL;
- MYSQL_ROW rowdata;
- // Usage: MySQL.dll,FetchRow,<LABEL> <ROW> [SEPARATOR]
- // Will return the row number specified by ROW, with data being separated by the character
- // specified by SEPARATOR. If SEPARATOR is not specified, a tab character will be assumed.
- labelname = strtok_s(data, " ", &tok);
- rownum = strtok_s(NULL, " ", &tok);
- seperator = strtok_s(NULL, " ", &tok);
- if ((label = GetLabel(labelname, "Query", &mIRCdata)) == NULL)
- {
- free(data);
- return Status::ReturnData;
- }
- if (label->result == NULL)
- {
- // 028 - No result available.
- snprintf(mIRCdata, BUFSIZE, "0 028 FetchRow %s", labelname);
- free(data);
- return Status::ReturnData;
- }
- if (rownum == NULL || strlen(rownum) == 0 || (row = atoi(rownum)) <= 0 || row > mysql_num_rows(label->result))
- {
- // 024 - Invalid row pointer.
- snprintf(mIRCdata, BUFSIZE, "0 024 FetchRow %s", labelname);
- free(data);
- return Status::ReturnData;
- }
- if (seperator == NULL)
- {
- seperator = "\t";
- }
- mysql_data_seek(label->result, (row - 1));
- numfields = mysql_num_fields(label->result);
- if ((rowdata = mysql_fetch_row(label->result)) == NULL)
- {
- // 031 - Null result.
- snprintf(mIRCdata, BUFSIZE, "0 031 FetchRow %s %d", labelname, row);
- free(data);
- return Status::ReturnData;
- }
- for (int i = 0; i < numfields; i++)
- {
- char *value = (rowdata[i] == NULL ? "NULL" : rowdata[i]);
- if (strlen(buffer) + strlen(value) + strlen(seperator) >= sizeof(buffer))
- {
- // 035 - Result too large.
- // TODO: Add to error file
- snprintf(mIRCdata, BUFSIZE, "0 035 FetchRow %s %d", labelname, row);
- free(data);
- return Status::ReturnData;
- }
- if (strlen(buffer) > 0) strcat(buffer, seperator);
- strcat(buffer, value);
- }
- // 025 - Successful row retrieval.
- snprintf(mIRCdata, BUFSIZE, "1 025 FetchRow %s %d %s", labelname, numfields, buffer);
- free(data);
- return Status::ReturnData;
- }
- char *IntEscapeString(char *data)
- {
- char *escaped = (char *)calloc(1, (strlen(data) * 2) + 1);
- if (data != NULL)
- {
- mysql_escape_string(escaped, data, strlen(data));
- }
- return escaped;
- }
- mIRC(EscapeString)
- {
- char *data = _strdup(mIRCdata);
- if (data == NULL || strlen(data) == 0)
- {
- // 027 - Successful escape string.
- strcpy(mIRCdata, "1 027 EscapeString");
- }
- else
- {
- char *escape = IntEscapeString(data);
- snprintf(mIRCdata, BUFSIZE, "1 027 EscapeString %s", escape);
- free(escape);
- }
- free(data);
- return Status::ReturnData;
- }
- mIRC(bEscapeString)
- {
- char *data = _strdup(mIRCdata);
- if (data == NULL || strlen(data) == 0)
- {
- strcpy(mIRCdata, "");
- }
- else
- {
- char *escape = IntEscapeString(data);
- strncpy(mIRCdata, escape, BUFSIZE);
- free(escape);
- }
- free(data);
- return Status::ReturnData;
- }
- mIRC(SetDatabase)
- {
- char *data = _strdup(mIRCdata);
- char *tok, *labelname, *dbname;
- LABELS *label = NULL;
- labelname = strtok_s(data, " ", &tok);
- dbname = strtok_s(NULL, "", &tok);
- if ((label = GetLabel(labelname, "SetDatabase", &mIRCdata)) == NULL)
- {
- free(data);
- return Status::ReturnData;
- }
- if (dbname == NULL || strlen(dbname) == 0)
- {
- // 019 - No database specified.
- snprintf(mIRCdata, BUFSIZE, "0 019 SetDatabase %s", labelname);
- free(data);
- return Status::ReturnData;
- }
- if (mysql_ping(&label->mysql) != 0)
- {
- // 034 - Server connection dropped.
- snprintf(mIRCdata, BUFSIZE, "0 034 SetDatabase %s", labelname);
- free(data);
- return Status::ReturnData;
- }
- if (mysql_select_db(&label->mysql, dbname) != 0)
- {
- // 020 - Unable to set DB.
- snprintf(mIRCdata, BUFSIZE, "0 020 SetDatabase %s", labelname);
- free(data);
- return Status::ReturnData;
- }
- // 022 - Successful query.
- snprintf(mIRCdata, BUFSIZE, "1 022 SetDatabase %s", labelname);
- free(data);
- return Status::ReturnData;
- }
- mIRC(Error)
- {
- char *data = _strdup(mIRCdata);
- LABELS *label;
- if ((label = GetLabel(data, "Error", &mIRCdata)) == NULL)
- {
- free(data);
- return Status::ReturnData;
- }
- // 029 - Successful error lookup.
- snprintf(mIRCdata, BUFSIZE, "1 029 Error %d %s", mysql_errno(&label->mysql), mysql_error(&label->mysql));
- free(data);
- return Status::ReturnData;
- }
Add Comment
Please, Sign In to add comment