Example #1
0
int main(int argc, char **argv)
{
	atexit(cleanup);
	options = calloc(sizeof(struct opts_t), 1);
	tableList = calloc(sizeof(*tableList), 1);
	options->mode = -1;
	options->rating = RATING_UNDEFINED;

	if (argc < 2)
		exitmsg(1, "Try -help for usage information\n");

	parseArgs(argv + 1, options);

	if (!options->backlog_path[0])
		snprintf(options->backlog_path, 256, "%s/.backlog", getenv("HOME"));

	if (!modified && readTables(tableList, options->backlog_path))
		printf("Couldn't open '%s': %s\nYou can force the creation of this file by passing -force\n\n", options->backlog_path, strerror(errno));

	struct entry_t *entry = 0;
	struct table_t *table = 0;

	if (options->mode == RATING_UNDEFINED && !options->searchMode && !modified)
		exitmsg(1, "Must specify a mode\n");

	if (options->rating != RATING_UNDEFINED && (options->rating < -1 || options->rating > 10))
		exitmsg(1, "Rating must be in the range of -1 to 10\n");

	char *entrystr = "Entry not found\n";
	char *tablestr = "Table not found\n";
	char *err_dupe = "An entry with that name already exists in that table\n";
	char *err_dupe_table = "A table with that name already exists\n";

	char help_msg[] =
		"Available options:\n"
		" -t\n"
		"\tSwitch to table mode\n"
		" -help\n"
		"\tPrint this message\n"
		" -insert\n"
		"\tAdd entry/table\n"
		" -rm\n"
		"\tRemove entry/table\n"
		" -mv\n"
		"\tMove entry between tables\n"
		" -ls\n"
		"\tList tables, can be used with -name to print specific table\n"
		" -find\n"
		"\tSearch for entry, can be used with -name to search within table\n"
		" -rate <INT>\n"
		"\tSet rating on entry\n"
		" -rename\n"
		"\tRename entry/table\n"
		" -name <STRING>\n"
		"\tSet name\n"
		" -new <STRING>\n"
		"\tSet new name\n"
		" -src <STRING>\n"
		"\tSet source name\n"
		" -dest <STRING>\n"
		"\tSet destination name\n"
		" -roll\n"
		"\tReturn random entry\n"
		" -mal\n"
		"\tSearch MAL for entry\n"
		" -nyaa\n"
		"\tSearch Nyaa for entry\n"
		" -baka\n"
		"\tSearch BakaBT for entry (through Google)\n"
		" -anidb\n"
		"\tSearch AniDB for entry\n"
		" -force\n"
		"\tForce write-out to backlog file\n"
		" -path\n"
		"\tOverride the default backlog path (~/.backlog)\n"
		" -nobak\n"
		"\tDon't backup backlog file";

	if (options->mode == MODE_LIST) {	//List tables.
		if (options->sourceName) {
			table = bklgGetTable(tableList, options->sourceName);
			if (!table)
				exitmsg(1, tablestr);
		}
		if (table) {
			bklgPrintTable(table, stdout);
		} else {
			char buffer[32];
			struct tm *tm;
			time_t utime = time(0);

			tm = localtime(&utime);
			strftime(buffer, 32, "%d/%m/%Y\n", tm);
			puts(buffer);
			bklgPrintAllTables(tableList, stdout);
		}
	}

	else if (options->mode == MODE_ENTRY_ADD && options->tableMode) {	//Add table.
		if (!options->entryName)
			exitmsg(1, "Name required\n");

		if (bklgGetTableExact(tableList, options->entryName))
			exitmsg(1, err_dupe_table);

		puts("Added table");
		bklgTableAdd(tableList, options->entryName);
		modified = 1;
	}

	else if (options->mode == MODE_ENTRY_REMOVE && options->tableMode) {	//Remove table.
		if (!options->entryName)
			exitmsg(1, "Name required\n");
		struct table_t *table = bklgGetTable(tableList, options->entryName);

		if (!table)
			exitmsg(1, tablestr);
		printf("Remove \"%s\"? [Y,n]: ", table->name);
		if (confirm()) {
			puts("Table removed");
			bklgLnodeDestroy(tableList, table->parent);
			bklgTableDestroy(table);
			modified = 1;
		}
	}

	else if (options->mode == MODE_ENTRY_ADD) {	//Add entry.
		if (!options->entryName || !options->destinationName)
			exitmsg(1, "Name and destination required, rating optional\n");

		table = bklgGetTable(tableList, options->destinationName);
		if (!table)
			exitmsg(1, tablestr);

		if (bklgGetEntryExact(table->entries, options->entryName))
			exitmsg(1, err_dupe);

		printf("Add entry to \"%s\"? [Y,n]: ", table->name);
		if (confirm()) {
			puts("Entry added");
			entry = bklgEntryAdd(table->entries, options->entryName, options->rating == -2 ? -1 : options->rating);
			modified = 1;
		}
	}

	else if (options->mode == MODE_ENTRY_REMOVE) {	//Remove entry.
		if (!options->entryName)
			exitmsg(1, "Name required, source optional\n");
		struct entry_t *entry = 0;

		if (!options->sourceName) {
			entry = bklgGetEntryAllTables(tableList, options->entryName, &table);
		} else {
			table = bklgGetTable(tableList, options->sourceName);
			if (!table)
				exitmsg(1, tablestr);
			entry = bklgGetEntry(table->entries, options->entryName);
		}
		if (!entry)
			exitmsg(1, entrystr);
		printf("Remove \"%s\" from \"%s\"? [Y,n]: ", entry->name, table->name);
		if (confirm()) {
			puts("Entry removed");
			bklgLnodeDestroy(table->entries, entry->parent);
			free(entry);
			modified = 1;
		}
	}

	else if (options->mode == MODE_ENTRY_MOVE) {	//Move entry to another table.
		if (!options->entryName || !options->destinationName)
			exitmsg(1, "Name and destination required, source optional\n");

		struct entry_t *oldEntry = 0;
		struct table_t *sourceTable = 0;

		if (!options->sourceName) {
			oldEntry = bklgGetEntryAllTables(tableList, options->entryName, &sourceTable);

		} else {
			sourceTable = bklgGetTable(tableList, options->sourceName);
			if (!sourceTable)
				exitmsg(1, "Source table not found\n");
			oldEntry = bklgGetEntry(sourceTable->entries, options->entryName);
		}
		if (!oldEntry)
			exitmsg(1, entrystr);

		struct table_t *destTable = bklgGetTable(tableList, options->destinationName);

		if (!destTable)
			exitmsg(1, "Destination table not found\n");

		if (bklgGetEntryExact(destTable->entries, oldEntry->name))
			exitmsg(1, err_dupe);

		printf("Move \"%s\" from \"%s\" to \"%s\"? [Y,n]: ", oldEntry->name, sourceTable->name, destTable->name);
		if (confirm()) {
			puts("Entry moved");
			entry = bklgEntryAdd(destTable->entries, oldEntry->name, (options->rating < -1 ? oldEntry->rating : options->rating));
			bklgLnodeDestroy(sourceTable->entries, oldEntry->parent);
			free(oldEntry);
			modified = 1;
		}
	}

	else if (options->mode == MODE_FIND) {	//Find entry.
		if (!options->entryName)
			exitmsg(1, "Name required, source optional\n");
		if (!options->sourceName) {
			entry = bklgGetEntryAllTables(tableList, options->entryName, &table);
		} else {
			table = bklgGetTable(tableList, options->sourceName);
			if (!table)
				exitmsg(1, tablestr);
			entry = bklgGetEntry(table->entries, options->entryName);
		}
		if (entry) {
			char rating[8];

			snprintf(rating, 8, "%i/10", entry->rating);
			printf("Best match:\n\tName: %s\n\tRating: %s\n\tParent table: %s\n", entry->name, entry->rating == -1 ? "none" : rating, table->name);
		} else {
			exitmsg(1, entrystr);
		}
	}

	else if (options->rating != RATING_UNDEFINED) {	//Set a rating.
		if (!options->entryName)
			exitmsg(1, "Name required, rating and source optional\n");
		if (!options->sourceName) {
			entry = bklgGetEntryAllTables(tableList, options->entryName, &table);
		} else {
			table = bklgGetTable(tableList, options->sourceName);
			if (!table)
				exitmsg(1, tablestr);
			entry = bklgGetEntry(table->entries, options->entryName);
		}
		if (!entry)
			exitmsg(1, entrystr);
		printf("Change rating of \"%s\"? [Y,n]: ", entry->name);
		if (confirm()) {
			puts("Changed rating");
			entry->rating = options->rating;
			modified = 1;
		}
	}

	else if (options->mode == MODE_ENTRY_RENAME && options->tableMode) {	//Rename table.
		if (!options->entryName || !options->entryNewName)
			exitmsg(1, "Name and new name required\n");

		table = bklgGetTable(tableList, options->entryName);
		if (!table)
			exitmsg(1, tablestr);

		if (bklgGetTableExact(tableList, options->entryNewName))
			exitmsg(1, err_dupe_table);

		printf("Rename \"%s\"? [Y,n]: ", table->name);
		if (confirm()) {
			puts("Renamed table");
			char tmp[64];
			char *tmpp = 0;

			strncpy(tmp, options->entryNewName, 64);
			tmpp = stripWhiteSpace(tmp);
			strncpy(table->name, tmpp, 64);
			modified = 1;
		}
	}

	else if (options->mode == MODE_ENTRY_RENAME) {	//Rename entry.
		if (!options->entryName || !options->entryNewName)
			exitmsg(1, "Name and new name required, source optional\n");

		if (!options->sourceName) {
			entry = bklgGetEntryAllTables(tableList, options->entryName, &table);
		} else {
			table = bklgGetTable(tableList, options->sourceName);
			if (!table)
				exitmsg(1, tablestr);
			entry = bklgGetEntry(table->entries, options->entryName);
		}

		if (!entry)
			exitmsg(1, entrystr);

		if (bklgGetEntryExact(table->entries, options->entryNewName))
			exitmsg(1, err_dupe);

		printf("Rename \"%s\"? [Y,n]: ", entry->name);
		if (confirm()) {
			puts("Renamed entry");
			char tmp[128];
			char *tmpp = 0;

			strncpy(tmp, options->entryNewName, 128);
			tmpp = stripWhiteSpace(tmp);
			strncpy(entry->name, tmpp, 128);
			modified = 1;
		}
	}

	else if (options->mode == MODE_RANDOM) {	//Choose random entry.
		if (!options->sourceName)
			exitmsg(1, "Source required\n");
		struct table_t *table = bklgGetTable(tableList, options->sourceName);

		if (!table)
			exitmsg(1, tablestr);
		srand(time(0) * getpid());
		size_t len = listlen(table->entries);
		int randomindex = rand() % len;
		struct lnode_t *current = table->entries->tail;

		for (int i = 0; i < randomindex; i++) {
			current = current->next;
		}
		entry = current->data;
		char rating[8];

		snprintf(rating, 8, "%i/10", entry->rating);
		printf("Random entry at index %i out of %zu:\n\tName: %s\n\tRating: %s\n\tParent table: %s\n", randomindex + 1, len, entry->name, entry->rating == -1 ? "none" : rating, table->name);
	}

	if (options->searchMode) {	//Search various sites for entry.
		if (!entry && !options->entryName) {
			exitmsg(1, "Name required, source optional\n");
		} else if (!entry) {
			struct table_t *table = 0;

			if (!options->sourceName) {
				entry = bklgGetEntryAllTables(tableList, options->entryName, &table);

			} else {
				if (!table) {
					table = bklgGetTable(tableList, options->sourceName);
					if (!table)
						exitmsg(1, tablestr);
				}
				entry = bklgGetEntry(table->entries, options->entryName);
			}
			if (!entry)
				exitmsg(1, entrystr);
		}
		printf("Searching web for \"%s\"\n", entry->name);
		webSearch(entry, options->searchMode);
	}

	else if (options->mode == MODE_HELP) {	//Print usage stuff.
		puts(help_msg);
	}

	if (modified) {
		if (backup) {
			char bakpath[256];

			snprintf(bakpath, 256, "%s.bak", options->backlog_path);
			if (rename(options->backlog_path, bakpath) == -1)
				exitmsg(1, "Failed to rename to '%s': %s\n", bakpath, strerror(errno));
		}
		if (writeTables(tableList, options->backlog_path))
			exitmsg(1, "Failed to open '%s' for writing: %s\n", options->backlog_path, strerror(errno));
	}
	exit(0);
}
Example #2
0
void parseArgs(char **argv, struct opts_t *options)
{
	char fmt[] = "Missing parameter to argument %s\n";

	while (*argv) {
		if (!strcmp(*argv, "-insert")) {
			options->mode = MODE_ENTRY_ADD;
		} else if (!strcmp(*argv, "-rm")) {
			options->mode = MODE_ENTRY_REMOVE;
		} else if (!strcmp(*argv, "-mv")) {
			options->mode = MODE_ENTRY_MOVE;
		} else if (!strcmp(*argv, "-ls")) {
			options->mode = MODE_LIST;
		} else if (!strcmp(*argv, "-find")) {
			options->mode = MODE_FIND;
		} else if (!strcmp(*argv, "-help")) {
			options->mode = MODE_HELP;
		} else if (!strcmp(*argv, "-rename")) {
			options->mode = MODE_ENTRY_RENAME;
		} else if (!strcmp(*argv, "-roll")) {
			options->mode = MODE_RANDOM;
		} else if (!strcmp(*argv, "-mal")) {
			options->searchMode |= MODE_SEARCH_MAL;
		} else if (!strcmp(*argv, "-nyaa")) {
			options->searchMode |= MODE_SEARCH_NYAA;
		} else if (!strcmp(*argv, "-baka")) {
			options->searchMode |= MODE_SEARCH_BAKA;
		} else if (!strcmp(*argv, "-anidb")) {
			options->searchMode |= MODE_SEARCH_ANIDB;
		} else if (!strcmp(*argv, "-force")) {
			modified = 1;
		} else if (!strcmp(*argv, "-nobak")) {
			backup = 0;
		} else if (!strcmp(*argv, "-t")) {
			options->tableMode = 1;
		} else if (!strcmp(*argv, "-name")) {
			if (!*(++argv))
				exitmsg(1, fmt, "-name");
			options->entryName = *(argv);
		} else if (!strcmp(*argv, "-new")) {
			if (!*(++argv))
				exitmsg(1, fmt, "-new");
			options->entryNewName = *(argv);
		} else if (!strcmp(*argv, "-src")) {
			if (!*(++argv))
				exitmsg(1, fmt, "-src");
			options->sourceName = *(argv);
		} else if (!strcmp(*argv, "-dest")) {
			if (!*(++argv))
				exitmsg(1, fmt, "-dest");
			options->destinationName = *(argv);
		} else if (!strcmp(*argv, "-rate")) {
			if (!*(++argv))
				exitmsg(1, fmt, "-rate");
			options->rating = (char) strtol(*(argv), 0, 10);
		} else if (!strcmp(*argv, "-path")) {
			if (!*(++argv))
				exitmsg(1, fmt, "-path");
			strncpy(options->backlog_path, *argv, 256);
		} else {
			exitmsg(1, "Unrecognised argument: %s\n", *argv);
		}
		argv++;
	}
}
Example #3
0
int protocol_arachne(struct HTTPrecord *cacheitem,struct Url *url,int *returnvalue)
{
 char *value;
#ifndef NOTCPIP
#ifndef CLEMTEST
  if(!strncmpi(url->file,"ftp-",4))
  {
   char back=1;

   if(!strncmpi(&(url->file[4]),"send",4))
    back=0;

   strcpy(url->user,configvariable(&ARACHNEcfg,"FTPusername",NULL));
   strcpy(url->password,configvariable(&ARACHNEcfg,"FTPpassword",NULL));
   strcpy(url->host,configvariable(&ARACHNEcfg,"FTPserver",NULL));
   strcpy(url->file,configvariable(&ARACHNEcfg,"FTPpath",NULL));
   url->port=21;

   if(tcpip)
    ftpsession(url,cacheitem,LASTlocname);

   if(!GLOBAL.gotolocation)
   {
    if(back)
     goback();
    else
     strcpy(GLOBAL.location,p->htmlframe[p->activeframe].cacheitem.URL);
    arachne.target=p->activeframe;
   }

   GLOBAL.reload=0;
   GLOBAL.postdata=0;
   return GOTO_IVEGOTNEWURL;
  }
#endif
#endif
#ifndef POSIX
  if(!strcmpi(url->file,"restart"))
  {
   char buf[IE_MAXLEN];
   char *ptr=getenv("ASETUP");

   if(ptr && !strcmp(ptr,"inst"))  //special case - "Finish Setup" button
    return GOTO_USEREND;

   arachne.target=0; //!!!
   strcpy(buf,"@arachne");
   if(tcpip)
    strcat(buf," -o\n");
   else
    strcat(buf,"\n");
   unlink("lock");
   *returnvalue=willexecute(buf);
   return GOTO_END;
  }
  else
#endif
 ///!!!!
 ///POSIX restart: TerminateArachne + exec.. (argv[0],argv[0],NULL) !
#ifndef NOTCPIP
  if(!strcmpi(url->file,"dialpage"))
  {
   value=configvariable(&ARACHNEcfg,"DialPage",NULL);

//!!glennmcc: Begin Feb 06, 2005 -- default to ppp_init.htm
//if 'DialPage' is missing from arachne.cfg
if(!value) value="file:ppp_init.htm";
//also use ppp_init.htm if DialPage does not begin with file:ppp
//indicating that it has been changed from one of the 4 included dialpages
//which are... ppp_init.htm, pppenhan.htm, pppframe.htm or ppp_fast.htm
//value=strlwr(value);
if(!strstr(value,"file:ppp")) value="file:ppp_init.htm";
//!!glennmcc: end

   if(value)
   {
    strcpy(GLOBAL.location,value);
    AnalyseURL(GLOBAL.location,url,IGNORE_PARENT_FRAME);
   }
  }
  else
#ifndef CLEMTEST
  if(!strcmpi(url->file,"dialer"))
  {
   *returnvalue=willexecute(ArachneDIAL());
   return GOTO_END;
  }
  else
  if(!strcmpi(url->file,"hangup"))
  {
   char buf[IE_MAXLEN];
   arachne.target=0; //!!!
   outs(MSG_HANGUP);
   if(reg && tcpip)
    PPPtimelog();
   process_form(0,IE_NULL); //updateovat Arachne.Cfg
   sprintf(buf,"%s\nif exist PPP.LOG del PPP.LOG\n",configvariable(&ARACHNEcfg,"Hangup",NULL));
   value=configvariable(&ARACHNEcfg,"ExitOnHangup",NULL);
   if(!(value && toupper(*value)=='Y'))
    strcat(buf,"@arachne -c\n");
   else
    exitmsg();
   *returnvalue=willexecute(buf);
   return GOTO_END;
  }
  else
#endif
#endif //NOTCPIP
  if(!strncmpi(url->file,"exit",4))
  {
   if(url->file[4]=='=' || url->file[4]=='?')
    *returnvalue=atoi(&(url->file[5]));
   return GOTO_USEREND;
  }
 return 0;
}