]> jfr.im git - irc/gameservirc.git/blame - gameserv/news.cpp
added items to the tavern.dat, added the filename option to the config file
[irc/gameservirc.git] / gameserv / news.cpp
CommitLineData
c260a8d7 1#include "extern.h"
466692a5 2#include <fstream>
16ee6441 3#include <string>
821ea0a0 4#include <stdio.h>
466692a5 5
6using std::ofstream;
7using std::ifstream;
c260a8d7 8
16ee6441 9using namespace std;
c260a8d7 10
16ee6441 11List<string> todaysnews;
12void addNews(List<string> &news, const char *fmt, ...);
13void clearNews(List<string> &news) {news.deleteNodes();};
14void showNews(char *nick, List<string> &news);
15void saveNews(char *filename, List<string> &news);
16void loadNews(char *filename, List<string> &news);
17
18void addNews(List<string> &news, const char *fmt, ...)
c260a8d7 19{
20 if (fmt[0] == '\0')
21 return;
22
23 va_list args;
24 char *input;
25 input = new char[1024];
174e7f8f 26 memset(input, 0, 1024);
c260a8d7 27 const char *t = fmt;
28
29 va_start(args, fmt);
30
31 for (; *t; t++)
32 {
33 if (*t == '%')
34 {
35 switch(*++t) {
36 case 'd': sprintf(input, "%s%d", input, va_arg(args, int)); break;
37 case 's': sprintf(input, "%s%s", input, va_arg(args, char *)); break;
38 case 'S': sprintf(input, "%s%s", input, s_GameServ); break;
39 case 'l':
40 if (*++t == 'd')
41 sprintf(input, "%s%ld", input, va_arg(args, long int)); break;
42 }
43 }
44 else
45 {
46 sprintf(input, "%s%c", input, *t);
47 }
48
49 }
50 #ifdef DEBUGMODE
51 log("New News Item: %s", input);
52 #endif
174e7f8f 53
89f30676 54 notice(s_GameServ, c_Forest, "News Flash: %s", input);
16ee6441 55 string *nstring;
56 nstring = new string(input);
c260a8d7 57 news.insertAtBack(nstring);
58
59 delete [] input;
60 delete nstring;
61
62va_end(args);
63}
64
16ee6441 65void showNews(char *nick, List<string> &news)
c260a8d7 66{
67 if (!news.isEmpty())
68 {
16ee6441 69 ListNode<string> *it;
c260a8d7 70 it = news.First();
71
72 notice(s_GameServ, nick, "The Daily Happenings: ");
73 while (it)
74 {
16ee6441 75 notice(s_GameServ, nick, "%s", it->getData()->c_str());
c260a8d7 76 it = it->Next();
77 }
78 notice(s_GameServ, nick, "End of News");
79 }
80}
466692a5 81
16ee6441 82void saveNews(char *filename, List<string> &news)
466692a5 83{
466692a5 84 ofstream outfile;
85 outfile.open(filename);
3c13eb61 86
466692a5 87 if (outfile.fail())
88 {
89 log("Error opening %s", filename);
90 return;
91 }
92
16ee6441 93 ListNode<string> *it;
466692a5 94 it = news.First();
95 while (it)
96 {
16ee6441 97 outfile << it->getData()->c_str() << endl;
466692a5 98 it = it->Next();
99 }
100 outfile.close();
101}
102
16ee6441 103void loadNews(char *filename, List<string> &news)
466692a5 104{
466692a5 105 // First clear the old news out
16ee6441 106 ListNode<string> *it, *temp;
466692a5 107 it = news.First();
108
109 while (it)
110 {
111 temp = it;
112 it = it->Next();
113 delete temp;
114 }
115
116 // Now load from the file
117 ifstream infile;
118 infile.open(filename);
119 if (infile.fail())
120 {
121 log("Error opening %s", filename);
122 return;
123 }
124
125 char *buf;
16ee6441 126 string *str;
466692a5 127 buf = new char [1024];
128
129 while (infile.getline(buf, 1024, '\n'))
130 {
3c13eb61 131 if (buf[0] == '\0' || buf[0] == '\n')
132 {
133 delete [] buf;
134 return;
135 }
16ee6441 136 str = new string(buf);
137 news.insertAtBack(str);
138 delete str;
466692a5 139 }
140
141 delete [] buf;
142}
5aa1d28d 143
144void do_news(char *u)
145{
146 showNews(u, todaysnews);
147}