PRBool urlEquals (const char* url1, const char* url2) { int16 n1 = charSearch('#', url1); int16 n2 = charSearch('#', url2); if ((n1 == -1) && (n2 == -1)) { return (strcmp(url1, url2) == 0); } else if ((n2 == -1) && (n1 > 0)) { return ((strlen(url2) == (size_t)(n1)) && (strncmp(url1, url2, n1) == 0)); } else if ((n1 == -1) && (size_t) (n2 > 0)) { return ((strlen(url1) == (size_t)(n2)) && (strncmp(url1, url2, n2) == 0)); } else return 0; }
RDF_Resource resolveReference (char *tok, RDFFile f) { RDF_Resource existing; char* url = getMem(MAX_URL_SIZE); if (tok[0] == '#') tok = &tok[1]; if (tok[strlen(tok)-1] == '"') tok[strlen(tok)-1] = '\0'; if (tok[0] == '"') tok = &tok[1]; memset(url, '\0', 200); if (charSearch(':', tok) == -1) { derelativizeURL(tok, url, f); } else { memcpy(url, tok, strlen(tok)); } if (strcmp(url,"this") == 0) { existing = f->top; } else { existing = RDF_GetResource(NULL, url, false); } if (existing != null) return existing; existing = RDF_GetResource(NULL, url, true); addToResourceList(f, existing); freeMem(url); return existing; }
RDF_Error parseSlotValue (RDFFile f, RDF_Resource s, char* value, void** parsed_value, RDF_ValueType* data_type) { if (value[0] == '"') { int32 size = strlen(value)-1; *parsed_value = getMem(size); value[size] = '\0'; *parsed_value = &value[1]; *data_type = RDF_STRING_TYPE; return noRDFErr; } else if (value[0] == '#') { if (value[1] == '"') { value[strlen(value)-1] = '\0'; value = &value[2]; } else { value = &value[1]; } *parsed_value = resolveReference(value, f); return noRDFErr; } else if (charSearch('.', value) == -1) { int16 ans = 0; /* XXX sscanf(value, "%ld", &ans); */ *data_type = RDF_INT_TYPE; return noRDFErr; } else { return -1; } }
RDF_Resource HistCreate (char* url, PRBool createp) { size_t size = strlen(url); char* nurl = getMem(size+8); RDF_Resource ans; if (charSearch(':', url) == -1) { if (url[size-1] == '/') { sprintf(nurl, "http://%s", url); nurl[strlen(nurl)-1] = '\0'; } else { sprintf(nurl, "http://%s/", url); } } else { if (url[size-1] == '/') { memcpy(nurl, url, size-1); } else { sprintf(nurl, "%s/", url); } } ans = RDF_GetResource(NULL, nurl, 0); if (ans != NULL) { freeMem(nurl); return ans; } freeMem(nurl); ans = RDF_GetResource(NULL, url, createp); if (ans != NULL) { return ans; } else { return NULL; } }
bool IrcBot::msgHandler(char * buf) { // add code here to do a search if(charSearch(buf, "hello")) sendData("PRIVMSG "+channelName+" :hello\r\n"); return true; }
bool IrcBot::isConnected(char *buf) { //If we find /MOTD then its ok join a channel if (charSearch(buf,"/MOTD") == true) return true; else return false; }
void stringAppendBase (char* dest, const char* addition) { int32 l1 = strlen(dest); int32 l2 = strlen(addition); int32 l3 = charSearch('#', addition); if (l3 != -1) l2 = l3; memcpy(&dest[l1], addition, l2); }
void newFolderBkItem(RDFFile f, char* token) { int16 start, end; start = charSearch('"', token); end = revCharSearch('"', token); token[end] = '\0'; gBkFolderDate = copyString(&token[start+1]); }
RDF_Resource hostUnitOfURL (RDF r, RDF_Resource top, RDF_Resource nu, char* title) { char host[100]; char* url = resourceID(nu); int16 s1, s2, s3; RDF_Resource hostResource, existing; if (strlen(url) > 100) return NULL; if (startsWith("file", url)) { return RDF_GetResource(NULL, "Local Files", 1); } else { memset(host, '\0', 100); s1 = charSearch(':', url)+3; s2 = charSearch('/', &url[s1]); s3 = charSearch(':', &url[s1]); if (s2 == -1) s2 = strlen(url)-s1; if ((s3 != -1) && (s2 > s3)) s2 = s3; if (startsWith("www", &url[s1])) {s1 = s1+4; s2 = s2-4;} if (s2<1) return(NULL); memcpy((char*)host, &url[s1], s2); host[0] = toupper(host[0]); hostResource = RDF_GetResource(NULL, host, 1); setContainerp(hostResource, 1); setResourceType(hostResource, HISTORY_RT); existing = PL_HashTableLookup(hostHash, hostResource); if (existing != NULL) { if (existing == nu) { return existing; } else if (existing == top) { return hostResource; } else { remoteStoreRemove(gRemoteStore, existing, gCoreVocab->RDF_parent, top, RDF_RESOURCE_TYPE); histAddParent(existing, hostResource); histAddParent(hostResource, top); PL_HashTableAdd(hostHash, hostResource, top); return hostResource; } } else { PL_HashTableAdd(hostHash, hostResource, nu); histAddParent(nu, top); return nu; } } }
char * getRelURL (RDF_Resource u, RDF_Resource top) { char* uID = resourceID(u); char* topID = resourceID(top); if (startsWith(topID, uID)) { int16 n = charSearch('#', uID); if (n == -1) return uID; return &(uID)[n+1]; } else return uID; }
char * getBaseURL (const char* url) { int n = charSearch('#' , url); char* ans; if (n == -1) return copyString(url); if (n == 0) return NULL; ans = getMem(n+1); memcpy(ans, url, n); return ans; }
void parseNextESFTPLine (RDFFile f, char* line) { int16 i1, i2; char url[100]; RDF_Resource ru; PRBool directoryp; if (f->fileType == FTP_RT) { if (!startsWith("201", line)) return; line = &line[5]; } i1 = charSearch(' ', line); if (i1 == -1) return; i2 = charSearch(' ', &line[i1+1]) + i1 + 1; line[i1] = '\0'; directoryp = 0; if (strlen(line) > 64) return; if (resourceType(f->top) == ES_RT) { directoryp = startsWith("Director", &line[i1+1]); sprintf(url, "nes:%s%s%s", f->url, line, (directoryp ? "/" : "")); } else { int i3 = charSearch(' ', &line[i1+i2+1]); directoryp = startsWith("Directory", &line[i1+i2+i3+2]); /* this is bad as files can be of zero-length! if ((charSearch('.', line) ==-1) && (startsWith("0", &line[i1+1]))) directoryp = 1; */ sprintf(url, "%s%s%s", f->url, line, (directoryp ? "/" : "")); } ru = RDF_GetResource(NULL, url, 1); setResourceType(ru, resourceType(f->top)); if (directoryp) setContainerp(ru, 1); /* remoteStoreAdd(gRemoteStore, ru, gCoreVocab->RDF_name, NET_UnEscape(line), RDF_STRING_TYPE, 1); */ remoteStoreAdd(gRemoteStore, ru, gCoreVocab->RDF_parent, f->top, RDF_RESOURCE_TYPE, 1); }
RDF_Resource resolveGenlPosReference(char* tok, RDFFile f) { RDF_Resource ans; char* url = (char*)getMem(MAX_URL_SIZE); long i1, i2; i1 = charSearch('"', tok); i2 = revCharSearch('"', tok); memcpy(url, &tok[i1], i2-i1+1); ans = resolveReference(url, f); freeMem(url); return ans; }
void derelativizeURL (char* tok, char* url, RDFFile f) { if ((tok[0] == '/') && (endsWith(".mco", tok))) { void stringAppendBase (char* dest, const char* addition) ; stringAppendBase(url, f->url); stringAppend(url, "#"); stringAppend(url, tok); } else if ((endsWith(".mco", tok)) && (charSearch('#', tok) == -1)) { void stringAppendBase (char* dest, const char* addition) ; stringAppendBase(url, f->url); stringAppend(url, "#"); stringAppend(url, tok); } else { memcpy(url, tok, strlen(tok)); } }
bool IrcBot::bot() { char *text; char buf[1024]; int bytes_read; int bytes_send; string sendCommands[5] = { "PONG", "USER "+userName+" 8 * :"+userName, "NICK "+nickName, "PRIVMSG nickserv identify "+userPass, "JOIN "+channelName }; printf("Connecting to %s:%s\nNick: %s | Channel: %s\n", hostName.c_str(), portString.c_str(), nickName.c_str(), channelName.c_str()); ConnectSocket = INVALID_SOCKET; struct addrinfo *result = NULL, *ptr = NULL, hints; const char *sendbuf = "\nPONG :Bot\n"; char recvbuf[DEFAULT_BUFLEN] = { }; int recvbuflen = DEFAULT_BUFLEN; #ifdef WIN32 // start WinSock Initialization WSADATA wsaData; // Initialize Winsock iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != 0) { printf("WSAStartup failed with error: %d\n", iResult); return 1; } #endif // WIN32 ZeroMemory( &hints, sizeof(hints) ); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; // Resolve the server address and port iResult = getaddrinfo(hostName.c_str(), portString.c_str(), &hints, &result); if ( iResult != 0 ) { printf("getaddrinfo failed with error: %d\n", iResult); #ifdef WIN32 WSACleanup(); #endif // WIN32 return 1; } // Attempt to connect to an address until one succeeds for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) { // Create a SOCKET for connecting to server ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); if (ConnectSocket == INVALID_SOCKET) { printf("socket failed with error: %ld\n", WSAGetLastError()); WSACleanup(); return 1; } // Connect to server. iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen); if (iResult == SOCKET_ERROR) { closesocket(ConnectSocket); ConnectSocket = INVALID_SOCKET; continue; } break; } freeaddrinfo(result); if (ConnectSocket == INVALID_SOCKET) { printf("Unable to connect to server!\n"); #ifdef WIN32 WSACleanup(); #endif // WIN32 return 1; } // Send an initial buffer iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 ); if(DEBUG == true) printf("Sendbuf: %s\n", sendbuf); if (iResult == SOCKET_ERROR) { #ifdef WIN32 printf("send failed with error: %d\n", WSAGetLastError()); closesocket(ConnectSocket); WSACleanup(); #endif // WIN32 return 1; } printf("Bytes Sent: %ld\n", iResult); // Receive until the peer closes the connection int count = 0; while(1) { count++; switch(count) { case 2: // we send data to server after 3 counts // nick sendCommand(ConnectSocket, "\nUSER %s Bot Bot : %s\n", userName.c_str(), userName.c_str()); // user sendCommand(ConnectSocket, "\nNICK %s\r\n", nickName.c_str()); break; case 11: // register sendCommand(ConnectSocket, "\nPRIVMSG nickserv identify %s\r\n", userPass.c_str()); break; case 12: // we join channel sendCommand(ConnectSocket, "\nJOIN %s\r\n", channelName.c_str()); default: break; } // Recv and print data bytes_read = recv(ConnectSocket, buf, MAXDATASIZE-1, 0); buf[bytes_read]='\0'; cout << buf << endl; // buf is the data that is recieved // pass buffer to message handler msgHandler(buf); // if we get ping if(charSearch(buf,"PING")) { sendPong(buf); } //break if connection closed if(bytes_read == 0) { cout << "Connection closed!" << endl; cout << timeNow() << endl; break; } } #ifdef WIN32 // cleanup closesocket(ConnectSocket); WSACleanup(); #elif LINUX close(ConnectSocket); #endif return true; }