|
|
1.1 root 1: /* chat.c */
2:
3: /* Synchronet for *nix sysop chat routines */
4:
5: /* $Id: chat.c,v 1.12 2004/08/09 06:27:41 deuce Exp $ */
6:
7: /****************************************************************************
8: * @format.tab-size 4 (Plain Text/Source Code File Header) *
9: * @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
10: * *
11: * Copyright 2003 Rob Swindell - http://www.synchro.net/copyright.html *
12: * *
13: * This program is free software; you can redistribute it and/or *
14: * modify it under the terms of the GNU General Public License *
15: * as published by the Free Software Foundation; either version 2 *
16: * of the License, or (at your option) any later version. *
17: * See the GNU General Public License for more details: gpl.txt or *
18: * http://www.fsf.org/copyleft/gpl.html *
19: * *
20: * Anonymous FTP access to the most recent released source is available at *
21: * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net *
22: * *
23: * Anonymous CVS access to the development source and modification history *
24: * is available at cvs.synchro.net:/cvsroot/sbbs, example: *
25: * cvs -d :pserver:[email protected]:/cvsroot/sbbs login *
26: * (just hit return, no password is necessary) *
27: * cvs -d :pserver:[email protected]:/cvsroot/sbbs checkout src *
28: * *
29: * For Synchronet coding style and modification guidelines, see *
30: * http://www.synchro.net/source.html *
31: * *
32: * You are encouraged to submit any modifications (preferably in Unix diff *
33: * format) via e-mail to [email protected] *
34: * *
35: * Note: If this box doesn't appear square, then you need to fix your tabs. *
36: ****************************************************************************/
37:
38: #include <sys/types.h>
39: #include <utime.h>
40:
41: #include "ciolib.h"
42: #define __COLORS
43: #include "sbbs.h"
44: #include "chat.h"
45:
46: #define PCHAT_LEN 1000 /* Size of Private chat file */
47:
48: typedef struct {
49: int left;
50: int top;
51: int right;
52: int bottom;
53: int x;
54: int y;
55: int colour;
56: } WINDOW;
57:
58: WINDOW uwin;
59: WINDOW swin;
60:
61: int togglechat(scfg_t *cfg, int node_num, node_t *node, int on)
62: {
63: static int org_act;
64:
65: int nodefile;
66: if(getnodedat(cfg,node_num,node,&nodefile)) {
67: return(FALSE);
68: }
69: if(on) {
70: org_act=node->action;
71: if(org_act==NODE_PCHT)
72: org_act=NODE_MAIN;
73: node->misc|=NODE_LCHAT;
74: } else {
75: node->action=org_act;
76: node->misc&=~NODE_LCHAT;
77: }
78: putnodedat(cfg,node_num,node,nodefile);
79: return(TRUE);
80: }
81:
82: void drawchatwin(box_t *boxch, char* topname, char* botname) {
83: struct text_info ti;
84: int i;
85:
86: _wscroll=0;
87: gettextinfo(&ti);
88: textcolor(WHITE);
89: textbackground(BLACK);
90: clrscr();
91: uwin.top=1;
92: uwin.left=1;
93: uwin.right=ti.screenwidth;
94: uwin.bottom=ti.screenheight/2-1;
95: uwin.colour=GREEN;
96: swin.top=ti.screenheight/2+1;
97: swin.left=1;
98: swin.right=ti.screenwidth;
99: swin.bottom=ti.screenheight;
100: swin.colour=LIGHTGREEN;
101:
102: gotoxy(1,ti.screenheight/2);
103: putch(196);
104: putch(196);
105: putch(196);
106: putch(196);
107: putch(180);
108: cputs(topname);
109: putch(195);
110: for(i=wherex();i<=ti.screenwidth;i++)
111: putch(196);
112:
113: _wscroll=1;
114: }
115:
116: int chatchar(WINDOW *win, int ch) {
117: window(win->left,win->top,win->right,win->bottom);
118: gotoxy(win->x,win->y);
119: textcolor(win->colour);
120: putch(ch);
121: if(ch=='\r')
122: putch('\n');
123: win->x=wherex();
124: win->y=wherey();
125: return(0);
126: }
127:
128: int chat(scfg_t *cfg, int nodenum, node_t *node, box_t *boxch, void(*timecallback)(void)) {
129: int in,out;
130: char inpath[MAX_PATH];
131: char outpath[MAX_PATH];
132: char usrname[128];
133: char *p;
134: char ch;
135: time_t now;
136: time_t last_nodechk=0;
137: struct text_info ti;
138: char *buf;
139:
140: gettextinfo(&ti);
141: if((buf=(char *)malloc(ti.screenwidth*ti.screenheight*2))==NULL) {
142: return(-1);
143: }
144:
145: if(getnodedat(cfg,nodenum,node,NULL)) {
146: free(buf);
147: return(-1);
148: }
149:
150: username(cfg,node->useron,usrname);
151:
152: gettext(1,1,ti.screenwidth,ti.screenheight,buf);
153: drawchatwin(boxch,usrname,cfg->sys_op);
154:
155: sprintf(outpath,"%slchat.dab",cfg->node_path[nodenum-1]);
156: if((out=sopen(outpath,O_RDWR|O_CREAT|O_BINARY,O_DENYNONE
157: ,S_IREAD|S_IWRITE))==-1) {
158: free(buf);
159: return(-1);
160: }
161:
162: sprintf(inpath,"%schat.dab",cfg->node_path[nodenum-1]);
163: if((in=sopen(inpath,O_RDWR|O_CREAT|O_BINARY,O_DENYNONE
164: ,S_IREAD|S_IWRITE))==-1) {
165: close(out);
166: free(buf);
167: return(-1);
168: }
169:
170: if((p=(char *)MALLOC(PCHAT_LEN))==NULL) {
171: close(in);
172: close(out);
173: free(buf);
174: return(-1);
175: }
176: memset(p,0,PCHAT_LEN);
177: write(in,p,PCHAT_LEN);
178: write(out,p,PCHAT_LEN);
179: FREE(p);
180: lseek(in,0,SEEK_SET);
181: lseek(out,0,SEEK_SET);
182:
183: togglechat(cfg,nodenum,node,TRUE);
184:
185: while(in != -1) {
186:
187: now=time(NULL);
188: if(now!=last_nodechk) {
189:
190: if(timecallback != NULL)
191: timecallback();
192:
193: if(getnodedat(cfg,nodenum,node,NULL)!=0)
194: break;
195: last_nodechk=now;
196: }
197: if(node->misc&NODE_LCHAT) {
198: if(kbhit()) {
199: ch=getch();
200: if(ch==ESC || ch==3) {
201: close(in);
202: in=-1;
203: }
204: if(ch==0 || ch==-1) { /* Special keys... eat 'em. */
205: getch();
206: }
207: }
208: YIELD();
209: continue;
210: }
211:
212: if(node->status==NODE_WFC || node->status>NODE_QUIET || node->action!=NODE_PCHT) {
213: close(in);
214: in=-1;
215: }
216:
217: utime(inpath,NULL);
218: _setcursortype(_NORMALCURSOR);
219: while(1) {
220: switch(read(in,&ch,1)) {
221: case -1:
222: close(in);
223: in=-1;
224: break;
225:
226: case 0:
227: lseek(in,0,SEEK_SET); /* Wrapped */
228: continue;
229:
230: case 1:
231: lseek(in,-1L,SEEK_CUR);
232: if(ch) {
233: chatchar(&uwin,ch);
234: ch=0;
235: write(in,&ch,1);
236: continue;
237: }
238: break;
239: }
240: break;
241: }
242:
243: if(kbhit()) {
244: ch=getch();
245: switch(ch) {
246: case 0: /* Special Chars */
247: case -1:
248: ch=0;
249: getch();
250: break;
251:
252: case ESC:
253: case 3:
254: close(in);
255: in=-1;
256: continue;
257:
258: default:
259: if(lseek(out,0,SEEK_CUR)>=PCHAT_LEN)
260: lseek(out,0,SEEK_SET);
261: chatchar(&swin,ch);
262: switch(write(out,&ch,1)) {
263: case -1:
264: close(in);
265: in=-1;
266: break;
267: }
268: break;
269: }
270: utime(outpath,NULL);
271: }
272: }
273: if(in != -1)
274: close(in);
275: if(out != -1)
276: close(out);
277: togglechat(cfg,nodenum,node,FALSE);
278: puttext(1,1,ti.screenwidth,ti.screenheight,buf);
279: free(buf);
280: window(ti.winleft,ti.wintop,ti.winright,ti.wintop);
281: gotoxy(ti.curx,ti.cury);
282: textattr(ti.attribute);
283: return(0);
284: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.