?? chat.c
字號:
/* chat.c For Free Chat (beta release 2e2) By Bill Kendrick kendrick@zippy.sonoma.edu http://zippy.sonoma.edu/kendrick/ September 29, 1996 - June 9, 1998*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>#include "cgi-util.h"#include "dump.h"#include "readline.h"#include "instr.h"#include "copyback.h"#include "defines.h"#include "whattime.h"#include "myopen.h"#include "nicetime.h"#define VER "b2e2"int noswear(char * text);int whohere(char * name, char * email, char * time, int only_posting, int only_watching);void spacetoplus(char * dest, char * source);void dumpform(char * name, char * email, char * time, char * name2, char * lastmesg, int only_posting, char * password, char * whisper_to);void addfaces(char * mesg);void addconverts(char * mesg);int isnew(char * this, char * last);void addurls(char * str);void robotspeak(char * time, char * name, char * mesg);void informadmin(char * name, char * mesg);int sysop, god, registered, joining;int main(int argc, char * argv[]){ char name[STRLEN], email[STRLEN], mesg[STRLEN], topic[STRLEN], name2[STRLEN], temp[STRLEN], time[STRLEN], xname[STRLEN], xtime[STRLEN], xmesg[STRLEN], lastmesg[STRLEN], lasttime[20], password[32], access_name[100], access_pass[100], access_type[100], access_url[512], your_url[512], temp2[STRLEN], whisper_to[STRLEN]; char shownfaces[MAXLINES][STRLEN]; int here, found, ok, i, x, space, only_watching, only_posting, num_shownfaces, j, showed_pict, fdi; FILE * fi, * fiface; sysop = 0; god = 0; registered = 0; /* Initialize CGI Interface: */ cgiinit(); printf("Content-type: text/html\n\n"); if (getentryyesno("version", 0)) { printf("Free Chat<br>version %s<p>\n", VER); printf("<a href=\"mailto:%s\"><code>%s</code></a><p>\n", ADMIN_EMAIL, ADMIN_EMAIL); exit(0); } joining = getentryyesno("join", 0); only_watching = getentryyesno("watch", 0); getentry(name, "name"); for (i = 0; i < strlen(name); i++) if (isalnum(name[i]) == 0) name[i] = '-'; while (name[0] == '-') { strcpy(temp, name + 1); strcpy(name, temp); } while (name[strlen(name) - 1] == '-') name[strlen(name) - 1] = '\0'; if (strlen(name) < 3) error("Please enter a more descriptive name!"); clean(name); strcpy(name2, name); getentry(email, "email"); clean(email); if (strlen(email) != 0 && goodemailaddress(email) == 0) error("Bad e-mail address!"); getentry(password, "password"); fi = fopen(ACCESS_FILE, "r"); strcpy(your_url, ""); registered = 0; if (fi != NULL) { do { readline(fi, access_name); if (!feof(fi)) { readline(fi, access_pass); readline(fi, access_type); readline(fi, access_url); readline(fi, temp); if (strcasecmp(access_name, name) == 0) { strcpy(your_url, access_url); if (strcmp(access_pass, password) == 0) { registered = 1; if (strcmp(access_type, "sysop") == 0) sysop = 1; else if (strcmp(access_type, "god") == 0) { sysop = 1; god = 1; } } else { printf("<h1>Password Required</h1>\n"); printf("<form action=\"chat.cgi\" method=\"post\">\n"); printf("<input type=\"hidden\" name=\"name\"\n"); printf("value=\"%s\">\n", name); printf("<input type=\"hidden\" name=\"email\"\n"); printf("value=\"%s\">\n", email); if (getentryyesno("duelenter", 0) == 1) { printf("<input type=\"hidden\"\n"); printf("name=\"duelenter\"\n"); printf("value=\"yes\">\n"); } printf("Password:\n"); printf("<input type=\"password\" name=\"password\">\n"); printf("<input type=\"submit\" value=\"Enter Chat\">\n"); printf("<br>\n</form>\n"); fclose(fi); exit(0); } } } } while (!feof(fi)); fclose(fi); } if (MUST_BE_REGISTERED == YES && registered == 0) { printf("Registered users only!<p>\n"); exit(0); } if (getentryyesno("duelenter", 0) == 1) { only_posting = 1; printf("<html><head><title>Free Chat - Frames Mode"); printf("</title></head>\n"); printf("<FRAMESET Rows=\"%d,%d,*\" FRAMEBORDER=NO BORDER=NO\n", FRAME1_HEIGHT, FRAME2_HEIGHT); printf("FRAMESPACING=\"0\">\n"); printf("<FRAME NAME=\"ad\" SRC=\"%sad.html\" SCROLLING=\"NO\"\n", PREPEND); printf("BORDER=NO NORESIZE MARGINWIDTH=1 MARGINHEIGHT=1>\n"); printf("<FRAME NAME=\"entry\" SRC=\""); printf("chat.cgi?name=%s&email=%s&password=%s&join=yes&", name, email, password); printf("post=yes\"\n"); printf("FRAMEBORDER=NO SCROLLING=NO>\n"); printf("<FRAME NAME=\"text\" SRC=\""); printf("chat.cgi?watch=yes&name=%s&email=%s&password=%s\"\n", name, email, password); printf("FRAMEBORDER=NO>\n"); printf("<NOFRAMES>\n"); printf("<body bgcolor=\"#FFFFFF\" text=\"#000000\" link=\"#0000FF\"\n"); printf("vlink=\"#FF0000\">\n"); printf("<h1 align=center>"); printf("<a href=\"chat.cgi?name=%s&email=%s&password=%s\">", name, email, password); printf("Enter Chat</h1>\n"); printf("</body></html>\n"); printf("</NOFRAMES>\n"); printf("</FRAMESET>\n"); exit(0); } else only_posting = getentryyesno("post", 0); /* Grab file lock on lock.dat so we don't let anyone clobber anything!: */ fdi = open("lock.dat", O_CREAT | O_WRONLY | O_TRUNC);#ifdef SYSV lockf(fdi, F_LOCK, 0);#else flock(fdi, LOCK_EX);#endif /* Get current topic: */ fi = myopen("topic.dat", "r"); if (fi != NULL) { readline(fi, topic); fclose(fi); } else strcpy(topic, ""); getentry(lasttime, "time"); /* Get current time: */ whattime(time); if (only_watching == 0) { fi = fopen("banned.dat", "r"); if (fi != NULL) { do { readline(fi, temp); if (!feof(fi)) { if (strcmp(temp, name) == 0) { printf("<b>YOU HAVE BEEN BANNED!</b><p>\n"); printf("Misconduct is not allowed in chat!<p>\n"); printf("<a href=\"mailto:%s\"><code>%s</code></a><p>\n", ADMIN_EMAIL); exit(0); } if (strstr(getenv("REMOTE_ADDR"), temp) != NULL) { printf("<b>IP ADDRESS BLOCK!</b><p>\n"); printf("Sorry!<p>\n"); printf("<a href=\"mailto:%s\"><code>%s</code></a><p>\n", ADMIN_EMAIL); exit(0); } } } while (!feof(fi)); fclose(fi); } getentry(temp, "refresh.x"); if (strlen(temp) == 0) { getentry(mesg, "mesg"); clean(mesg); if (strlen(mesg) != 0) { getentry(whisper_to, "whisper"); if (strlen(whisper_to) != 0 && mesg[0] != '/' && strcmp(whisper_to, "NONE") != 0) { sprintf(temp, "/%s %s", whisper_to, mesg); strcpy(mesg, temp); } } } else strcpy(mesg, ""); /* Kill whispers if whispering is turned off: */ if (WHISPERING == OFF || (WHISPERING == SYSOPS_ONLY && sysop == 0) || (WHISPERING == REGISTERED_ONLY && registered == 0)) { if (mesg[0] == '/') { strcmp(temp, mesg + 1); strcpy(mesg, temp); } } getentry(temp, "refresh"); if (strlen(temp) != 0) strcpy(mesg, ""); getentry(lastmesg, "lastmesg"); space = -1; x = 0; for (i = 0; i < strlen(mesg); i++) { if (mesg[i] == ' ') { x = 0; space = i; } x = x + 1; if (x > 80) { strcpy(temp, mesg + i); mesg[i] = ' '; mesg[i + 1] = '\0'; strcat(mesg, temp); space = i; x = 0; } } /* No blank names! */ if (strlen(name) == 0 || noswear(name) == 0) { if (strcmp(email, "") == 0) { dump("included/noname.html"); exit(0); } else { strcpy(name, email); if (strchr(name, '@') != NULL) strcpy(strchr(name, '@'), "\0"); } } /* Find out how many people are here.. making sure you're in this list. */ here = whohere(name, email, time, only_posting, 0); /* Ban users! */ if (strstr(mesg, "BAN ") == mesg) { if (INFORM_ADMIN == YES || INFORM_ADMIN == SMALL) informadmin(name, mesg); if (sysop == 1) { fi = fopen("banned.dat", "a"); if (fi != NULL) { fprintf(fi, "%s\n", strchr(mesg, ' ') + 1); fclose(fi); sprintf(temp, "%s has been banned!", strchr(mesg, ' ') + 1); strcpy(mesg, temp); } } else ok = 0; } /* Change the TOPIC: */ if (strstr(mesg, "TOPIC ") == mesg) { if (INFORM_ADMIN == YES) informadmin(name, mesg); if (sysop == 1) { fi = fopen("topic.dat", "w"); if (fi != NULL) { fprintf(fi, "%s\n", strchr(mesg, ' ') + 1); fclose(fi); sprintf(temp, "The topic is now \"%s\"!", strchr(mesg, ' ') + 1); strcpy(mesg, temp); } } else ok = 0; } /* Dump top of page: */ if (only_posting == 0) { dump("included/chat-top.html"); printf("<td>\n"); #ifdef AD_CMD fflush(stdout); system(AD_CMD); fflush(stdout);#endif printf("</td></tr></table></center>\n"); printf("<table border=1 cellspacing=0 width=100%%\n"); printf("cellpadding=8>\n"); printf("<tr><td valign=top width=20%% align=center>\n"); printf("<h2 align=center>%s</h2>\n", topic); printf("<font size=+1><b>"); if (here == 1 || here == 0) printf("You are the only person here."); else printf("There are %d people here.", here); printf("</b></font><br>\n"); dump("included/chat-left.html"); printf("</td><td valign=top>\n"); } else { dump("included/chat-post-top.html"); } printf("<table border=0 cellspacing=0 cellpadding=0><tr><td>\n"); dumpform(name, email, time, name2, mesg, only_posting, password, whisper_to); /* Add their message: */ if (strlen(mesg) != 0) { ok = noswear(mesg); if (ok == 0) printf("</td><td><b><i><blink><u>NO SWEARING!!!</u></blink></b></i>\n"); } else ok = 0; if (strcmp(lastmesg, mesg) == 0 && strlen(mesg) > 0) { printf("</td><td><b><i><blink><u>YOU ALREADY SAID\n"); printf("THAT!</u></blink></b></i>\n"); ok = 0; } if (ok == 1) { chatwrite(time, name, mesg); robotspeak(time, name, mesg); } /* Notify them if their message is in all caps! */ if (mesg[0] != '/') strcpy(temp, mesg); else { if (strchr(mesg, ' ') != NULL) strcpy(temp, strchr(mesg, ' ') + 1); else strcpy(temp, ""); } ok = 0; for (i = 0; i < strlen(temp); i++) ok = ok + (islower(temp[i])); if (ok == 0) { ok = 1; for (i = 0; i < strlen(temp); i++) if (isalpha(temp[i])) ok = 0; } if (ok == 0 && strlen(temp) > 5) { printf("</td><td><b><i><blink><u>PLEASE TURN OFF YOUR CAPS\n"); printf("KEY!</u></blink></b></i>\n"); } printf("</td></tr></table>\n"); } else { printf("<html><head>\n"); printf("<meta http-equiv=\"refresh\"\n"); printf("content=\"20;URL=chat.cgi?watch=yes&"); printf("name=%s&email=%s&password=%s&time=%s\"></head>\n", name, email, password, time); dump("included/chat-watch-top.html"); printf("<table border=0><tr>\n"); printf("<td valign=top width=120>\n"); printf("<h2 align=center>%s</h2>\n", topic); printf("<h3 align=center>"); whattime(time); here = whohere(name, email, time, 0, 1); if (here == 1 || here == 0) printf("You are the only person here."); else printf("There are %d people here.", here); printf("</h3>\n"); dump("included/chat-left.html"); printf("</td><td valign=top>\n"); } if (only_posting == 0) { /* Dump messages: */ fi = myopen("chat.dat", "r"); if (fi == NULL) { printf("System is down!\n"); exit(0); } printf("<table border=0 width=100%% cellpadding=4>\n"); num_shownfaces = 0; do { readline(fi, xtime); if (!feof(fi)) { readline(fi, xname); readline(fi, xmesg); ok = 1; if (xmesg[0] == '/') { ok = -1; sscanf(xmesg, "%s", temp); if (strcmp(temp + 1, name) == 0 || strcmp(xname, name) == 0 || god == 1) ok = 2; } strcpy(your_url, ""); if (ok != 0) { printf("<tr><td valign=top align=right>\n"); printf("<table border=0 cellspacing=0 cellpadding=0\n"); printf("width=100%%><tr>"); printf("<td valign=top align=right>\n"); /* Show their face if we haven't seen it already: */ found = 0; for (i = 0; i < num_shownfaces; i++) { if (strcmp(shownfaces[i], xname) == 0) found = 1; } showed_pict = 0; if (found == 0) { for (j = 0; j <= strlen(xname); j++) { if (xname[j] == '-') temp2[j] = '_'; else temp2[j] = xname[j]; } strcpy(shownfaces[num_shownfaces], xname); num_shownfaces++; sprintf(temp, "%sphotos/%s.gif", UNIX_PREPEND, temp2); fiface = fopen(temp, "r"); if (fiface != NULL) { fclose(fiface); printf("<img src=\"%sphotos/%s.gif\" width=40 height=40\n", PREPEND, temp2); printf("width=40 height=40 alt=\"\" border=0\n"); printf("alt=\"\" border=0>\n"); showed_pict = 1; } printf("</td><td valign=top align=right>\n"); getentry(password, "password"); fiface = fopen(ACCESS_FILE, "r"); if (fiface != NULL) { do { readline(fiface, access_name); if (!feof(fiface)) { readline(fiface, access_pass); readline(fiface, access_type); readline(fiface, access_url); readline(fiface, temp); if (strcasecmp(access_name, xname) == 0) strcpy(your_url, access_url); } } while (!feof(fiface)); fclose(fiface); } } printf("<font size=-"); if (showed_pict == 1) printf("2"); else printf("1"); printf(" face=\"Arial\">"); if (strlen(your_url) != 0 && strcmp(your_url, "nourl") != 0)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -