int read_pdb(FILE *pdbInFile, Str *str, int coarse, int hydrogens) { unsigned int i, j; unsigned int k = 0; char line[80]; char stopline[80] = ""; int stopflag = 0; unsigned int allocated_atom = 64; unsigned int allocated_residue = 64; char atomName[] = " "; char resbuf; int ca_p = 0; /* for HETATM entries */ regex_t *regexPattern; /* regular atom patterns */ /* allowed HETATM atom types (standard N,CA,C,O) and elements (any N,C,O,P,S) */ const int nHetAtom = 9; char hetAtomPattern[9][32] = {{" N "},{" CA "},{" C "},{" O "},{".{1}C[[:print:]]{1,3}"},{".{1}N[[:print:]]{1,3}"},{".{1}O[[:print:]]{1,3}"},{".{1}P[[:print:]]{1,3}"},{".{1}S[[:print:]]{1,3}"}}; char hetAtomNewname[9][32] = {{" N "},{" CA "},{" C "},{" O "},{" C_ "},{" N_ "},{" O_ "},{" P_ "},{" S_ "}}; /*____________________________________________________________________________*/ /* initialise/allocate memory for set of (64) selected (CA) atom entries */ str->nAtom = 0; str->nAllAtom = 0; str->nResidue = 0; str->nAllResidue = 0; str->nChain = 0; str->atom = safe_malloc(allocated_atom * sizeof(Atom)); str->atomMap = safe_malloc(allocated_atom * sizeof(int)); /* allocate memory for sequence residues */ str->sequence.res = safe_malloc(allocated_residue * sizeof(char)); /* compile allowed HETATM element patterns */ regexPattern = safe_malloc(nHetAtom * sizeof(regex_t)); compile_patterns(regexPattern, &(hetAtomPattern[0]), nHetAtom); /*____________________________________________________________________________*/ /* count the number of models */ while(fgets(line, 80, pdbInFile) != 0) { if (strncmp(line, "MODEL ", 6) == 0) { if (stopflag == 0) { stopflag = 1; continue; } else { strcpy(stopline, line); break; } } } /* rewind the file handle to the start */ if (fseek(pdbInFile, 0L, SEEK_SET) != 0) { /* handle repositioning error */ } /*____________________________________________________________________________*/ /* not all PDB data types are used in this program to save resources */ while(fgets(line, 80, pdbInFile) != 0) { ca_p = 0; /* CA or P flag */ /*____________________________________________________________________________*/ /* check conditions to start assigning this entry */ /* skip other models */ if((strcmp(line, stopline) == 0) && (stopflag == 1)) break; /* read only ATOM/HETATM records */ if((strncmp(line, "ATOM ", 6) != 0) && (strncmp(line, "HETATM", 6) != 0)) continue; /* skip alternative locations except for location 'A' */ if (line[16] != 32 && line[16] != 65) { /*fprintf(stderr, "Warning: Skipping atom %d in alternative location %c\n", atoi(&line[6]), line[16]);*/ continue; } /*____________________________________________________________________________*/ /* read this entry */ /* atom number */ str->atom[str->nAtom].atomNumber = atoi(&line[6]); /* atom name */ for (i = 12, j = 0; i < 16; ) str->atom[str->nAtom].atomName[j++] = line[i++]; str->atom[str->nAtom].atomName[j] = '\0'; /* alternative location */ /*str->atom[str->nAtom].alternativeLocation[0] = line[16]; str->atom[str->nAtom].alternativeLocation[1] = '\0';*/ /* residue name */ for (i = 17, j = 0; i < 20; ) str->atom[str->nAtom].residueName[j++] = line[i++]; str->atom[str->nAtom].residueName[j] = '\0'; /* chain identifier */ str->atom[str->nAtom].chainIdentifier[0] = line[21]; str->atom[str->nAtom].chainIdentifier[1] = '\0'; /* residue number */ str->atom[str->nAtom].residueNumber = atoi(&line[22]); /* code for insertion of residues */ /*str->atom[str->nAtom].icode[0] = line[26]; str->atom[str->nAtom].icode[1] = '\0';*/ /* coordinates */ str->atom[str->nAtom].pos.x = atof(&line[30]); str->atom[str->nAtom].pos.y = atof(&line[38]); str->atom[str->nAtom].pos.z = atof(&line[46]); /*printf("x %6.4f, y %6.4f, z %6.4f\n", str->atom[str->nAtom].x, str->atom[str->nAtom].y, str->atom[str->nAtom].z);*/ /* occupancy */ /*str->atom[str->nAtom].occupancy = atof(&line[54]);*/ /* temperature factor */ /*str->atom[str->nAtom].temp_f = atof(&line[60]);*/ /* segment identifier */ /*for (i = 72, j = 0; i < 76; ) str->atom[str->nAtom].segmentIdentifier[j++] = line[i++]; str->atom[str->nAtom].segmentIdentifier[j] = '\0';*/ /* element */ for (i = 76, j = 0; i < 78; ) str->atom[str->nAtom].element[j++] = line[i++]; str->atom[str->nAtom].element[j] = '\0'; /* charge */ /*for (i = 78, j = 0; i < 80; ) str->atom[str->nAtom].charge[j++] = line[i++]; str->atom[str->nAtom].charge[j] = '\0';*/ /* description: everything before coordinates */ for (i = 0, j = 0; i < 30; ) str->atom[str->nAtom].description[j++] = line[i++]; str->atom[str->nAtom].description[j] = '\0'; /*____________________________________________________________________________*/ /* check conditions to record this entry */ /* if no hydrogens set, skip hydrogen lines */ if (! hydrogens) { strip_char(str->atom[str->nAtom].atomName, &(atomName[0])); /* skip patterns 'H...' and '?H..', where '?' is a digit */ if ((atomName[0] == 'H') || \ ((atomName[0] >= 48) && (atomName[0] <= 57) && (atomName[1] == 'H'))) { ++ str->nAllAtom; continue; } } /* check whether ATOM residue name is standard */ if (strncmp(line, "ATOM ", 6) == 0) resbuf = aacode(str->atom[str->nAtom].residueName); /* detect CA and P atoms of standard residues for residue allocation */ if ((strncmp(line, "ATOM ", 6) == 0) && ((strncmp(str->atom[str->nAtom].atomName, " CA ", 4) == 0) || (strncmp(str->atom[str->nAtom].atomName, " P ", 4) == 0))) { str->sequence.res[k++] = aacode(str->atom[str->nAtom].residueName); ++ ca_p; if (k == allocated_residue) str->sequence.res = safe_realloc(str->sequence.res, (allocated_residue += 64) * sizeof(char)); } /* standardise non-standard atom names */ standardise_name(str->atom[str->nAtom].residueName, str->atom[str->nAtom].atomName); /* process HETATM entries */ if (strncmp(line, "HETATM", 6) == 0) if (process_het(str, &(line[0]), regexPattern, &(hetAtomNewname[0]), nHetAtom) != 0) continue; /* in coarse mode record only CA and P entries */ if (!ca_p && coarse) continue; /*____________________________________________________________________________*/ /* count number of allResidues (including HETATM residues) */ if (str->nAtom == 0 || str->atom[str->nAtom].residueNumber != str->atom[str->nAtom - 1].residueNumber) ++ str->nAllResidue; /*____________________________________________________________________________*/ /* count number of chains */ if (str->nAtom == 0 || str->atom[str->nAtom].chainIdentifier[0] != str->atom[str->nAtom - 1].chainIdentifier[0]) ++ str->nChain; /*____________________________________________________________________________*/ /* recors original atom order (count) */ str->atomMap[str->nAtom] = str->nAllAtom; /* increment to next atom entry */ ++ str->nAtom; ++ str->nAllAtom; /*____________________________________________________________________________*/ /* allocate more memory if needed */ if (str->nAtom == allocated_atom) { allocated_atom += 64; str->atom = safe_realloc(str->atom, allocated_atom * sizeof(Atom)); str->atomMap = safe_realloc(str->atomMap, allocated_atom * sizeof(int)); } } str->sequence.res[k] = '\0'; str->nResidue = k; /*____________________________________________________________________________*/ /* free the compiled regular expressions */ free_patterns(regexPattern, nHetAtom); free(regexPattern); /*____________________________________________________________________________*/ return 0; }
int main(int argc, char **argv) { tic(); char *conf_file = NULL; socket_fd = -1; for (int i=1; i<argc; i++) { if (!strcmp(argv[i], "-c")) { if (argc <= i) { print_usage(); } conf_file = argv[++i]; } else if (!strcmp(argv[i], "-fd")) { if (argc <= i) { print_usage(); } socket_fd = atoi(argv[++i]); } else { printf(" >> unknown option: %s\n", argv[i]); } } if (!conf_file) conf_file = "cbot.conf"; load_config(conf_file); // Set rand seed srand(time(NULL)); // Set up cURL curl_global_init(CURL_GLOBAL_ALL); // Set up db connection for logging if (config->enabled_modules & MODULE_LOG) { log_init(); } // Parse markov corpus if (config->enabled_modules & MODULE_MARKOV) { markov_init(config->markovcorpus); } irc_init(); if (socket_fd == -1) { printf(" - Connecting to %s:%s with nick %s, joining channels...\n", config->host, config->port, config->nick); net_connect(); } else { // In-place upgrade yo printf(" >> Already connected, upgraded in-place!\n"); join_channels(); } struct recv_data *irc = malloc(sizeof(struct recv_data)); patterns = malloc(sizeof(*patterns)); compile_patterns(patterns); // Select param fd_set socket_set; FD_ZERO(&socket_set); FD_SET(STDIN_FILENO, &socket_set); FD_SET(socket_fd, &socket_set); int recv_size; char buffer[BUFFER_SIZE]; char input[BUFFER_SIZE]; memset(buffer, 0, BUFFER_SIZE); size_t buffer_length = 0; while (1) { int ret = select(socket_fd+1, &socket_set, 0, 0, 0); if (ret == -1) { printf(" >> Disconnected, reconnecting...\n"); close(socket_fd); net_connect(); } if (FD_ISSET(STDIN_FILENO, &socket_set)) { if (fgets(input, BUFFER_SIZE, stdin) == NULL) { printf(" >> Error while reading from stdin!\n"); continue; } if (strcmp(input, "quit\n") == 0) { printf(" >> Bye!\n"); break; } else if (strcmp(input, "reload\n") == 0) { terminate(); free(irc); free_patterns(patterns); free(patterns); // Set up arguments char * arguments[6]; arguments[0] = argv[0]; arguments[1] = "-c"; arguments[2] = conf_file; arguments[3] = "-fd"; char fdstring[snprintf(NULL, 0, "%d", socket_fd)]; sprintf(fdstring, "%d", socket_fd); arguments[4] = fdstring; arguments[5] = NULL; printf(" >> Upgrading...\n"); execvp(argv[0], arguments); printf(" !!! Execvp failing, giving up...\n"); exit(-1); } else if (strncmp(input, "say ", 4) == 0) { int offsets[30]; int offsetcount = pcre_exec(patterns->command_say, 0, input, strlen(input), 0, 0, offsets, 30); if (offsetcount > 0) { char channel[BUFFER_SIZE]; char message[BUFFER_SIZE]; pcre_copy_substring(input, offsets, offsetcount, 1, channel, BUFFER_SIZE); pcre_copy_substring(input, offsets, offsetcount, 2, message, BUFFER_SIZE); char sendbuf[strlen("PRIVMSG : ") + strlen(channel) + strlen(message)]; sprintf(sendbuf, "PRIVMSG %s :%s\n", channel, message); irc_send_str(sendbuf); } } else if (strncmp(input, "kick ", 5) == 0) { int offsets[30]; int offsetcount = pcre_exec(patterns->command_kick, 0, input, strlen(input), 0, 0, offsets, 30); if (offsetcount > 0) { char channel[BUFFER_SIZE]; char user[BUFFER_SIZE]; pcre_copy_substring(input, offsets, offsetcount, 1, channel, BUFFER_SIZE); pcre_copy_substring(input, offsets, offsetcount, 2, user, BUFFER_SIZE); char sendbuf[strlen("KICK :Gene police! You! Out of the pool, now!\n") + strlen(channel) + strlen(user)]; sprintf(sendbuf, "KICK %s %s :Gene police! You! Out of the pool, now!\n", channel, user); irc_send_str(sendbuf); } } else { printf(" >> Unrecognized command. Try 'quit'\n"); } FD_SET(socket_fd, &socket_set); } else { if (buffer_length >= BUFFER_SIZE - 1) { printf(" >> what the f**k, IRCd, a line longer than 4k? dropping some buffer\n"); memset(buffer, 0, BUFFER_SIZE); buffer_length = 0; continue; } recv_size = recv(socket_fd, buffer + buffer_length, BUFFER_SIZE - buffer_length - 1, 0); buffer_length += recv_size; buffer[buffer_length] = '\0'; if (recv_size == 0) { printf(" >> recv_size is 0, assuming closed remote socket, reconnecting\n"); close(socket_fd); printf("closed\n"); net_connect(); printf("reconnected\n"); } char *newlinepos = 0; char *bufbegin = buffer; while ((newlinepos = strchr(bufbegin, '\n'))) { *newlinepos = 0; printf(" ~ %s\n", bufbegin); // Only handle privmsg if (irc_parse_input(bufbegin, irc, patterns)) { irc_handle_input(irc, patterns); } bufbegin = newlinepos + 1; } size_t bytes_removed = bufbegin - buffer; memmove(buffer, bufbegin, buffer_length - bytes_removed); buffer_length -= bytes_removed; memset(buffer + buffer_length, 0, BUFFER_SIZE - buffer_length); FD_SET(STDIN_FILENO, &socket_set); } } printf(" >> Socket closed, quitting...\n"); close(socket_fd); free(irc); free_patterns(patterns); free(patterns); terminate(); return 0; }
int main(int argc, char **argv) { openlog("redirect_rewrite", LOG_PID|LOG_CONS, LOG_USER); compile_patterns(); /* Allocate some memory */ content = (char*) malloc(sizeof(char) * BUF_SIZE); memset (content,0,sizeof(char) * BUF_SIZE); /* Check if the memory couldn't be allocated; if it's the case, handle the problem as appropriate. */ if (NULL == content) { #ifdef DEBUG perror("Could not allocate memory"); #endif return EXIT_FAILURE; } unsigned int n = 0; unsigned int localAllocatedSize, oldSize; localAllocatedSize = allocatedSize; memset (content,0,sizeof(char) * localAllocatedSize); int c; while((c = fgetc(stdin)) != EOF){ if(n==localAllocatedSize){ oldSize = localAllocatedSize; localAllocatedSize += INCR_SIZE; allocatedSize = localAllocatedSize; content = (char*) realloc(content, sizeof(char) * localAllocatedSize); if (NULL == content) { perror("Could not allocate memory"); exit(1); } if(oldSize<localAllocatedSize) memset (content+oldSize,0,sizeof(char) * INCR_SIZE); } /* Read line into contents */ if (c != '\n'){ content[n] = c; n++; continue; } n=0; //printf("[X]Content %s \n\n", content); /* Grab the text up to the space character */ char* channel = strtok (content, " "); char* url; if(channel != NULL){ url = match(channel); /* Grab more text up to the next space character * and try to get a redirect url match */ char* original_url; if(NULL == url){ original_url = strtok (NULL, " "); if(NULL != original_url){ url = match(original_url); } printf("%s ", channel); } if(NULL == url){ printf("\n"); fflush(stdout); }else{ if(NULL != url){ char buffer[2048]; printf("302:%s\n", url); fflush(stdout); sprintf (buffer, "Redirecting: %s", url); syslog(LOG_INFO, buffer); }else{ printf("\n"); fflush(stdout); } } }else{ syslog(LOG_INFO, "No url found"); } memset (content,0,sizeof(char) * localAllocatedSize); } closelog(); return EXIT_SUCCESS; }