root/nntp/group.cc

Revision 4fe3f5c8da7bfdee156169fb3ee8be251ab8995b, 4.7 KB (checked in by Antti-Juhani Kaijanaho <antti-juhani@…>, 2 years ago)

[nntp::group] Current article is invalid (0) if the group is empty.

Signed-off-by: Antti-Juhani Kaijanaho <antti-juhani@…>

  • Property mode set to 100644
Line 
1/*  This file is part of Alue, the multiprotocol Internet discussion daemon
2
3    Copyright © 2009, 2010 Antti-Juhani Kaijanaho
4
5    Alue is free software: you can redistribute it and/or modify it
6    under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    Alue is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with Alue.  If not, see <http://www.gnu.org/licenses/>.
17
18 */
19
20#include "connection.hh"
21#include "lexutils.hh"
22
23#include "../db/db.hh"
24
25namespace nntp
26{
27        class group : public connection::command
28        {
29        public:
30                enum cmd_type { GROUP, LISTGROUP };
31                group(const char *s, cmd_type cmd) : cmd(cmd) {
32                        connection::register_command(s, this);
33                }
34
35                connection::continuation::ptr
36                perform(connection::cb, const std::string [],  size_t) const;
37        private:
38                cmd_type cmd;
39        };
40
41        connection::continuation::ptr
42        group::perform(connection::cb c, const std::string args[], size_t nargs)
43                const
44        {
45                std::string grp;
46                db::group::number rl = 0, rh = db::group::number_max;
47                switch (nargs) {
48                case 0:
49                        if (cmd == GROUP) {
50                                c.send_line("501 group name missing");
51                                return c.dispatch();
52                        }
53                        break;
54                case 1:
55                        grp = args[0];
56                        break;
57                case 2:
58                        if (cmd == LISTGROUP) {
59                                grp = args[0];
60                                try {
61                                        boost::tie(rl, rh) =
62                                                parse_range(args[1]);
63                                } catch (parse_exception) {
64                                        c.send_line("501 malformed range");
65                                        return c.dispatch();
66                                }
67                                break;
68                        }
69                        /*passthrough*/
70                default:
71                        c.send_line("501 too many parameters");
72                        return c.dispatch();
73                }
74
75                try {
76                        if (grp.length() > 0) {
77                                boost::shared_ptr<db::group> gr =
78                                        c.dbase().lookup_group(grp);
79                                if (!gr->reading_authz(c.identity()))
80                                {
81                                        c.send_line("480 " + grp +
82                                                    " is restricted");
83                                        return c.dispatch();
84                                }
85                                c.current_group() = gr;
86                        } else if (!c.current_group()) {
87                                throw db::no_such_group();
88                        }
89                } catch (db::no_such_group) {
90                        c.send_line("411 no such group");
91                        return c.dispatch();
92                }
93                db::group::number low = c.current_group()->low_mark();
94                db::group::number high = c.current_group()->high_mark();
95
96                c.current_article() = low <= high ? low : 0;
97                std::ostringstream ss;
98                ss << "211 "
99                   << c.current_group()->get_count()
100                   << ' '
101                   << low
102                   << ' '
103                   << high
104                   << ' '
105                   << c.current_group()->name();
106                c.send_line(ss.str());
107
108                if (cmd == GROUP) return c.dispatch();
109
110                if (rl < low) rl = low;
111                if (rh > high) rh = high;
112
113                for (db::group::number i = rl; i <= rh; i++) {
114                        if (!c.current_group()->is_article(i)) continue;
115                        std::ostringstream ss;
116                        ss << i;
117                        c.send_line(ss.str());
118                }
119                c.send_line(".");
120                return c.dispatch();
121        }
122}
123namespace {
124        nntp::group group("group", nntp::group::GROUP);
125        nntp::group listgroup("listgroup", nntp::group::LISTGROUP);
126}
Note: See TracBrowser for help on using the browser.