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