src/lib/commands/flash.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  flash.c: Flash a message to another player
00029  * 
00030  *  Known contributors to this file:
00031  *     Ken Stevens, 1995
00032  *     Steve McClure, 1998
00033  *     Ron Koenderink, 2005
00034  *     Markus Armbruster, 2004-2007
00035  */
00036 
00037 #include <config.h>
00038 
00039 #include "commands.h"
00040 
00041 static int chat(struct natstr *, struct natstr *, char *);
00042 static int sendmessage(struct natstr *, struct natstr *, char *, int);
00043 
00044 int
00045 flash(void)
00046 {
00047     struct natstr *us;
00048     struct natstr *to;
00049     int tocn;
00050 
00051     us = getnatp(player->cnum);
00052     if ((tocn = natarg(player->argp[1], "to which country? ")) < 0)
00053         return RET_SYN;
00054     to = getnatp(tocn);
00055 
00056     if (us->nat_stat == STAT_GOD) {
00057         /* We are gods, we can flash anyone */
00058     } else if (us->nat_stat == STAT_VIS) {
00059         /* We are a visitor.  We can only flash the gods. :) */
00060         if (to->nat_stat != STAT_GOD) {
00061             pr("Visitors can only flash the gods.\n");
00062             return RET_SYN;
00063         }
00064     } else {
00065         /* Ok, we are a normal country, can we flash them? */
00066         if (to->nat_stat != STAT_GOD && getrel(to, player->cnum) < FRIENDLY) {
00067             pr("%s is not a deity or friendly with us.\n", to->nat_cnam);
00068             return RET_SYN;
00069         }
00070     }
00071 
00072     return chat(us, to, player->comtail[2]);
00073 }
00074 
00075 int
00076 wall(void)
00077 {
00078     return chat(getnatp(player->cnum), NULL, player->comtail[1]);
00079 }
00080 
00081 /*
00082  * Send flash message(s) from US to TO.
00083  * Null TO broadcasts to all.
00084  * MESSAGE is UTF-8.  If it is null, prompt for messages interactively.
00085  * Return RET_OK.
00086  */
00087 static int
00088 chat(struct natstr *us, struct natstr *to, char *message)
00089 {
00090     char buf[1024];             /* UTF-8 */
00091 
00092     if (message) {
00093         buf[0] = ':';
00094         buf[1] = ' ';
00095         strcpy(buf+2, message);
00096         sendmessage(us, to, buf, 1);
00097     } else {
00098         sendmessage(us, to, "...", 1);
00099         while (ugetstring("> ", buf)) {
00100             if (*buf == '.')
00101                 break;
00102             sendmessage(us, to, buf, 0);
00103         }
00104         sendmessage(us, to, "<EOT>", 0);
00105     }
00106     return RET_OK;
00107 }
00108 
00109 /*
00110  * Send flash message MESSAGE from US to TO.
00111  * MESSAGE is UTF-8.
00112  * Null TO broadcasts to all.
00113  * A header identifying US is prepended to the message.  It is more
00114  * verbose if VERBOSE.
00115  */
00116 static int
00117 sendmessage(struct natstr *us, struct natstr *to, char *message, int verbose)
00118 {
00119     struct player *other;
00120     struct tm *tm;
00121     time_t now;
00122     int sent = 0;
00123     struct natstr *wto;
00124 
00125     time(&now);
00126     tm = localtime(&now);
00127     for (other = player_next(0); other != 0; other = player_next(other)) {
00128         if (other->state != PS_PLAYING)
00129             continue;
00130         if (to && other->cnum != to->nat_cnum)
00131             continue;
00132         if (!(wto = getnatp(other->cnum)))
00133             continue;
00134         if (!to && !player->god && getrel(wto, player->cnum) != ALLIED)
00135             continue;
00136         if (!player->god && !(wto->nat_flags & NF_FLASH))
00137             continue;
00138         if (player == other)
00139             continue;
00140         if (verbose)
00141             if (to)
00142                 pr_flash(other, "FLASH from %s (#%d) @ %02d:%02d%s\n",
00143                          us->nat_cnam, us->nat_cnum, tm->tm_hour,
00144                          tm->tm_min, message);
00145             else
00146                 pr_flash(other, "BROADCAST from %s (#%d) @ %02d:%02d%s\n",
00147                          us->nat_cnam, us->nat_cnum, tm->tm_hour,
00148                          tm->tm_min, message);
00149 
00150         else
00151             pr_flash(other, "%s (#%d): %s\n",
00152                      us->nat_cnam, us->nat_cnum, message);
00153         sent++;
00154     }
00155     if (player->god) {
00156         if (to)
00157             if (sent)
00158                 pr("Flash sent to %s\n", to->nat_cnam);
00159             else
00160                 pr("%s is not logged on\n", to->nat_cnam);
00161         else if (sent)
00162             pr("Broadcast sent to %d players\n", sent);
00163         else
00164             pr("No-one is logged in\n");
00165     }
00166     if (to && !player->god) {
00167         /* If they are allied with us, we would normally see that
00168          * they are logged in anyway, so just tell us */
00169         if ((getrel(to, player->cnum) == ALLIED) && !sent) {
00170             if (to->nat_flags & NF_FLASH)
00171                 pr("%s is not logged on\n", to->nat_cnam);
00172             else
00173                 pr("%s is not accepting flashes\n", to->nat_cnam);
00174         }
00175     }
00176     return 0;
00177 }

Generated on Fri Mar 28 11:01:12 2008 for empserver by  doxygen 1.5.2