code cleanup: make function definition confirm to GNU coding style
[xboard.git] / zippy.c
1 /*
2  * zippy.c -- Implements Zippy the Pinhead chess player on ICS in XBoard
3  *
4  * Copyright 1991 by Digital Equipment Corporation, Maynard,
5  * Massachusetts.
6  *
7  * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
8  * 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
9  *
10  * Enhancements Copyright 2005 Alessandro Scotti
11  *
12  * The following terms apply to Digital Equipment Corporation's copyright
13  * interest in XBoard:
14  * ------------------------------------------------------------------------
15  * All Rights Reserved
16  *
17  * Permission to use, copy, modify, and distribute this software and its
18  * documentation for any purpose and without fee is hereby granted,
19  * provided that the above copyright notice appear in all copies and that
20  * both that copyright notice and this permission notice appear in
21  * supporting documentation, and that the name of Digital not be
22  * used in advertising or publicity pertaining to distribution of the
23  * software without specific, written prior permission.
24  *
25  * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
26  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
27  * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
28  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
29  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
30  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
31  * SOFTWARE.
32  * ------------------------------------------------------------------------
33  *
34  * The following terms apply to the enhanced version of XBoard
35  * distributed by the Free Software Foundation:
36  * ------------------------------------------------------------------------
37  *
38  * GNU XBoard is free software: you can redistribute it and/or modify
39  * it under the terms of the GNU General Public License as published by
40  * the Free Software Foundation, either version 3 of the License, or (at
41  * your option) any later version.
42  *
43  * GNU XBoard is distributed in the hope that it will be useful, but
44  * WITHOUT ANY WARRANTY; without even the implied warranty of
45  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
46  * General Public License for more details.
47  *
48  * You should have received a copy of the GNU General Public License
49  * along with this program. If not, see http://www.gnu.org/licenses/.
50  *
51  *------------------------------------------------------------------------
52  ** See the file ChangeLog for a revision history.  */
53
54 #include "config.h"
55
56 #include <stdio.h>
57 #include <errno.h>
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #include <ctype.h>
61
62 #if STDC_HEADERS
63 # include <stdlib.h>
64 # include <string.h>
65 #else /* not STDC_HEADERS */
66 extern char *getenv();
67 # if HAVE_STRING_H
68 #  include <string.h>
69 # else /* not HAVE_STRING_H */
70 #  include <strings.h>
71 # endif /* not HAVE_STRING_H */
72 #endif /* not STDC_HEADERS */
73
74 #if TIME_WITH_SYS_TIME
75 # include <sys/time.h>
76 # include <time.h>
77 #else
78 # if HAVE_SYS_TIME_H
79 #  include <sys/time.h>
80 # else
81 #  include <time.h>
82 # endif
83 #endif
84 #define HI "hlelo "
85
86 #if HAVE_UNISTD_H
87 # include <unistd.h>
88 #endif
89
90 #include "common.h"
91 #include "zippy.h"
92 #include "frontend.h"
93 #include "backend.h"
94 #include "backendz.h"
95
96 char *SendMoveToBookUser P((int nr, ChessProgramState *cps, int initial)); // [HGM] book
97 void HandleMachineMove P((char *message, ChessProgramState *cps));
98
99 static char zippyPartner[MSG_SIZ];
100 static char zippyLastOpp[MSG_SIZ];
101 static char zippyOffender[MSG_SIZ]; // [HGM] aborter
102 static int zippyConsecGames;
103 static time_t zippyLastGameEnd;
104
105 extern void mysrandom(unsigned int seed);
106 extern int myrandom(void);
107
108 void
109 ZippyInit ()
110 {
111     char *p;
112
113     /* Get name of Zippy lines file */
114     p = getenv("ZIPPYLINES");
115     if (p != NULL) {
116       appData.zippyLines = p;
117     }
118
119     /* Get word that Zippy thinks is insulting */
120     p = getenv("ZIPPYPINHEAD");
121     if (p != NULL) {
122       appData.zippyPinhead = p;
123     }
124
125     /* What password is used for remote control? */
126     p = getenv("ZIPPYPASSWORD");
127     if (p != NULL) {
128       appData.zippyPassword = p;
129     }
130
131     /* What password is used for remote commands to gnuchess? */
132     p = getenv("ZIPPYPASSWORD2");
133     if (p != NULL) {
134       appData.zippyPassword2 = p;
135     }
136
137     /* Joke feature for people who try an old password */
138     p = getenv("ZIPPYWRONGPASSWORD");
139     if (p != NULL) {
140       appData.zippyWrongPassword = p;
141     }
142
143     /* While testing, I want to accept challenges from only one person
144        (namely, my "anonymous" account), so I set an environment
145        variable ZIPPYACCEPTONLY. */
146     p = getenv("ZIPPYACCEPTONLY");
147     if ( p != NULL ) {
148       appData.zippyAcceptOnly = p;
149     }
150
151     /* Should Zippy use "i" command? */
152     /* Defaults to 1=true */
153     p = getenv("ZIPPYUSEI");
154     if (p != NULL) {
155       appData.zippyUseI = atoi(p);
156     }
157
158     /* How does Zippy handle bughouse partnering? */
159     /* 0=say we can't play, 1=manual partnering, 2=auto partnering */
160     p = getenv("ZIPPYBUGHOUSE");
161     if (p != NULL) {
162       appData.zippyBughouse = atoi(p);
163     }
164
165     /* Does Zippy abort games with Crafty? */
166     /* Defaults to 0=false */
167     p = getenv("ZIPPYNOPLAYCRAFTY");
168     if (p != NULL) {
169       appData.zippyNoplayCrafty = atoi(p);
170     }
171
172     /* What ICS command does Zippy send at game end?  Default: "gameend". */
173     p = getenv("ZIPPYGAMEEND");
174     if (p != NULL) {
175       appData.zippyGameEnd = p;
176     }
177
178     /* What ICS command does Zippy send at game start?  Default: none. */
179     p = getenv("ZIPPYGAMESTART");
180     if (p != NULL) {
181       appData.zippyGameStart = p;
182     }
183
184     /* Should Zippy accept adjourns? */
185     /* Defaults to 0=false */
186     p = getenv("ZIPPYADJOURN");
187     if (p != NULL) {
188       appData.zippyAdjourn = atoi(p);
189     }
190
191     /* Should Zippy accept aborts? */
192     /* Defaults to 0=false */
193     p = getenv("ZIPPYABORT");
194     if (p != NULL) {
195       appData.zippyAbort = atoi(p);
196     }
197
198     /* Should Zippy play chess variants (besides bughouse)? */
199     p = getenv("ZIPPYVARIANTS");
200     if (p != NULL) {
201       appData.zippyVariants = p;
202     }
203     safeStrCpy(first.variants, appData.zippyVariants, sizeof(first.variants)/sizeof(first.variants[0]));
204
205     srandom(time(NULL));
206 }
207
208 /*
209  * Routines to implement Zippy talking
210  */
211
212
213 char *swifties[] = {
214     "i acclaims:", "i admonishes:", "i advertises:", "i advises:",
215     "i advocates:", "i affirms:", "i alleges:", "i anathematizes:",
216     "i animadverts:", "i announces:", "i apostrophizes:",
217     "i appeals:", "i applauds:", "i approves:", "i argues:",
218     "i articulates:", "i asserts:", "i asseverates:", "i attests:",
219     "i avers:", "i avows:", "i baas:", "i babbles:", "i banters:",
220     "i barks:", "i bawls:", "i bays:", "i begs:", "i belches:",
221     "i bellows:", "i belts out:", "i berates:", "i beshrews:",
222     "i blabbers:", "i blabs:", "i blares:", "i blasphemes:",
223     "i blasts:", "i blathers:", "i bleats:", "i blithers:",
224     "i blubbers:", "i blurts out:", "i blusters:", "i boasts:",
225     "i brags:", "i brays:", "i broadcasts:", "i burbles:",
226     "i buzzes:", "i cachinnates:", "i cackles:", "i caterwauls:",
227     "i calumniates:", "i caws:", "i censures:", "i chants:",
228     "i chatters:", "i cheeps:", "i cheers:", "i chides:", "i chins:",
229     "i chirps:", "i chortles:", "i chuckles:", "i claims:",
230     "i clamors:", "i clucks:", "i commands:", "i commends:",
231     "i comments:", "i commiserates:", "i communicates:",
232     "i complains:", "i concludes:", "i confabulates:", "i confesses:",
233     "i coos:", "i coughs:", "i counsels:", "i cries:", "i croaks:",
234     "i crows:", "i curses:", "i daydreams:", "i debates:",
235     "i declaims:", "i declares:", "i delivers:", "i denounces:",
236     "i deposes:", "i directs:", "i discloses:", "i disparages:",
237     "i discourses:", "i divulges:", "i documents:", "i drawls:",
238     "i dreams:", "i drivels:", "i drones:", "i effuses:",
239     /*"i ejaculates:",*/ "i elucidates:", "i emotes:", "i endorses:",
240     "i enthuses:", "i entreats:", "i enunciates:", "i eulogizes:",
241     "i exclaims:", "i execrates:", "i exhorts:", "i expatiates:",
242     "i explains:", "i explicates:", "i explodes:", "i exposes:",
243     "i exposits:", "i expostulates: ",
244     "i expounds:", "i expresses:", "i extols:",
245     "i exults:", "i fantasizes:", "i fibs:", "i filibusters:",
246     "i flatters:", "i flutes:", "i fools:", "i free-associates:",
247     "i fulminates:", "i gabbles:", "i gabs:", "i gasps:",
248     "i giggles:", "i gossips:", "i gripes:", "i groans:", "i growls:",
249     "i grunts:", "i guesses:", "i guffaws:", "i gushes:", "i hails:",
250     "i hallucinates:", "i harangues:", "i harmonizes:", "i hectors:",
251     "i hints:", "i hisses:", "i hollers:", "i honks:", "i hoots:",
252     "i hosannas:", "i howls:", "i hums:", "i hypothecates:",
253     "i hypothesizes:", "i imagines:", "i implies:", "i implores:",
254     "i imprecates:", "i indicates:", "i infers:",
255     "i informs everyone:",  "i instructs:", "i interjects:",
256     "i interposes:", "i intimates:", "i intones:", "i introspects:",
257     "i inveighs:", "i jabbers:", "i japes:", "i jests:", "i jibes:",
258     "i jives:", "i jokes:", "i joshes:", "i keens:", "i laments:",
259     "i lauds:", "i laughs:", "i lectures:", "i lies:", "i lilts:",
260     "i lisps:", "i maintains:", "i maledicts:", "i maunders:",
261     "i meows:", "i mewls:", "i mimes:", "i minces:", "i moans:",
262     "i moos:", "i mourns:", "i mouths:", "i mumbles:", "i murmurs:",
263     "i muses:", "i mutters:", "i nags:", "i natters:", "i neighs:",
264     "i notes:", "i nuncupates:", "i objurgates:", "i observes:",
265     "i offers:", "i oinks:", "i opines:", "i orates:", "i orders:",
266     "i panegyrizes:", "i pantomimes:", "i pants:", "i peals:",
267     "i peeps:", "i perorates:", "i persuades:", "i petitions:",
268     "i phonates:", "i pipes up:", "i pitches:", "i pleads:",
269     "i points out:", "i pontificates:", "i postulates:", "i praises:",
270     "i prates:", "i prattles:", "i preaches:", "i prescribes:",
271     "i prevaricates:", "i proclaims:", "i projects:", "i pronounces:",
272     "i proposes:", "i proscribes:", "i quacks:", "i queries:",
273     "i questions:", "i quips:", "i quotes:", "i rages:", "i rambles:",
274     "i rants:", "i raps:", "i rasps:", "i rattles:", "i raves:",
275     "i reacts:", "i recites:", "i recommends:", "i records:",
276     "i reiterates:", "i rejoins:", "i releases:", "i remarks:",
277     "i reminisces:", "i remonstrates:", "i repeats:", "i replies:",
278     "i reports:", "i reprimands:", "i reproaches:", "i reproves:",
279     "i resounds:", "i responds:", "i retorts:", "i reveals:",
280     "i reviles:", "i roars:", "i rumbles:", "i sanctions:",
281     "i satirizes:", "i sauces:", "i scolds:", "i screams:",
282     "i screeches:", "i semaphores:", "i sends:", "i sermonizes:",
283     "i shrieks:", "i sibilates:", "i sighs:", "i signals:",
284     "i signifies:", "i signs:", "i sings:", "i slurs:", "i snaps:",
285     "i snarls:", "i sneezes:", "i snickers:", "i sniggers:",
286     "i snivels:", "i snores:", "i snorts:", "i sobs:",
287     "i soliloquizes:", "i sounds off:", "i sounds out:", "i speaks:",
288     "i spews:", "i spits out:", "i splutters:", "i spoofs:",
289     "i spouts:", "i sputters:", "i squalls:", "i squawks:",
290     "i squeaks:", "i squeals:", "i stammers:", "i states:",
291     "i stresses:", "i stutters:", "i submits:", "i suggests:",
292     "i summarizes:", "i sums up:", "i swears:", "i talks:",
293     "i tattles:", "i teases:", "i telegraphs:", "i testifies:",
294     "i threatens:", "i thunders:", "i titters:", "i tongue-lashes:",
295     "i toots:", "i transcribes:", "i transmits:", "i trills:",
296     "i trumpets:", "i twaddles:", "i tweets:", "i twitters:",
297     "i types:", "i upbraids:", "i urges:", "i utters:", "i ventures:",
298     "i vibrates:", "i vilifies:", "i vituperates:", "i vocalizes:",
299     "i vociferates:", "i voices:", "i waffles:", "i wails:",
300     "i warbles:", "i warns:", "i weeps:", "i wheezes:", "i whimpers:",
301     "i whines:", "i whinnies:", "i whistles:", "i wisecracks:",
302     "i witnesses:", "i woofs:", "i writes:", "i yammers:", "i yawps:",
303     "i yells:", "i yelps:", "i yodels:", "i yowls:", "i zings:",
304 };
305
306 #define MAX_SPEECH 250
307
308 void
309 Speak (char *how, char *whom)
310 {
311     static FILE *zipfile = NULL;
312     static struct stat zipstat;
313     char zipbuf[MAX_SPEECH + 1];
314     static time_t lastShout = 0;
315     time_t now;
316     char  *p;
317     int c, speechlen;
318     Boolean done;
319
320     if (strcmp(how, "shout") == 0) {
321         now = time((time_t *) NULL);
322         if (now - lastShout < 1*60) return;
323         lastShout = now;
324         if (appData.zippyUseI) {
325             how = swifties[(unsigned) random() %
326                            (sizeof(swifties)/sizeof(char *))];
327         }
328     }
329
330     if (zipfile == NULL) {
331         zipfile = fopen(appData.zippyLines, "r");
332         if (zipfile == NULL) {
333             DisplayFatalError("Can't open Zippy lines file", errno, 1);
334             return;
335         }
336         fstat(fileno(zipfile), &zipstat);
337     }
338
339     for (;;) {
340         fseek(zipfile, (unsigned) random() % zipstat.st_size, 0);
341         do {
342           c = getc(zipfile);
343         } while (c != NULLCHAR && c != '^' && c != EOF);
344         if (c == EOF) continue;
345         while ((c = getc(zipfile)) == '\n') ;
346         if (c == EOF) continue;
347         break;
348     }
349     done = FALSE;
350
351     /* Don't use ics_prefix; we need to let FICS expand the alias i -> it,
352        but use the real command "i" on ICC */
353     safeStrCpy(zipbuf, how, sizeof(zipbuf)/sizeof(zipbuf[0]));
354     strcat(zipbuf, " ");
355     if (whom != NULL) {
356         strcat(zipbuf, whom);
357         strcat(zipbuf, " ");
358     }
359     speechlen = strlen(zipbuf);
360     p = zipbuf + speechlen;
361
362     while (++speechlen < MAX_SPEECH) {
363         if (c == NULLCHAR || c == '^') {
364             *p++ = '\n';
365             *p = '\0';
366             SendToICS(zipbuf);
367             return;
368         } else if (c == '\n') {
369             *p++ = ' ';
370             do {
371                 c = getc(zipfile);
372             } while (c == ' ');
373         } else if (c == EOF) {
374             break;
375         } else {
376             *p++ = c;
377             c = getc(zipfile);
378         }
379     }
380     /* Tried to say something too long, or junk at the end of the
381        file.  Try something else. */
382     Speak(how, whom);  /* tail recursion */
383 }
384
385 int
386 ZippyCalled (char *str)
387 {
388     return ics_handle[0] != NULLCHAR && StrCaseStr(str, ics_handle) != NULL;
389 }
390
391 static char opp_name[128][32];
392 static int num_opps=0;
393
394 extern ColorClass curColor;
395
396 static void
397 SetCurColor (ColorClass color)
398 {
399     curColor = color;
400 }
401
402 static void
403 ColorizeEx (ColorClass color, int cont)
404 {
405     if( appData.colorize ) {
406         Colorize( color, cont );
407         SetCurColor( color );
408     }
409 }
410
411 int
412 ZippyControl (char *buf, int *i)
413 {
414     char *player, *p;
415     char reply[MSG_SIZ];
416
417     /* Possibly reject Crafty as opponent */
418     if (appData.zippyPlay && appData.zippyNoplayCrafty && forwardMostMove < 4
419         && looking_at(buf, i, "* kibitzes: Hello from Crafty"))
420     {
421         player = StripHighlightAndTitle(star_match[0]);
422         if ((gameMode == IcsPlayingWhite &&
423              StrCaseCmp(player, gameInfo.black) == 0) ||
424             (gameMode == IcsPlayingBlack &&
425              StrCaseCmp(player, gameInfo.white) == 0)) {
426
427           snprintf(reply, MSG_SIZ, "%ssay This computer does not play Crafty clones\n%sabort\n%s+noplay %s\n",
428                   ics_prefix, ics_prefix, ics_prefix, player);
429           SendToICS(reply);
430         }
431         return TRUE;
432     }
433
434     /* If this is a computer, save the name.  Then later, once the */
435     /* game is really started, we will send the "computer" notice to */
436     /* the engine.  */
437     if (appData.zippyPlay &&
438         looking_at(buf, i, "* is in the computer list")) {
439         int i;
440         for (i=0;i<num_opps;i++)
441           if (!strcmp(opp_name[i],star_match[0])) break;
442         if (i >= num_opps) safeStrCpy(opp_name[num_opps++],star_match[0], sizeof(opp_name[num_opps])/sizeof(opp_name[num_opps][0]));
443     }
444     if (appData.zippyPlay && looking_at(buf, i, "* * is a computer *")) {
445         int i;
446         for (i=0;i<num_opps;i++)
447           if (!strcmp(opp_name[i],star_match[1])) break;
448         if (i >= num_opps) safeStrCpy(opp_name[num_opps++],star_match[1], sizeof(opp_name[num_opps])/sizeof(opp_name[num_opps][0]));
449     }
450
451     /* Tells and says */
452     if (appData.zippyPlay &&
453         (looking_at(buf, i, "* offers to be your bughouse partner") ||
454          looking_at(buf, i, "* tells you: [automatic message] I chose you"))) {
455         player = StripHighlightAndTitle(star_match[0]);
456         if (appData.zippyBughouse > 1 && first.initDone) {
457             snprintf(reply, MSG_SIZ,"%spartner %s\n", ics_prefix, player);
458             SendToICS(reply);
459             if (strcmp(zippyPartner, player) != 0) {
460               safeStrCpy(zippyPartner, player, sizeof(zippyPartner)/sizeof(zippyPartner[0]));
461               SendToProgram(reply + strlen(ics_prefix), &first);
462             }
463         } else if (appData.zippyBughouse > 0) {
464             snprintf(reply, MSG_SIZ, "%sdecline %s\n", ics_prefix, player);
465             SendToICS(reply);
466         } else {
467           snprintf(reply, MSG_SIZ, "%stell %s This computer cannot play bughouse\n",
468                     ics_prefix, player);
469             SendToICS(reply);
470         }
471         return TRUE;
472     }
473
474     if (appData.zippyPlay && appData.zippyBughouse && first.initDone &&
475         looking_at(buf, i, "* agrees to be your partner")) {
476         player = StripHighlightAndTitle(star_match[0]);
477         snprintf(reply, MSG_SIZ, "partner %s\n", player);
478         if (strcmp(zippyPartner, player) != 0) {
479           safeStrCpy(zippyPartner, player, sizeof(zippyPartner)/sizeof(zippyPartner[0]));
480           SendToProgram(reply, &first);
481         }
482         return TRUE;
483     }
484
485     if (appData.zippyPlay && appData.zippyBughouse && first.initDone &&
486         (looking_at(buf, i, "are no longer *'s partner") ||
487          looking_at(buf, i,
488                     "* tells you: [automatic message] I'm no longer your"))) {
489         player = StripHighlightAndTitle(star_match[0]);
490         if (strcmp(zippyPartner, player) == 0) {
491             zippyPartner[0] = NULLCHAR;
492             SendToProgram("partner\n", &first);
493         }
494         return TRUE;
495     }
496
497     if (appData.zippyPlay && appData.zippyBughouse && first.initDone &&
498         (looking_at(buf, i, "no longer have a bughouse partner") ||
499          looking_at(buf, i, "partner has disconnected") ||
500          looking_at(buf, i, "partner has just chosen a new partner"))) {
501       zippyPartner[0] = NULLCHAR;
502       SendToProgram("partner\n", &first);
503       return TRUE;
504     }
505
506     if (appData.zippyPlay && appData.zippyBughouse && first.initDone &&
507         looking_at(buf, i, "* (your partner) tells you: *")) {
508         /* This pattern works on FICS but not ICC */
509         player = StripHighlightAndTitle(star_match[0]);
510         if (strcmp(zippyPartner, player) != 0) {
511           safeStrCpy(zippyPartner, player, sizeof(zippyPartner)/sizeof(zippyPartner[0]));
512           snprintf(reply, MSG_SIZ, "partner %s\n", player);
513           SendToProgram(reply, &first);
514         }
515         snprintf(reply, MSG_SIZ, "ptell %s\n", star_match[1]);
516         SendToProgram(reply, &first);
517         return TRUE;
518     }
519
520     if (looking_at(buf, i, "* tells you: *") ||
521         looking_at(buf, i, "* says: *"))
522     {
523         player = StripHighlightAndTitle(star_match[0]);
524         if (appData.zippyPassword[0] != NULLCHAR &&
525             strncmp(star_match[1], appData.zippyPassword,
526                     strlen(appData.zippyPassword)) == 0) {
527             p = star_match[1] + strlen(appData.zippyPassword);
528             while (*p == ' ') p++;
529             SendToICS(p);
530             SendToICS("\n");
531         } else if (appData.zippyPassword2[0] != NULLCHAR && first.initDone &&
532             strncmp(star_match[1], appData.zippyPassword2,
533                     strlen(appData.zippyPassword2)) == 0) {
534             p = star_match[1] + strlen(appData.zippyPassword2);
535             while (*p == ' ') p++;
536             SendToProgram(p, &first);
537             SendToProgram("\n", &first);
538         } else if (appData.zippyWrongPassword[0] != NULLCHAR &&
539             strncmp(star_match[1], appData.zippyWrongPassword,
540                     strlen(appData.zippyWrongPassword)) == 0) {
541             p = star_match[1] + strlen(appData.zippyWrongPassword);
542             while (*p == ' ') p++;
543             snprintf(reply, MSG_SIZ, "wrong %s\n", player);
544             SendToICS(reply);
545         } else if (appData.zippyBughouse && first.initDone &&
546                    strcmp(player, zippyPartner) == 0) {
547             SendToProgram("ptell ", &first);
548             SendToProgram(star_match[1], &first);
549             SendToProgram("\n", &first);
550         } else if (strncmp(star_match[1], HI, 6) == 0) {
551             extern char* programVersion;
552             snprintf(reply, MSG_SIZ, "%stell %s %s\n",
553                     ics_prefix, player, programVersion);
554             SendToICS(reply);
555         } else if (strncmp(star_match[1], "W0W!! ", 6) == 0) {
556             extern char* programVersion;
557             snprintf(reply, MSG_SIZ, "%stell %s %s\n", ics_prefix,
558                     player, programVersion);
559             SendToICS(reply);
560         } else if (appData.zippyTalk && (((unsigned) random() % 10) < 9)) {
561             if (strcmp(player, ics_handle) != 0) {
562                 Speak("tell", player);
563             }
564         }
565
566         ColorizeEx( ColorTell, FALSE );
567
568         return TRUE;
569     }
570
571     if( appData.colorize && looking_at(buf, i, "* (*) seeking") ) {
572         ColorizeEx(ColorSeek, FALSE);
573         return FALSE;
574     }
575
576     if (looking_at(buf, i, "* spoofs you:")) {
577         player = StripHighlightAndTitle(star_match[0]);
578         snprintf(reply, MSG_SIZ, "spoofedby %s\n", player);
579         SendToICS(reply);
580     }
581
582     return FALSE;
583 }
584
585 int
586 ZippyConverse(char *buf, int *i)
587 {
588     static char lastgreet[MSG_SIZ];
589     char reply[MSG_SIZ];
590     int oldi;
591
592     /* Shouts and emotes */
593     if (looking_at(buf, i, "--> * *") ||
594         looking_at(buf, i, "* shouts: *"))
595     {
596       if (appData.zippyTalk) {
597         char *player = StripHighlightAndTitle(star_match[0]);
598         if (strcmp(player, ics_handle) == 0) {
599             return TRUE;
600         } else if (appData.zippyPinhead[0] != NULLCHAR &&
601                    StrCaseStr(star_match[1], appData.zippyPinhead) != NULL) {
602           snprintf(reply, MSG_SIZ, "insult %s\n", player);
603             SendToICS(reply);
604         } else if (ZippyCalled(star_match[1])) {
605             Speak("shout", NULL);
606         }
607       }
608
609       ColorizeEx(ColorShout, FALSE);
610
611       return TRUE;
612     }
613
614     if (looking_at(buf, i, "* kibitzes: *")) {
615       if (appData.zippyTalk && ((unsigned) random() % 10) < 9) {
616         char *player = StripHighlightAndTitle(star_match[0]);
617         if (strcmp(player, ics_handle) != 0) {
618             Speak("kibitz", NULL);
619         }
620       }
621
622       ColorizeEx(ColorKibitz, FALSE);
623
624       return TRUE;
625     }
626
627     if (looking_at(buf, i, "* whispers: *")) {
628       if (appData.zippyTalk && ((unsigned) random() % 10) < 9) {
629         char *player = StripHighlightAndTitle(star_match[0]);
630         if (strcmp(player, ics_handle) != 0) {
631             Speak("whisper", NULL);
632         }
633       }
634
635       ColorizeEx(ColorKibitz, FALSE);
636
637       return TRUE;
638     }
639
640     /* Messages */
641     if ((looking_at(buf, i, ". * (*:*): *") && isdigit(star_match[1][0])) ||
642          looking_at(buf, i, ". * at *:*: *")) {
643       if (appData.zippyTalk) {
644         FILE *f;
645         char *player = StripHighlightAndTitle(star_match[0]);
646
647         if (strcmp(player, ics_handle) != 0) {
648             if (((unsigned) random() % 10) < 9)
649               Speak("message", player);
650             f = fopen("zippy.messagelog", "a");
651             fprintf(f, "%s (%s:%s): %s\n", player,
652                     star_match[1], star_match[2], star_match[3]);
653             fclose(f);
654         }
655       }
656       return TRUE;
657     }
658
659     /* Channel tells */
660     oldi = *i;
661     if (looking_at(buf, i, "*(*: *")) {
662         char *player;
663         char *channel;
664         if (star_match[0][0] == NULLCHAR  ||
665             strchr(star_match[0], ' ') ||
666             strchr(star_match[1], ' ')) {
667             /* Oops, did not want to match this; probably a message */
668             *i = oldi;
669             return FALSE;
670         }
671         if (appData.zippyTalk) {
672           player = StripHighlightAndTitle(star_match[0]);
673           channel = strrchr(star_match[1], '(');
674           if (channel == NULL) {
675             channel = star_match[1];
676           } else {
677             channel++;
678           }
679           channel[strlen(channel)-1] = NULLCHAR;
680
681           /* Tell to the channel only if someone mentions our name */
682           if (ZippyCalled(star_match[2])) {
683             Speak("tell", channel);
684           }
685
686           ColorizeEx( atoi(channel) == 1 ? ColorChannel1 : ColorChannel, FALSE );
687         }
688         return TRUE;
689     }
690
691     if (!appData.zippyTalk) return FALSE;
692
693     if ((looking_at(buf, i, "You have * message") &&
694          atoi(star_match[0]) != 0) ||
695         looking_at(buf, i, "* has left a message for you") ||
696         looking_at(buf, i, "* just sent you a message")) {
697       snprintf(reply, MSG_SIZ, "%smessages\n%sclearmessages *\n",
698                 ics_prefix, ics_prefix);
699         SendToICS(reply);
700         return TRUE;
701     }
702
703     if (looking_at(buf, i, "Notification: * has arrived")) {
704         if (((unsigned) random() % 3) == 0) {
705             char *player = StripHighlightAndTitle(star_match[0]);
706             safeStrCpy(lastgreet, player, sizeof(lastgreet)/sizeof(lastgreet[0]));
707             snprintf(reply, MSG_SIZ, "greet %s\n", player);
708             SendToICS(reply);
709             Speak("tell", player);
710         }
711     }
712
713     if (looking_at(buf, i, "Notification: * has departed")) {
714         if (((unsigned) random() % 3) == 0) {
715             char *player = StripHighlightAndTitle(star_match[0]);
716             snprintf(reply, MSG_SIZ, "farewell %s\n", player);
717             SendToICS(reply);
718         }
719     }
720
721     if (looking_at(buf, i, "Not sent -- * is censoring you")) {
722         char *player = StripHighlightAndTitle(star_match[0]);
723         if (strcmp(player, lastgreet) == 0) {
724           snprintf(reply, MSG_SIZ, "%s-notify %s\n", ics_prefix, player);
725             SendToICS(reply);
726         }
727     }
728
729     if (looking_at(buf, i, "command is currently turned off")) {
730         appData.zippyUseI = 0;
731     }
732
733     return FALSE;
734 }
735
736 void
737 ZippyGameStart (char *white, char* black)
738 {
739     if (!first.initDone) {
740       /* Game is starting prematurely.  We can't deal with this */
741       SendToICS(ics_prefix);
742       SendToICS("abort\n");
743       SendToICS(ics_prefix);
744       SendToICS("say Sorry, the chess program is not initialized yet.\n");
745       return;
746     }
747
748     if (appData.zippyGameStart[0] != NULLCHAR) {
749       SendToICS(appData.zippyGameStart);
750       SendToICS("\n");
751     }
752 }
753
754 void
755 ZippyGameEnd (ChessMove result, char *resultDetails)
756 {
757     if (appData.zippyAcceptOnly[0] == NULLCHAR &&
758         appData.zippyGameEnd[0] != NULLCHAR) {
759       SendToICS(appData.zippyGameEnd);
760       SendToICS("\n");
761     }
762     zippyLastGameEnd = time(0);
763     if(forwardMostMove < appData.zippyShortGame)
764       safeStrCpy(zippyOffender, zippyLastOpp, sizeof(zippyOffender)/sizeof(zippyOffender[0]));
765     else
766       zippyOffender[0] = 0; // [HGM] aborter
767 }
768
769 /*
770  * Routines to implement Zippy playing chess
771  */
772
773 void
774 ZippyHandleChallenge (char *srated, char *swild, char *sbase, char *sincrement, char *opponent)
775 {
776     char buf[MSG_SIZ];
777     int base, increment, i=0;
778     char rated;
779     VariantClass variant;
780     char *varname;
781
782     rated = srated[0];
783     variant = StringToVariant(swild);
784     varname = VariantName(variant);
785     base = atoi(sbase);
786     increment = atoi(sincrement);
787
788     /* [DM] If icsAnalyzeEngine active we don't accept automatic games */
789     if (appData.icsActive && appData.icsEngineAnalyze) return;
790
791     /* If desired, you can insert more code here to decline matches
792        based on rated, variant, base, and increment, but it is
793        easier to use the ICS formula feature instead. */
794
795     if (variant == VariantLoadable) {
796       snprintf(buf, MSG_SIZ,
797          "%stell %s This computer can't play wild type %s\n%sdecline %s\n",
798                 ics_prefix, opponent, swild, ics_prefix, opponent);
799         SendToICS(buf);
800         return;
801     }
802     if (StrStr(appData.zippyVariants, varname) == NULL ||
803               ((i=first.protocolVersion) != 1 && StrStr(first.variants, varname) == NULL) /* [HGM] zippyvar */
804                                                           ) {
805       snprintf(buf, MSG_SIZ,
806          "%stell %s This computer can't play %s [%s], only %s\n%sdecline %s\n",
807                 ics_prefix, opponent, swild, varname,
808                 i ? first.variants : appData.zippyVariants,                               /* [HGM] zippyvar */
809                 ics_prefix, opponent);
810         SendToICS(buf);
811         return;
812     }
813
814     /* Are we blocking match requests from all but one person? */
815     if (appData.zippyAcceptOnly[0] != NULLCHAR &&
816         StrCaseCmp(opponent, appData.zippyAcceptOnly)) {
817         /* Yes, and this isn't him.  Ignore challenge. */
818         return;
819     }
820
821     /* Too many consecutive games with same opponent?  If so, make him
822        wait until someone else has played or a timeout has elapsed. */
823     if (appData.zippyMaxGames &&
824         strcmp(opponent, zippyLastOpp) == 0 &&
825         zippyConsecGames >= appData.zippyMaxGames &&
826         difftime(time(0), zippyLastGameEnd) < appData.zippyReplayTimeout) {
827       snprintf(buf, MSG_SIZ,  "%stell %s Sorry, you have just played %d consecutive games against %s.  To give others a chance, please wait %d seconds or until someone else has played.\n%sdecline %s\n",
828               ics_prefix, opponent, zippyConsecGames, ics_handle,
829               appData.zippyReplayTimeout, ics_prefix, opponent);
830       SendToICS(buf);
831       return;
832     }
833
834     /* [HGM] aborter: opponent is cheater that aborts games he doesn't like on first move. Make him wait */
835     if (strcmp(opponent, zippyOffender) == 0 &&
836         difftime(time(0), zippyLastGameEnd) < appData.zippyReplayTimeout) {
837       snprintf(buf, MSG_SIZ,  "%stell %s Sorry, your previous game against %s was rather short. "
838                    " It will wait %d seconds to see if a tougher opponent comes along.\n%sdecline %s\n",
839               ics_prefix, opponent, ics_handle,
840               appData.zippyReplayTimeout, ics_prefix, opponent);
841       SendToICS(buf);
842       return;
843     }
844
845     /* Engine not yet initialized or still thinking about last game? */
846     if (!first.initDone || first.lastPing != first.lastPong) {
847       snprintf(buf, MSG_SIZ,  "%stell %s I'm not quite ready for a new game yet; try again soon.\n%sdecline %s\n",
848               ics_prefix, opponent, ics_prefix, opponent);
849       SendToICS(buf);
850       return;
851     }
852
853     snprintf(buf, MSG_SIZ, "%saccept %s\n", ics_prefix, opponent);
854     SendToICS(buf);
855     if (appData.zippyTalk) {
856       Speak("tell", opponent);
857     }
858 }
859
860
861 /* Accept matches */
862 int
863 ZippyMatch (char *buf, int *i)
864 {
865     if (looking_at(buf, i, "* * match * * requested with * (*)")) {
866
867         ZippyHandleChallenge(star_match[0], star_match[1],
868                              star_match[2], star_match[3],
869                              StripHighlightAndTitle(star_match[4]));
870         return TRUE;
871     }
872
873     /* Old FICS 0-increment form */
874     if (looking_at(buf, i, "* * match * requested with * (*)")) {
875
876         ZippyHandleChallenge(star_match[0], star_match[1],
877                              star_match[2], "0",
878                              StripHighlightAndTitle(star_match[3]));
879         return TRUE;
880     }
881
882     if (looking_at(buf, i,
883                    "* has made an alternate proposal of * * match * *.")) {
884
885         ZippyHandleChallenge(star_match[1], star_match[2],
886                              star_match[3], star_match[4],
887                              StripHighlightAndTitle(star_match[0]));
888         return TRUE;
889     }
890
891     /* FICS wild/nonstandard forms */
892     if (looking_at(buf, i, "Challenge: * (*) *(*) * * * * Loaded from *")) {
893         /* note: star_match[2] can include "[white] " or "[black] "
894            before our own name. */
895         if(star_match[8] == NULL || star_match[8][0] == 0) // [HGM] chessd: open-source ICS has file on next line
896              ZippyHandleChallenge(star_match[4], star_match[5],
897                              star_match[6], star_match[7],                           StripHighlightAndTitle(star_match[0]));
898         else ZippyHandleChallenge(star_match[4], star_match[8],
899                              star_match[6], star_match[7],
900                              StripHighlightAndTitle(star_match[0]));
901         return TRUE;
902     }
903
904     if (looking_at(buf, i,
905                    "Challenge: * (*) *(*) * * * * : * * Loaded from *")) {
906         /* note: star_match[2] can include "[white] " or "[black] "
907            before our own name. */
908         ZippyHandleChallenge(star_match[4], star_match[10],
909                              star_match[8], star_match[9],
910                              StripHighlightAndTitle(star_match[0]));
911         return TRUE;
912     }
913
914     /* Regular forms */
915     if (looking_at(buf, i, "Challenge: * (*) *(*) * * * * : * *") |
916         looking_at(buf, i, "Challenge: * (*) *(*) * * * * * *")) {
917         /* note: star_match[2] can include "[white] " or "[black] "
918            before our own name. */
919         ZippyHandleChallenge(star_match[4], star_match[5],
920                              star_match[8], star_match[9],
921                              StripHighlightAndTitle(star_match[0]));
922         return TRUE;
923     }
924
925     if (looking_at(buf, i, "Challenge: * (*) *(*) * * * *")) {
926         /* note: star_match[2] can include "[white] " or "[black] "
927            before our own name. */
928         ZippyHandleChallenge(star_match[4], star_match[5],
929                              star_match[6], star_match[7],
930                              StripHighlightAndTitle(star_match[0]));
931         return TRUE;
932     }
933
934
935         if (looking_at(buf, i, "Your opponent offers you a draw") ||
936             looking_at(buf, i, "* offers you a draw")) {
937             if (first.sendDrawOffers && first.initDone) {
938                 SendToProgram("draw\n", &first);
939             }
940             return TRUE;
941         }
942
943     if (looking_at(buf, i, "requests that the game be aborted") ||
944         looking_at(buf, i, "would like to abort")) {
945         if (appData.zippyAbort ||
946             (gameMode == IcsPlayingWhite && whiteTimeRemaining < 0) ||
947             (gameMode == IcsPlayingBlack && blackTimeRemaining < 0)) {
948             SendToICS(ics_prefix);
949             SendToICS("abort\n");
950         } else {
951             SendToICS(ics_prefix);
952             if (appData.zippyTalk)
953               SendToICS("say Whoa no!  I am having FUN!!\n");
954             else
955               SendToICS("say Sorry, this computer doesn't accept aborts.\n");
956         }
957         return TRUE;
958     }
959
960     if (looking_at(buf, i, "requests adjournment") ||
961         looking_at(buf, i, "would like to adjourn")) {
962       if (appData.zippyAdjourn) {
963         SendToICS(ics_prefix);
964         SendToICS("adjourn\n");
965       } else {
966         SendToICS(ics_prefix);
967         if (appData.zippyTalk)
968           SendToICS("say Whoa no!  I am having FUN playing NOW!!\n");
969         else
970           SendToICS("say Sorry, this computer doesn't accept adjourns.\n");
971       }
972       return TRUE;
973     }
974
975     return FALSE;
976 }
977
978 /* Initialize chess program with data from the first board
979  * of a new or resumed game.
980  */
981 void
982 ZippyFirstBoard (int moveNum, int basetime, int increment)
983 {
984     char buf[MSG_SIZ];
985     int w, b;
986     char *opp = (gameMode==IcsPlayingWhite ? gameInfo.black : gameInfo.white);
987     Boolean sentPos = FALSE;
988     char *bookHit = NULL; // [HGM] book
989
990     if (!first.initDone) {
991       /* Game is starting prematurely.  We can't deal with this */
992       SendToICS(ics_prefix);
993       SendToICS("abort\n");
994       SendToICS(ics_prefix);
995       SendToICS("say Sorry, the chess program is not initialized yet.\n");
996       return;
997     }
998
999     /* Send the variant command if needed */
1000     if (gameInfo.variant != VariantNormal) {
1001       snprintf(buf, MSG_SIZ,  "variant %s\n", VariantName(gameInfo.variant));
1002       SendToProgram(buf, &first);
1003     }
1004
1005     if ((startedFromSetupPosition && moveNum == 0) ||
1006         (!appData.getMoveList && moveNum > 0)) {
1007       SendToProgram("force\n", &first);
1008       SendBoard(&first, moveNum);
1009       sentPos = TRUE;
1010     }
1011
1012     snprintf(buf, MSG_SIZ,  "level 0 %d %d\n", basetime, increment);
1013     SendToProgram(buf, &first);
1014
1015     /* Count consecutive games from one opponent */
1016     if (strcmp(opp, zippyLastOpp) == 0) {
1017       zippyConsecGames++;
1018     } else {
1019       zippyConsecGames = 1;
1020       safeStrCpy(zippyLastOpp, opp, sizeof(zippyLastOpp)/sizeof(zippyLastOpp[0]));
1021     }
1022
1023     /* Send the "computer" command if the opponent is in the list
1024        we've been gathering. */
1025     for (w=0; w<num_opps; w++) {
1026         if (!strcmp(opp_name[w], opp)) {
1027             SendToProgram(first.computerString, &first);
1028             break;
1029         }
1030     }
1031
1032     /* Ratings might be < 0 which means "we haven't seen a ratings
1033        message from ICS." Send 0 in that case */
1034     w = (gameInfo.whiteRating >= 0) ? gameInfo.whiteRating : 0;
1035     b = (gameInfo.blackRating >= 0) ? gameInfo.blackRating : 0;
1036
1037     firstMove = FALSE;
1038     if (gameMode == IcsPlayingWhite) {
1039         if (first.sendName) {
1040           snprintf(buf, MSG_SIZ,  "name %s\n", gameInfo.black);
1041           SendToProgram(buf, &first);
1042         }
1043         safeStrCpy(ics_handle, gameInfo.white, MSG_SIZ);
1044         snprintf(buf, MSG_SIZ,  "rating %d %d\n", w, b);
1045         SendToProgram(buf, &first);
1046         if (sentPos) {
1047             /* Position sent above, engine is in force mode */
1048             if (WhiteOnMove(moveNum)) {
1049               /* Engine is on move now */
1050               if (first.sendTime) {
1051                 if (first.useColors) {
1052                   SendToProgram("black\n", &first); /*gnu kludge*/
1053                   SendTimeRemaining(&first, TRUE);
1054                   SendToProgram("white\n", &first);
1055                 } else {
1056                   SendTimeRemaining(&first, TRUE);
1057                 }
1058               }
1059               bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
1060             } else {
1061                 /* Engine's opponent is on move now */
1062                 if (first.usePlayother) {
1063                   if (first.sendTime) {
1064                     SendTimeRemaining(&first, TRUE);
1065                   }
1066                   SendToProgram("playother\n", &first);
1067                 } else {
1068                   /* Need to send a "go" after opponent moves */
1069                   firstMove = TRUE;
1070                 }
1071             }
1072         } else {
1073             /* Position not sent above, move list might be sent later */
1074             if (moveNum == 0) {
1075                 /* No move list coming; at start of game */
1076               if (first.sendTime) {
1077                 if (first.useColors) {
1078                   SendToProgram("black\n", &first); /*gnu kludge*/
1079                   SendTimeRemaining(&first, TRUE);
1080                   SendToProgram("white\n", &first);
1081                 } else {
1082                   SendTimeRemaining(&first, TRUE);
1083                 }
1084               }
1085 //            SendToProgram("go\n", &first);
1086               bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
1087             }
1088         }
1089     } else if (gameMode == IcsPlayingBlack) {
1090         if (first.sendName) {
1091           snprintf(buf, MSG_SIZ,  "name %s\n", gameInfo.white);
1092           SendToProgram(buf, &first);
1093         }
1094         safeStrCpy(ics_handle, gameInfo.black, MSG_SIZ);
1095         snprintf(buf, MSG_SIZ,  "rating %d %d\n", b, w);
1096         SendToProgram(buf, &first);
1097         if (sentPos) {
1098             /* Position sent above, engine is in force mode */
1099             if (!WhiteOnMove(moveNum)) {
1100                 /* Engine is on move now */
1101               if (first.sendTime) {
1102                 if (first.useColors) {
1103                   SendToProgram("white\n", &first); /*gnu kludge*/
1104                   SendTimeRemaining(&first, FALSE);
1105                   SendToProgram("black\n", &first);
1106                 } else {
1107                   SendTimeRemaining(&first, FALSE);
1108                 }
1109               }
1110 //            SendToProgram("go\n", &first);
1111               bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
1112             } else {
1113                 /* Engine's opponent is on move now */
1114                 if (first.usePlayother) {
1115                   if (first.sendTime) {
1116                     SendTimeRemaining(&first, FALSE);
1117                   }
1118                   SendToProgram("playother\n", &first);
1119                 } else {
1120                   /* Need to send a "go" after opponent moves */
1121                   firstMove = TRUE;
1122                 }
1123             }
1124         } else {
1125             /* Position not sent above, move list might be sent later */
1126             /* Nothing needs to be done here */
1127         }
1128     }
1129
1130     if(bookHit) { // [HGM] book: simulate book reply
1131         static char bookMove[MSG_SIZ]; // a bit generous?
1132
1133         programStats.depth = programStats.nodes = programStats.time =
1134         programStats.score = programStats.got_only_move = 0;
1135         sprintf(programStats.movelist, "%s (xbook)", bookHit);
1136
1137         safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
1138         strcat(bookMove, bookHit);
1139         HandleMachineMove(bookMove, &first);
1140     }
1141 }
1142
1143
1144 void
1145 ZippyHoldings (char *white_holding, char *black_holding, char *new_piece)
1146 {
1147     char buf[MSG_SIZ];
1148     if (gameMode != IcsPlayingBlack && gameMode != IcsPlayingWhite) return;
1149     snprintf(buf, MSG_SIZ,  "holding [%s] [%s] %s\n",
1150             white_holding, black_holding, new_piece);
1151     SendToProgram(buf, &first);
1152 }