1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#include "AppHdr.h"
#ifdef DGL_SIMPLE_MESSAGING
#include "dgl-message.h"
#include "files.h"
#include "format.h"
#include "initfile.h"
#include "libutil.h"
#include "message.h"
#include "notes.h"
#include "options.h"
#include "output.h"
#include <errno.h>
static struct stat mfilestat;
static void _show_message_line(std::string line)
{
const std::string::size_type sender_pos = line.find(":");
if (sender_pos == std::string::npos)
mpr(line.c_str());
else
{
std::string sender = line.substr(0, sender_pos);
line = line.substr(sender_pos + 1);
trim_string(line);
formatted_string fs;
fs.textcolor(WHITE);
fs.cprintf("%s: ", sender.c_str());
fs.textcolor(LIGHTGREY);
fs.cprintf("%s", line.c_str());
formatted_mpr(fs, MSGCH_PLAIN, 0);
take_note(Note(NOTE_MESSAGE, MSGCH_PLAIN, 0,
(sender + ": " + line).c_str()));
}
}
static void _kill_messaging(FILE *mf)
{
if (mf)
fclose(mf);
SysEnv.have_messages = false;
Options.messaging = false;
}
static void _read_each_message()
{
bool say_got_msg = true;
FILE *mf = fopen(SysEnv.messagefile.c_str(), "r+");
if (!mf)
{
mprf(MSGCH_ERROR, "Couldn't read %s: %s", SysEnv.messagefile.c_str(),
strerror(errno));
_kill_messaging(mf);
return;
}
// Read messages, code borrowed from the SIMPLEMAIL patch.
char line[120];
if (!lock_file_handle(mf, F_RDLCK))
{
mprf(MSGCH_ERROR, "Failed to lock %s: %s", SysEnv.messagefile.c_str(),
strerror(errno));
_kill_messaging(mf);
return;
}
while (fgets(line, sizeof line, mf))
{
unlock_file_handle(mf);
const int len = strlen(line);
if (len)
{
if (line[len - 1] == '\n')
line[len - 1] = 0;
if (say_got_msg)
{
mprf(MSGCH_PROMPT, "Your messages:");
say_got_msg = false;
}
_show_message_line(line);
}
if (!lock_file_handle(mf, F_RDLCK))
{
mprf(MSGCH_ERROR, "Failed to lock %s: %s",
SysEnv.messagefile.c_str(),
strerror(errno));
_kill_messaging(mf);
return;
}
}
if (!lock_file_handle(mf, F_WRLCK))
{
mprf(MSGCH_ERROR, "Unable to write lock %s: %s",
SysEnv.messagefile.c_str(),
strerror(errno));
}
if (!ftruncate(fileno(mf), 0))
mfilestat.st_mtime = 0;
unlock_file_handle(mf);
fclose(mf);
SysEnv.have_messages = false;
}
void read_messages()
{
_read_each_message();
update_message_status();
}
static void _announce_messages()
{
// XXX: We could do a NetHack-like mail daemon here at some point.
mprf("Beep! Your pager goes off! Use _ to check your messages.");
}
void check_messages()
{
if (!Options.messaging
|| SysEnv.have_messages
|| SysEnv.messagefile.empty()
|| kbhit()
|| (SysEnv.message_check_tick++ % DGL_MESSAGE_CHECK_INTERVAL))
{
return;
}
const bool had_messages = SysEnv.have_messages;
struct stat st;
if (stat(SysEnv.messagefile.c_str(), &st))
{
mfilestat.st_mtime = 0;
return;
}
if (st.st_mtime > mfilestat.st_mtime)
{
if (st.st_size)
SysEnv.have_messages = true;
mfilestat.st_mtime = st.st_mtime;
}
if (SysEnv.have_messages && !had_messages)
{
_announce_messages();
update_message_status();
}
}
#endif
|