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