]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/commandinterpreter.php
Merge branch '0.8.x' of git://gitorious.org/~brion/statusnet/brion-fixes into 0.8.x
[quix0rs-gnu-social.git] / lib / commandinterpreter.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 require_once INSTALLDIR.'/lib/command.php';
23
24 class CommandInterpreter
25 {
26     function handle_command($user, $text)
27     {
28         # XXX: localise
29
30         $text = preg_replace('/\s+/', ' ', trim($text));
31         list($cmd, $arg) = explode(' ', $text, 2);
32
33         # We try to support all the same commands as Twitter, see
34         # http://getsatisfaction.com/twitter/topics/what_are_the_twitter_commands
35         # There are a few compatibility commands from earlier versions of
36         # StatusNet
37
38         switch(strtolower($cmd)) {
39          case 'help':
40             if ($arg) {
41                 return null;
42             }
43             return new HelpCommand($user);
44          case 'on':
45             if ($arg) {
46                 list($other, $extra) = explode(' ', $arg, 2);
47                 if ($extra) {
48                     return null;
49                 } else {
50                     return new OnCommand($user, $other);
51                 }
52             } else {
53                 return new OnCommand($user);
54             }
55          case 'off':
56             if ($arg) {
57                 list($other, $extra) = explode(' ', $arg, 2);
58                 if ($extra) {
59                     return null;
60                 } else {
61                     return new OffCommand($user, $other);
62                 }
63             } else {
64                 return new OffCommand($user);
65             }
66          case 'stop':
67          case 'quit':
68             if ($arg) {
69                 return null;
70             } else {
71                 return new OffCommand($user);
72             }
73          case 'join':
74              if (!$arg) {
75                 return null;
76             }
77             list($other, $extra) = explode(' ', $arg, 2);
78             if ($extra) {
79                 return null;
80             } else {
81                 return new JoinCommand($user, $other);
82             }
83          case 'drop':
84             if (!$arg) {
85                 return null;
86             }
87             list($other, $extra) = explode(' ', $arg, 2);
88             if ($extra) {
89                 return null;
90             } else {
91                 return new DropCommand($user, $other);
92             }
93          case 'follow':
94          case 'sub':
95             if (!$arg) {
96                 return null;
97             }
98             list($other, $extra) = explode(' ', $arg, 2);
99             if ($extra) {
100                 return null;
101             } else {
102                 return new SubCommand($user, $other);
103             }
104          case 'leave':
105          case 'unsub':
106             if (!$arg) {
107                 return null;
108             }
109             list($other, $extra) = explode(' ', $arg, 2);
110             if ($extra) {
111                 return null;
112             } else {
113                 return new UnsubCommand($user, $other);
114             }
115          case 'get':
116          case 'last':
117             if (!$arg) {
118                 return null;
119             }
120             list($other, $extra) = explode(' ', $arg, 2);
121             if ($extra) {
122                 return null;
123             } else {
124                 return new GetCommand($user, $other);
125             }
126          case 'd':
127          case 'dm':
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 MessageCommand($user, $other, $extra);
136             }
137          case 'whois':
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 WhoisCommand($user, $other);
146             }
147          case 'fav':
148             if (!$arg) {
149                 return null;
150             }
151             list($other, $extra) = explode(' ', $arg, 2);
152             if ($extra) {
153                 return null;
154             } else {
155                 return new FavCommand($user, $other);
156             }
157          case 'nudge':
158             if (!$arg) {
159                 return null;
160             }
161             list($other, $extra) = explode(' ', $arg, 2);
162             if ($extra) {
163                 return null;
164             } else {
165                 return new NudgeCommand($user, $other);
166             }
167          case 'stats':
168             if ($arg) {
169                 return null;
170             }
171             return new StatsCommand($user);
172          case 'invite':
173             if (!$arg) {
174                 return null;
175             }
176             list($other, $extra) = explode(' ', $arg, 2);
177             if ($extra) {
178                 return null;
179             } else {
180                 return new InviteCommand($user, $other);
181             }
182          case 'track':
183             if (!$arg) {
184                 return null;
185             }
186             list($word, $extra) = explode(' ', $arg, 2);
187             if ($extra) {
188                 return null;
189             } else if ($word == 'off') {
190                 return new TrackOffCommand($user);
191             } else {
192                 return new TrackCommand($user, $word);
193             }
194          case 'untrack':
195             if (!$arg) {
196                 return null;
197             }
198             list($word, $extra) = explode(' ', $arg, 2);
199             if ($extra) {
200                 return null;
201             } else if ($word == 'all') {
202                 return new TrackOffCommand($user);
203             } else {
204                 return new UntrackCommand($user, $word);
205             }
206          case 'tracks':
207          case 'tracking':
208             if ($arg) {
209                 return null;
210             }
211             return new TrackingCommand($user);
212          default:
213             return false;
214         }
215     }
216 }
217