]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/CommandInterpreter.php
move opening brace of class declaration to next line
[quix0rs-gnu-social.git] / classes / CommandInterpreter.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/classes/Command.php');
23
24 class CommandInterpreter
25 {
26     
27     function handle_command($user, $text)
28     {
29         # XXX: localise
30
31         $text = preg_replace('/\s+/', ' ', trim($text));
32         list($cmd, $arg) = explode(' ', $text, 2);
33
34         # We try to support all the same commands as Twitter, see
35         # http://getsatisfaction.com/twitter/topics/what_are_the_twitter_commands
36         # There are a few compatibility commands from earlier versions of 
37         # Laconica
38         
39         switch(strtolower($cmd)) {
40          case 'help':
41             if ($arg) {
42                 return null;
43             }
44             return new HelpCommand($user);
45          case 'on':
46             if ($arg) {
47                 list($other, $extra) = explode(' ', $arg, 2);
48                 if ($extra) {
49                     return null;
50                 } else {
51                     return new OnCommand($user, $other);
52                 }
53             } else {
54                 return new OnCommand($user);
55             }
56          case 'off':
57             if ($arg) {
58                 list($other, $extra) = explode(' ', $arg, 2);
59                 if ($extra) {
60                     return null;
61                 } else {
62                     return new OffCommand($user, $other);
63                 }
64             } else {
65                 return new OffCommand($user);
66             }
67          case 'stop':
68          case 'quit':
69             if ($arg) {
70                 return null;
71             } else {
72                 return new OffCommand($user);
73             }
74          case 'follow':
75          case 'sub':
76             if (!$arg) {
77                 return null;
78             }
79             list($other, $extra) = explode(' ', $arg, 2);
80             if ($extra) {
81                 return null;
82             } else {
83                 return new SubCommand($user, $other);
84             }
85          case 'leave':
86          case 'unsub':
87             if (!$arg) {
88                 return null;
89             }
90             list($other, $extra) = explode(' ', $arg, 2);
91             if ($extra) {
92                 return null;
93             } else {
94                 return new UnsubCommand($user, $other);
95             }
96          case 'get':
97          case 'last':
98             if (!$arg) {
99                 return null;
100             }
101             list($other, $extra) = explode(' ', $arg, 2);
102             if ($extra) {
103                 return null;
104             } else {
105                 return new GetCommand($user, $other);
106             }
107          case 'd':
108             if (!$arg) {
109                 return null;
110             }
111             list($other, $extra) = explode(' ', $arg, 2);
112             if (!$extra) {
113                 return null;
114             } else {
115                 return new MessageCommand($user, $other, $extra);
116             }
117          case 'whois':
118             if (!$arg) {
119                 return null;
120             }
121             list($other, $extra) = explode(' ', $arg, 2);
122             if ($extra) {
123                 return null;
124             } else {
125                 return new WhoisCommand($user, $other);
126             }
127          case 'fav':
128             if (!$arg) {
129                 return null;
130             }
131             list($other, $extra) = explode(' ', $arg, 2);
132             if ($extra) {
133                 return null;
134             } else {
135                 return new FavCommand($user, $other);
136             }
137          case 'nudge':
138             if (!$arg) {
139                 return null;
140             }
141             list($other, $extra) = explode(' ', $arg, 2);
142             if ($extra) {
143                 return null;
144             } else {
145                 return new NudgeCommand($user, $other);
146             }
147          case 'stats':
148             if ($arg) {
149                 return null;
150             }
151             return new StatsCommand($user);
152          case 'invite':
153             if (!$arg) {
154                 return null;
155             }
156             list($other, $extra) = explode(' ', $arg, 2);
157             if ($extra) {
158                 return null;
159             } else {
160                 return new InviteCommand($user, $other);
161             }
162          case 'track':
163             if (!$arg) {
164                 return null;
165             }
166             list($word, $extra) = explode(' ', $arg, 2);
167             if ($extra) {
168                 return null;
169             } else if ($word == 'off') {
170                 return new TrackOffCommand($user);
171             } else {
172                 return new TrackCommand($user, $word);
173             }
174          case 'untrack':
175             if (!$arg) {
176                 return null;
177             }
178             list($word, $extra) = explode(' ', $arg, 2);
179             if ($extra) {
180                 return null;
181             } else if ($word == 'all') {
182                 return new TrackOffCommand($user);
183             } else {
184                 return new UntrackCommand($user, $word);
185             }
186          case 'tracks':
187          case 'tracking':
188             if ($arg) {
189                 return null;
190             }
191             return new TrackingCommand($user);
192          default:
193             return false;
194         }
195     }
196 }
197