]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/commandinterpreter.php
0679f5462dd4c0b03f7cfdb5b0ddc57a105ffeec
[quix0rs-gnu-social.git] / lib / 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          case 'dm':
109             if (!$arg) {
110                 return null;
111             }
112             list($other, $extra) = explode(' ', $arg, 2);
113             if (!$extra) {
114                 return null;
115             } else {
116                 return new MessageCommand($user, $other, $extra);
117             }
118          case 'whois':
119             if (!$arg) {
120                 return null;
121             }
122             list($other, $extra) = explode(' ', $arg, 2);
123             if ($extra) {
124                 return null;
125             } else {
126                 return new WhoisCommand($user, $other);
127             }
128          case 'fav':
129             if (!$arg) {
130                 return null;
131             }
132             list($other, $extra) = explode(' ', $arg, 2);
133             if ($extra) {
134                 return null;
135             } else {
136                 return new FavCommand($user, $other);
137             }
138          case 'nudge':
139             if (!$arg) {
140                 return null;
141             }
142             list($other, $extra) = explode(' ', $arg, 2);
143             if ($extra) {
144                 return null;
145             } else {
146                 return new NudgeCommand($user, $other);
147             }
148          case 'stats':
149             if ($arg) {
150                 return null;
151             }
152             return new StatsCommand($user);
153          case 'invite':
154             if (!$arg) {
155                 return null;
156             }
157             list($other, $extra) = explode(' ', $arg, 2);
158             if ($extra) {
159                 return null;
160             } else {
161                 return new InviteCommand($user, $other);
162             }
163          case 'track':
164             if (!$arg) {
165                 return null;
166             }
167             list($word, $extra) = explode(' ', $arg, 2);
168             if ($extra) {
169                 return null;
170             } else if ($word == 'off') {
171                 return new TrackOffCommand($user);
172             } else {
173                 return new TrackCommand($user, $word);
174             }
175          case 'untrack':
176             if (!$arg) {
177                 return null;
178             }
179             list($word, $extra) = explode(' ', $arg, 2);
180             if ($extra) {
181                 return null;
182             } else if ($word == 'all') {
183                 return new TrackOffCommand($user);
184             } else {
185                 return new UntrackCommand($user, $word);
186             }
187          case 'tracks':
188          case 'tracking':
189             if ($arg) {
190                 return null;
191             }
192             return new TrackingCommand($user);
193          default:
194             return false;
195         }
196     }
197 }
198